Cookies took Secure from r.TLS, which is nil behind a proxy that terminates TLS, so sign-in cookies went out without it. Every dashboard cookie now goes through one builder that reads the scheme the way the origin check does. The worktree reset and the agent worker's chown walk use os.Root, so a symlink swapped in mid-walk can't point them outside the worktree. gosec runs in golangci-lint, tests included. G104, G115, G204 and G304 are excluded in config; each remaining finding is fixed or suppressed at the site with its reason.
62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
package webui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
)
|
|
|
|
// TestChatFileDownload: only a file a turn offered is served, and only until
|
|
// its token expires.
|
|
func TestChatFileDownload(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "report.txt")
|
|
if err := os.WriteFile(path, []byte("contents"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now()
|
|
srv := newChatAPI(&Server{})
|
|
srv.files.now = func() time.Time { return now }
|
|
token := srv.files.offer(path, "")
|
|
private := srv.files.offerFor(path, "", "alice")
|
|
|
|
tests := []struct {
|
|
name string
|
|
user string
|
|
token string
|
|
after time.Duration
|
|
want int
|
|
}{
|
|
{name: "an offered file is served", token: token, want: http.StatusOK},
|
|
{name: "a token nobody was given is refused", token: "0123456789abcdef0123456789abcdef", want: http.StatusNotFound},
|
|
{name: "a path is not a token", token: "..%2f..%2fetc%2fpasswd", want: http.StatusNotFound}, //nolint:gosec // G101: a fake file token.
|
|
{name: "owner downloads private file", user: "alice", token: private, want: http.StatusOK},
|
|
{name: "another person cannot download", user: "bob", token: private, want: http.StatusNotFound},
|
|
{name: "unidentified caller cannot download private file", token: private, want: http.StatusNotFound},
|
|
{name: "an expired token is refused", token: token, after: chatFileTTL + time.Second, want: http.StatusNotFound},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
srv.files.now = func() time.Time { return now.Add(tt.after) }
|
|
ctx := t.Context()
|
|
if tt.user != "" {
|
|
ctx = WithActingIdentity(ctx, identity.Identity{ID: identity.IdentityID(tt.user)})
|
|
}
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/chat/file/x", nil)
|
|
req.SetPathValue("token", tt.token)
|
|
rec := httptest.NewRecorder()
|
|
srv.handleChatFile(rec, req)
|
|
if rec.Code != tt.want {
|
|
t.Fatalf("status = %d, want %d", rec.Code, tt.want)
|
|
}
|
|
if tt.want == http.StatusOK && rec.Body.String() != "contents" {
|
|
t.Fatalf("body = %q", rec.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|