# SAML roulette: the hacker always wins

Ruby-SAML parser differential (REXML vs Nokogiri) plus namespace confusion via DOCTYPE ATTLIST lets attackers forge SAML assertions and gain unauthenticated admin access to GitLab.

- Payout: Research
- Severity: critical
- Program: GitLab
- Write-up by: PortSwigger Research
- Disclosed: 2025-03-18
- Write-up quality: 9/10
- Original: https://portswigger.net/research/saml-roulette-the-hacker-always-wins
- Library page: https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-saml-roulette-the-hacker-always-wins
- Tags: area:web domain:web impact:ato impact:internal-access impact:privesc kind:research platform:research sev:critical tech:saml technique:chained technique:parser-differential vuln:access-control vuln:auth-bypass

**Target:** Ruby-SAML library / GitLab SAML SSO endpoint
**Root cause:** Ruby-SAML parses the SAMLResponse with REXML for signature validation and Nokogiri for attribute access; REXML mutates the document on round-trip (single-to-double quote in SYSTEM identifier) and honors duplicate namespace attributes declared via DOCTYPE !ATTLIST, so the two parsers disagree on which Digest is authoritative.
**Steps:**
1. Obtain a legitimately signed XML document containing a Signature element signed by the target IdP's cert (e.g. WS-Federation federation metadata at `https://login.microsoftonline.com/<tenant>/FederationMetadata/2007-06/FederationMetadata.xml`).
2. Craft a SAMLResponse with two Signature elements: one carrying the real DigestValue, one forged.
3. Use a DOCTYPE `!ATTLIST` to declare duplicate namespace attributes on the Signature element so REXML reads the fake digest while Nokogiri reads the real one.
4. Inject a SYSTEM identifier with embedded double quotes so REXML's single-to-double quote mutation on re-serialization turns an XML comment into a live node, swapping the original assertion for the attacker's assertion.
5. Submit the SAMLResponse to GitLab's SAML ACS endpoint; signature validation passes on the original element while all other checks run against the forged assertion, logging the attacker in as any user (e.g. admin).
**How found:** Started from Juho Forsén's XML round-trip post; built a testbed across SAML libraries, probed DOCTYPE/entity handling, found SYSTEM identifier quote mutation, then Zak streamlined the vector; separately noticed REXML accepts duplicate namespace attributes in !ATTLIST while Nokogiri doesn't, yielding a parser differential on DigestValue lookup.
**Exploitation:** Attacker needs no credentials: fetch public WS-Federation metadata for the tenant to get a valid Signature, wrap it with namespace-confusion DOCTYPE and round-trip mutation, and POST the forged SAMLResponse to GitLab's SAML callback to authenticate as an arbitrary user including administrators.
**Escalation:** Chains round-trip mutation (Ruby <3.4.2 ATTLIST truncation workaround) with namespace confusion and reuse of public WS-Federation signed metadata to convert an authenticated-only forgery into fully unauthenticated admin access.
**Insight:** When a library validates signatures with one XML parser and reads fields with another, hunt for DOCTYPE-level constructs (!ATTLIST duplicate xmlns, SYSTEM identifier quoting) that mutate on re-serialization; also mine public WS-Federation metadata endpoints for legitimately signed XML to satisfy schema validation.

