Files
archie/internal/webui/cookies_test.go
T
sam db346b159d fix(webui): mark cookies Secure behind a TLS proxy; run gosec in the gate (#1919)
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.
2026-11-13 03:16:15 +11:00

51 lines
1.5 KiB
Go

package webui
import (
"crypto/tls"
"net/http"
"net/http/httptest"
"testing"
)
// sendCookie attaches a cookie to a request as a browser sends it: a name and
// a value, with none of the attributes a response sets.
func sendCookie(req *http.Request, name, value string) {
req.Header.Add("Cookie", name+"="+value)
}
// TestSessionCookiesSecureBehindProxy pins that a cookie is marked Secure
// whenever the browser reached the dashboard over HTTPS, including through a
// trusted proxy that terminates TLS.
func TestSessionCookiesSecureBehindProxy(t *testing.T) {
tests := []struct {
name string
tls bool
forwarded string
trust bool
wantSecure bool
}{
{"direct https", true, "", false, true},
{"plain http", false, "", false, false},
{"trusted proxy terminating https", false, "https", true, true},
{"untrusted forwarded header", false, "https", false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Server{TrustForwardedHeaders: tt.trust}
r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/", nil)
if tt.tls {
r.TLS = &tls.ConnectionState{}
}
if tt.forwarded != "" {
r.Header.Set("X-Forwarded-Proto", tt.forwarded)
}
w := httptest.NewRecorder()
s.setOrgCookie(w, r, "acme")
cookies := w.Result().Cookies()
if len(cookies) != 1 || cookies[0].Secure != tt.wantSecure {
t.Fatalf("cookies = %+v, want one with Secure=%v", cookies, tt.wantSecure)
}
})
}
}