Repository navigation
feat: realm-bind the authorize and token flow - #3194
Conversation
|
Note Reviews pausedUse the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds server-side enforcement that an authenticated identity's realm must match the target client's realm across ChangesRealm Binding Enforcement
Estimated code review effort: 4 (Complex) | ~60 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant AuthorizeGrant as HTTPOAuth2AuthorizeGrant
participant Authorization as OAuth2Authorization
participant CodeVerifier as OAuth2AuthorizationCodeVerifier
participant RefreshGrant as HTTPOAuth2RefreshTokenGrant
Client->>AuthorizeGrant: POST /authorize
AuthorizeGrant->>Authorization: authorize(data, identity)
Authorization->>Authorization: compare data.realm_id vs identity.data.realm.id
Authorization-->>AuthorizeGrant: OAuth2LoginRequiredError.realmMismatch() (on mismatch)
Client->>AuthorizeGrant: POST /token (code redemption)
AuthorizeGrant->>CodeVerifier: verify(code, realmId: client.realm_id)
CodeVerifier-->>AuthorizeGrant: OAuth2GrantError.invalid() (on mismatch)
Client->>RefreshGrant: POST /token (refresh)
RefreshGrant->>RefreshGrant: compare payload.realm_id vs client.realm_id (public client only)
RefreshGrant-->>Client: OAuth2GrantError.invalid() (on mismatch)
sequenceDiagram
participant AuthorizeForm
participant Authorize as Authorize.vue
participant Store as Pinia store
participant Mismatch as AuthorizeRealmMismatch
AuthorizeForm->>AuthorizeForm: auto-consent fails with error=login_required
AuthorizeForm-->>Authorize: emit loginRequired
Authorize->>Store: switchAccount() calls store.logout()
Authorize->>Authorize: compare codeRequest.realm_id vs realmId
Authorize->>Mismatch: render with client/realm names, redirectUriVerified, onSwitch
Mismatch-->>Store: emit switch -> switchAccount()
Possibly related PRs
🚥 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 |
authup
@authup/client-web
@authup/server-core
@authup/access
@authup/client-web-kit
@authup/client-web-kit-theme
@authup/client-web-nuxt
@authup/client-web-theme
@authup/core-http-kit
@authup/core-kit
@authup/core-realtime-kit
@authup/errors
@authup/i18n
@authup/kit
@authup/server-adapter-kit
@authup/server-adapter-node
@authup/server-adapter-socket-io
@authup/server-adapter-web
@authup/server-kit
@authup/server-test-kit
@authup/specs
commit: |
There was a problem hiding this comment.
Pull request overview
This PR closes a cross-realm “confused deputy” gap by enforcing that an authenticated identity can only authorize/redeem/refresh tokens against clients in the same realm, with server-side checks as the source of truth and UI changes providing safe UX around realm mismatch.
Changes:
- Add a
login_requiredOAuth/OIDC error (ErrorCode.OAUTH_LOGIN_REQUIRED,OAuth2ErrorCode.LOGIN_REQUIRED) and translations. - Enforce realm-binding at three layers:
/authorizeissuance,/tokencode redemption, and/tokenrefresh (public clients only), plus deterministic client resolution for name-identified clients. - Add SSR/kit UX and headers: realm-mismatch card + guarded “return to app”, and clickjacking protection headers on SSR workflow pages.
Reviewed changes
Copilot reviewed 31 out of 31 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/specs/src/oauth2/error/login-required.ts | Introduces OAuth2LoginRequiredError for login_required cases (incl. realm mismatch). |
| packages/specs/src/oauth2/error/index.ts | Exports the new login-required error. |
| packages/specs/src/oauth2/constants.ts | Adds OAuth2ErrorCode.LOGIN_REQUIRED. |
| packages/i18n/src/constants.ts | Adds client-facing translation keys for realm-mismatch UI. |
| packages/i18n/src/catalogs/fr/error.ts | Adds French translation for OAUTH_LOGIN_REQUIRED. |
| packages/i18n/src/catalogs/fr/client.ts | Adds French realm-mismatch UI strings. |
| packages/i18n/src/catalogs/es/error.ts | Adds Spanish translation for OAUTH_LOGIN_REQUIRED. |
| packages/i18n/src/catalogs/es/client.ts | Adds Spanish realm-mismatch UI strings. |
| packages/i18n/src/catalogs/en/error.ts | Adds English translation for OAUTH_LOGIN_REQUIRED. |
| packages/i18n/src/catalogs/en/client.ts | Adds English realm-mismatch UI strings. |
| packages/i18n/src/catalogs/de/error.ts | Adds German translation for OAUTH_LOGIN_REQUIRED. |
| packages/i18n/src/catalogs/de/client.ts | Adds German realm-mismatch UI strings. |
| packages/errors/src/constants.ts | Adds ErrorCode.OAUTH_LOGIN_REQUIRED. |
| packages/client-web-kit/src/components/workflows/authorize/AuthorizeRealmMismatch.vue | New realm-mismatch card with safe “return to application” behavior. |
| packages/client-web-kit/src/components/workflows/authorize/AuthorizeForm.vue | Emits loginRequired on server login_required during auto-consent. |
| packages/client-web-kit/src/components/workflows/authorize/Authorize.vue | Gates built-in auto-consent on resolved identity realm; renders mismatch UI. |
| apps/server-core/ui/src/pages/authorize.vue | Passes realm summary + redirect-uri verification into SSR authorize page payload. |
| apps/server-core/test/unit/http/controllers/workflows/token/grant-refresh-token.spec.ts | Adds refresh-token realm-parity tests for public clients (reject/allow controls). |
| apps/server-core/test/unit/http/controllers/workflows/token/grant-authorize.spec.ts | Adds realm-mismatch /authorize test + same-realm control; updates code issuance to use same-realm user. |
| apps/server-core/test/unit/core/oauth2/authorization/code/verifier/module.spec.ts | Adds tests for verifier realmId option (match/mismatch/no-bound-realm). |
| apps/server-core/test/unit/core/oauth2/authorization/code-request/verifier/module.spec.ts | Adds test for deterministic name+realm hint requirement and adjusts unknown-client test. |
| apps/server-core/src/core/oauth2/authorization/module.ts | Enforces identity-realm == client-realm at authorization issuance (login_required). |
| apps/server-core/src/core/oauth2/authorization/code/verifier/types.ts | Adds realmId option to code verification. |
| apps/server-core/src/core/oauth2/authorization/code/verifier/module.ts | Enforces code realm parity during redemption (defense in depth). |
| apps/server-core/src/core/oauth2/authorization/code-request/verifier/types.ts | Extends verifier result with redirectUriVerified. |
| apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts | Requires realm hint for name-identified clients; stamps data.realm_id; computes redirectUriVerified. |
| apps/server-core/src/adapters/http/ui/render.ts | Adds clickjacking protection headers for SSR workflow pages. |
| apps/server-core/src/adapters/http/controllers/workflows/authorize/module.ts | Adds realm summary + redirectUriVerified to SSR payload. |
| apps/server-core/src/adapters/http/adapters/oauth2/grant-types/refresh_token.ts | Rejects cross-realm refresh for public clients (invalid_grant). |
| apps/server-core/src/adapters/http/adapters/oauth2/grant-types/authorize.ts | Passes realmId into code verifier during code redemption. |
| .agents/architecture.md | Documents plan 041 realm-binding design and enforcement points. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts (1)
105-115: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueRedirect-URI check condition duplicated instead of reusing the new flag.
redirectUriVerified(line 108) computes the same boolean already re-checked on line 109 (if (client.redirect_uri && data.redirect_uri)). Reusing the variable avoids re-evaluating the same condition twice.♻️ Proposed simplification
const redirectUriVerified = !!(client.redirect_uri && data.redirect_uri); - if (client.redirect_uri && data.redirect_uri) { + if (redirectUriVerified) { const redirectUris = client.redirect_uri.split(',');🤖 Prompt for 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. In `@apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts` around lines 105 - 115, In the authorization code verifier module, the redirect-URI presence check is duplicated in the same block. Reuse the existing redirectUriVerified flag instead of repeating the client.redirect_uri && data.redirect_uri condition, and keep the isSimpleMatch / OAuth2GrantError.redirectUriMismatch logic inside that single guarded branch.
🤖 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.
Nitpick comments:
In
`@apps/server-core/src/core/oauth2/authorization/code-request/verifier/module.ts`:
- Around line 105-115: In the authorization code verifier module, the
redirect-URI presence check is duplicated in the same block. Reuse the existing
redirectUriVerified flag instead of repeating the client.redirect_uri &&
data.redirect_uri condition, and keep the isSimpleMatch /
OAuth2GrantError.redirectUriMismatch logic inside that single guarded branch.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 3e2dc9c4-d0ba-4559-8e05-1508654b9898
📒 Files selected for processing (31)
.agents/architecture.mdapps/server-core/src/adapters/http/adapters/oauth2/grant-types/authorize.tsapps/server-core/src/adapters/http/adapters/oauth2/grant-types/refresh_token.tsapps/server-core/src/adapters/http/controllers/workflows/authorize/module.tsapps/server-core/src/adapters/http/ui/render.tsapps/server-core/src/core/oauth2/authorization/code-request/verifier/module.tsapps/server-core/src/core/oauth2/authorization/code-request/verifier/types.tsapps/server-core/src/core/oauth2/authorization/code/verifier/module.tsapps/server-core/src/core/oauth2/authorization/code/verifier/types.tsapps/server-core/src/core/oauth2/authorization/module.tsapps/server-core/test/unit/core/oauth2/authorization/code-request/verifier/module.spec.tsapps/server-core/test/unit/core/oauth2/authorization/code/verifier/module.spec.tsapps/server-core/test/unit/http/controllers/workflows/token/grant-authorize.spec.tsapps/server-core/test/unit/http/controllers/workflows/token/grant-refresh-token.spec.tsapps/server-core/ui/src/pages/authorize.vuepackages/client-web-kit/src/components/workflows/authorize/Authorize.vuepackages/client-web-kit/src/components/workflows/authorize/AuthorizeForm.vuepackages/client-web-kit/src/components/workflows/authorize/AuthorizeRealmMismatch.vuepackages/errors/src/constants.tspackages/i18n/src/catalogs/de/client.tspackages/i18n/src/catalogs/de/error.tspackages/i18n/src/catalogs/en/client.tspackages/i18n/src/catalogs/en/error.tspackages/i18n/src/catalogs/es/client.tspackages/i18n/src/catalogs/es/error.tspackages/i18n/src/catalogs/fr/client.tspackages/i18n/src/catalogs/fr/error.tspackages/i18n/src/constants.tspackages/specs/src/oauth2/constants.tspackages/specs/src/oauth2/error/index.tspackages/specs/src/oauth2/error/login-required.ts
|
@coderabbitai pause |
✅ Action performedReviews paused. |
Reject cross-realm authorization: an identity may only authorize, redeem a
code for, or refresh a token against a client in its own realm. Previously a
lingering session for realm A, redirected to /authorize for realm B's client
(a downstream app's realm picker), silently minted realm-A tokens against
realm B's client — a confused deputy whose artifact carried realm-A
iss/signing-key with a realm-B aud.
Enforced server-side at three points (the kit realm-mismatch card is UX only):
- POST /authorize throws login_required (OAUTH_LOGIN_REQUIRED, HTTP 400, with
no identity data in the body) when the identity realm != the client realm.
- /token code redemption rejects code.realm_id != client.realm_id with
invalid_grant (covers identity-provider-issued and in-flight codes).
- /token refresh rejects a public client refreshing a foreign-realm token
(confidential clients stay exempt for the cross-realm password grant).
Additionally: a name-identified client at /authorize now requires a realm hint
(invalid_request otherwise; every realm has a web client, so a bare name is
ambiguous), all SSR auth pages send frame-ancestors 'none' + X-Frame-Options:
DENY, and the authorize page renders a realm-mismatch card ("sign in to
{realm}" / "return to application", the latter only when the redirect_uri
matched a registered pattern) instead of silently auto-consenting.
BREAKING CHANGE: master-realm admins can no longer authorize into another
realm's app via the built-in web client (login_required at /authorize,
invalid_grant at /token). Previously-issued cross-realm artifacts were
malformed, so intentional reliance is implausible; use realm-local accounts.
1747002 to
5e10923
Compare
…-ups) (#3197) * fix: rp-initiated logout & authorize hardening (plan 041 audit follow-ups) Post-merge fixes for defects and test gaps surfaced by an audit of plan 041 (#3194/#3195/#3196). Closes plan 042 items #1, #11 and most of #12. Security / correctness: - AEndSessionForm gates its on-mount local sign-out on `serverRevoked && hintSub === store.user.id` — a cross-site /logout?id_token_hint=<attacker's own token> can no longer force-log-out an unrelated victim who merely renders the page (forced-logout CSRF). The controller forwards `hintSub` (verified hints only) into the payload. - LogoutController bounces back to the RP only when the logout was actually performed (serverRevoked && validated redirect). A hint-less or forged request renders the click-gated confirm page and redirects after sign-out, instead of 302-ing back while the session survives. - Authorize.vue: a fresh login no longer re-prompts prompt=select_account (LoginForm `done` sets accountConfirmed), and the chooser waits for `user` to resolve (no empty-name flash). - /authorize hydration payload trimmed to a ClientSummary DTO (no redirect_uri patterns, grant_types, internal URLs, secret flags). - prompt=none removed from discovery prompt_values_supported + docs (its silent-auth error redirect is unimplemented). - empty `max_age=` is treated as absent instead of coercing to 0 (z.coerce.number('') === 0 would force re-authentication). - AuthorizeForm renders a "Signed in as X - Not you?" switch chip (+ i18n). - Remove the dead OAuth2AuthorizationCode.auth_time field; use specs enums for prompt/error string comparisons instead of literals. Tests: - /logout HTTP e2e matrix (revoke, redirect+state, hint-less no-op, access-token/forged hint no-revoke, unregistered redirect dropped). - server-kit ignoreExpiry (expired+valid-sig accepted; bad-sig/nbf rejected). - auth_time passthrough + auth_time!=refreshed_at pins; redirectUriVerified; confidential refresh exemption; cross-realm login_required no-identity-oracle body; clickjacking headers + trimmed-DTO; max_age empty-vs-0; kit gate spec. * fix: use a button for the "Not you?" switch affordance The account-switch affordance is an action, not navigation — render it as a <button type="button"> styled like a link instead of an <a href="#"> so it has correct button semantics and keyboard behavior (Enter/Space activation), and matches the rest of the form's actions. * fix: reliably skip the account chooser after a fresh login on /authorize The prompt=select_account chooser reappeared right after entering username/password on the hosted /authorize page. The previous fix keyed off LoginForm's `done` emit, but on login the access-token flip re-renders Authorize and unmounts LoginForm before `done` fires, so the flag that skips the chooser was never set — the chooser stayed. Key off the store's LOGGED_IN dispatcher event instead. It is emitted only by an explicit login() (the form), never by a session restored from cookies (resolve() → RESOLVED), so a lingering session still gets the chooser while a fresh login skips it — independent of component mount/unmount timing. Adds a component spec pinning all three cases (lingering shows, LOGGED_IN skips, RESOLVED does not suppress).
Problem
A downstream app lets the user pick a realm and redirects to
/authorizewith that realm's client. If the browser still carries an authup session for another realm (e.g. the user logged in to RealmA earlier and logged out only in the downstream app — authup-origin cookies are untouched), authup silently continued as the RealmA identity: because every realm'swebclient isbuilt_in, the consent step auto-submitted, and the server performed no identity-realm ↔ client-realm check. The downstream app asked for RealmB and received RealmA tokens — a confused deputy whose artifact carried a RealmAiss/signing-key with a RealmBaud.Fix
An identity may only authorize, redeem a code for, or refresh a token against a client in its own realm, enforced server-side at three independent points (the kit UI is UX only — the server is authoritative):
POST /authorizeissuance —OAuth2Authorization.authorize()throwslogin_required(OAUTH_LOGIN_REQUIRED, HTTP 400, no identity data in the body — no realm-enumeration oracle) when the identity realm ≠ the client realm./tokencode redemption — the code verifier's newrealmIdoption (fedclient.realm_id) rejectscode.realm_id != client.realm_idwithinvalid_grant. Covers identity-provider-callback codes and any in-flight pre-deploy codes./tokenrefresh parity — a public client refreshing a token whose realm differs from the client's realm →invalid_grant(kills legacy cross-realmweb-client refresh tokens). Confidential clients stay exempt — the secret proves identity, and the documented cross-realm password grant relies on it.Also included:
/authorizepage renders a "sign in to {realm}" / "return to application" card (the latter only when theredirect_urimatched a registered pattern) instead of auto-consenting, gated so abuilt_inclient can't auto-consent before the store resolves the identity's realm.Content-Security-Policy: frame-ancestors 'none'+X-Frame-Options: DENY./authorizenow requires a realm hint (invalid_requestotherwise); every realm has awebclient, so a bare name was ambiguous.Testing
Full
apps/server-coresuite (1111 tests) green, plus@authup/i18nlocale parity (77) and@authup/client-web-kit(11). New tests: scenario-1 reproduction (login_required) + same-realm control, the code-verifier realm branch, the refresh-parity reject + control, and the name-hint guard. Two pre-existing tests that minted codes via a cross-realm master-admin authorize were updated to authorize as a same-realm user (their fixture relied on the now-closed path).Breaking change
Master-realm admins can no longer authorize into another realm's app via the built-in
webclient. Previously-issued cross-realm artifacts were malformed (RealmAiss/key + RealmBaud), so intentional reliance is implausible — use realm-local accounts.Scope / follow-ups
This is PR A of the plan in
.agents/plans/041-authorize-session-binding.md. It fully closes the cross-realm scenario. The account-switch UX (prompt=select_account+ chooser) and cross-app logout via the standardend_session_endpointare PRs B and C — the latter is the RP-agnostic mechanism that ends a lingering authup session on a downstream logout (kit and non-kit RPs alike), so this PR deliberately does not add a kit-specific session-delete tostore.logout().Summary by CodeRabbit
New Features
Bug Fixes
Chores