XML signatures and wrapping attacks
A SAMLThe Security Assertion Markup Language, a federation standard widely used for workplace single sign-on, in which the identity provider sends the application a signed assertion about the user. assertion is protected by an XML signature over one element, not over the whole message. This lesson follows the signature on Avery's assertion through the training service's check, then looks at the attacks that work when a service verifies one element and reads another, and at the tests a service providerSAML's name for the application that relies on an identity provider's sign-in, which OpenID Connect calls a relying party. In SCIM, the same term means the application that stores the accounts. runs to prove it refuses them.
A signature over part of a document
XML and XML signatures for identity showed how an XML signature works: a Reference names the signed element by its ID, transforms prepare it, a digest summarizes it, and SignatureValue signs SignedInfo, which holds the digest. Unlike a JSON Web TokenA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing., which is signed as one block of bytes, the rest of the document can change without affecting the signature. That flexibility lets one SAML response carry a signed assertion inside an unsigned envelope, and it is also behind the most serious family of SAML vulnerabilities.
Here is the signature from Avery's assertion in Reading an assertion, with long values shortened:
<saml:Assertion ID="_c5e27a9f4d18" ...>
<saml:Issuer>https://login.cedar.example</saml:Issuer>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<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="#_c5e27a9f4d18">
<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>u5Qx0C9yPq4k...</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>kF2b7LwQe8Rz...</ds:SignatureValue>
<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIIDBzCCAe+g...</ds:X509Certificate></ds:X509Data></ds:KeyInfo>
</ds:Signature>
<saml:Subject>...</saml:Subject>
...
</saml:Assertion>The signature sits inside the element it signs, the enveloped form. URI="#_c5e27a9f4d18" names the assertion itself. The enveloped-signature transform leaves the ds:Signature element out of what is hashed, and exclusive canonicalization, exc-c14n, rewrites the assertion into one standard byte form first, so that equivalent copies produce the same digest. The digest and signature use SHA-256 with RSA. KeyInfo carries Cedar Inc.'s certificateA signed statement that a public key belongs to a name, such as a website's hostname, for stated purposes and a limited period. A certificate authority signs it, so software that trusts the authority can connect the key to the name. as a hint about which key was used.
In this response only the assertion is signed. The samlp:Response around it, with its own Destination and InResponseTo, has no signature, so those values are protected only by their copies inside the assertion, as Reading an assertion described.
How a verifier checks it
When the training service verifies Avery's assertion, its XML signature library follows these steps:
- Find the
ds:Signatureand readSignedInfo. Check that the algorithms are on the training service's allowed list. - Resolve the
ReferenceURI to an element in the document by itsID. - Apply the transforms to that element and hashTurning data into a short, fixed-size value called a hash, in a way that cannot be reversed and where changing one character changes the result completely. Services store password hashes instead of passwords, and signatures are made over a hash. the result. The hash must equal
DigestValue. - Canonicalize
SignedInfoand verifySignatureValueover it with Cedar Inc.'s public keyThe half of a key pair that can be shared freely. It checks signatures made with the matching private key and can encrypt data that only that private key can decrypt. from the metadata the two organizations exchanged.
The key in step 4 comes from that metadata, never from KeyInfo. The certificate in KeyInfo was put there by whoever made the document, so trusting it would let anyone sign with their own key and include their own certificate. A library may use KeyInfo to choose among the keys it already trusts, nothing more. Metadata, certificates, and key rollover covered where those trusted keys come from and how they change.
Look closely at what a successful result means. It does not mean "this document is authentic". It means "the element with ID _c5e27a9f4d18, after these transforms, was signed by this key". Everything outside that element, and any other element that happens to look like an assertion, is unprotected. The security of the whole sign-in depends on the training service using exactly the element the signature covered.
Signature wrapping
XML and XML signatures for identity named this family of problems XML signature wrapping, often shortened to XSW. In SAML it takes a concrete form: someone who holds one genuinely signed assertion rearranges the response so that the signature check and the application read different elements. The signature verifies; the application acts on content nobody signed.
The simplest variant adds a second assertion. Cedar Inc.'s signed assertion for Avery stays intact, and an unsigned one naming a different account is placed in front of it:
<samlp:Response ...>
<saml:Issuer>https://login.cedar.example</saml:Issuer>
<samlp:Status>...</samlp:Status>
<saml:Assertion ID="_inserted"> <!-- unsigned, chosen subject -->
<saml:Subject><saml:NameID>payroll-admin</saml:NameID>...</saml:Subject>
...
</saml:Assertion>
<saml:Assertion ID="_c5e27a9f4d18"> <!-- the original, still signed -->
<ds:Signature>...<ds:Reference URI="#_c5e27a9f4d18">...</ds:Signature>
...
</saml:Assertion>
</samlp:Response>The library resolves #_c5e27a9f4d18, verifies the original and reports success. If the application then reads "the first assertion in the response", it reads the inserted one. Other variants hide the signed original somewhere the application never looks, such as inside samlp:Extensions, an element the SAML schema allows in a response for additional data, or a ds:Object inside the signature, and put a different assertion in its place. Some give two elements the same ID and rely on the signature library and the application resolving it differently. When only the response is signed, the whole signed response can be wrapped inside a new one in the same way.
The defenses all come down to one rule: consume what you verified. A safe service provider asks its library for the element the signature actually covered and reads only that element. It accepts exactly one assertion, at the position the profile expects, and refuses a response containing more. It refuses duplicate ID values, checks the document against the SAML schema, the formal description of which elements may appear where, before trusting any part of it, and requires the signature on the element it consumes, not merely somewhere in the document. Good SAML libraries do all of this together; trouble starts when an application verifies with one tool and then reads values with another.
Algorithms, parsers and libraries
Older SAML deployments sign with http://www.w3.org/2000/09/xmldsig#rsa-sha1 and hash with http://www.w3.org/2000/09/xmldsig#sha1. SHA-1 is broken for collision resistance: practical attacks can produce two different inputs with the same hash. A service provider should refuse SHA-1 signatures and digests, accept SHA-256 or stronger, and check the algorithm against its own allowed list rather than trusting whatever SignatureMethod says.
The parser settings that XML and XML signatures for identity described apply before any signature is checked. SAML messages never need a document type definition, so the parser refuses one outright, which shuts out external entities and entity expansion, and it limits message size and nesting depth.
Some of the most recent signature bypasses came from parser differentials: a library that parsed the same message with two different XML parsers, one for the signature check and one for reading values, where the two disagreed about the document's structure. Flaws of this kind were still being found in widely used SAML libraries in 2024 and 2025. That history is the strongest argument for the plainest advice: use a mature, actively maintained SAML library, keep it updated, follow its security announcements, and never write your own signature verification or patch around a library's checks.
Testing your own service provider
A service provider should prove it refuses broken responses, not only that it accepts good ones. The training service keeps a set of prepared test responses that it sends to its own test environment, each with the result it expects:
| Test response | Expected result |
|---|---|
| An assertion with its signature removed | Refused: the agreement requires a signed assertion |
| A correct signature made with a key that is not in Cedar Inc.'s metadata | Refused, even if KeyInfo carries the key's certificate |
| One attribute value changed after signing | Refused: the digest no longer matches |
| A second, unsigned assertion added before the signed one | Refused: more than one assertion |
Two elements with the same ID | Refused by schema and ID checks |
| A comment inside the NameID | The full identifier is read, so no other account is reached |
| A response signed with RSA-SHA1 | Refused: algorithm not allowed |
| A response containing a document type definition | Refused before parsing continues |
Run tests like these only against services you operate. Each one checks that the service consumes the element it verified and nothing else.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Read what an XML signature covers, and see the trusted key decideDecode your own SAML response, find the element its XML signature covers and the key it names, then see your service provider refuse it when its trusted key differs.
Comments and canonicalization
A related flaw, disclosed across several SAML libraries in 2018, came from comments. Exclusive canonicalization without comments, the form SAML signatures use, ignores XML comments, so adding a comment to a signed element does not change its digest.
Imagine someone whose real identifier at an identity provider is
[email protected], an account they legitimately control at a domain they own. They sign in, receive a genuinely signed assertion, and an empty comment ends up inside the NameIDThe value in a SAML assertion's subject that identifies the user to the service provider, in a declared format such as a persistent opaque identifier or an email address.:The signature still verifies, because canonicalization drops the comment and the bytes match what was signed. But the element now holds two separate pieces of text, and a service that reads only "the first text node" gets
[email protected], Avery's identifier. The fix is to read the element's complete text content, ignoring comments, exactly as canonicalization does, and the affected libraries were patched to do so. It is another case of the verifier and the application seeing different things.