Improve token exchange errors#4313
Conversation
|
Warning Review limit reached
Next review available in: 12 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughChangesOAuth rejection reporting
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant JWTBearerGrantHandler
participant TokenExchangeGrantHandler
participant TokenValidator
participant OAuthClient
JWTBearerGrantHandler->>TokenValidator: ValidateIDJAGAssertion
TokenExchangeGrantHandler->>TokenValidator: ValidateSubjectToken or actor_token
TokenValidator-->>JWTBearerGrantHandler: Classified validation error
TokenValidator-->>TokenExchangeGrantHandler: Classified validation error
JWTBearerGrantHandler-->>OAuthClient: invalid_grant with rejection description
TokenExchangeGrantHandler-->>OAuthClient: invalid_request or server_error response
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@backend/internal/oauth/oauth2/tokenservice/validator.go`:
- Around line 265-266: Extend validator_test.go to cover each new issuer,
audience, and expiry classification path, asserting errors.Is against the
corresponding sentinel error even when the underlying errors are mocked.
Preserve the existing validation setup and verify the returned errors retain the
expected sentinel classification.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: b680b00c-9ff5-416e-890f-40c5402280fd
📒 Files selected for processing (7)
backend/internal/oauth/oauth2/granthandlers/jwt_bearer.gobackend/internal/oauth/oauth2/granthandlers/jwt_bearer_test.gobackend/internal/oauth/oauth2/granthandlers/token_exchange.gobackend/internal/oauth/oauth2/granthandlers/token_exchange_test.gobackend/internal/oauth/oauth2/tokenservice/error_constants.gobackend/internal/oauth/oauth2/tokenservice/validator.godocs/content/guides/protocols/oauth-oidc/token-exchange.mdx
0680089 to
7b7ed80
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@docs/content/guides/protocols/oauth-oidc/token-exchange.mdx`:
- Around line 111-118: Revise the token rejection documentation around the
subject_token validation table to scope the invalid_request response to
token-validation failures only. Add a note that revocation enforcement being
unavailable causes the handler to return server_error instead, so the section
does not imply every rejected subject_token produces invalid_request.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 708a4e3b-8432-4048-b84f-91a7d2d84fdf
📒 Files selected for processing (8)
backend/internal/oauth/oauth2/granthandlers/jwt_bearer.gobackend/internal/oauth/oauth2/granthandlers/jwt_bearer_test.gobackend/internal/oauth/oauth2/granthandlers/token_exchange.gobackend/internal/oauth/oauth2/granthandlers/token_exchange_test.gobackend/internal/oauth/oauth2/tokenservice/error_constants.gobackend/internal/oauth/oauth2/tokenservice/validator.godocs/content/guides/protocols/oauth-oidc/token-exchange.mdxtests/integration/oauth/token/tokenexchange_test.go
🚧 Files skipped from review as they are similar to previous changes (6)
- backend/internal/oauth/oauth2/tokenservice/error_constants.go
- backend/internal/oauth/oauth2/granthandlers/jwt_bearer.go
- backend/internal/oauth/oauth2/tokenservice/validator.go
- backend/internal/oauth/oauth2/granthandlers/jwt_bearer_test.go
- backend/internal/oauth/oauth2/granthandlers/token_exchange_test.go
- backend/internal/oauth/oauth2/granthandlers/token_exchange.go
7b7ed80 to
f5ec16d
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Purpose
The token exchange grant returned a single generic
Invalid subject_tokenfor every subject-token failure, so a caller could not tell an expired token from an untrusted issuer or an audience mismatch (#3835, item 2). This PR attributes the reasons that are safe to disclose and reports them in theerror_description, applying the same treatment toactor_tokenand to the jwt-bearer ID-JAG assertion.Approach
tokenservicepackage (ErrTokenExpired,ErrIssuerNotTrusted,ErrAudienceNotAccepted), named for the failure rather than the token that carried it so any validator in the package can return them. The validator wraps them with%w, soerrors.Isclassifies the failure while the underlying context stays in the server-side debug log.subject_tokenandactor_token(invalid_request); the jwt-bearer handler does the same for the assertion (invalid_grant). Any unattributed failure keeps the original generic description.error_descriptioncharacter set.ValidateSubjectTokenkeeps its existing signature, so no interface or mock changes.Related Issues