mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
fix(auth): refuse oidc id tokens as access tokens
The gateway accepted an ID token when its aud named one of the configured audiences. Keycloak ID tokens carry no other marker. The gateway now refuses a token whose payload typ is ID, or whose header typ is id+jwt, in any letter case. The log reason is id_token. The header check runs before any key fetch. The payload check runs after the signature check. Access tokens still pass.
This commit is contained in:
@@ -861,8 +861,10 @@ How the gateway verifies a token:
|
||||
passes, and that service can replay it to the gateway. Prefer an
|
||||
audience mapper in the IdP that puts the gateway audience in `aud`. In
|
||||
Keycloak, add an "Audience" mapper to the Open WebUI client.
|
||||
- The gateway does not check the `typ` header, so an ID token whose `aud`
|
||||
names one of `audiences` also passes.
|
||||
- The gateway refuses a token whose payload `typ` is `ID` or whose header
|
||||
`typ` is `id+jwt` (any letter case), because that is an ID token. An
|
||||
IdP that marks ID tokens in another way still lets an ID token pass if
|
||||
its `aud` names one of `audiences`.
|
||||
- A bearer token over 16384 bytes gets 401 before any parse or IdP call.
|
||||
- **Opaque token.** Only with `userinfo: true`, the gateway sends the token
|
||||
to the IdP's `userinfo_endpoint` and reads the claims from a 200 reply.
|
||||
@@ -932,8 +934,8 @@ aud)`. For `no_group`, the line also names the identity. When the IdP
|
||||
cannot be reached, a separate error line gives the cause, for example an
|
||||
issuer mismatch. The gateway writes that line at most once per 30 seconds. The reasons are `expired`, `not_yet_valid`, `bad_sig`, `bad_alg`,
|
||||
`issuer`, `aud`, `unknown_kid`, `malformed`, `no_identity`, `no_group`,
|
||||
`email_unverified`, `userinfo_rejected`, `opaque_disabled` and
|
||||
`idp_unavailable`. The gateway never logs the token.
|
||||
`email_unverified`, `id_token`, `userinfo_rejected`, `opaque_disabled`
|
||||
and `idp_unavailable`. The gateway never logs the token.
|
||||
|
||||
To connect Open WebUI:
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ const (
|
||||
oidcReasonUserinfo = "userinfo_rejected"
|
||||
oidcReasonIDPUnavailable = "idp_unavailable"
|
||||
oidcReasonEmailUnverified = "email_unverified"
|
||||
oidcReasonIDToken = "id_token"
|
||||
)
|
||||
|
||||
// oidcCall is one verification in flight; waiters read slot after done.
|
||||
@@ -375,6 +376,8 @@ func oidcErrorReason(err error) string {
|
||||
return "unknown_kid"
|
||||
case errors.Is(err, errEmailNotVerified):
|
||||
return oidcReasonEmailUnverified
|
||||
case errors.Is(err, errTokenIsIDToken):
|
||||
return oidcReasonIDToken
|
||||
}
|
||||
return "malformed"
|
||||
}
|
||||
|
||||
@@ -308,3 +308,41 @@ func TestClaims_WholeKeyBeforeDottedPath(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticate_IDTokenRejectedByTyp(t *testing.T) {
|
||||
tests := []struct {
|
||||
payload any
|
||||
name string
|
||||
headerTyp string
|
||||
wantOK bool
|
||||
}{
|
||||
{name: "payload typ ID", headerTyp: "JWT", payload: "ID"},
|
||||
{name: "payload typ id lowercase", headerTyp: "JWT", payload: "id"},
|
||||
{name: "header typ id+jwt", headerTyp: "id+jwt"},
|
||||
{name: "header typ ID+JWT", headerTyp: "ID+JWT"},
|
||||
{name: "payload Bearer", headerTyp: "JWT", payload: "Bearer", wantOK: true},
|
||||
{name: "header at+jwt", headerTyp: "at+jwt", wantOK: true},
|
||||
{name: "no typ anywhere", headerTyp: "", wantOK: true},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
fx := newAuthFixture(t, nil)
|
||||
c := baseClaims(fx.clock.Now())
|
||||
c["iss"] = fx.idp.srv.URL
|
||||
if tc.payload != nil {
|
||||
c["typ"] = tc.payload
|
||||
}
|
||||
tok := signTestJWTTyp(t, tc.headerTyp, "RS256", "rsa", oidcKeys(t), nil, c)
|
||||
_, st, reason := fx.run(t, tok, nil)
|
||||
if tc.wantOK {
|
||||
if st != authOK {
|
||||
t.Fatalf("status %v reason %q, want accepted", st, reason)
|
||||
}
|
||||
return
|
||||
}
|
||||
if st != authUnauthorized || reason != oidcReasonIDToken {
|
||||
t.Fatalf("status %v reason %q, want 401 %q", st, reason, oidcReasonIDToken)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+19
@@ -27,11 +27,20 @@ var (
|
||||
errTokenAudience = errors.New("oidc: token audience not accepted")
|
||||
errTokenUnknownKid = errors.New("oidc: token signed by an unknown key")
|
||||
errEmailNotVerified = errors.New("oidc: email not verified")
|
||||
errTokenIsIDToken = errors.New("oidc: an ID token is not an access token")
|
||||
)
|
||||
|
||||
// oidcClockSkew tolerates small clock drift between the IdP and the gateway.
|
||||
const oidcClockSkew = 60 * time.Second
|
||||
|
||||
const (
|
||||
// oidcPayloadTypID is the payload typ Keycloak sets on ID tokens.
|
||||
oidcPayloadTypID = "id"
|
||||
// oidcHeaderTypID is the header typ of an ID token JWT (RFC 9068 names
|
||||
// "at+jwt" for access tokens).
|
||||
oidcHeaderTypID = "id+jwt"
|
||||
)
|
||||
|
||||
// jwk is one JSON Web Key (RFC 7517) as published in a JWKS document.
|
||||
type jwk struct {
|
||||
Kty string `json:"kty"`
|
||||
@@ -180,6 +189,7 @@ const oidcMinRSABits = 2048
|
||||
type jwtHeader struct {
|
||||
Alg string `json:"alg"`
|
||||
Kid string `json:"kid"`
|
||||
Typ string `json:"typ"`
|
||||
}
|
||||
|
||||
func splitJWT(token string) ([]string, *jwtHeader, error) {
|
||||
@@ -215,6 +225,9 @@ func jwtKid(token string) (string, error) {
|
||||
if _, ok := allowedJWSAlgs[hdr.Alg]; !ok {
|
||||
return "", errTokenBadAlg
|
||||
}
|
||||
if strings.EqualFold(hdr.Typ, oidcHeaderTypID) {
|
||||
return "", errTokenIsIDToken
|
||||
}
|
||||
return hdr.Kid, nil
|
||||
}
|
||||
|
||||
@@ -229,6 +242,9 @@ func verifyJWT(token string, keys *jwkSet, s *oidcSettings, now time.Time) (map[
|
||||
if !ok {
|
||||
return nil, errTokenBadAlg
|
||||
}
|
||||
if strings.EqualFold(hdr.Typ, oidcHeaderTypID) {
|
||||
return nil, errTokenIsIDToken
|
||||
}
|
||||
pub, err := keys.lookup(hdr.Kid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -318,6 +334,9 @@ func hashInput(h crypto.Hash, input []byte) []byte {
|
||||
}
|
||||
|
||||
func checkClaims(claims map[string]any, s *oidcSettings, now time.Time) error {
|
||||
if typ, _ := claims["typ"].(string); strings.EqualFold(typ, oidcPayloadTypID) {
|
||||
return errTokenIsIDToken
|
||||
}
|
||||
if iss, _ := claims["iss"].(string); iss != s.issuer {
|
||||
return errTokenIssuer
|
||||
}
|
||||
|
||||
+11
-1
@@ -98,7 +98,17 @@ func digestFor(alg string, data []byte) (crypto.Hash, []byte) {
|
||||
// "HS256" signs with secret.
|
||||
func signTestJWT(t *testing.T, alg, kid string, k *testKeys, secret []byte, claims map[string]any) string {
|
||||
t.Helper()
|
||||
hdr, err := json.Marshal(map[string]string{"alg": alg, "kid": kid, "typ": "JWT"})
|
||||
return signTestJWTTyp(t, "JWT", alg, kid, k, secret, claims)
|
||||
}
|
||||
|
||||
// signTestJWTTyp is signTestJWT with a chosen header typ ("" omits it).
|
||||
func signTestJWTTyp(t *testing.T, typ, alg, kid string, k *testKeys, secret []byte, claims map[string]any) string {
|
||||
t.Helper()
|
||||
h := map[string]string{"alg": alg, "kid": kid}
|
||||
if typ != "" {
|
||||
h["typ"] = typ
|
||||
}
|
||||
hdr, err := json.Marshal(h)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user