mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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:
1 parent
fde0688ef3
commit
59fda495a6
5 files changed
+229
-6
No files matched your search
@@ -16,6 +16,7 @@ import software.amazon.awssdk.services.sts.model.*;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import javax.xml.crypto.dsig.CanonicalizationMethod;
|
||||
import javax.xml.crypto.dsig.DigestMethod;
|
||||
import javax.xml.crypto.dsig.Reference;
|
||||
import javax.xml.crypto.dsig.SignatureMethod;
|
||||
@@ -23,6 +24,10 @@ import javax.xml.crypto.dsig.SignedInfo;
|
||||
import javax.xml.crypto.dsig.Transform;
|
||||
import javax.xml.crypto.dsig.XMLSignatureFactory;
|
||||
import javax.xml.crypto.dsig.dom.DOMSignContext;
|
||||
import javax.xml.crypto.dsig.spec.TransformParameterSpec;
|
||||
import javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec;
|
||||
import javax.xml.crypto.dsig.spec.XPathFilterParameterSpec;
|
||||
import javax.xml.crypto.dsig.spec.XPathType;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
@@ -40,6 +45,7 @@ import java.util.Base64;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@@ -224,6 +230,21 @@ class StsTest {
|
||||
assertThat(response.assumedRoleUser().arn()).contains("assumed-role/" + allowedRoleName + "/");
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithSamlRejectsXPathTransform() {
|
||||
assertSamlSignatureInvalid(factory -> List.of(transform(factory, Transform.ENVELOPED, null),
|
||||
transform(factory, Transform.XPATH, new XPathFilterParameterSpec("true()")),
|
||||
transform(factory, CanonicalizationMethod.EXCLUSIVE, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithSamlRejectsXPathFilter2Transform() {
|
||||
assertSamlSignatureInvalid(factory -> List.of(transform(factory, Transform.ENVELOPED, null),
|
||||
transform(factory, Transform.XPATH2,
|
||||
new XPathFilter2ParameterSpec(List.of(new XPathType("/", XPathType.Filter.UNION)))),
|
||||
transform(factory, CanonicalizationMethod.EXCLUSIVE, null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithWebIdentityMissingTokenThrows400() {
|
||||
assertThatThrownBy(() -> sts.assumeRoleWithWebIdentity(
|
||||
@@ -295,6 +316,30 @@ class StsTest {
|
||||
.build());
|
||||
}
|
||||
|
||||
private static void assertSamlSignatureInvalid(Function<XMLSignatureFactory, List<Transform>> transforms) {
|
||||
String issuer = "https://sdk-test.example.test/saml";
|
||||
AssumeRoleWithSamlRequest request = AssumeRoleWithSamlRequest.builder()
|
||||
.roleArn(allowedRoleArn)
|
||||
.principalArn(providerArn)
|
||||
.samlAssertion(signedAssertion(allowedRoleArn, providerArn, issuer, transforms))
|
||||
.build();
|
||||
|
||||
assertThatThrownBy(() -> sts.assumeRoleWithSAML(request))
|
||||
.isInstanceOfSatisfying(StsException.class, e -> {
|
||||
assertThat(e.statusCode()).isEqualTo(400);
|
||||
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("InvalidIdentityToken");
|
||||
assertThat(e.awsErrorDetails().errorMessage()).isEqualTo("Response signature invalid");
|
||||
});
|
||||
}
|
||||
|
||||
private static Transform transform(XMLSignatureFactory factory, String algorithm, TransformParameterSpec spec) {
|
||||
try {
|
||||
return factory.newTransform(algorithm, spec);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Could not create transform " + algorithm, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static KeyPair createSigningKeys() throws Exception {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||
@@ -316,6 +361,12 @@ class StsTest {
|
||||
|
||||
|
||||
private static String signedAssertion(String roleArn, String principalArn, String issuer) {
|
||||
return signedAssertion(roleArn, principalArn, issuer,
|
||||
factory -> List.of(transform(factory, Transform.ENVELOPED, null)));
|
||||
}
|
||||
|
||||
private static String signedAssertion(String roleArn, String principalArn, String issuer,
|
||||
Function<XMLSignatureFactory, List<Transform>> transforms) {
|
||||
String id = "_" + UUID.randomUUID();
|
||||
Instant expiry = Instant.now().plusSeconds(300);
|
||||
String xml = "<saml:Assertion xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\" ID=\"" + id
|
||||
@@ -334,8 +385,7 @@ class StsTest {
|
||||
XMLSignatureFactory factory = XMLSignatureFactory.getInstance("DOM");
|
||||
Reference reference = factory.newReference("#" + id,
|
||||
factory.newDigestMethod(DigestMethod.SHA256, null),
|
||||
List.of(factory.newTransform(Transform.ENVELOPED,
|
||||
(javax.xml.crypto.dsig.spec.TransformParameterSpec) null)), null, null);
|
||||
transforms.apply(factory), null, null);
|
||||
SignedInfo signedInfo = factory.newSignedInfo(
|
||||
factory.newCanonicalizationMethod("http://www.w3.org/2001/10/xml-exc-c14n#",
|
||||
(javax.xml.crypto.dsig.spec.C14NMethodParameterSpec) null),
|
||||
|
||||
Reference in new issue
Block a user