From 2998e3a1bc75b33f455db347659c80f16b263f21 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Wed, 2 Dec 2026 01:55:37 +0000 Subject: [PATCH] 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. --- oidc_auth_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/oidc_auth_test.go b/oidc_auth_test.go index c49d6a0..470646a 100644 --- a/oidc_auth_test.go +++ b/oidc_auth_test.go @@ -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) + } +}