test(auth): pin the cheap pre-auth checks for oidc tokens

A token of the maximum size with a disallowed alg is refused from the
header alone. It costs no IdP call and no payload decode. A bad
signature is refused before the payload decode.

The new tests pin both facts, so a later change cannot move the costly
steps ahead of the cheap ones.
This commit is contained in:
2026-12-02 02:02:45 +00:00
parent 0f14cd8ad5
commit 2998e3a1bc
+37
View File
@@ -346,3 +346,40 @@ func TestAuthenticate_IDTokenRejectedByTyp(t *testing.T) {
})
}
}
// A token of the maximum size with a disallowed alg is refused from its
// header alone: no IdP call, and the oversized payload is never decoded.
func TestAuthenticate_MaxSizeBadAlgTokenCostsNoIdPCall(t *testing.T) {
for _, alg := range []string{"none", "HS256", "bogus"} {
t.Run(alg, func(t *testing.T) {
fx := newAuthFixture(t, nil)
hdr := b64([]byte(`{"alg":"` + alg + `","kid":"rsa"}`))
// "!" is not base64url: a payload decode would fail as malformed.
filler := strings.Repeat("!", oidcMaxTokenBytes-len(hdr)-len(".."))
tok := hdr + "." + filler + "."
if len(tok) != oidcMaxTokenBytes {
t.Fatalf("token is %d bytes, want %d", len(tok), oidcMaxTokenBytes)
}
_, st, reason := fx.run(t, tok, nil)
if st != authUnauthorized || reason != "bad_alg" {
t.Fatalf("status %v reason %q, want 401 bad_alg", st, reason)
}
d, j, u := fx.idp.discoveryHits.Load(), fx.idp.jwksHits.Load(), fx.idp.userinfoHits.Load()
if d+j+u != 0 {
t.Fatalf("IdP hits discovery %d jwks %d userinfo %d, want 0", d, j, u)
}
})
}
}
// A signature that fails is refused before the payload is decoded: the
// payload here is invalid base64, yet the reason is bad_sig, not malformed.
func TestVerifyJWT_SignatureBeforePayloadDecode(t *testing.T) {
fx := newAuthFixture(t, nil)
hdr := b64([]byte(`{"alg":"RS256","kid":"rsa"}`))
tok := hdr + ".!!!!." + b64([]byte("not a signature"))
_, st, reason := fx.run(t, tok, nil)
if st != authUnauthorized || reason != "bad_sig" {
t.Fatalf("status %v reason %q, want 401 bad_sig", st, reason)
}
}