Skip to content

fix: enforce state for public clients in code flow at /authorize - #3022

Merged
tada5hi merged 1 commit into
masterfrom
fix/oauth2-iss-trailing-slash
Apr 28, 2026
Merged

tada5hi merged 1 commit into
masterfrom
fix/oauth2-iss-trailing-slash

Conversation

@tada5hi

@tada5hi tada5hi commented Apr 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

Updates

  • Public clients using OAuth2 authorization code flow must now include the state parameter; requests without it will be rejected
  • Issuer URLs are normalized to consistently remove trailing slashes when constructing realm-specific endpoints

Copilot AI review requested due to automatic review settings April 28, 2026 14:07
@coderabbitai

coderabbitai Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Adds 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

Cohort / File(s) Summary
Authorization Code Verifier
apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts, apps/server-core/test/unit/core/oauth2/authorization/code-request/verifier/module.spec.ts
Adds state parameter validation requiring data.state for public clients in the authorization code flow; rejects missing state with OAuth2Error.requestInvalid. Confidential clients exempt. New test suite validates client lookup, PKCE requirements, state enforcement for public clients in code flow, and implicit flow exemption.
Token Issuer URL Normalization
apps/server-core/src/core/oauth2/token/issuer/base.ts, apps/server-core/test/unit/core/oauth2/token/issuer/access/module.spec.ts
Normalizes configured issuer by removing trailing slashes before appending realm paths. Extended test suite verifies iss claim construction with realm-based and non-realm issuer configurations, and trailing slash removal across variants.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 Hop hop, the state must now be here,
Public clients, crystal clear!
Trailing slashes fade away,
Tests guard the OAuth dance today,
Authorization flows leap high! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: enforcing state parameter requirement for public clients in the OAuth2 authorization code flow at the /authorize endpoint.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/oauth2-iss-trailing-slash

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI 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.

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 state for public clients when the authorization request will issue an authorization code.
  • Normalize issuer values by stripping trailing slashes before building realm-scoped iss claims.
  • Add unit tests covering both the new state enforcement and the iss formatting 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.

Comment on lines +70 to +73
// 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.

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

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).

Suggested change
// 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.

Copilot uses AI. Check for mistakes.
Comment on lines +31 to +40
// 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;

Copilot AI Apr 28, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
@tada5hi

tada5hi commented Apr 28, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Apr 28, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai 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.

🧹 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 indexing insertCalls[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

📥 Commits

Reviewing files that changed from the base of the PR and between bac38b1 and d889edc.

📒 Files selected for processing (4)
  • apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts
  • apps/server-core/src/core/oauth2/token/issuer/base.ts
  • apps/server-core/test/unit/core/oauth2/authorization/code-request/verifier/module.spec.ts
  • apps/server-core/test/unit/core/oauth2/token/issuer/access/module.spec.ts

@tada5hi
tada5hi merged commit 5d8c0bf into master Apr 28, 2026
14 checks passed
@tada5hi
tada5hi deleted the fix/oauth2-iss-trailing-slash branch April 28, 2026 14:31
@github-actions github-actions Bot mentioned this pull request Apr 28, 2026
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.

2 participants