fix(sts): Reject SAML signature transforms that AWS rejects (bonus: native image shrunk by 7 MB) (#4372)

* perf(sts): drop the XSLT and XPath signature transforms from the native image

AWS answers AssumeRoleWithSAML with InvalidIdentityToken "Response signature
invalid" when the signature reference uses an XPath, XPath Filter 2.0 or
inclusive canonicalization transform, even though the signature itself is
valid. It accepts the enveloped signature transform alone or with exclusive
canonicalization, which is what SAML 2.0 core 5.4.4 allows. The verifier now
rejects the other transforms the same way.

These transforms can no longer run, but every registered transform is
reachable through one virtual call in the JDK XML signature code, so the
XSLT compiler and the XPath engine were still compiled into the native image.
Substitutions cut them out.

* chore(sts): cover the exclusive canonicalization with comments transform

AWS accepts a SAML signature whose reference uses the enveloped signature
transform with exclusive canonicalization with comments.

* fix(sts): check SAML signature transforms before unmarshalling the signature

In the native image, unmarshalling an XPath Filter 2.0 transform throws
MissingReflectionRegistrationError for XPathType[], so AssumeRoleWithSAML
returned 500 instead of InvalidIdentityToken "Response signature invalid".
The transform algorithms are now read from the DOM before the JDK parses
their parameters.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-25 10:45:51 -04:00
1 parent fde0688ef3
commit 59fda495a6
5 files changed
+229 -6

No files matched your search

@@ -5,6 +5,8 @@ import io.github.hectorvent.floci.services.iam.model.SAMLProvider;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.crypto.dsig.CanonicalizationMethod;
import javax.xml.crypto.dsig.Transform;
import javax.xml.crypto.dsig.XMLSignature;
import javax.xml.crypto.dsig.XMLSignatureFactory;
import javax.xml.crypto.dsig.dom.DOMValidateContext;
@@ -18,12 +20,18 @@ import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Set;
/** Verifies the specific SAML assertion contract consumed by STS. */
final class SAMLAssertionVerifier {
private static final String SAML = "urn:oasis:names:tc:SAML:2.0:assertion";
private static final String DS = "http://www.w3.org/2000/09/xmldsig#";
private static final String AUDIENCE = "urn:amazon:webservices";
static final String ASSERTION_INVALID = "The SAML assertion is invalid.";
static final String SIGNATURE_INVALID = "Response signature invalid";
// SAML 2.0 core 5.4.4. AWS rejects any other reference transform as an invalid signature.
private static final Set<String> ALLOWED_TRANSFORMS = Set.of(
Transform.ENVELOPED, CanonicalizationMethod.EXCLUSIVE, CanonicalizationMethod.EXCLUSIVE_WITH_COMMENTS);
private SAMLAssertionVerifier() {}
@@ -52,6 +60,18 @@ final class SAMLAssertionVerifier {
if (signature == null) {
throw invalid("signature missing");
}
Element signedInfo = first(signature, DS, "SignedInfo");
if (signedInfo == null) {
throw invalid("signed info missing");
}
// Checked on the DOM because unmarshalling some transform parameters fails in the native image.
NodeList transforms = signedInfo.getElementsByTagNameNS(DS, "Transform");
for (int i = 0; i < transforms.getLength(); i++) {
String algorithm = ((Element) transforms.item(i)).getAttribute("Algorithm");
if (!ALLOWED_TRANSFORMS.contains(algorithm)) {
throw new InvalidAssertionException("reference transform " + algorithm, SIGNATURE_INVALID);
}
}
X509Certificate certificate = certificate(provider.getCertificate());
var context = new DOMValidateContext(certificate.getPublicKey(), signature);
context.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.TRUE);
@@ -197,8 +217,25 @@ final class SAMLAssertionVerifier {
}
static final class InvalidAssertionException extends Exception {
InvalidAssertionException(Throwable cause) { super(cause); }
InvalidAssertionException(String message) { super(message); }
private final String awsMessage;
InvalidAssertionException(Throwable cause) {
super(cause);
this.awsMessage = ASSERTION_INVALID;
}
InvalidAssertionException(String message) {
this(message, ASSERTION_INVALID);
}
InvalidAssertionException(String reason, String awsMessage) {
super(reason);
this.awsMessage = awsMessage;
}
String awsMessage() {
return awsMessage;
}
}
}
@@ -351,7 +351,7 @@ public class StsQueryHandler {
try {
verified = SAMLAssertionVerifier.verify(getParam(params, "SAMLAssertion"), provider, Instant.now());
} catch (SAMLAssertionVerifier.InvalidAssertionException e) {
throw new AwsException("InvalidIdentityToken", "The SAML assertion is invalid.", 400);
throw new AwsException("InvalidIdentityToken", e.awsMessage(), 400);
}
boolean rolePair = verified.roles().stream().anyMatch(pair ->
roleArn.equals(pair.roleArn()) && principalArn.equals(pair.principalArn()));
@@ -0,0 +1,59 @@
package io.github.hectorvent.floci.services.iam;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import org.w3c.dom.Element;
import java.io.OutputStream;
/**
* Native image substitutions for the XSLT and XPath transforms of the JDK XML signature code.
* SAMLAssertionVerifier rejects every transform except enveloped signature and exclusive
* canonicalization before it validates, as AWS does, so these never run. Every registered transform
* is reachable through one virtual call, so without these the XSLT compiler and the XPath engine
* stay in the image.
*/
final class XmlSignatureTransformSubstitutions {
static final String UNAVAILABLE = " transform is not supported";
private XmlSignatureTransformSubstitutions() {
}
}
@TargetClass(className = "com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput")
final class Target_XMLSignatureInput {
}
@TargetClass(className = "com.sun.org.apache.xml.internal.security.transforms.implementations.TransformXSLT")
final class Target_TransformXSLT {
@Substitute
protected Target_XMLSignatureInput enginePerformTransform(Target_XMLSignatureInput input, OutputStream os,
Element transformElement, String baseURI,
boolean secureValidation) {
throw new UnsupportedOperationException("XSLT" + XmlSignatureTransformSubstitutions.UNAVAILABLE);
}
}
@TargetClass(className = "com.sun.org.apache.xml.internal.security.transforms.implementations.TransformXPath")
final class Target_TransformXPath {
@Substitute
protected Target_XMLSignatureInput enginePerformTransform(Target_XMLSignatureInput input, OutputStream os,
Element transformElement, String baseURI,
boolean secureValidation) {
throw new UnsupportedOperationException("XPath" + XmlSignatureTransformSubstitutions.UNAVAILABLE);
}
}
@TargetClass(className = "com.sun.org.apache.xml.internal.security.transforms.implementations.TransformXPath2Filter")
final class Target_TransformXPath2Filter {
@Substitute
protected Target_XMLSignatureInput enginePerformTransform(Target_XMLSignatureInput input, OutputStream os,
Element transformElement, String baseURI,
boolean secureValidation) {
throw new UnsupportedOperationException("XPath Filter 2.0" + XmlSignatureTransformSubstitutions.UNAVAILABLE);
}
}