Skip to content

Agent Mesh

console: stop handing the session cookie back to JavaScript

Antonio Ojea8d93019b4282
diff --git a/internal/console/server.go b/internal/console/server.go
index c79c6818..ad8a3b5d 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -339,6 +339,12 @@ func (s *Server) HandleCallback(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, s.cfg.BasePath+"/", http.StatusFound)
}
+// HandleSession reports whether a session cookie is present. It deliberately
+// does not return the cookie's value: the credential is stored HttpOnly so an
+// XSS in the console cannot exfiltrate mesh admin rights, and handing the raw
+// token back to any same-origin fetch would undo exactly that. The SPA never
+// needs it, since the reverse proxy injects the cookie as Authorization for
+// /api/ calls.
func (s *Server) HandleSession(w http.ResponseWriter, r *http.Request) {
sessionCookie, err := r.Cookie("sam_session")
if err != nil || sessionCookie.Value == "" {
@@ -350,8 +356,8 @@ func (s *Server) HandleSession(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
- _ = json.NewEncoder(w).Encode(map[string]string{
- "token": sessionCookie.Value,
+ _ = json.NewEncoder(w).Encode(map[string]bool{
+ "authenticated": true,
})
}
diff --git a/internal/console/session_test.go b/internal/console/session_test.go
new file mode 100644
index 00000000..5783f0b5
--- /dev/null
+++ b/internal/console/session_test.go
@@ -0,0 +1,60 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package console
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+// TestHandleSessionDoesNotReturnTheToken is the regression test for the endpoint
+// that undid its own defence: the session credential is stored HttpOnly so an
+// XSS in the console cannot exfiltrate mesh admin rights, and /auth/session read
+// that cookie back and handed the raw value to any same-origin fetch. The SPA
+// never needs it, since the reverse proxy injects the cookie as Authorization.
+func TestHandleSessionDoesNotReturnTheToken(t *testing.T) {
+ const secret = "super-secret-mesh-admin-token"
+ srv := &Server{}
+
+ t.Run("no session", func(t *testing.T) {
+ rec := httptest.NewRecorder()
+ srv.HandleSession(rec, httptest.NewRequest(http.MethodGet, "/auth/session", nil))
+
+ if rec.Code != http.StatusUnauthorized {
+ t.Errorf("status = %d, want %d", rec.Code, http.StatusUnauthorized)
+ }
+ })
+
+ t.Run("active session", func(t *testing.T) {
+ req := httptest.NewRequest(http.MethodGet, "/auth/session", nil)
+ req.AddCookie(&http.Cookie{Name: "sam_session", Value: secret})
+
+ rec := httptest.NewRecorder()
+ srv.HandleSession(rec, req)
+
+ if rec.Code != http.StatusOK {
+ t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
+ }
+ body := rec.Body.String()
+ if strings.Contains(body, secret) {
+ t.Errorf("response body leaks the session token: %s", body)
+ }
+ if !strings.Contains(body, "authenticated") {
+ t.Errorf("response body = %s, want it to report session state", body)
+ }
+ })
+}