Skip to content

fix(oidc): map ErrSubjectInvalid to invalid_grant instead of server_error - #950

Open
youdie006 wants to merge 1 commit into
zitadel:mainfrom
youdie006:fix/945-subject-invalid-invalid-grant
Open

fix(oidc): map ErrSubjectInvalid to invalid_grant instead of server_error#950
youdie006 wants to merge 1 commit into
zitadel:mainfrom
youdie006:fix/945-subject-invalid-invalid-grant

Conversation

@youdie006

Copy link
Copy Markdown

Which Problems Are Solved

  • A JWT-profile (RFC 7523) client assertion or authorization grant where iss != sub is rejected with ErrSubjectInvalid ("delegation not allowed, issuer and sub must be identical"). DefaultToServerError did not list ErrSubjectInvalid in its invalid_grant case, so it fell through to default: and was returned as server_error (HTTP 500) instead of invalid_grant (HTTP 400).
  • This surfaces at the token endpoint as a spurious 500 for a request that is simply invalid, and (because server_error is logged at error level) generates misleading error-level noise for ordinary bad client input.

How the Problems Are Solved

  • Add errors.Is(err, ErrSubjectInvalid) to the invalid_grant case in DefaultToServerError, immediately after the sibling ErrSubjectMissing entry. ErrSubjectMissing (the other subject-validation failure) was already mapped to invalid_grant; ErrSubjectInvalid being absent was a clear oversight.
  • Per RFC 7523 / OpenID Connect, a rejected client assertion is invalid_grant. The fix only corrects the HTTP status/error code of an already-rejected request; it weakens no security check -- an assertion with iss != sub is still rejected exactly as before.

Additional Changes

  • Add TestDefaultToServerError_SubjectInvalid asserting ErrSubjectInvalid maps to InvalidGrant, matching the existing test style in pkg/oidc/error_test.go.

Additional Context

…rror

A JWT-profile (RFC 7523) grant where iss != sub is rejected with
ErrSubjectInvalid. DefaultToServerError did not list ErrSubjectInvalid in its
invalid_grant case, so it fell through to default and was returned as
server_error (HTTP 500) instead of invalid_grant (HTTP 400) - a spurious 500 for
a merely-invalid request, logged at error level. Its sibling ErrSubjectMissing
was already mapped to invalid_grant; ErrSubjectInvalid being absent was an
oversight. The fix only corrects the error code of an already-rejected request
and weakens no security check.

Closes zitadel#945

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7cc22b6b6a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pkg/oidc/error.go
oidcErr = ErrInvalidRequest().WithParent(err).WithDescription("%s", description)
case errors.Is(err, ErrIssuerInvalid),
errors.Is(err, ErrSubjectMissing),
errors.Is(err, ErrSubjectInvalid),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve invalid_client for private-key JWT assertions

When a private_key_jwt used to authenticate an authorization-code or refresh-token request has iss != sub, VerifyJWTAssertion returns this same sentinel and AuthorizePrivateJWTKey propagates it unchanged, so RequestError now reports invalid_grant. RFC 7523 §3.2 requires invalid_client for an invalid JWT used as client authentication; invalid_grant applies to the JWT authorization-grant flow in §3.1. Because this verifier is shared by both modes, translate the error in the mode-specific callers rather than mapping it globally here.

Useful? React with 👍 / 👎.

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.

[Bug]: JWT-profile assertion with iss != sub returns 500 server_error instead of 400 invalid_grant

1 participant