Decoding a SAMLResponse from Base64
Open a SAML log and the body of an HTTP form post holds something like SAMLResponse=PHNhbWxwOlJlc3BvbnNl.... That is XML wrapped in Base64, and to analyse a login problem you first have to take the wrapper off. The steps are simple, but without knowing what got mixed in, decoding keeps failing.
What the POST binding actually does
- The IdP builds the Response XML.
- It encodes the XML bytes as Base64, sometimes with line breaks and sometimes without.
- The browser posts to the SP's ACS as
application/x-www-form-urlencoded, where the Base64+and=may become%2Band%3D. - The SP takes the form value, decodes the Base64 and validates the XML.
If you copied the value from an address bar or a proxy log, the percent-encoding of step 3 may still be attached. Passing that straight to a Base64 decoder fails at the percent sign.
How Base64 is recognised
Base64 uses only A–Z, a–z, 0–9, + and /, has a length that is a multiple of four, and pads the end with =. Line breaks, spaces and tabs can be ignored. Using - and _ instead means Base64URL, which is common in JWT and not the SAML POST binding's form.
| Symptom | Cause | Fix |
|---|---|---|
| Length not a multiple of four | Truncated copy, removed line breaks | Restore the = padding or copy the original again |
| Contains % | Form encoding still present | Percent-decode first |
| Whitespace inside | Log wrapping | Strip whitespace, then decode |
| Unreadable bytes after decode | DEFLATE-compressed value | Check for the redirect binding and inflate |
Check the first byte after decoding
If the decoded bytes do not start with <, the value is not XML. A HTTP-Redirect binding value is raw DEFLATE, so it starts with a compression header near 0x78 or, for a headerless raw stream, values such as 0x9C or 0xEC. Inflate first in that case.
Handling it safely from a log
- A SAMLResponse contains a NameID and attribute values. Reproducing with a synthetic response from a test tenant is safer.
- For anything you keep or share, use a copy with the NameID, attribute values, SessionIndex and certificate masked.
- Before pasting a production IdP response, check whether your screen is being shared.
samlview detects percent-encoding and Base64 in turn and, when the result is not XML, tries inflation. It shows each step it took as a chip, so you can see exactly where it stopped.
Frequently asked questions
How do I tell Base64 from Base64URL?
By the character set. <code>+</code> and <code>/</code> mean standard Base64; only <code>-</code> and <code>_</code> means Base64URL. The SAML POST binding uses standard Base64.
Are line breaks allowed?
Yes. A Base64 decoder ignores spaces and line breaks. It fails only when a break cut the string and the <code>=</code> padding disappeared.
Why is the decoded result not XML?
The value is compressed, percent-encoding remains, or you copied another field such as RelayState. The first bytes and the length tell you which.
Paste the Base64 string and see which steps turned it into XML.
Open the toolThis article follows OASIS SAML 2.0 Core (2005), SAML 2.0 Bindings, SAML 2.0 Profiles, XML Signature 1.1 and RFC 1951 (DEFLATE). Every example is synthetic data.