Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(server): skip CloudFront cookie signing for CLI and daemon requests (MUL-8190) (#9459)
Multica Eveb294ce404753
diff --git a/server/internal/middleware/cloudfront.go b/server/internal/middleware/cloudfront.goindex 52d67ad914..93e5661079 100644--- a/server/internal/middleware/cloudfront.go+++ b/server/internal/middleware/cloudfront.go@@ -17,12 +17,23 @@ import (// mounted on every group that can renew one. This middleware only skips its// own work when that already happened, so a single response does not carry// two sets of CDN cookies (MUL-7436).+//+// Non-browser clients (CLI, daemon) are skipped entirely: they never keep a+// cookie jar, so the cookie is "missing" on every request and each one paid an+// RSA signature for Set-Cookie headers that were thrown away — ~18% of API CPU+// in production (MUL-8190). Trusting the client-controlled X-Client-Platform+// header is safe here because a spoofed value can only make the server+// withhold CDN cookies from the spoofer; it never grants anything.func RefreshCloudFrontCookies(signer *auth.CloudFrontSigner) func(http.Handler) http.Handler {return func(next http.Handler) http.Handler {if signer == nil {return next}return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if isCookielessClient(r) {+ next.ServeHTTP(w, r)+ return+ }_, err := r.Cookie("CloudFront-Policy")if err != nil && !SessionRenewed(r) {ttl := auth.AuthTokenTTL()@@ -34,3 +45,20 @@ func RefreshCloudFrontCookies(signer *auth.CloudFrontSigner) func(http.Handler)})}}++// cookielessClientPlatforms are X-Client-Platform values sent by first-party+// clients that never store cookies (server/internal/cli and+// server/internal/daemon). Browser-based clients (web, desktop, mobile) are+// intentionally absent: they rely on the CDN cookies to load attachments.+var cookielessClientPlatforms = map[string]struct{}{+ "cli": {},+ "daemon": {},+}++// isCookielessClient reads the header directly rather than the+// ClientMetadata context value so the check does not depend on middleware+// ordering.+func isCookielessClient(r *http.Request) bool {+ _, ok := cookielessClientPlatforms[r.Header.Get(HeaderClientPlatform)]+ return ok+}diff --git a/server/internal/middleware/cloudfront_test.go b/server/internal/middleware/cloudfront_test.goindex 3f0273eaa5..fa9c9f7467 100644--- a/server/internal/middleware/cloudfront_test.go+++ b/server/internal/middleware/cloudfront_test.go@@ -198,3 +198,47 @@ func cloudFrontPolicies(rec *httptest.ResponseRecorder) []*http.Cookie {}return out}++// MUL-8190: CLI and daemon never keep cookies, so signing for them was pure+// RSA cost on every request. Browser-family platforms must keep receiving+// cookies, including when the header is absent.+func TestRefreshCloudFrontCookies_SkipsCookielessClients(t *testing.T) {+ signer := testSigner(t)++ cases := []struct {+ platform string+ wantCookies bool+ }{+ {platform: "cli", wantCookies: false},+ {platform: "daemon", wantCookies: false},+ {platform: "web", wantCookies: true},+ {platform: "desktop", wantCookies: true},+ {platform: "mobile", wantCookies: true},+ {platform: "", wantCookies: true},+ {platform: "CLI", wantCookies: true}, // exact match only+ }+ for _, tc := range cases {+ t.Run("platform="+tc.platform, func(t *testing.T) {+ nextCalled := false+ handler := RefreshCloudFrontCookies(signer)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ nextCalled = true+ w.WriteHeader(http.StatusOK)+ }))++ req := httptest.NewRequest(http.MethodGet, "/api/issues/x", nil)+ if tc.platform != "" {+ req.Header.Set(HeaderClientPlatform, tc.platform)+ }+ rec := httptest.NewRecorder()+ handler.ServeHTTP(rec, req)++ if !nextCalled {+ t.Fatal("next handler was not called")+ }+ got := len(rec.Result().Cookies()) > 0+ if got != tc.wantCookies {+ t.Fatalf("cookies set = %v, want %v", got, tc.wantCookies)+ }+ })+ }+}