One account, twenty apps
A college runs email, a learning platform, a library system, the timetable and a dozen more. Staff do not want twenty passwords, and IT does not want twenty places to remove a leaver.
Single sign-on lets one account at one provider open all of them. Newer apps use OpenID Connect for it. Many older ones, and many big suppliers, still use SAML.
Only your own lab
Warning
The signature wrapping attack here runs inside a script in your own lab, against a service provider written for it. Trying it on a real service, without written permission from its owner, is a crime in the UK under the Computer Misuse Act 1990.
By the end of this lesson
Note
I can check that a SAML sign-in proves what it claims.
Starter
Have a go
Starter
3 questionsKey words
| Word | What it means |
|---|---|
| Single sign-on | one sign-in that opens many apps |
| Identity provider | the service that signs people in for other apps |
| Service provider | an app that trusts the identity provider |
| SAML assertion | a signed XML statement about who signed in |
| Signature wrapping | moving a signed element so the app reads a different one |
The cells students fill in are left blank in this preview.
One identity provider, many apps
With single sign-on every app sends the browser to the same identity provider. Each app gets its own token, with its own audience, but the same person behind it.
Most identity providers also keep a sign-in session of their own, a cookie on their domain, so the second app opens without a password. Dex in this lab keeps none, so Amira types her password each time. The identity is still the same.
Two apps, one person
docker exec tools python3 steps/sso.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsOpenID Connect and SAML side by side
| OpenID Connect | SAML 2.0 | |
|---|---|---|
| Statement | ID token, a JWT | Assertion, XML |
| Signed with | JWS, for example RS256 | XML Signature |
| Reaches the app | Back channel, after a code | Posted by the browser |
| Common in | New web and phone apps | Older and enterprise apps |
The cells students fill in are left blank in this preview.
What a SAML assertion holds
In SAML the identity provider signs a SAML assertion and the browser posts it to the service provider. NameID names the person and Audience the app it is meant for. Recipient says where it may be posted, InResponseTo which request it answers, and NotOnOrAfter when it stops counting.
An XML signature inside it holds a Reference naming the signed element by its ID.
Read a signed SAML response for Dan
docker exec tools python3 steps/saml.py showShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA service provider checks five things
First the signature must hold over the very assertion the service provider will read. The issuer must be the provider it trusts, and Audience must be itself. Recipient and InResponseTo must match its own request, and the time must be before NotOnOrAfter.
The time check stops replay: an assertion copied from a browser works only for a few minutes. This lab's script fixes its clock at 10:00:30 so every copy prints the same.
Check a genuine response and a changed one
docker exec tools python3 steps/saml.py checkShown here, not run. Press Start lab to type it into your own lab’s terminal.
Replay the same response at 10:06
docker exec tools python3 steps/saml.py replayShown here, not run. Press Start lab to type it into your own lab’s terminal.
Signature wrapping reads the wrong assertion
A naive service provider checks that a signature in the document holds, then reads the first assertion it finds. In signature wrapping, Dan takes his own genuinely signed response and hides his assertion inside an Extensions element. In front he puts a forged assertion for Chen, the IT admin.
The signature still holds over Dan's assertion. The naive app reads Chen's. Real SAML libraries have had this bug.
Wrap Dan's response and send it to both
docker exec tools python3 steps/saml.py wrapShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a SAML response is safe once its signature verifies. A signature proves one element is genuine, not that it is the element the app reads. Read only the assertion the signature covers, and refuse a response holding more than one.
Check
Have a go
Check
3 questionsTry it: find the assertion the signature covers
import xml.etree.ElementTree as ET
A = "{urn:oasis:names:tc:SAML:2.0:assertion}"
text = """<Response xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
<saml:Assertion ID="a-forged"><saml:NameID>[email protected]</saml:NameID></saml:Assertion>
<Extensions>
<saml:Assertion ID="a-dan"><saml:NameID>[email protected]</saml:NameID></saml:Assertion>
</Extensions>
</Response>"""
root = ET.fromstring(text)
found = root.findall(".//" + A + "Assertion")
print("assertions anywhere:", len(found))
print("first one found names:", found[0].find(A + "NameID").text)
# The signature covers a-dan. Change the print to show the NameID of the assertion with that ID.
Make a free account to save your work and track your progress.
Fixing it and spotting it
The fixes: use a maintained SAML library and keep it patched. Accept exactly one assertion, directly inside the response, and read only the element the signature's Reference names. Check audience, recipient, request and time, and refuse an InResponseTo you have already seen.
To spot attacks, log refused responses with the reason. More than one assertion, or a reference to an unexpected ID, is never an accident.
Assignment: check what a SAML response covers
A library has already checked that the signature value is right. You check what it covers and what it says. Write saml_problems(text, sp, acs, request_id, now), which reads a SAML response with xml.etree.ElementTree and returns a list of problems.
If the response does not hold exactly one Assertion, and that one directly inside the response, return ["assertions"] and stop. Otherwise check, in this order: "reference" when the assertion has no Signature/SignedInfo/Reference whose URI is # and its ID; "audience" when Audience is not sp; "recipient" when Recipient is not acs; "request" when InResponseTo is not request_id; "time" when now is not before NotOnOrAfter. The last three are attributes of SubjectConfirmationData; times are strings like 2026-11-21T10:05:00Z, which compare correctly as text.
import xml.etree.ElementTree as ET
A = "{urn:oasis:names:tc:SAML:2.0:assertion}"
DS = "{http://www.w3.org/2000/09/xmldsig#}"
def saml_problems(text, sp, acs, request_id, now):
return []
Make a free account to save your work and track your progress.
Exit quiz
Have a go