Repository navigation
fix: enforce state for public clients in code flow at /authorize - #3022
Conversation
📝 WalkthroughWalkthroughAdds state parameter validation for public clients in the authorization code flow, normalizes issuer URL construction by removing trailing slashes before appending realm paths, and introduces comprehensive test coverage for both changes. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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.
Pull request overview
This PR tightens OAuth2 authorization request validation by requiring state for public clients in the authorization code flow (to strengthen CSRF protections at /authorize). It also adjusts token issuer construction to normalize configured issuer URLs by stripping trailing slashes.
Changes:
- Enforce
statefor public clients when the authorization request will issue an authorization code. - Normalize
issuervalues by stripping trailing slashes before building realm-scopedissclaims. - Add unit tests covering both the new
stateenforcement and theissformatting behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts | Enforces state for public clients in code(-issuing) response types. |
| apps/server-core/test/unit/core/oauth2/authorization/code-request/verifier/module.spec.ts | Adds unit tests for client validation + PKCE/state requirements. |
| apps/server-core/src/core/oauth2/token/issuer/base.ts | Strips trailing slashes from configured issuer before building iss. |
| apps/server-core/test/unit/core/oauth2/token/issuer/access/module.spec.ts | Adds unit tests for issuer/realm iss formatting and trailing slash handling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Public clients SHOULD include state to bind the redirect to the | ||
| // initiating session and prevent CSRF (RFC 6749 §10.12). Confidential | ||
| // clients are exempt because the /token exchange already authenticates | ||
| // them via client_secret. |
There was a problem hiding this comment.
The preceding comment says public clients "SHOULD" include state, but the implementation rejects requests without state (i.e., effectively making it required). Please align the comment with the enforced behavior (e.g., say MUST/required, or explain that this implementation enforces the SHOULD).
| // Public clients SHOULD include state to bind the redirect to the | |
| // initiating session and prevent CSRF (RFC 6749 §10.12). Confidential | |
| // clients are exempt because the /token exchange already authenticates | |
| // them via client_secret. | |
| // Public clients are required by this implementation to include state | |
| // in the code flow to bind the redirect to the initiating session and | |
| // prevent CSRF (RFC 6749 §10.12). Confidential clients are exempt | |
| // because the /token exchange already authenticates them via | |
| // client_secret. |
| // Strip a trailing slash so a configured issuer like | ||
| // `https://auth.example.com/` doesn't produce a malformed | ||
| // `https://auth.example.com//realms/master`. | ||
| const base = this.options.issuer.replace(/\/+$/, ''); | ||
|
|
||
| if (input.realm_name) { | ||
| return `${this.options.issuer}/realms/${input.realm_name}`; | ||
| return `${base}/realms/${input.realm_name}`; | ||
| } | ||
|
|
||
| return this.options.issuer; | ||
| return base; |
There was a problem hiding this comment.
The PR title is about enforcing state for public clients in the /authorize code flow, but this hunk also changes issuer normalization (trailing-slash stripping) for all tokens. If this is intentional, consider updating the PR title/description to reflect the additional behavior change; otherwise, it may be better split into a separate PR to keep review and rollout scoped.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
🧹 Nitpick comments (2)
apps/server-core/test/unit/core/oauth2/token/issuer/access/module.spec.ts (1)
62-63: Optional: assert insert call count before indexinginsertCalls[0].Adding
expect(repository.insertCalls).toHaveLength(1)before indexing improves failure clarity and makes regressions easier to diagnose.Also applies to: 73-74, 86-87, 98-99, 110-111
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/server-core/test/unit/core/oauth2/token/issuer/access/module.spec.ts` around lines 62 - 63, Add an explicit assertion that repository.insertCalls has the expected length before indexing into insertCalls[0] to improve failure messages: insert a line like expect(repository.insertCalls).toHaveLength(1) immediately before any use of repository.insertCalls[0] in the test file (the assertions around repository.insertCalls[0].iss at the blocks that currently reference insertCalls[0] — also apply the same change for the other occurrences noted near lines that check insertCalls[0] in the token issuer access module tests). Ensure you add the same length-assertion before each indexing site to make test failures clearer.apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts (1)
70-76: Align the inline RFC wording with enforced behavior.At Line 70, the comment says public clients “SHOULD include state”, but Lines 74-75 enforce it as required. Consider updating the comment to “MUST” to avoid future confusion.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts` around lines 70 - 76, The inline comment above the state check is inconsistent with the enforced behavior; update the comment to state that public clients "MUST include state" for the code flow (instead of "SHOULD") so it matches the runtime enforcement around the client.is_confidential / data.state check and willIssueCode(response_type) that throws OAuth2Error.requestInvalid when missing state.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts`:
- Around line 70-76: The inline comment above the state check is inconsistent
with the enforced behavior; update the comment to state that public clients
"MUST include state" for the code flow (instead of "SHOULD") so it matches the
runtime enforcement around the client.is_confidential / data.state check and
willIssueCode(response_type) that throws OAuth2Error.requestInvalid when missing
state.
In `@apps/server-core/test/unit/core/oauth2/token/issuer/access/module.spec.ts`:
- Around line 62-63: Add an explicit assertion that repository.insertCalls has
the expected length before indexing into insertCalls[0] to improve failure
messages: insert a line like expect(repository.insertCalls).toHaveLength(1)
immediately before any use of repository.insertCalls[0] in the test file (the
assertions around repository.insertCalls[0].iss at the blocks that currently
reference insertCalls[0] — also apply the same change for the other occurrences
noted near lines that check insertCalls[0] in the token issuer access module
tests). Ensure you add the same length-assertion before each indexing site to
make test failures clearer.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8fc2b755-9209-4ae0-8abe-aa38299ab192
📒 Files selected for processing (4)
apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.tsapps/server-core/src/core/oauth2/token/issuer/base.tsapps/server-core/test/unit/core/oauth2/authorization/code-request/verifier/module.spec.tsapps/server-core/test/unit/core/oauth2/token/issuer/access/module.spec.ts
Summary by CodeRabbit
Updates