Binding SAML responses to the request that started them
A SAML signature proves who signed an assertion. It does not automatically prove that the assertion belongs to the browser session that is trying to use it. A service provider that validates the signer but ignores the request binding can accept a response from a different authentication transaction, or accept an unsolicited response where its policy expects a request-bound flow.
The SAML 2.0 technical overview explains the basic web browser SSO exchange: the service provider sends an authentication request, the identity provider returns a response containing an assertion, and the browser posts the response back to the service provider. The protocol has explicit state that connects the request and response. [1]
Keep the request ID
When a service provider creates an authentication request, it should store the request ID with the browser transaction. The response includes InResponseTo, and the service provider should verify that the value matches the pending request ID before it creates a session. The OASIS errata for the SAML core specification states that when a response is in reply to an authentication request, the InResponseTo attribute must match the request ID. [2]
Do not use a global "last request ID" in a multi-user service. Bind the ID to the browser session, expiration time, target application, and requested authentication context. Consume the ID at most once. A replay of a previously successful response should fail even if the XML signature and assertion timestamps are still inside their validity window.
RelayState is routing state
RelayState lets the identity provider return state supplied by the service provider, often a deep-link destination. The binding specification requires the responder to return the exact RelayState data from the request when it uses a binding that supports RelayState. [3]
That makes RelayState useful for routing, but it is not a user identity claim. Treat it as untrusted input after verifying that it matches state stored for the transaction. Do not redirect to an arbitrary absolute URL from RelayState. Use an opaque state identifier that maps to a server-side record, or validate a relative destination against an allowlist.
Decide how unsolicited responses are handled
Some deployments intentionally support identity-provider-initiated SSO, where the service provider receives a response without its own authentication request. That is a policy decision, not an error to hide. If the deployment does not need unsolicited responses, reject them. If it does, require a configured issuer, a valid signature, a destination that matches the service provider endpoint, an audience that matches the service, and an explicit application or tenant mapping.
The destination and audience checks are separate from the request binding. A response can be validly signed but intended for another service provider, another tenant, or another application URL. Validate the exact endpoint and audience expected for this transaction, not only a shared organization identifier.
Session fixation and account linking
After a successful response, create a new session identifier and discard any pre-authentication session that could be controlled by an attacker. Bind the new session to the identity, tenant, and authentication method that were actually validated. If the application supports account linking, make the linking step explicit and require an existing authenticated session or a verified recovery process.
Log the request ID hash, issuer, subject, audience, destination, signature validation result, and session transition. Do not log the full assertion or the session cookie. Store enough metadata to reconstruct the authentication event without turning the authentication log into a credential store.
Test the negative cases
Test a response with a missing InResponseTo, a wrong request ID, a reused request ID, an expired pending request, a mismatched audience, a mismatched destination, and a RelayState that points to an external host. Test a valid response in a browser that has no pending transaction. Test simultaneous login attempts in two tabs. The expected result is a consistent rejection or a clearly supported identity-provider-initiated flow, not a best-effort session.
A signature check is one gate in a chain. The browser transaction, the response metadata, the session lifecycle, and the application authorization rules must all describe the same login. Bind them together before the user reaches a protected resource.
References
[1] OASIS, "SAML 2.0 Technical Overview." https://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-tech-overview-2.0.html
[2] OASIS, "SAML 2.0 Errata Document." https://docs.oasis-open.org/security/saml/v2.0/sstc-saml-errata-2.0-cd-04.html
[3] OASIS, "SAML Binding Simplesign CD 04." https://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-binding-simplesign-cd-04.html