Skip to content

Agent Mesh

test: cover the logout deletion cookie and forged-token claims

Antonio Ojea6859ce081d97
diff --git a/internal/console/server_test.go b/internal/console/server_test.go
index 0c42211f..f41f324c 100644
--- a/internal/console/server_test.go
+++ b/internal/console/server_test.go
@@ -528,6 +528,51 @@ func TestHandleTokenLogin(t *testing.T) {
})
}
+// A deletion cookie must carry the same attributes as the cookie it clears,
+// or browsers may keep the original session cookie alive after logout.
+func TestHandleLogoutCookieAttributes(t *testing.T) {
+ controlPlane := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ }))
+ defer controlPlane.Close()
+
+ srv, err := NewServer(Config{
+ ControlPlaneURL: controlPlane.URL,
+ AdminToken: "test-admin-token",
+ StaticDir: t.TempDir(),
+ BasePath: "/console",
+ })
+ if err != nil {
+ t.Fatalf("failed to create server: %v", err)
+ }
+
+ req := httptest.NewRequest(http.MethodGet, "/console/auth/logout", nil)
+ rec := httptest.NewRecorder()
+ srv.HandleLogout(rec, req)
+
+ var cookie *http.Cookie
+ for _, c := range rec.Result().Cookies() {
+ if c.Name == "sam_session" {
+ cookie = c
+ }
+ }
+ if cookie == nil {
+ t.Fatal("logout did not set a sam_session deletion cookie")
+ }
+ if cookie.MaxAge >= 0 {
+ t.Errorf("MaxAge = %d, want negative to delete the cookie", cookie.MaxAge)
+ }
+ if !cookie.HttpOnly {
+ t.Error("deletion cookie is not HttpOnly, unlike the session cookie it clears")
+ }
+ if cookie.SameSite != http.SameSiteLaxMode {
+ t.Errorf("SameSite = %v, want Lax to match the session cookie", cookie.SameSite)
+ }
+ if cookie.Path != "/console/" {
+ t.Errorf("cookie path = %q, want %q", cookie.Path, "/console/")
+ }
+}
+
// BasePath is concatenated into cookie paths, redirect URLs and mux patterns, so malformed
// flag values (trailing slash, missing leading slash) must be normalized where the flag is read.
func TestNormalizeBasePath(t *testing.T) {
diff --git a/internal/identity/oidc.go b/internal/identity/oidc.go
index 67df8681..efc821e5 100644
--- a/internal/identity/oidc.go
+++ b/internal/identity/oidc.go
@@ -98,7 +98,7 @@ func VerifyJWT(ctx context.Context, jwtStr string, allowedAudiences []string, pr
// Return claims decoded from the verified token, not the unverified
// pre-parse used above only to route to the right issuer.
- verifiedClaims := jwt.MapClaims{}
+ var verifiedClaims jwt.MapClaims
if err := token.Claims(&verifiedClaims); err != nil {
return nil, nil, fmt.Errorf("failed to decode verified claims: %w", err)
}
diff --git a/internal/identity/oidc_test.go b/internal/identity/oidc_test.go
index e418f621..ed9b1f17 100644
--- a/internal/identity/oidc_test.go
+++ b/internal/identity/oidc_test.go
@@ -23,6 +23,7 @@ import (
"math/big"
"net/http"
"net/http/httptest"
+ "strings"
"testing"
"time"
@@ -215,4 +216,31 @@ func TestVerifyJWT(t *testing.T) {
t.Fatal("expected error for bad signature")
}
})
+
+ t.Run("tampered payload yields no claims", func(t *testing.T) {
+ // Forge a token by swapping the payload of a validly signed token
+ // while keeping the original signature: the routing claims still
+ // parse, but verification must reject it and no claims may escape.
+ tokenStr := signToken(t, key, testKID, validClaims())
+ parts := strings.Split(tokenStr, ".")
+ forgedClaims := validClaims()
+ forgedClaims["sub"] = "attacker"
+ payload, err := json.Marshal(forgedClaims)
+ if err != nil {
+ t.Fatalf("failed to marshal forged claims: %v", err)
+ }
+ parts[1] = base64.RawURLEncoding.EncodeToString(payload)
+ forged := strings.Join(parts, ".")
+
+ claims, idToken, err := VerifyJWT(ctx, forged, allowedAudiences, providers)
+ if err == nil {
+ t.Fatal("expected error for tampered payload")
+ }
+ if claims != nil {
+ t.Fatalf("claims leaked from an unverified token: %v", claims)
+ }
+ if idToken != nil {
+ t.Fatal("ID token returned for an unverified token")
+ }
+ })
}