fix(auth): explain signature date and service mismatches (#5591)

* fix(auth): explain expired query signatures and service mismatches

* fix(auth): distinguish future and expired signatures
This commit is contained in:
David Phillips authored and GitHub committed 2026-10-13 20:11:09 -04:00
1 parent dbf6c3016d
commit d5c6a36ac9
5 files changed
+226 -35

No files matched your search

@@ -9,6 +9,7 @@ import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.auth.signer.Aws4Signer;
import software.amazon.awssdk.auth.signer.params.Aws4PresignerParams;
import software.amazon.awssdk.auth.signer.params.Aws4SignerParams;
import software.amazon.awssdk.http.SdkHttpFullRequest;
import software.amazon.awssdk.http.SdkHttpMethod;
import software.amazon.awssdk.services.iam.IamClient;
@@ -26,8 +27,12 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import static org.assertj.core.api.Assertions.assertThat;
@@ -95,12 +100,7 @@ class StsPresignedIdentityTest {
private static Document presignedIdentity(AwsCredentials credentials, HttpClient.Version version) throws Exception {
byte[] body = "Action=GetCallerIdentity&Version=2011-06-15".getBytes(StandardCharsets.UTF_8);
SdkHttpFullRequest signed = Aws4Signer.create().presign(SdkHttpFullRequest.builder()
.uri(TestFixtures.endpoint()).method(SdkHttpMethod.POST)
.putHeader("Content-Type", "application/x-www-form-urlencoded")
.contentStreamProvider(() -> new ByteArrayInputStream(body)).build(),
Aws4PresignerParams.builder().awsCredentials(credentials).signingRegion(TestFixtures.region())
.signingName("sts").expirationTime(Instant.now().plusSeconds(60)).build());
SdkHttpFullRequest signed = presign(credentials, body, "sts", Instant.now());
HttpResponse<byte[]> response = send(signed, signed.getUri(), body, version);
assertThat(response.statusCode()).as(new String(response.body(), StandardCharsets.UTF_8)).isEqualTo(200);
boolean signaturesEnabled = Boolean.parseBoolean(System.getenv("FLOCI_AUTH_VALIDATE_SIGNATURES"));
@@ -108,6 +108,27 @@ class StsPresignedIdentityTest {
URI forged = URI.create(signed.getUri().toString().replaceAll("X-Amz-Signature=[^&]*",
"X-Amz-Signature=" + "0".repeat(64)));
assertSignatureRefused(send(signed, forged, body, version));
Instant signedAt = Instant.now().minusSeconds(120).truncatedTo(ChronoUnit.SECONDS);
SdkHttpFullRequest expired = presign(credentials, body, "sts", signedAt);
Instant before = Instant.now().minusSeconds(60).truncatedTo(ChronoUnit.SECONDS);
HttpResponse<byte[]> expiryResponse = send(expired, expired.getUri(), body, version);
Instant after = Instant.now().minusSeconds(60).truncatedTo(ChronoUnit.SECONDS);
assertSignatureRefused(expiryResponse);
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
String prefix = "Signature expired: " + format.format(signedAt) + " is now earlier than ";
String expiryMessage = parse(expiryResponse.body()).getElementsByTagNameNS("*", "Message")
.item(0).getTextContent();
assertThat(expiryMessage).startsWith(prefix);
Instant cutoff = Instant.from(format.parse(expiryMessage.substring(prefix.length())));
assertThat(cutoff).isBetween(before, after);
assertClockRefusal(credentials, body, version, true, 1200);
assertClockRefusal(credentials, body, version, false, 1200);
assertClockRefusal(credentials, body, version, false, -1200);
SdkHttpFullRequest wrongService = presign(credentials, body, "iam", Instant.now());
HttpResponse<byte[]> scopeResponse = send(wrongService, wrongService.getUri(), body, version);
assertSignatureRefused(scopeResponse);
assertThat(parse(scopeResponse.body()).getElementsByTagNameNS("*", "Message").item(0).getTextContent())
.isEqualTo("Credential should be scoped to correct service: 'sts'");
assertSignatureRefused(send(signed, signed.getUri(),
(new String(body, StandardCharsets.UTF_8) + "&Extra=changed").getBytes(StandardCharsets.UTF_8), version));
}
@@ -120,6 +141,44 @@ class StsPresignedIdentityTest {
assertThat(new String(invalid.body(), StandardCharsets.UTF_8)).contains("InvalidClientTokenId");
}
}
return parse(response.body());
}
private static void assertClockRefusal(AwsCredentials credentials, byte[] body, HttpClient.Version version,
boolean query, long offset) throws Exception {
Instant signedAt = Instant.now().plusSeconds(offset).truncatedTo(ChronoUnit.SECONDS);
SdkHttpFullRequest signed = query ? presign(credentials, body, "sts", signedAt)
: Aws4Signer.create().sign(SdkHttpFullRequest.builder()
.uri(TestFixtures.endpoint()).method(SdkHttpMethod.POST)
.putHeader("Content-Type", "application/x-www-form-urlencoded")
.contentStreamProvider(() -> new ByteArrayInputStream(body)).build(),
Aws4SignerParams.builder().awsCredentials(credentials).signingRegion(TestFixtures.region())
.signingName("sts").signingClockOverride(Clock.fixed(signedAt, ZoneOffset.UTC)).build());
long cutoffOffset = offset > 0 ? 900 : -900;
Instant before = Instant.now().plusSeconds(cutoffOffset).truncatedTo(ChronoUnit.SECONDS);
HttpResponse<byte[]> response = send(signed, signed.getUri(), body, version);
Instant after = Instant.now().plusSeconds(cutoffOffset).truncatedTo(ChronoUnit.SECONDS);
assertSignatureRefused(response);
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
String prefix = offset > 0
? "Signature not yet current: " + format.format(signedAt) + " is still later than "
: "Signature expired: " + format.format(signedAt) + " is now earlier than ";
String message = parse(response.body()).getElementsByTagNameNS("*", "Message").item(0).getTextContent();
assertThat(message).startsWith(prefix);
assertThat(Instant.from(format.parse(message.substring(prefix.length())))).isBetween(before, after);
}
private static SdkHttpFullRequest presign(AwsCredentials credentials, byte[] body, String service, Instant signedAt) {
return Aws4Signer.create().presign(SdkHttpFullRequest.builder()
.uri(TestFixtures.endpoint()).method(SdkHttpMethod.POST)
.putHeader("Content-Type", "application/x-www-form-urlencoded")
.contentStreamProvider(() -> new ByteArrayInputStream(body)).build(),
Aws4PresignerParams.builder().awsCredentials(credentials).signingRegion(TestFixtures.region())
.signingName(service).signingClockOverride(Clock.fixed(signedAt, ZoneOffset.UTC))
.expirationTime(signedAt.plusSeconds(60)).build());
}
private static Document parse(byte[] body) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
@@ -128,7 +187,7 @@ class StsPresignedIdentityTest {
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
factory.setXIncludeAware(false);
factory.setExpandEntityReferences(false);
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(response.body()));
return factory.newDocumentBuilder().parse(new ByteArrayInputStream(body));
}
private static void assertSignatureRefused(HttpResponse<byte[]> response) {