Files
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

61 lines
2.0 KiB
Go

package webui
import (
"net/http"
"strings"
)
// cookieNames are the dashboard's cookie names. Browsers scope cookies by host
// and ignore the port, so each name carries the instance: two instances on
// one host would otherwise overwrite each other's sessions.
type cookieNames struct {
token, account, providerToken, loginState, loginVerifier, passkeyRegistration, passkeyLogin, org string
}
func newCookieNames(instance string) cookieNames {
prefix := strings.ReplaceAll(instance, "-", "_")
if prefix == "" {
prefix = "archie"
}
return cookieNames{
token: prefix + "_ui",
account: prefix + "_session",
providerToken: prefix + "_provider_token",
loginState: prefix + "_login_state",
loginVerifier: prefix + "_login_verifier",
passkeyRegistration: prefix + "_passkey_registration",
passkeyLogin: prefix + "_passkey_login",
org: prefix + "_org",
}
}
func (s *Server) cookies() cookieNames { return newCookieNames(s.Instance) }
// cookie is what varies between the dashboard's cookies. Every one is
// HttpOnly, and Secure whenever the browser reached the dashboard over HTTPS.
type cookie struct {
name, value, path string
// sameSite defaults to Strict; the sign-in flow needs Lax to survive the
// provider's redirect back.
sameSite http.SameSite
// maxAge is in seconds; negative deletes the cookie.
maxAge int
}
func (s *Server) setCookie(w http.ResponseWriter, r *http.Request, c cookie) {
if c.path == "" {
c.path = "/"
}
if c.sameSite == 0 {
c.sameSite = http.SameSiteStrictMode
}
http.SetCookie(w, &http.Cookie{ //nolint:gosec // G124: a fixed Secure would stop plain-HTTP deployments signing in.
Name: c.name, Value: c.value, Path: c.path, MaxAge: c.maxAge,
HttpOnly: true, Secure: s.secureRequest(r), SameSite: c.sameSite,
})
}
func (s *Server) clearCookie(w http.ResponseWriter, r *http.Request, name, path string) {
s.setCookie(w, r, cookie{name: name, path: path, maxAge: -1})
}