The vulnerability in ruby-saml (GHSA-x4h9-gwv3-r4m4) is a critical authentication bypass caused by two related issues: improper handling of XML canonicalization errors and a signature wrapping flaw.
- Canonicalization Bypass: The core of the vulnerability lies in the
XMLSecurity::SignedDocument.validate_signature function. Before the patch, this function would attempt to canonicalize and then compute a digest of a signed XML element. However, due to an issue in the underlying libxml2 library, if the XML element was malformed, the canonicalization process could fail and return an empty string. The ruby-saml library did not check for this failure and would proceed to compute the digest of the empty string. An attacker could exploit this by crafting a SAML response with a malformed element and a pre-computed digest for an empty string, thereby bypassing the signature check.
The fix involves introducing strict XML validation before attempting canonicalization. The validate_signature function was refactored, and a new cache_referenced_xml function was added, which uses a new safe_load_xml utility. This utility explicitly checks for XML parsing errors and rejects invalid documents, preventing the canonicalization flaw from being triggered.
- Signature Wrapping: The
OneLogin::RubySaml::Response.xpath_first_from_signed_assertion and xpath_from_signed_assertion functions were vulnerable to signature wrapping. These functions were used to retrieve attributes and other data from a signed assertion. However, their XPath queries searched the entire SAML document for an assertion with a matching ID. This allowed an attacker to include a validly signed dummy assertion alongside a malicious, unsigned assertion. The vulnerable functions could then mistakenly extract data from the malicious assertion, believing it to be authenticated.
The patch corrects this by changing the logic to operate only on the XML that is explicitly covered by the signature, which is cached by the cache_referenced_xml function. This ensures that data is only extracted from the authenticated part of the document, mitigating the signature wrapping attack.