diff --git a/CHANGELOG.md b/CHANGELOG.md index 682cc50e..4858cfe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project are documented in this file. The format is b ### Security - **Company egress is promoted only from operator-approved company scope.** A sandboxed worker could register assets with `insert_assets` (an attacker IP or domain attributed to the company through its domain or ICP rule), and the agent could add rules with `add_company_scope`; the next operator selection or re-approval of the company copied those hosts into `task_scope_egress`, persistently widening the approved sandbox egress. The approval snapshot now copies only `company_scope` domain and IP/CIDR rules with the new `approved_by_operator` flag (migration `0025_company_scope_approved.sql`), which only the operator company API sets; `add_company_scope` and the ScopeSentry sync store rules with the flag off. Existing rules are backfilled as approved. Behaviour change: a domain or IP discovered only in assets is no longer reachable from the sandbox until it is added explicitly to the company scope (operator) or to the task scope. Egress rows already frozen by earlier approvals are not purged by this change, and re-approving the company does not remove them (the snapshot only adds rows); removing the company from the task scope drops its rows, and approving it again rebuilds them from approved rules only. +- **Session tokens accept only HS256 and carry an issuer and audience.** Token checks accepted any HMAC algorithm (HS384, HS512) signed with the session key; they now accept only HS256. New tokens have `iss: artex` and `aud: artex-api`, and a token that has either claim with another value is rejected. Tokens issued by earlier releases have neither claim and stay valid until they expire, so upgrading does not sign anyone out. ### Added diff --git a/server/auth.go b/server/auth.go index e218668a..a7cf109a 100644 --- a/server/auth.go +++ b/server/auth.go @@ -13,6 +13,7 @@ import ( "net/url" "os" "path/filepath" + "slices" "strconv" "strings" "time" @@ -175,6 +176,13 @@ func samePath(a, b string) bool { return a == b } +// jwtIssuer and jwtAudience go into every new session token. Tokens issued +// before they were added carry neither claim and are still accepted. +const ( + jwtIssuer = "artex" + jwtAudience = "artex-api" +) + // sessionClaims carries the password version the token was signed under. // Two changes in the same second get different versions, so a cutoff based on // the issued-at second cannot leave the older token valid. @@ -193,6 +201,8 @@ func signJWTVersion(key []byte, version int64) (string, error) { return jwt.NewWithClaims(jwt.SigningMethodHS256, sessionClaims{ Pwd: version, RegisteredClaims: jwt.RegisteredClaims{ + Issuer: jwtIssuer, + Audience: jwt.ClaimStrings{jwtAudience}, Subject: "ARTEX", ExpiresAt: jwt.NewNumericDate(time.Now().Add(jwtTTL)), IssuedAt: jwt.NewNumericDate(time.Now()), @@ -200,18 +210,33 @@ func signJWTVersion(key []byte, version int64) (string, error) { }).SignedString(key) } -// verifyJWT returns true when tokenStr is a valid, non-expired HS256 token. -// version 0 means no password change is stored yet, and any valid token is -// accepted. A later version requires the token's pwd claim to equal it. -func verifyJWT(tokenStr string, key []byte, version int64) bool { +// parseSessionJWT returns the claims of tokenStr when it is a valid, +// non-expired session token. Only HS256 is accepted. The iss and aud claims +// are checked when present; tokens issued before they were added have neither. +func parseSessionJWT(tokenStr string, key []byte) (*sessionClaims, bool) { claims := &sessionClaims{} - t, err := jwt.ParseWithClaims(tokenStr, claims, func(t *jwt.Token) (any, error) { - if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("unexpected signing method") - } + t, err := jwt.ParseWithClaims(tokenStr, claims, func(*jwt.Token) (any, error) { return key, nil - }) + }, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()})) if err != nil || !t.Valid { + return nil, false + } + if claims.Issuer != "" && claims.Issuer != jwtIssuer { + return nil, false + } + if len(claims.Audience) > 0 && !slices.Contains(claims.Audience, jwtAudience) { + return nil, false + } + return claims, true +} + +// verifyJWT returns true when tokenStr is a valid, non-expired session token +// (parseSessionJWT). version 0 means no password change is stored yet, and any +// valid token is accepted. A later version requires the token's pwd claim to +// equal it. +func verifyJWT(tokenStr string, key []byte, version int64) bool { + claims, ok := parseSessionJWT(tokenStr, key) + if !ok { return false } if version > 0 && claims.Pwd != version { @@ -251,16 +276,10 @@ func (s *Server) applyPasswordVersion(n int64) { } // jwtSubject returns the subject of tokenStr when it is a valid, non-expired -// HS256 token, and "" otherwise. +// session token (parseSessionJWT), and "" otherwise. func jwtSubject(tokenStr string, key []byte) string { - var claims jwt.RegisteredClaims - t, err := jwt.ParseWithClaims(tokenStr, &claims, func(t *jwt.Token) (any, error) { - if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("unexpected signing method") - } - return key, nil - }) - if err != nil || !t.Valid { + claims, ok := parseSessionJWT(tokenStr, key) + if !ok { return "" } return claims.Subject diff --git a/server/auth_jwt_test.go b/server/auth_jwt_test.go new file mode 100644 index 00000000..90f220fc --- /dev/null +++ b/server/auth_jwt_test.go @@ -0,0 +1,92 @@ +package server + +import ( + "slices" + "testing" + "time" + + "github.com/golang-jwt/jwt/v5" +) + +func testJWTClaims(iss string, aud []string, pwd int64) sessionClaims { + return sessionClaims{ + Pwd: pwd, + RegisteredClaims: jwt.RegisteredClaims{ + Issuer: iss, + Audience: aud, + Subject: "ARTEX", + ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Hour)), + IssuedAt: jwt.NewNumericDate(time.Now()), + }, + } +} + +func signTestJWT(t *testing.T, method jwt.SigningMethod, key any, c sessionClaims) string { + t.Helper() + tok, err := jwt.NewWithClaims(method, c).SignedString(key) + if err != nil { + t.Fatal(err) + } + return tok +} + +// New tokens carry iss and aud and are accepted. +func TestSignJWTSetsIssuerAndAudience(t *testing.T) { + key := []byte("0123456789abcdef0123456789abcdef") + tok, err := signJWTVersion(key, 3) + if err != nil { + t.Fatal(err) + } + claims, ok := parseSessionJWT(tok, key) + if !ok { + t.Fatal("new token rejected") + } + if claims.Issuer != jwtIssuer || !slices.Equal(claims.Audience, jwt.ClaimStrings{jwtAudience}) { + t.Errorf("iss=%q aud=%v, want %q and [%q]", claims.Issuer, claims.Audience, jwtIssuer, jwtAudience) + } + if !verifyJWT(tok, key, 3) || jwtSubject(tok, key) != "ARTEX" { + t.Error("new token not accepted by verifyJWT or jwtSubject") + } +} + +func TestVerifyJWTClaimsAndAlgorithm(t *testing.T) { + key := []byte("0123456789abcdef0123456789abcdef") + aud := []string{jwtAudience} + for _, tc := range []struct { + name string + tok string + accept bool + }{ + // Tokens issued by earlier releases have no iss or aud and keep working. + {"legacy claims, HS256", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims("", nil, 0)), true}, + {"iss without aud", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims(jwtIssuer, nil, 0)), true}, + {"aud without iss", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims("", aud, 0)), true}, + {"aud list with ours", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims(jwtIssuer, []string{"other", jwtAudience}, 0)), true}, + {"wrong iss", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims("someone-else", aud, 0)), false}, + {"wrong aud", signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims(jwtIssuer, []string{"other-api"}, 0)), false}, + {"HS384", signTestJWT(t, jwt.SigningMethodHS384, key, testJWTClaims(jwtIssuer, aud, 0)), false}, + {"HS512", signTestJWT(t, jwt.SigningMethodHS512, key, testJWTClaims(jwtIssuer, aud, 0)), false}, + {"HS512 legacy claims", signTestJWT(t, jwt.SigningMethodHS512, key, testJWTClaims("", nil, 0)), false}, + {"alg none", signTestJWT(t, jwt.SigningMethodNone, jwt.UnsafeAllowNoneSignatureType, testJWTClaims(jwtIssuer, aud, 0)), false}, + {"other key", signTestJWT(t, jwt.SigningMethodHS256, []byte("another-key-another-key-another-k"), testJWTClaims(jwtIssuer, aud, 0)), false}, + } { + if got := verifyJWT(tc.tok, key, 0); got != tc.accept { + t.Errorf("%s: verifyJWT=%v, want %v", tc.name, got, tc.accept) + } + if got := jwtSubject(tc.tok, key) == "ARTEX"; got != tc.accept { + t.Errorf("%s: jwtSubject accepted=%v, want %v", tc.name, got, tc.accept) + } + } +} + +// The password version check still applies to legacy tokens. +func TestVerifyJWTLegacyTokenPasswordVersion(t *testing.T) { + key := []byte("0123456789abcdef0123456789abcdef") + tok := signTestJWT(t, jwt.SigningMethodHS256, key, testJWTClaims("", nil, 2)) + if !verifyJWT(tok, key, 2) { + t.Error("legacy token at the current password version rejected") + } + if verifyJWT(tok, key, 3) { + t.Error("legacy token at an old password version accepted") + } +}