Repository navigation
fix(plugin-auth): a refused SAML sign-in lands on the error URL, as OIDC does - #31231
Conversation
…IDC does @better-auth/sso redirects an account refusal from handleOAuthUserInfo (account_not_linked and its siblings) to the success callbackURL, and a response over maxResponseSize to the app origin. The SSO callback refusal scope now also wraps the SAML ACS: it reads the sign-in's relay state before the vendor consumes it and re-bases any refusal that is not on the flow's error URL onto it, error= and error_description unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5mjnQAcpdnyqK2Xi6y
📓 Docs Drift CheckThis PR changes 1 package(s): 4 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:
⛔ 3 release-owned page(s) also name something this change touched. These are read-only:
What this run could not see
Coarse fallback — 22 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): Which tree this was computed onThis run read A worktree cut from an older # while this PR is open — GitHub drops the merge commit once it closes
git fetch origin a1b070c91b1bab22da8a8644ffc6d024eafc268d && git checkout a1b070c91b1bab22da8a8644ffc6d024eafc268d
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin 065bba721e4ba180c6bdbbf2c5fb787d49bac596 ffdd6e0fc391d8ca76cf9aba4a490d1852563942 && git checkout -B drift-repro 065bba721e4ba180c6bdbbf2c5fb787d49bac596 && git merge --no-ff ffdd6e0fc391d8ca76cf9aba4a490d1852563942
node scripts/docs-audit/affected-docs.mjs --json 065bba721e4ba180c6bdbbf2c5fb787d49bac596
|
|
domain:server#6 · os-project-manager · session_01FEkQ5p3T5YWhLjurajudnQ 席位核对 · ACCEPT · 2026-11-07T19:20Z · head
|
Requested by Leehom · project thread
Fixes #30966
Clause-②: no
Metadata-protocol: none
维护者速读
account_not_linked),不再跳回「登录成功」页,而是跳到登录时给的errorCallbackURL;没给就跳到平台登录页,与 OIDC 以及 SAML 其它拒绝一致。响应过大被拒时原来跳到站点根,也一并改为错误页。error=、error_description原样保留。@better-auth/sso:账号类拒绝被发往成功地址。sso-oidc-refusal-cause.ts末尾的 SAML 一节(风险在readSamlAcsLanding的安全规则与landSamlRefusal的成功判定);auth-manager-request-routing.ts(同一作用域挂载点);sso-saml-refusal-landing.test.ts(真实 ACS 端到端)。pnpm --filter @objectstack/plugin-auth exec vitest run src/sso-saml-refusal-landing.test.ts,预期 15 个用例全绿(含成功对照与 takeover 落到错误页)。What changes
@better-auth/sso1.7.12processSAMLResponsesends nearly every SAML refusal to the flow's error URL (samlErrorRedirectUrl: the sign-in'serrorCallbackURL, elseonAPIError.errorURL, which plugin-auth points at the Console login page). Two refusals do not:handleOAuthUserInfocomes back asresult.error, and the vendor answersctx.redirect(buildSAMLRedirectUrl(callbackUrl, { error })): the SUCCESScallbackURLwith only?error=. Values:account_not_linked(the takeover case on the card),signup_disabled,unable_to_link_account,unable_to_create_user,email_not_verified,unable_to_create_session.maxResponseSizeis refused before the vendor reads the relay state, so it lands on the provider's fallback candidates. For a provider stored by the Register SAML Provider bridge (whosecallbackUrlis the ACS itself) that is the app origin/.The cause is upstream, so the fix wraps the ACS in plugin-auth at the seam PR #31194 uses for the OIDC callback, as the seat ruled. There is no vendored patch.
auth-manager-request-routing.tsnow asksssoCallbackOf(endpointPath, request), which answers the OIDC callback (unchanged) or aPOSTto/sso/saml2/sp/acs/:providerId. Both run through the one host memberAuthManager.runSsoCallback(renamed fromrunSsoOidcCallback) andrunSsoCallbackScopeinsso-oidc-refusal-cause.ts. The OIDC branch calls the existingrunSsoOidcCallbackScopeunchanged. The file keeps its name and gains a "SAML: where a refusal lands" header section.samlErrorRedirectUrlfrom the relay state and offers no hook on it, and it deletes the relay-state row once read. So before the vendor runs,readSamlAcsLandingreads the same row (auth-state:RELAY_STATE) through the vendor's owninternalAdapter.findVerificationValue, which does not consume it. It applies the vendor's default (onAPIError.errorURL, else${baseURL}/error) and the vendor's redirect safety rule (getSafeRedirectCandidate: a same-origin path not starting//or/\, or an absolute URL on the app origin or a trusted origin, never the ACS itself). No relay state, no row, an unreadable row, an unsafe target, or a throw while reading means no landing, and the vendor's answer stands.landSamlRefusalre-bases a redirect whoseLocationcarrieserror=and is not the success URL onto the error URL, keepingerroranderror_descriptionas the vendor wrote them. It builds the URL as the vendor'sbuildSAMLRedirectUrldoes: values set on the target, and a relative target kept relative. A refusal already on the error URL comes out byte-identical; status and every other header are kept.error=is never changed (seat ruling).error_description(plugin-auth SSO: SAML refusals name their cause (expiry, issuer, signature, signing key) once @better-auth/sso keeps samlify's error #31168, waiting on upstream). Neither field's value changes, so this PR and plugin-auth SSO: SAML refusals name their cause (expiry, issuer, signature, signing key) once @better-auth/sso keeps samlify's error #31168 stay separable. Nopackages/spec, ADR-0112, or package export change (index.tsstill exports only the OIDC cause constants).content/docs/permissions/sso.mdx"SAML sign-in flow" gains step 5 (where a refusal lands) and names the optionalerrorCallbackURL.@objectstack/plugin-authpatch,.changeset/30966-saml-refusal-lands-on-error-url.md.Every SAML ACS refusal, and where it lands
Measured with this PR's rig on
origin/main66bf5b4a(before) and on the head (after), sign-in witherrorCallbackURLgiven:account_not_linked(takeover)callbackURL?error=account_not_linkederrorCallbackURL?error=account_not_linkederrorCallbackURLcallbackURL?error=account_not_linked?error=account_not_linkedhandleOAuthUserInforesults (signup_disabled,unable_to_link_account,unable_to_create_user,email_not_verified,unable_to_create_session)callbackURL(same vendor line, read in source)errorCallbackURL(same rule; not driven end to end)maxResponseSize/?error=saml_error&…errorCallbackURL?error=saml_error&…saml_error)errorCallbackURLsaml_no_assertion)errorCallbackURLinvalid_statevariants,resolveUser/validateUserInforefusals (APIError with a code, e.g. plugin-auth's own implicit-link gate)POSTwithoutSAMLResponse(400)RelayState)Tests
New
packages/plugins/plugin-auth/src/sso-saml-refusal-landing.test.ts, 15 cases.AuthManagerwith the realsso()plugin over the in-memory engine, driven/sign-in/sso→ IdP →POSTACS. The IdP is played with thesamlifythe installed@better-auth/ssoresolves (loaded throughcreateRequirefrom the vendor's own location, so no new dependency). It reads the vendor's SP metadata and AuthnRequest ID and answers a Response signed with an RSA key and a self-signed certificate generated in the test (a small DER builder checked byX509Certificate), so each refusal comes from the vendor's real pipeline.callbackURL, session cookie set.errorCallbackURLwitherror=account_not_linked, no session.errorCallbackURL: the Console login page, the same page the vendor's ownsaml_errorrefusal lands on in the same sign-in shape.errorCallbackURL:/oops?from=sso&error=account_not_linked.errorCallbackURL,error=saml_error, description kept.//,/\, the ACS itself, a relative path); the rewrite keeps headers and both params, leaves a success alone even when its own URL carrieserror=, leaves an already-landed refusal and a non-redirect identical; no landing, or a throw while reading it, leaves the vendor answer.Red first on
origin/main66bf5b4a(first version of the file, CONTROL + takeover): 1 failed, 1 passed; the takeover case receivedbase: http://localhost:3000/after-sign-inforsign-in-failed.Ablations (each through scripts/ablation-replace.mjs, restored with blob == HEAD 9e4e9e53 and `git diff HEAD` empty)
The test imports the module from
src/by relative path, so nodist/rebuild sits between a mutation and its reading.ssoCallbackOfmatches methodABLATED-1instead ofPOST): 5 failed / 10 passed (the four end-to-end refusal cases and the scope unit).readSamlAcsLandingtakes the raw error URL): 1 failed / 14 passed ("applies the vendor redirect safety rule").location === landing.successnever true): 1 failed / 14 passed ("leaves a success alone, even one whose own URL carries error=").Acceptance notes
errorCallbackURL. The OIDC callback then lands ononAPIError.errorURL(the Console login page), and so does every other SAML refusal; this PR sends the account refusals there too instead of tocallbackURL. The card's literal "when one is given" case is the first end-to-end refusal test.auth-state:RELAY_STATE(better-authgetAuthStateVerificationIdentifier, not exported). A vendor bump that renames it makesreadSamlAcsLandingfind no row, and the end-to-end takeover cases go red (ablation 1 is the same shape), so it cannot fall back silently. plugin-auth always runs better-auth with a database, so the relay state is always database-stored (storeStateStrategyis never set here).getSafeRedirectCandidateandisSafeSAMLRedirectPathare not exported); it only ever narrows: an error URL it refuses leaves the vendor's answer as is.Locationof a refused ACSPOST(302 either way). Nothing is admitted, no status, body,error=orerror_descriptionchanges, and no published type or export changes. Sono, as the claim read it.callbackUrl→errorUrlin thatresult.errorredirect, plus reading the relay state before the size check. Not filed here.Verification
All local readings are at head
ffdd6e0f.pnpm --filter @objectstack/plugin-auth test(afterpnpm --filter @objectstack/plugin-auth build, under the verify lock): Test Files 236 passed (236), Tests 4198 passed | 10 skipped (4208).pnpm --filter @objectstack/plugin-auth typecheck: exit 0 (includescheck:test-typecheck, debt unchanged).npx eslint --no-inline-config packages/plugins/plugin-auth: exit 0.node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack, 95 families) after a fullpnpm build(119 packages, exit 0): 95 derived, 95 run, every one:: exit 0;--ranreconciliation: 95 run, 0 NOT-MEASURED, 0 UNRUN.check-plugin-teardown-shape --self-testran after fetching its fixture commit.Human Minimum: 1 n/a (no spec), 2 yes, 3 yes, 4 yes, 5 yes, 6 n/a, 7 yes, 8 yes.
🤖 Generated with Claude Code
https://claude.ai/code/session_01FEkQ5mjnQAcpdnyqK2Xi6y
Generated by Claude Code