Appearance
SAML Security and Operations
124. How are SAML assertions digitally signed?
The short answer
SAML uses XML Signature. The IdP takes the assertion, or the whole response, puts it into a standard byte form called canonicalisation, so whitespace and attribute order cannot change the result, computes a digest with SHA-256, and signs that digest with its private key, usually RSA-SHA256. The <ds:Signature> element is placed inside the signed element and records which element it covers, by its ID attribute in a Reference URI="#_a75adf55". The SP checks it with the IdP's public certificate from metadata.
What the signature block contains:
xml
<ds:Signature>
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<ds:Reference URI="#_a75adf55">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<ds:DigestValue>…</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>…</ds:SignatureValue>
</ds:Signature>Reading it:
Reference URI="#_a75adf55": the signature covers the element whoseIDis_a75adf55, the assertion.enveloped-signature: remove this signature block before hashing, since it sits inside what it signs.xml-exc-c14n: canonicalise first, so harmless formatting changes do not break the hash.DigestValue: the SHA-256 of the canonical assertion.SignatureValue: the RSA signature overSignedInfo, which contains that digest.
Response or assertion? The IdP can sign the outer Response, the inner Assertion, or both. Signing the assertion is the most important, because the assertion is what the SP trusts. Many SPs require the assertion to be signed. Assertions can also be encrypted to the SP's certificate, so the browser cannot read attributes.
125. How is a SAML assertion validated and verified?
The short answer
The SP must: parse the XML safely, with external entities disabled; check the signature with the IdP's certificate and allowed algorithms, and confirm the signature covers the exact assertion it will read; check Issuer is the expected IdP; check Destination and Recipient equal its ACS URL; check Audience is its entity ID; check NotBefore and NotOnOrAfter with small clock tolerance; check InResponseTo matches a request it sent; reject a reused assertion ID; and only then read the NameID and attributes. Use a well-maintained library and keep it patched.
| Check | Stops |
|---|---|
| Safe XML parsing, no external entities | XXE attacks that read files from the server |
| Signature valid with the IdP's certificate | Forged assertions |
| Signature covers the element you read | Signature wrapping (question 127) |
Issuer equals the configured IdP | Assertions from another IdP |
Destination, Recipient equal our ACS URL | Assertions meant for another app |
Audience is our entity ID | Same |
| Time window, a minute or two of skew | Old or future assertions |
InResponseTo matches our request | Unsolicited, injected or replayed responses |
Assertion ID not seen before | Replay within the validity window |
The order is not optional. Read user data only from the element the signature verified. Several real vulnerabilities came from libraries that verified one element and then read the NameID from another.
126. How do certificates work in SAML, and what happens when a certificate expires?
The short answer
The IdP signs with a private key, and publishes the matching public key inside an X.509 certificate in its metadata. The SP stores that certificate, or trusts the metadata URL, and uses it to check signatures. SAML certificates are usually self-signed: trust comes from exchanging metadata, not from a certificate authority. When the IdP's signing certificate expires or is replaced and the SP has not loaded the new one, every login for that customer fails with a signature error. So rotation must be planned with an overlap.
Rotating a SAML signing certificate without an outage:
- Add the new certificate to the IdP's metadata as a second signing key, while still signing with the old one.
- Update the SP to trust both. SPs that read the metadata URL do this automatically; others need the new certificate pasted in by an admin.
- Switch the IdP to sign with the new key.
- Confirm logins work for every SP.
- Remove the old certificate from both sides after it is no longer used.
Microsoft Entra ID, for example, lets you create a new certificate, keep it inactive while SPs are updated, then make it active, and it emails admins before a certificate expires.
Does expiry itself matter? Many SP libraries check only the key, not the certificate's dates, so an "expired" certificate may keep working. Do not rely on that. Some SPs do enforce dates, and an IdP will stop using an expired certificate anyway.
Encryption certificates are the reverse. If assertions are encrypted, the SP publishes its own certificate, the IdP encrypts with it, and the SP must rotate that one with the same overlap.
127. How does SAML prevent replay attacks and assertion tampering?
The short answer
Tampering is stopped by the XML signature: changing any signed byte breaks it. But the SP must also make sure the signature covers the element it actually reads, or an attacker can use signature wrapping: keep the original signed assertion somewhere in the document so the signature still checks, and add a forged unsigned assertion that the app reads instead. Replay is stopped by a short NotOnOrAfter window, Recipient and Audience checks, InResponseTo tying the response to one request, and the SP remembering assertion IDs until they expire.
This is a live problem, not history. In 2024 and 2025, signature-bypass bugs of this kind were found in widely used libraries, such as ruby-saml, which affected GitLab, and samlify, a common Node.js SAML library. Defences:
- Use a maintained library, and apply its security updates quickly.
- Require the assertion to be signed, and reject documents with more than one assertion.
- Read the identity only from the element whose
IDthe signature references. - Validate the document against the SAML schema before processing.
Replay defences, together:
| Check | Limits replay to |
|---|---|
NotOnOrAfter of a few minutes | That window |
Recipient and Audience | This SP only |
InResponseTo | The one browser and request that started the login |
| Stored assertion IDs until expiry | Exactly one use |
128. How would you troubleshoot a SAML login failure?
The short answer
First capture the actual SAML response, with the browser's network tab, a SAML tracer extension, or the SP's debug log, and decode it from Base64. Then compare it, field by field, with what the SP expects: the Issuer against the configured entity ID, the Destination and Audience against the SP's ACS URL and entity ID, the signing certificate against the one the SP has stored, the time window against both servers' clocks, the NameID format and the attribute names. Also check the IdP's sign-in logs, which often name the error directly.
The most common failures, and their fixes:
| Error or symptom | Usual cause | Fix |
|---|---|---|
| Invalid signature | IdP rotated its certificate; SP has the old one | Update the SP's certificate or metadata |
| Audience mismatch | SP entity ID typed differently at the IdP, often a trailing slash | Make them identical |
| Destination or Recipient mismatch | ACS URL differs: http vs https, a port, a path | Fix the registered ACS URL |
| Assertion expired or not yet valid | Clock drift on a server | Sync time with NTP; small skew allowance |
| User not found | NameID format or attribute name differs from what the SP expects | Map the right attribute; agree on the format |
| User not assigned | User or group not assigned to the app at the IdP | Assign them |
| Login loop | SP session cookie not set on the ACS POST, often SameSite | Set the right cookie attributes (the ACS question) |
| Works for IdP-initiated, fails for SP-initiated | InResponseTo state lost, for example across load-balanced servers | Store request state in a shared store |
Decoding a captured response in Node:
ts
import { inflateRawSync } from 'node:zlib'
const xml = Buffer.from(samlResponseFromForm, 'base64').toString('utf8') // POST binding
// for the Redirect binding, the message is also DEFLATE-compressed:
const req = inflateRawSync(Buffer.from(decodeURIComponent(samlRequestParam), 'base64')).toString('utf8')129. How would you design SAML login for an enterprise application?
The short answer
Make SSO per tenant: each customer organisation gets its own IdP connection, with its entity ID, SSO URL and certificates, ideally imported from their metadata URL. Use SP-initiated login with home realm discovery by email domain. Validate assertions with a maintained library. Link users by a stable ID such as a persistent NameID or object ID, not email. Map groups to roles per tenant. Support JIT and SCIM provisioning. Offer an "enforce SSO" setting with a break-glass admin. Monitor certificate expiry. Give admins a self-service setup page with test logins and clear errors.
The data model:
| Table | Holds |
|---|---|
tenants | Customer organisation, verified email domains, "SSO required" flag |
saml_connections | Tenant, IdP entity ID, SSO URL, certificates, metadata URL, attribute mapping |
identities | Tenant, IdP entity ID, stable subject, linked local user |
role_mappings | Tenant, IdP group name, local role |
The login path, step by step:
- The user types
asha@acme.example. The app finds the tenant by its verified domain,acme.example. - The app sends an
AuthnRequestto that tenant's IdP and stores the request ID in a shared store. - The response arrives at a single ACS URL, or one per tenant, such as
/saml/acs/acme. - The library validates the response against that tenant's certificate and entity ID only.
- The app finds the identity by tenant, issuer and subject, or creates a user just in time if the tenant allows it.
- Roles come from the tenant's group mappings.
- A new session starts.
Security points to mention:
- Verify domain ownership before a tenant can claim
acme.example, with a DNS TXT record. Otherwise a rival could route Acme's users to their own IdP. - Never accept an assertion from tenant A's IdP for a user of tenant B.
- Keep a break-glass login for the tenant's admin, protected by a passkey, for when their IdP is misconfigured.
130. How would you support customers using both SAML and OIDC?
The short answer
Put a single identity broker layer in front of your application. Each tenant's connection is either SAML or OIDC, but the broker turns both into the same internal result: tenant, issuer, stable subject, email, name and groups. Everything after that, account linking, role mapping, JIT provisioning and session creation, is shared code that does not know which protocol was used. You can build the broker with libraries, or use a product such as Keycloak, Auth0, Okta, WorkOS or Entra External ID, which speak both protocols outward and give your app one OIDC integration.
The normalised identity, in TypeScript:
ts
type ExternalIdentity = {
tenantId: string
protocol: 'saml' | 'oidc'
issuer: string // SAML entity ID or OIDC iss
subject: string // persistent NameID or OIDC sub
email?: string // only if the IdP vouches for it
name?: string
groups: string[]
}Mapping between the two protocols:
| Concept | SAML | OIDC |
|---|---|---|
| Who issued it | Issuer, the entity ID | iss |
| Stable user ID | Persistent NameID or an object ID attribute | sub |
| Intended app | Audience | aud |
| Expiry | NotOnOrAfter | exp |
| Request binding | InResponseTo | state and nonce |
| Profile data | Attribute statement | Claims |
| Logout | SLO | Front-channel and back-channel logout |