Skip to content

Return invalid_client for failed client assertions - #7036

Merged
jhrozek merged 3 commits into
stacklok:mainfrom
pcbeingused333:fix/client-assertion-invalid-client
Nov 3, 2026
Merged

jhrozek merged 3 commits into
stacklok:mainfrom
pcbeingused333:fix/client-assertion-invalid-client

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Closes #7028

Summary

A failed private_key_jwt client assertion got a different answer depending on which check failed:

  • expired, nbf in the future, or iat in the future: 500 server_error (fosite returns the claim validation error unwrapped);
  • exp beyond MaxAssertionLifespan: 500 server_error (SetClientAssertionJWT returned a plain error, in both the memory and Redis stores);
  • kid not in the client's key set: 400 invalid_request;
  • reused jti: 400 jti_known.

RFC 6749 §5.2 assigns invalid_client (401) to every failed client authentication, and clients treat server_error as a retryable outage. This covers the cases in #7028 and the two folded in from #6995.

  • New newClientAssertionErrorStrategy (pkg/authserver/server/client_assertion_errors.go) wraps fosite's default client authentication strategy, inside the SPIFFE dispatcher. For jwt-bearer assertions it turns the claim validation errors, invalid_request (unknown key) and jti_known into invalid_client. Other errors, such as a storage outage or a server misconfiguration, pass through unchanged and stay 500s.
  • SetClientAssertionJWT in MemoryStorage and RedisStorage returns invalid_client when the lifetime exceeds the maximum.
  • The two integration tests that pinned the old codes (jti_known on replay, 400 for a rotated-out kid) now expect 401 invalid_client.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

TestClientAssertionErrorStrategy runs real fosite client authentication with an ES256 client: a valid assertion still authenticates; expired, future iat, future nbf, reused jti, unknown kid, wrong signing key and wrong aud all give 401 invalid_client; a storage failure is returned untouched. Lifetime-limit tests were added for both storage backends. go test ./pkg/authserver/... passes; without the change the updated integration and storage tests fail.

Does this introduce a user-facing change?

Yes. Every rejected private_key_jwt client assertion now gets 401 invalid_client from the token endpoint instead of 500 server_error, 400 invalid_request or 400 jti_known. A storage failure while checking the jti is still 500 server_error; a missing client_assertion is 400 invalid_request.

Special notes for reviewers

The optional strict-audience setting (issuer as the only aud value) from #7028 is not included here, and clock skew stays with #6954. Happy to add the strict audience in this PR or a follow-up, whichever you prefer.

Without a clock-skew margin (#6954), a client whose clock runs slightly fast now gets 401 invalid_client for a future iat instead of a 500.


This pull request was prepared with the assistance of an AI coding agent (Claude Code) and submitted by Alex Castillo, who is responsible for its content.

…wt assertion

An expired, not-yet-valid or future-issued client assertion surfaced as a
plain error (500), an assertion whose exp exceeds MaxAssertionLifespan as a
plain error from SetClientAssertionJWT (500), and an unknown signing key or
a reused jti as invalid_request / jti_known (400). RFC 6749 section 5.2
assigns invalid_client to every failed client authentication.

Map those outcomes to invalid_client in a strategy wrapped around fosite's
default one, and have both storage backends return invalid_client for an
over-long lifetime. Storage failures keep surfacing as server errors.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Alex Castillo <[email protected]>

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up. The remap fixes the cases from #7028 and #6995 (I checked it against fosite v0.49.0). There are a few changes needed before merge, listed below in order of importance. Each inline comment says what to change and which test should prove it.

  1. A Redis failure during the jti check must stay a 500 (inline on client_assertion_errors.go:40). This is the only change that needs some design. I've sketched it there.
  2. The integration test for the expired case must assert the status code (inline on integration_test.go, about line 6810).
  3. Keep a missing client_assertion as 400 invalid_request (inline on client_assertion_errors.go:41).
  4. Fix the doc comment and add the two comments explaining the matching (inline).
  5. Docs: add this paragraph to docs/arch/17-token-exchange-delegation.md, after "rejects replay while the assertion is valid. No client_secret …" (around line 344):
    Every failed assertion check (unknown `kid`, bad signature, wrong `iss`, `sub`
    or `aud`, expired or not yet valid, `exp` more than 5 minutes ahead, reused
    `jti`) is refused with HTTP 401 `invalid_client`. The `error_description`
    names the check that failed. A storage failure while checking or recording
    the `jti` is HTTP 500 `server_error`, so clients can retry it. A request
    that sets `client_assertion_type` but omits `client_assertion` is HTTP 400
    `invalid_request`.
    Also add a before/after line under "Does this introduce a user-facing change?" for the storage-failure and missing-assertion cases.
  6. PR housekeeping:
    • Change Fixes #7028 to Part of #7028. The issue's acceptance also includes the strict issuer-only aud check, which this PR doesn't do.
    • Drop the fix(authserver): prefix from the title and the commit subject. This repo doesn't use conventional commits (CONTRIBUTING.md). Something like "Return invalid_client for failed client assertions" works.
    • Add one line under special notes: "Without a clock-skew margin (#6954), a client whose clock runs slightly fast now gets 401 invalid_client for a future iat instead of a 500."

You don't need to do #6954 (clock skew) or the strict audience check here.

Comment thread pkg/authserver/integration_test.go Resolved
Comment thread pkg/authserver/storage/memory_test.go Resolved
pcbeingused333 and others added 2 commits November 3, 2026 07:25
…n errors

Address review: a Redis failure while reserving the jti stays HTTP 500
server_error (recorded through RecordClientAssertionStoreFailure, since
fosite discards the storage error), a missing client_assertion stays 400
invalid_request, the expired integration case asserts 401 invalid_client,
and the matching rules and docs are explained.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Alex Castillo <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Alex Castillo <[email protected]>
@pcbeingused333 pcbeingused333 changed the title fix(authserver): return invalid_client for every failed private_key_jwt assertion Return invalid_client for failed client assertions Nov 3, 2026
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks, all points addressed in the new commit:

  1. A Redis failure while reserving the jti stays a 500: RedisStorage.ClientAssertionJWTValid calls server.RecordClientAssertionStoreFailure in the non-redis.Nil branch only, and the strategy wrapper returns server_error when something was recorded. I added an exported NewClientAssertionStoreFailureContext helper so the Redis test can set up the recorder from the storage package. Tests: stub case that records and returns an error (500 server_error), ErrJTIKnown without a record (401 invalid_client), and Redis cases with mr.Close() (recorded) and a replay (not recorded).
  2. The wrong_audience, wrong_subject and expired integration subtests now assert 401 and invalid_client.
  3. A missing client_assertion skips the remap and stays 400 invalid_request, with a test case.
  4. Doc comment updated, and the two matching comments added.
  5. Paragraph added to docs/arch/17-token-exchange-delegation.md.
  6. PR body now says Part of #7028, with the clock-skew note added; title changed to "Return invalid_client for failed client assertions". The memory test now lives in TestMemoryStorage_ClientAssertionJWT.

This comment was drafted with Claude Code.

@jhrozek

jhrozek commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Thanks, all six review threads are addressed, I resolved them.

On the strict audience question: let's leave it out of this PR. It changes which assertions are accepted, not what we return on failure, and it needs a migration option, so I'll split it out of #7028 into its own issue. That way this PR covers everything left in #7028, so please change "Part of #7028" to "Closes #7028" in the description.

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks for the quick turnaround. Please do switch the description to "Closes #7028" before merging.

@jhrozek
jhrozek merged commit 3c8d5cb into stacklok:main Nov 3, 2026
13 of 17 checks passed
@codecov

codecov Bot commented Nov 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.50%. Comparing base (ccdcf6d) to head (adea630).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7036      +/-   ##
==========================================
+ Coverage   80.46%   80.50%   +0.04%     
==========================================
  Files         852      853       +1     
  Lines       87035    87063      +28     
==========================================
+ Hits        70030    70094      +64     
+ Misses      17000    16964      -36     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhrozek jhrozek mentioned this pull request Nov 3, 2026
2 of 10 tasks
jhrozek added a commit that referenced this pull request Nov 3, 2026
The private_key_jwt invalid_client remap (#7036) merged before its lint
and e2e jobs finished, and both fail on main. The virtualmcp e2e spec
still expected a replayed assertion to get 400 jti_known; it now gets
401 invalid_client, as intended. gosec G101 flagged the assertion type
constant because its name contained "Bearer", so it is renamed, and an
unused test parameter is blanked for revive.

Co-authored-by: Mecatl <[email protected]>
jhrozek added a commit that referenced this pull request Nov 3, 2026
Failed client assertions now return 401 invalid_client (#7036), including
when the kid is no longer registered. The JWKS rollout e2e spec still
expected 400 invalid_request for the retired key, so it failed on every
Kubernetes version.

Co-authored-by: Mecatl <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Authorization server: refuse every failed private_key_jwt client assertion with 401 invalid_client

2 participants