A Node.js library for validating SAML responses. This library provides security-first SAML validation to protect against common vulnerabilities.
- XML External Entity (XXE) Attack Protection: Prevents malicious external entity references
- XML Comment Injection Detection: Identifies and blocks responses with embedded XML comments
- Multiple SignedInfo Element Detection: Catches signature manipulation attempts
- Processing Instruction Validation: Detects potentially malicious processing instructions
- Signature Validation: Ensures either the response or assertion is properly signed
- Structural Validation: Verifies proper SAML response structure
This library supports npm and yarn package managers:
# Using npm
npm install @stytch/samlshield
# Using yarn
yarn add @stytch/samlshieldFor contributing to this project:
yarn install
yarn hooks # registers Git hooks locallyimport {
validateSAMLResponse,
safeValidateSAMLResponse,
} from "@stytch/samlshield";
// Basic usage - throws errors on validation failure
try {
await validateSAMLResponse({
response_xml: "base64-encoded-saml-response",
});
console.log("SAML response is valid!");
} catch (error) {
console.error("SAML validation failed:", error.message);
}
// Safe usage - returns result object instead of throwing
const result = await safeValidateSAMLResponse({
response_xml: "base64-encoded-saml-response",
});
if (result.valid) {
console.log("SAML response is valid!");
} else {
console.error("Validation errors:", result.errors);
}For those who prefer not to manage dependencies and updates, samlshield.com offers a managed version of this validation service. The managed version provides the same security-first SAML validation without requiring you to maintain or update the library yourself. Check out the Getting Start here.
Main validation function that validates a SAML response for security vulnerabilities.
Parameters:
options.response_xml(string): Base64-encoded SAML response XML
Throws:
ValidationError: For basic input validation failuresXMLValidationError: For XML parsing and structure issuesSAMLExpectedAtLeastOneSignatureError: When neither response nor assertion is signedSAMLResponseFailureError: When the SAML response indicates authentication failure
Wrapper around validateSAMLResponse that returns a result object instead of throwing.
Returns:
{
valid: boolean;
errors?: string[];
}All error classes extend SAMLShieldError with additional context:
ValidationError: Basic input validation failuresXMLValidationError: XML parsing and structure issuesSAMLExpectedAtLeastOneSignatureError: Missing required signaturesXMLExternalEntitiesForbiddenError: External entity reference attemptsSAMLResponseFailureError: SAML authentication failures
This library implements multiple layers of security validation:
Prevents attackers from including external entities that could expose sensitive files or cause denial of service.
Blocks SAML responses containing XML comments, which can be used to bypass signature validation (CVE-2017-11428 family).
Identifies responses with multiple SignedInfo elements within a single signature, which can be used for signature wrapping attacks.
Always detects and blocks processing instructions that could be used for XML canonicalization attacks.
Ensures that either the SAML response or the assertion within it is digitally signed.
The library also exports low-level XML utilities for advanced use cases:
import {
createSelector,
xmlStringToDOM,
xmlBase64ToDOM,
} from "@stytch/samlshield";
// Parse XML from string
const dom = xmlStringToDOM("<saml:Response>...</saml:Response>");
// Parse XML from base64
const dom2 = xmlBase64ToDOM("base64-encoded-xml");
// Create XPath selector with SAML namespaces
const selector = createSelector(dom);
const elements = selector.selectElements("//saml2p:Response");SAML Shield includes comprehensive test coverage:
# Run all tests
yarn testThe library uses a contract testing pattern, providing systematic testing of:
- Valid SAML responses from major identity providers
- Security vulnerability detection (XXE, comment injection, etc.)
- Edge cases and error conditions
See CONTRACT_TESTING.md for details on the testing approach.
yarn format- formats TypeScript codeyarn lint- runs linteryarn build- builds and checks typesyarn test- runs test suite
We use husky to run these all in a pre-commit hook.
This library is based on the battle-tested SAML validation logic from Stytch's production Auth API service, which processes millions of SAML authentications. The original implementation has been adapted for standalone use while maintaining the same security-first approach.
Apache-2.0