Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Single sign-on and SAML

After this lesson: I can check that a SAML sign-in proves what it claims.

60 min lesson5 commandsIdentity and access lab: OpenID Connect, OAuth 2.0 and JWTs

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Key words

WordWhat it means
Single sign-onone sign-in that opens many apps
Identity providerthe service that signs people in for other apps
Service provideran app that trusts the identity provider
SAML assertiona signed XML statement about who signed in
Signature wrappingmoving 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

Command
docker exec tools python3 steps/sso.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

OpenID Connect and SAML side by side

OpenID ConnectSAML 2.0
StatementID token, a JWTAssertion, XML
Signed withJWS, for example RS256XML Signature
Reaches the appBack channel, after a codePosted by the browser
Common inNew web and phone appsOlder 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

Command
docker exec tools python3 steps/saml.py show

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Command
docker exec tools python3 steps/saml.py check

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Replay the same response at 10:06

Command
docker exec tools python3 steps/saml.py replay

Shown 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

Command
docker exec tools python3 steps/saml.py wrap

Shown 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try it: find the assertion the signature covers

Code: python
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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.