Skip to content

private_key_jwt authentication failures return 500 or 400 instead of 401 invalid_client #6995

Closed as not planned

Description

@jhrozek

Bug description

When a private_key_jwt client fails authentication at the token endpoint, the embedded authorization server often does not answer 401 invalid_client. RFC 6749 section 5.2 says a failed client authentication is invalid_client, so a client cannot tell what went wrong.

Reported by Chris Burns while reviewing #6953, who saw:

  • an iat slightly in the future, an expired assertion, or a lifetime over 5 minutes returns HTTP 500 with "error":"error";
  • an unknown kid returns HTTP 400 invalid_request.

I traced the causes in the code (fosite v0.49.0 and ToolHive's storage). I did not reproduce them with a test.

Steps to reproduce

Authenticate a private_key_jwt client at /oauth/token with an assertion that:

  1. has an iat or exp that fails claim validation (future iat, past exp), or
  2. has an exp more than 5 minutes ahead, or
  3. carries a kid that is not in the client's JWKS.

This applies to any private_key_jwt client, DCR-registered or configured.

Expected behavior

401 invalid_client with a hint that says why (assertion expired, not yet valid, lifetime too long, unknown key id). A key that is not registered yet, for example during a key rotation, should read as a client configuration fault, not as a problem with the user's access token.

Actual behavior

  • Claim validation failures (future iat, past exp). In client_authentication.go fosite returns the raw inner error (return nil, e.Inner, lines 137 to 141). It is not an RFC 6749 error, and ErrorToRFC6749Error (errors.go:279) turns an unrecognised error into HTTP 500 with the error name error.
  • Lifetime over 5 minutes. fosite calls SetClientAssertionJWT and returns its error unchanged (lines 179 to 181). ToolHive's storage returns a plain fmt.Errorf("assertion lifetime exceeds maximum of ...") in both backends (pkg/authserver/storage/memory.go:978, redis.go:978), so this is also a 500. This one is ToolHive's own code.
  • Unknown kid. fosite's findPublicKey returns ErrInvalidRequest (lines 280, 299, 301), which is 400 invalid_request.

Additional context

Proposed fix

  • In both storage backends, return fosite.ErrInvalidClient with a hint from SetClientAssertionJWT when the lifetime is too long.
  • In the client authentication wrapper, for requests that carry a client_assertion: map non-RFC 6749 errors from claim validation to invalid_client, and map "unknown kid" and "no matching key" to invalid_client. Leave genuine storage failures as 500.
  • Keep the specific reason in the error hint, and never include assertion or key material in it.

Acceptance criteria

  • A future iat, an expired assertion, a lifetime over 5 minutes, and an unknown kid each return 401 invalid_client with a hint that names the cause.
  • A real storage failure still returns 500.
  • Tests cover each case for a DCR client and a configured client, on the memory and Redis backends.

Activity

  1. added
    bug
    on Oct 29, 2026
  2. added
    needs-triage
    on Oct 29, 2026
  3. pcbeingused333 commented on Nov 1, 2026

    @pcbeingused333
    Contributor

    I'd like to work on this.

    I have a fix ready that maps these private_key_jwt assertion failures (future iat, expired exp, lifetime over the limit, unknown kid) to 401 invalid_client per RFC 6749 §5.2, with tests for each case in pkg/authserver/server and pkg/authserver/storage. Happy to open the PR once assigned.

  4. jhrozek commented on Nov 3, 2026

    @jhrozek
    ContributorAuthor

    Closing as a duplicate of "Authorization server: refuse every failed private_key_jwt client assertion with 401 invalid_client" (#7028). It covers the same errors and is linked under the epic "Authorization server: refresh tokens and ID-JAG issuance for delegated agents" (#6886). The two cases only this issue lists, an unknown kid and an iat slightly in the future, are copied to #7028.

  5. jhrozek commented on Nov 3, 2026

    @jhrozek
    ContributorAuthor

    @pcbeingused333 thank you, I will assign you the issue that turned out to be a duplicate instead.

  6. removed
    needs-triage
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    authenticationbugoauth

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions