Skip to content

enhancement(auth): support IdP-initiated SAML and harden the SSO completion flow#406

Merged
therealbrad merged 1 commit into
mainfrom
feat/saml-auth-hardening
Jun 5, 2026
Merged

enhancement(auth): support IdP-initiated SAML and harden the SSO completion flow#406
therealbrad merged 1 commit into
mainfrom
feat/saml-auth-hardening

Conversation

@therealbrad
Copy link
Copy Markdown
Contributor

Description

Follow-up to #405 (SP-initiated SAML). Three changes to the SAML SSO flow:

IdP-initiated SSO (new capability)

An Okta dashboard-tile launch posts an assertion to the ACS with no RelayState, which #405 rejected with a 400. The ACS now handles it: with no RelayState it selects the enabled SAML config whose certificate validates the assertion's signature (the stored issuer is the SP entity id, not a reliable IdP identifier, so matching is by cryptographic validation, not a field compare) and defaults the post-login destination to /.

Enforce 2FA for SAML logins (security)

force2FAAllLogins was bypassed for SAML, because that gate only fires inside NextAuth's jwt callback (which needs an account) and this flow mints the session directly. /api/auth/saml/complete now reads the setting and stamps the same twoFactorRequired / twoFactorSetupRequired claims, so SAML sessions hit the existing middleware 2FA gate.

Single-use completion token (security)

The token handed to /api/auth/saml/complete rides in a redirect URL (Referer, logs, history), so it now registers a single-use jti in Valkey (deleted on consume) with a signed-JWT fallback, and its lifetime is cut from 5 minutes to 2. A replayed link is rejected.

Related Issue

Follow-up to #405; addresses review observations on IdP-initiated launches, 2FA bypass, and token replay.

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix / hardening

Testing

  • 66 SAML unit tests pass, including: single-use replay rejection, force-2FA claims stamped into the session token (decoded via NextAuth's decode), and IdP-initiated config selection by signature.
  • tsc clean for the changed files; prettier clean.

Checklist

  • Code follows the project's style guidelines
  • Added tests that prove the changes are effective
  • New and existing unit tests pass locally

…letion flow

Follow-up to the SP-initiated SAML fix (#405), three changes:

- IdP-initiated SSO: an Okta dashboard-tile launch posts an assertion with no
  RelayState. Rather than rejecting it, the ACS now picks the enabled SAML
  config whose certificate validates the assertion's signature (the stored
  issuer is the SP entity id, not a reliable IdP identifier) and defaults the
  post-login destination to /.

- Enforce 2FA for SAML logins: the completion route now reads force2FAAllLogins
  and stamps the same twoFactorRequired / twoFactorSetupRequired claims the
  NextAuth jwt callback sets at sign-in, so SAML sessions hit the existing
  middleware 2FA gate instead of bypassing it.

- Single-use completion token: the token handed to /api/auth/saml/complete
  rides in a redirect URL, so it now registers a single-use jti in Valkey
  (deleted on consume) and its lifetime is cut to two minutes — a replayed link
  is rejected.
@therealbrad therealbrad changed the title feat(auth): support IdP-initiated SAML and harden the SSO completion flow enhancement(auth): support IdP-initiated SAML and harden the SSO completion flow Jun 5, 2026
@therealbrad therealbrad force-pushed the feat/saml-auth-hardening branch from 9c93c05 to bb3ede4 Compare June 5, 2026 19:49
@therealbrad therealbrad merged commit 7f3a653 into main Jun 5, 2026
5 checks passed
@therealbrad therealbrad deleted the feat/saml-auth-hardening branch June 5, 2026 20:05
@therealbrad
Copy link
Copy Markdown
Contributor Author

🎉 This PR is included in version 0.35.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

therealbrad added a commit that referenced this pull request Jun 5, 2026
Accepting IdP-initiated (no-RelayState) POSTs in #406 opened a replay
vector: any captured assertion — including an SP-initiated one with its
RelayState stripped — could be re-POSTed and accepted until it expired,
since the only remaining bound was its notBefore/notOnOrAfter window.

The ACS now records each validated assertion by its signed ID in Valkey
for the remainder of its validity window and rejects a second use — the
same single-use pattern as the relay state and completion token. The ID
is the dedup key (not a byte hash) because it lives inside the signed
element: an attacker can't change it without the IdP's key, whereas the
surrounding bytes can be mutated (re-canonicalization, namespace/attribute
reordering, the unsigned Response wrapper) while keeping a valid signature.
Without Valkey the assertion's expiry window remains the only bound.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant