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.
This commit is contained in:
46 files changed
+199
-115
No files matched your search
@@ -57,7 +57,7 @@ func TestDenialsRouteRequiresAdmin(t *testing.T) {
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example"+path, nil)
|
||||
req.Header.Set("X-Archie-CSRF", "1")
|
||||
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
||||
sendCookie(req, "archie_session", "valid")
|
||||
rec := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != tt.want {
|
||||
|
||||
@@ -152,14 +152,13 @@ func (s *Server) signInAccount(w http.ResponseWriter, r *http.Request, email, pa
|
||||
if s.Log != nil {
|
||||
s.Log.InfoContext(r.Context(), "account signed in", "identity", value.Identity.ID)
|
||||
}
|
||||
s.clearProviderCookie(w)
|
||||
s.clearCookie(w, r, s.cookies().providerToken, "/")
|
||||
s.setAccountCookie(w, r, value)
|
||||
writeJSON(w, map[string]any{"user": value.Identity})
|
||||
}
|
||||
|
||||
func (s *Server) setAccountCookie(w http.ResponseWriter, r *http.Request, value identity.AccountSession) {
|
||||
scheme, _ := s.expectedOrigin(r)
|
||||
http.SetCookie(w, &http.Cookie{Name: s.cookies().account, Value: value.Token, Path: "/", HttpOnly: true, Secure: scheme == "https", SameSite: http.SameSiteStrictMode, Expires: value.Expires, MaxAge: int(time.Until(value.Expires).Seconds())})
|
||||
s.setCookie(w, r, cookie{name: s.cookies().account, value: value.Token, maxAge: int(time.Until(value.Expires).Seconds())})
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
}
|
||||
|
||||
@@ -174,7 +173,7 @@ func (s *Server) handleAccountLogout(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
for _, name := range []string{s.cookies().account, s.cookies().providerToken} {
|
||||
http.SetCookie(w, &http.Cookie{Name: name, Path: "/", HttpOnly: true, MaxAge: -1, SameSite: http.SameSiteStrictMode})
|
||||
s.clearCookie(w, r, name, "/")
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -214,7 +213,7 @@ func (s *Server) handleAccountPassword(w http.ResponseWriter, r *http.Request) {
|
||||
s.writeAccountError(w, err)
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: s.cookies().account, Path: "/", MaxAge: -1, HttpOnly: true, SameSite: http.SameSiteStrictMode})
|
||||
s.clearCookie(w, r, s.cookies().account, "/")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
@@ -356,10 +355,6 @@ func (s *Server) handleAccountRecover(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) clearProviderCookie(w http.ResponseWriter) {
|
||||
http.SetCookie(w, &http.Cookie{Name: s.cookies().providerToken, Path: "/", HttpOnly: true, SameSite: http.SameSiteStrictMode, MaxAge: -1})
|
||||
}
|
||||
|
||||
func (s *Server) authorizeAuthenticationMutation(w http.ResponseWriter, r *http.Request) bool {
|
||||
if !s.authorizeTaskMutation(w, r) {
|
||||
return false
|
||||
|
||||
@@ -74,7 +74,7 @@ func TestAccountBoundary(t *testing.T) {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Archie-CSRF", tc.csrf)
|
||||
if tc.cookie != "" {
|
||||
req.AddCookie(&http.Cookie{Name: "archie_session", Value: tc.cookie})
|
||||
sendCookie(req, "archie_session", tc.cookie)
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(recorder, req)
|
||||
|
||||
@@ -35,7 +35,7 @@ type ModelReader interface {
|
||||
// rather than imported so the dashboard package stays a leaf.
|
||||
const (
|
||||
providerSettingsKind = "provider-settings"
|
||||
credentialBindingsKind = "credential-bindings"
|
||||
credentialBindingsKind = "credential-bindings" //nolint:gosec // G101: a resource kind, not a credential.
|
||||
)
|
||||
|
||||
// providerDoc is the part of one provider-settings entry the read needs: where
|
||||
|
||||
+11
-28
@@ -85,19 +85,12 @@ func (s *Server) requireToken(h http.Handler) http.Handler {
|
||||
http.Error(w, "unauthorised", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: s.cookies().token,
|
||||
Value: tok,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
Secure: r.TLS != nil,
|
||||
})
|
||||
s.setCookie(w, r, cookie{name: s.cookies().token, value: tok})
|
||||
clean := *r.URL
|
||||
q := clean.Query()
|
||||
q.Del("t")
|
||||
clean.RawQuery = q.Encode()
|
||||
http.Redirect(w, r, sameOriginPath(clean.Path, clean.RawQuery), http.StatusSeeOther)
|
||||
http.Redirect(w, r, sameOriginPath(clean.Path, clean.RawQuery), http.StatusSeeOther) //nolint:gosec // G710: sameOriginPath keeps the redirect on this origin.
|
||||
return
|
||||
}
|
||||
|
||||
@@ -172,8 +165,8 @@ func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
s.refuseUnidentified(w, r, err)
|
||||
return
|
||||
}
|
||||
setFlowCookie(w, r, s.cookies().loginState, state)
|
||||
setFlowCookie(w, r, s.cookies().loginVerifier, verifier)
|
||||
s.setFlowCookie(w, r, s.cookies().loginState, state)
|
||||
s.setFlowCookie(w, r, s.cookies().loginVerifier, verifier)
|
||||
http.Redirect(w, r, url, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@@ -211,8 +204,8 @@ func (s *Server) handleCallback(w http.ResponseWriter, r *http.Request) {
|
||||
s.refuseUnidentified(w, r, err)
|
||||
return
|
||||
}
|
||||
clearFlowCookie(w, r, s.cookies().loginState)
|
||||
clearFlowCookie(w, r, s.cookies().loginVerifier)
|
||||
s.clearFlowCookie(w, r, s.cookies().loginState)
|
||||
s.clearFlowCookie(w, r, s.cookies().loginVerifier)
|
||||
if security, ok := s.Accounts.(identity.AccountSecurity); ok {
|
||||
principal, resolveErr := s.Principals.PrincipalFor(r.Context(), value.ID)
|
||||
if resolveErr != nil {
|
||||
@@ -226,26 +219,16 @@ func (s *Server) handleCallback(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
s.setAccountCookie(w, r, local)
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: s.cookies().providerToken, Value: session.Token, Path: "/",
|
||||
HttpOnly: true, SameSite: http.SameSiteStrictMode, Secure: r.TLS != nil,
|
||||
})
|
||||
s.setCookie(w, r, cookie{name: s.cookies().providerToken, value: session.Token})
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func setFlowCookie(w http.ResponseWriter, r *http.Request, name, value string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: name, Value: value, Path: "/oauth2",
|
||||
HttpOnly: true, SameSite: http.SameSiteLaxMode, Secure: r.TLS != nil,
|
||||
MaxAge: loginCookieMaxAge,
|
||||
})
|
||||
func (s *Server) setFlowCookie(w http.ResponseWriter, r *http.Request, name, value string) {
|
||||
s.setCookie(w, r, cookie{name: name, value: value, path: "/oauth2", sameSite: http.SameSiteLaxMode, maxAge: loginCookieMaxAge})
|
||||
}
|
||||
|
||||
func clearFlowCookie(w http.ResponseWriter, r *http.Request, name string) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: name, Value: "", Path: "/oauth2",
|
||||
HttpOnly: true, SameSite: http.SameSiteLaxMode, Secure: r.TLS != nil, MaxAge: -1,
|
||||
})
|
||||
func (s *Server) clearFlowCookie(w http.ResponseWriter, r *http.Request, name string) {
|
||||
s.setCookie(w, r, cookie{name: name, path: "/oauth2", sameSite: http.SameSiteLaxMode, maxAge: -1})
|
||||
}
|
||||
|
||||
// randomToken returns a fresh URL-safe nonce for a sign-in's state.
|
||||
|
||||
@@ -148,7 +148,7 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) {
|
||||
req.Header.Set("X-Archie-CSRF", "1")
|
||||
want := http.StatusUnauthorized
|
||||
if signedIn {
|
||||
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
||||
sendCookie(req, "archie_session", "valid")
|
||||
want = http.StatusForbidden
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
@@ -189,7 +189,7 @@ func TestResourceHistoryNeedsPolicyManagement(t *testing.T) {
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example"+path, nil)
|
||||
req.Header.Set("X-Archie-CSRF", "1")
|
||||
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
||||
sendCookie(req, "archie_session", "valid")
|
||||
rec := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != tt.want {
|
||||
@@ -231,7 +231,7 @@ func TestAuditRoutesFollowTheReadAuditRole(t *testing.T) {
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example"+route.path, nil)
|
||||
req.Header.Set("X-Archie-CSRF", "1")
|
||||
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
||||
sendCookie(req, "archie_session", "valid")
|
||||
rec := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != tt.want {
|
||||
|
||||
@@ -33,7 +33,7 @@ func TestChatFileDownload(t *testing.T) {
|
||||
}{
|
||||
{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},
|
||||
{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},
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package webui
|
||||
|
||||
import "strings"
|
||||
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
|
||||
@@ -27,3 +30,31 @@ func newCookieNames(instance string) cookieNames {
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -41,14 +41,11 @@ func (s *Server) workspaces() access.WorkspaceLister {
|
||||
}
|
||||
|
||||
func (s *Server) setOrgCookie(w http.ResponseWriter, r *http.Request, id string) {
|
||||
cookie := &http.Cookie{
|
||||
Name: s.cookies().org, Value: id, Path: "/",
|
||||
HttpOnly: true, SameSite: http.SameSiteStrictMode, Secure: r.TLS != nil, MaxAge: orgCookieMaxAge,
|
||||
}
|
||||
if id == "" {
|
||||
cookie.MaxAge = -1
|
||||
s.clearCookie(w, r, s.cookies().org, "/")
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
s.setCookie(w, r, cookie{name: s.cookies().org, value: id, maxAge: orgCookieMaxAge})
|
||||
}
|
||||
|
||||
// handleOrgSelect makes the caller's chosen org the one its tasks, workflows
|
||||
|
||||
@@ -484,7 +484,7 @@ func TestOrgSelection(t *testing.T) {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if tt.cookie != "" {
|
||||
req.AddCookie(&http.Cookie{Name: s.cookies().org, Value: tt.cookie})
|
||||
sendCookie(req, s.cookies().org, tt.cookie)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
orgRoutes(s).ServeHTTP(rec, req)
|
||||
|
||||
@@ -40,8 +40,7 @@ func (s *Server) handlePasskeyBegin(w http.ResponseWriter, r *http.Request, regi
|
||||
s.writeAccountError(w, err)
|
||||
return
|
||||
}
|
||||
scheme, _ := s.expectedOrigin(r)
|
||||
http.SetCookie(w, &http.Cookie{Name: s.passkeyCookie(registration), Value: value.Token, Path: "/api/", HttpOnly: true, Secure: scheme == "https", SameSite: http.SameSiteStrictMode, MaxAge: 300})
|
||||
s.setCookie(w, r, cookie{name: s.passkeyCookie(registration), value: value.Token, path: "/api/", maxAge: 300})
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeJSON(w, json.RawMessage(value.Options))
|
||||
}
|
||||
@@ -63,7 +62,7 @@ func (s *Server) handlePasskeyFinish(w http.ResponseWriter, r *http.Request, reg
|
||||
http.Error(w, "passkey request expired", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &http.Cookie{Name: s.passkeyCookie(registration), Path: "/api/", HttpOnly: true, SameSite: http.SameSiteStrictMode, MaxAge: -1})
|
||||
s.clearCookie(w, r, s.passkeyCookie(registration), "/api/")
|
||||
var request struct {
|
||||
Credential json.RawMessage `json:"credential"`
|
||||
Name string `json:"name"`
|
||||
@@ -77,7 +76,7 @@ func (s *Server) handlePasskeyFinish(w http.ResponseWriter, r *http.Request, reg
|
||||
return
|
||||
}
|
||||
if !registration {
|
||||
s.clearProviderCookie(w)
|
||||
s.clearCookie(w, r, s.cookies().providerToken, "/")
|
||||
s.setAccountCookie(w, r, value)
|
||||
}
|
||||
writeJSON(w, map[string]any{"user": value.Identity})
|
||||
|
||||
@@ -243,6 +243,13 @@ type Server struct {
|
||||
latest map[string]liveUpdate
|
||||
}
|
||||
|
||||
// secureRequest reports whether the browser reached the dashboard over HTTPS,
|
||||
// directly or through a trusted proxy that terminates TLS.
|
||||
func (s *Server) secureRequest(r *http.Request) bool {
|
||||
scheme, _ := s.expectedOrigin(r)
|
||||
return scheme == "https"
|
||||
}
|
||||
|
||||
func (s *Server) trustForwardedHeaders() bool {
|
||||
if s == nil {
|
||||
return false
|
||||
|
||||
@@ -48,7 +48,7 @@ func TestUsageNeedsAdminOrOwner(t *testing.T) {
|
||||
}
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example/api/usage", nil)
|
||||
req.Header.Set("X-Archie-CSRF", "1")
|
||||
req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"})
|
||||
sendCookie(req, "archie_session", "valid")
|
||||
rec := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != tt.want {
|
||||
|
||||
Reference in new issue
Block a user