Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): verify the SigV4 header signature when enforce-auth is enabled (#3573)

Davidbfe2b715870c
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
index c7fb023996..8d509bc042 100644
--- a/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/PreSignedUrlFilter.java
@@ -286,8 +286,8 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
return encoded.toString();
}
- private static byte[] deriveSigningKey(String secretKey, String date, String region,
- String service) throws Exception {
+ static byte[] deriveSigningKey(String secretKey, String date, String region,
+ String service) throws Exception {
byte[] kSecret = ("AWS4" + secretKey).getBytes(StandardCharsets.UTF_8);
byte[] kDate = hmacSha256(kSecret, date);
byte[] kRegion = hmacSha256(kDate, region);
@@ -295,18 +295,18 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
return hmacSha256(kService, "aws4_request");
}
- private static byte[] hmacSha256(byte[] key, String data) throws Exception {
+ static byte[] hmacSha256(byte[] key, String data) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key, "HmacSHA256"));
return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
}
- private static String sha256Hex(String input) throws Exception {
+ static String sha256Hex(String input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return hexEncode(digest.digest(input.getBytes(StandardCharsets.UTF_8)));
}
- private static String hexEncode(byte[] bytes) {
+ static String hexEncode(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
sb.append(String.format("%02x", b));
@@ -314,7 +314,7 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
return sb.toString();
}
- private Response errorResponse(int status, String code, String message) {
+ static Response errorResponse(int status, String code, String message) {
String xml = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("Error")
diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java
new file mode 100644
index 0000000000..7846273347
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java
@@ -0,0 +1,291 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.github.hectorvent.floci.services.iam.IamService;
+import jakarta.annotation.Priority;
+import jakarta.inject.Inject;
+import jakarta.ws.rs.Priorities;
+import jakarta.ws.rs.container.ContainerRequestContext;
+import jakarta.ws.rs.container.ContainerRequestFilter;
+import jakarta.ws.rs.container.ResourceInfo;
+import jakarta.ws.rs.core.Context;
+import jakarta.ws.rs.ext.Provider;
+import org.jboss.logging.Logger;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+import java.util.Optional;
+
+/**
+ * Verifies the SigV4 signature carried in the {@code Authorization} header of an S3 request when
+ * {@code floci.services.s3.enforce-auth} is enabled.
+ *
+ * <p>The presigned placements are already verified elsewhere ({@link PreSignedUrlFilter} for the
+ * query string, {@link S3PostPolicySigner} for a browser POST). This filter closes the remaining
+ * placement, the one every AWS SDK uses for ordinary calls: the canonical request is rebuilt from
+ * the request as it arrived (raw path, canonical query string, the headers named in
+ * {@code SignedHeaders}, and the {@code x-amz-content-sha256} value the client signed), the
+ * signature is derived with the key's secret, and the two are compared in constant time. When the
+ * declared content hash is a real digest rather than an {@code UNSIGNED-PAYLOAD} or
+ * {@code STREAMING-*} sentinel, the body is hashed too, so a signed request cannot be replayed
+ * with a different payload.
+ *
+ * <p>Error codes, statuses and ordering follow real S3: a structurally bad header is
+ * {@code 400 AuthorizationHeaderMalformed}; a request with no date is {@code 403 AccessDenied};
+ * a missing {@code x-amz-content-sha256} is {@code 400 InvalidRequest}; an unregistered key is
+ * {@code 403 InvalidAccessKeyId}; a request more than fifteen minutes from server time is
+ * {@code 403 RequestTimeTooSkewed}; a signature that does not verify is
+ * {@code 403 SignatureDoesNotMatch}; and a body that does not match its declared hash is
+ * {@code 400 XAmzContentSHA256Mismatch}.
+ *
+ * <h2>Deliberate deviations from real AWS</h2>
+ * <ul>
+ * <li>The credential scope's region is not checked against the bucket's region. Floci resolves
+ * a request's region <em>from</em> that scope, so pinning it would be circular.</li>
+ * <li>The per-chunk signatures of an {@code aws-chunked} upload are not verified; only the seed
+ * signature over the headers is. The controller strips the chunk framing itself.</li>
+ * <li>The well-known local-dev {@code test}/{@code test} credential pair is honoured, mirroring
+ * {@link PreSignedUrlFilter} and {@link S3PostPolicySigner}.</li>
+ * </ul>
+ *
+ * <p>Nothing here runs with the flag off: the filter returns before reading a single header, so
+ * the default configuration keeps accepting any well-formed {@code Authorization} header exactly
+ * as before.
+ */
+@Provider
+@Priority(Priorities.AUTHENTICATION)
+public class S3HeaderSignatureFilter implements ContainerRequestFilter {
+
+ private static final Logger LOG = Logger.getLogger(S3HeaderSignatureFilter.class);
+
+ private static final String ALGORITHM = "AWS4-HMAC-SHA256";
+ private static final String SIGNING_SERVICE = "s3";
+ private static final String TERMINATOR = "aws4_request";
+ private static final Duration MAX_CLOCK_SKEW = Duration.ofMinutes(15);
+ private static final DateTimeFormatter AMZ_DATE =
+ DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
+
+ private final S3Service s3Service;
+ private final IamService iamService;
+
+ @Context
+ ResourceInfo resourceInfo;
+
+ @Inject
+ public S3HeaderSignatureFilter(S3Service s3Service, IamService iamService) {
+ this.s3Service = s3Service;
+ this.iamService = iamService;
+ }
+
+ @Override
+ public void filter(ContainerRequestContext ctx) throws IOException {
+ if (!s3Service.isAuthEnforced() || !routedToS3()) {
+ return;
+ }
+ String authorization = ctx.getHeaderString("Authorization");
+ if (authorization == null || !authorization.startsWith(ALGORITHM + " ")) {
+ return;
+ }
+
+ String credential = component(authorization, "Credential");
+ String signedHeaders = component(authorization, "SignedHeaders");
+ String signature = component(authorization, "Signature");
+ if (credential == null || signedHeaders == null || signature == null) {
+ abort(ctx, 400, "AuthorizationHeaderMalformed", "The authorization header is malformed; "
+ + "the authorization header requires three components: "
+ + "Credential, SignedHeaders, and Signature.");
+ return;
+ }
+ String[] scope = credential.split("/");
+ if (scope.length != 5 || !TERMINATOR.equals(scope[4])) {
+ abort(ctx, 400, "AuthorizationHeaderMalformed", "The authorization header is malformed; "
+ + "the Credential is mal-formed; expecting "
+ + "\"<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request\".");
+ return;
+ }
+ String accessKeyId = scope[0];
+ String scopeDate = scope[1];
+ String region = scope[2];
+ if (!SIGNING_SERVICE.equals(scope[3])) {
+ abort(ctx, 400, "AuthorizationHeaderMalformed", "The authorization header is malformed; "
+ + "incorrect service \"" + scope[3] + "\". This endpoint belongs to \""
+ + SIGNING_SERVICE + "\".");
+ return;
+ }
+
+ Instant requestTime = requestTime(ctx);
+ if (requestTime == null) {
+ abort(ctx, 403, "AccessDenied", "AWS authentication requires a valid Date or x-amz-date header");
+ return;
+ }
+ String amzDate = AMZ_DATE.format(requestTime);
+ if (!amzDate.startsWith(scopeDate)) {
+ abort(ctx, 400, "AuthorizationHeaderMalformed", "The authorization header is malformed; "
+ + "Date in Credential scope does not match YYYYMMDD from ISO-8601 version of "
+ + "date from HTTP.");
+ return;
+ }
+
+ String declaredHash = ctx.getHeaderString("x-amz-content-sha256");
+ if (declaredHash == null || declaredHash.isBlank()) {
+ abort(ctx, 400, "InvalidRequest", "Missing required header for this request: x-amz-content-sha256");
+ return;
+ }
+ declaredHash = declaredHash.trim();
+
+ Optional<String> secretKey = S3PostPolicySigner.resolveSecretKey(
+ iamService, accessKeyId, ctx.getHeaderString("X-Amz-Security-Token"));
+ if (secretKey.isEmpty()) {
+ abort(ctx, 403, "InvalidAccessKeyId",
+ "The AWS Access Key Id you provided does not exist in our records.");
+ return;
+ }
+
+ if (Duration.between(requestTime, Instant.now()).abs().compareTo(MAX_CLOCK_SKEW) > 0) {
+ abort(ctx, 403, "RequestTimeTooSkewed",
+ "The difference between the request time and the server's time is too large.");
+ return;
+ }
+
+ boolean matches;
+ try {
+ matches = containsHeader(signedHeaders, "host") && signatureMatches(
+ ctx, secretKey.get(), scopeDate, region, amzDate, signedHeaders, declaredHash, signature);
+ } catch (Exception e) {
+ LOG.debugv(e, "S3 header SigV4 verification failed to complete for accessKey={0}", accessKeyId);
+ matches = false;
+ }
+ if (!matches) {
+ abort(ctx, 403, "SignatureDoesNotMatch", "The request signature we calculated does not "
+ + "match the signature you provided. Check your key and signing method.");
+ return;
+ }
+
+ if (isSha256Hex(declaredHash) && !bodyMatches(ctx, declaredHash)) {
+ abort(ctx, 400, "XAmzContentSHA256Mismatch",
+ "The provided 'x-amz-content-sha256' header does not match what was computed.");
+ }
+ }
+
+ private boolean routedToS3() {
+ return resourceInfo != null && S3Controller.class.equals(resourceInfo.getResourceClass());
+ }
+
+ private boolean signatureMatches(ContainerRequestContext ctx, String secretKey, String scopeDate,
+ String region, String amzDate, String signedHeaders,
+ String payloadHash, String signature) throws Exception {
+ URI requestUri = ctx.getProperty(S3VirtualHostFilter.ORIGINAL_REQUEST_URI_PROPERTY) instanceof URI uri
+ ? uri
+ : ctx.getUriInfo().getRequestUri();
+ String host = S3VirtualHostFilter.resolveHost(ctx.getHeaderString("Host"), requestUri);
+
+ StringBuilder canonicalHeaders = new StringBuilder();
+ for (String name : signedHeaders.split(";")) {
+ String value = "host".equals(name) ? host : ctx.getHeaderString(name);
+ canonicalHeaders.append(name).append(':')
+ .append(PreSignedUrlFilter.canonicalizeHeaderValue(value)).append('\n');
+ }
+
+ String canonicalRequest = ctx.getMethod() + "\n"
+ + requestUri.getRawPath() + "\n"
+ + PreSignedUrlFilter.buildCanonicalQueryString(ctx.getUriInfo().getQueryParameters()) + "\n"
+ + canonicalHeaders + "\n"
+ + signedHeaders + "\n"
+ + payloadHash;
+ String stringToSign = ALGORITHM + "\n"
+ + amzDate + "\n"
+ + scopeDate + "/" + region + "/" + SIGNING_SERVICE + "/" + TERMINATOR + "\n"
+ + PreSignedUrlFilter.sha256Hex(canonicalRequest);
+ byte[] signingKey = PreSignedUrlFilter.deriveSigningKey(secretKey, scopeDate, region, SIGNING_SERVICE);
+ String expected = PreSignedUrlFilter.hexEncode(PreSignedUrlFilter.hmacSha256(signingKey, stringToSign));
+ return MessageDigest.isEqual(
+ expected.getBytes(StandardCharsets.UTF_8),
+ signature.getBytes(StandardCharsets.UTF_8));
+ }
+
+ /**
+ * Hashes the entity and hands it back to the request untouched. The S3 controllers already
+ * receive the body as a {@code byte[]}, so buffering it here costs nothing extra. The stream
+ * is read directly rather than gated on {@code hasEntity()}: that flag is false for a request
+ * without a {@code Content-Type}, and the AWS CLI sends most bucket configuration bodies
+ * (and {@code put-object --body}) without one.
+ */
+ private static boolean bodyMatches(ContainerRequestContext ctx, String declaredHash) throws IOException {
+ byte[] body = ctx.getEntityStream() != null ? ctx.getEntityStream().readAllBytes() : new byte[0];
+ ctx.setEntityStream(new ByteArrayInputStream(body));
+ try {
+ String actual = PreSignedUrlFilter.hexEncode(MessageDigest.getInstance("SHA-256").digest(body));
+ return MessageDigest.isEqual(
+ actual.getBytes(StandardCharsets.UTF_8),
+ declaredHash.toLowerCase().getBytes(StandardCharsets.UTF_8));
+ } catch (java.security.NoSuchAlgorithmException e) {
+ throw new IllegalStateException("SHA-256 is required by SigV4", e);
+ }
+ }
+
+ /**
+ * The request timestamp the client signed: {@code x-amz-date} in ISO 8601 basic form, or the
+ * HTTP {@code Date} header when the client signed that instead. Either header may carry the
+ * timestamp in SigV4; {@code x-amz-date} wins when both are present.
+ */
+ private static Instant requestTime(ContainerRequestContext ctx) {
+ String amzDate = ctx.getHeaderString("x-amz-date");
+ try {
+ if (amzDate != null && !amzDate.isBlank()) {
+ return Instant.from(AMZ_DATE.parse(amzDate.trim()));
+ }
+ String httpDate = ctx.getHeaderString("Date");
+ if (httpDate != null && !httpDate.isBlank()) {
+ return Instant.from(DateTimeFormatter.RFC_1123_DATE_TIME.parse(httpDate.trim()));
+ }
+ } catch (DateTimeParseException e) {
+ return null;
+ }
+ return null;
+ }
+
+ /** The value of one {@code Name=value} component of a SigV4 {@code Authorization} header. */
+ private static String component(String authorization, String name) {
+ String parameters = authorization.substring(ALGORITHM.length() + 1);
+ for (String part : parameters.split(",")) {
+ String trimmed = part.trim();
+ if (trimmed.startsWith(name + "=")) {
+ String value = trimmed.substring(name.length() + 1).trim();
+ return value.isEmpty() ? null : value;
+ }
+ }
+ return null;
+ }
+
+ private static boolean containsHeader(String signedHeaders, String name) {
+ for (String header : signedHeaders.split(";")) {
+ if (name.equals(header)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static boolean isSha256Hex(String value) {
+ if (value.length() != 64) {
+ return false;
+ }
+ for (int index = 0; index < value.length(); index++) {
+ if (Character.digit(value.charAt(index), 16) < 0) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static void abort(ContainerRequestContext ctx, int status, String code, String message) {
+ ctx.abortWith(PreSignedUrlFilter.errorResponse(status, code, message));
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java
index 866c01423e..fa22619f48 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3AuthEnforcementIntegrationTest.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.services.iam.IamService;
+import io.github.hectorvent.floci.testutil.S3RequestSigner;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
@@ -10,7 +11,11 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
+import java.net.URI;
import java.net.URLEncoder;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.Instant;
@@ -25,6 +30,7 @@ import javax.crypto.spec.SecretKeySpec;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
@QuarkusTest
@@ -65,18 +71,18 @@ class S3AuthEnforcementIntegrationTest {
.withZone(ZoneOffset.UTC)
.format(Instant.now());
private static final String SIGNING_DATE = SIGNING_TIMESTAMP.substring(0, 8);
- private static final String LOCAL_AUTH_HEADER = authorizationHeader("test");
- private static final String BAD_AUTH_HEADER = authorizationHeader("bad-key");
- private static final String ACCOUNT_SHAPED_AUTH_HEADER = authorizationHeader("123456789012");
+ private static final S3RequestSigner LOCAL_SIGNER = S3RequestSigner.signedAs("test", "test");
+ private static final S3RequestSigner BAD_KEY_SIGNER = S3RequestSigner.signedAs("bad-key", "bad-secret");
+ private static final S3RequestSigner ACCOUNT_SHAPED_SIGNER = S3RequestSigner.signedAs("123456789012", "test");
@Test
@Order(1)
void createBucketsAndObjects() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + PUBLIC_BUCKET).then().statusCode(200);
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + PRIVATE_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + PUBLIC_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + PRIVATE_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("public body")
.when()
.put("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
@@ -84,7 +90,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("private body")
.when()
.put("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
@@ -92,7 +98,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicReadPolicy(PUBLIC_BUCKET))
.when()
@@ -161,7 +167,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(6)
void signedRequestWithBadAccessKeyCannotUsePublicAccess() {
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.when()
.get("/" + PUBLIC_BUCKET + "/" + PUBLIC_KEY)
.then()
@@ -173,7 +179,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(7)
void signedRequestWithAccountShapedAccessKeyCannotReadPrivateObject() {
given()
- .header("Authorization", ACCOUNT_SHAPED_AUTH_HEADER)
+ .filter(ACCOUNT_SHAPED_SIGNER)
.when()
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
.then()
@@ -217,7 +223,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(10)
void signedRequestWithLocalAccessKeyCanReadPrivateObject() {
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY)
.then()
@@ -247,10 +253,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(12)
void websiteRootAuthorizesIndexObjectReadNotBucketList() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + WEBSITE_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + WEBSITE_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("text/html")
.body("<html>index</html>")
.when()
@@ -259,7 +265,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/xml")
.body(websiteConfiguration())
.when()
@@ -268,7 +274,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicGetObjectPolicy(WEBSITE_BUCKET))
.when()
@@ -296,10 +302,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(13)
void websiteRootUsesErrorDocumentForDeniedIndexObject() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + WEBSITE_ERROR_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + WEBSITE_ERROR_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("text/html")
.body("<html>private index</html>")
.when()
@@ -308,7 +314,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("text/html")
.body("<html>denied</html>")
.when()
@@ -317,7 +323,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/xml")
.body(websiteConfiguration(ERROR_KEY))
.when()
@@ -326,7 +332,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicGetObjectPolicy(WEBSITE_ERROR_BUCKET, ERROR_KEY))
.when()
@@ -348,10 +354,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(14)
void bucketAclPublicReadAllowsUnsignedList() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + BUCKET_ACL_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + BUCKET_ACL_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("listed")
.when()
.put("/" + BUCKET_ACL_BUCKET + "/" + ACL_KEY)
@@ -359,7 +365,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/xml")
.body(publicReadAcl())
.when()
@@ -378,10 +384,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(15)
void explicitBucketPolicyDenyOverridesPublicObjectAcl() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + DENY_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + DENY_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.header("x-amz-acl", "public-read")
.body("denied")
.when()
@@ -390,7 +396,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(denyGetObjectPolicy(DENY_BUCKET))
.when()
@@ -452,14 +458,14 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(403);
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.when()
.head("/" + PUBLIC_BUCKET)
.then()
.statusCode(403);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.head("/" + PRIVATE_BUCKET)
.then()
@@ -479,7 +485,7 @@ class S3AuthEnforcementIntegrationTest {
.body("Error.Code", equalTo("AccessDenied"));
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.contentType("application/xml")
.body(selectRequest())
.when()
@@ -492,10 +498,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(20)
void missingObjectRequiresListBucketToReturnNoSuchKey() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + GET_ONLY_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + GET_ONLY_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicGetObjectPolicy(GET_ONLY_BUCKET))
.when()
@@ -521,10 +527,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(21)
void versionedObjectReadRequiresGetObjectVersion() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + VERSION_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + VERSION_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/xml")
.body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when()
@@ -533,7 +539,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
String versionId = given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("versioned body")
.when()
.put("/" + VERSION_BUCKET + "/" + VERSION_KEY)
@@ -542,7 +548,7 @@ class S3AuthEnforcementIntegrationTest {
.extract().header("x-amz-version-id");
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(VERSION_BUCKET, "s3:GetObject"))
.when()
@@ -558,7 +564,7 @@ class S3AuthEnforcementIntegrationTest {
.body("Error.Code", equalTo("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(VERSION_BUCKET, "s3:GetObjectVersion"))
.when()
@@ -715,10 +721,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(29)
void unsignedRequestCannotPutObject() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + WRITE_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + WRITE_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("signed body")
.when()
.put("/" + WRITE_BUCKET + "/" + WRITE_KEY)
@@ -734,7 +740,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + WRITE_BUCKET + "/" + ANON_WRITE_KEY)
.then()
@@ -752,7 +758,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + WRITE_BUCKET + "/" + WRITE_KEY)
.then()
@@ -760,7 +766,7 @@ class S3AuthEnforcementIntegrationTest {
.body(equalTo("signed body"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.delete("/" + WRITE_BUCKET + "/" + WRITE_KEY)
.then()
@@ -771,7 +777,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(31)
void signedRequestWithBadAccessKeyCannotWriteObject() {
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.body("bad key body")
.when()
.put("/" + WRITE_BUCKET + "/" + ANON_WRITE_KEY)
@@ -780,7 +786,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("InvalidAccessKeyId"));
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.when()
.delete("/" + WRITE_BUCKET + "/" + ANON_WRITE_KEY)
.then()
@@ -791,10 +797,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(32)
void bucketPolicyCanExplicitlyAllowAnonymousWrite() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + PUBLIC_WRITE_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + PUBLIC_WRITE_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(PUBLIC_WRITE_BUCKET, "s3:PutObject"))
.when()
@@ -810,7 +816,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(PUBLIC_WRITE_BUCKET, "s3:DeleteObject"))
.when()
@@ -829,7 +835,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(33)
void unsignedRequestCannotWriteObjectSubresources() {
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("subresource body")
.when()
.put("/" + WRITE_BUCKET + "/subresource.txt")
@@ -891,7 +897,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
String uploadId = given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.post("/" + WRITE_BUCKET + "/multipart.txt?uploads")
.then()
@@ -907,7 +913,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
String eTag = given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("part data")
.when()
.put("/" + WRITE_BUCKET + "/multipart.txt?uploadId=" + uploadId + "&partNumber=1")
@@ -932,7 +938,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/xml")
.body(completeMultipartBody(eTag))
.when()
@@ -954,7 +960,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(35)
void unsignedRequestCannotCopyObject() {
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("copy source body")
.when()
.put("/" + WRITE_BUCKET + "/copy-source.txt")
@@ -970,7 +976,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.header("x-amz-copy-source", "/" + WRITE_BUCKET + "/copy-source.txt")
.when()
.put("/" + WRITE_BUCKET + "/copy-dest.txt")
@@ -982,7 +988,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(36)
void batchDeleteObjectsDeniesUnauthorizedKeysButAllowsAuthorized() {
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("batch a")
.when()
.put("/" + WRITE_BUCKET + "/batch-a.txt")
@@ -990,7 +996,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("batch b")
.when()
.put("/" + WRITE_BUCKET + "/batch-b.txt")
@@ -1017,7 +1023,7 @@ class S3AuthEnforcementIntegrationTest {
.body(not(containsString("<Deleted>")));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + WRITE_BUCKET + "/batch-a.txt")
.then()
@@ -1025,7 +1031,7 @@ class S3AuthEnforcementIntegrationTest {
.body(equalTo("batch a"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/xml")
.body(deleteXml)
.when()
@@ -1040,7 +1046,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(37)
void batchDeleteObjectsWithUnknownAccessKeyFailsWholeRequest() {
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("bad-key batch a")
.when()
.put("/" + WRITE_BUCKET + "/bad-key-batch-a.txt")
@@ -1054,7 +1060,7 @@ class S3AuthEnforcementIntegrationTest {
""";
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.contentType("application/xml")
.body(deleteXml)
.when()
@@ -1064,7 +1070,7 @@ class S3AuthEnforcementIntegrationTest {
.body("Error.Code", equalTo("InvalidAccessKeyId"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + WRITE_BUCKET + "/bad-key-batch-a.txt")
.then()
@@ -1078,10 +1084,10 @@ class S3AuthEnforcementIntegrationTest {
// Per AWS's ACL docs, a bucket-ACL WRITE grant to a non-owner (like AllUsers) "denies
// non-owners the ability to overwrite or delete existing objects" -- it only lets them
// create new ones.
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + PUBLIC_WRITE_ACL_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + PUBLIC_WRITE_ACL_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.header("x-amz-acl", "public-read-write")
.when()
.put("/" + PUBLIC_WRITE_ACL_BUCKET + "?acl")
@@ -1096,7 +1102,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + PUBLIC_WRITE_ACL_BUCKET + "/" + ANON_WRITE_KEY)
.then()
@@ -1122,10 +1128,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(39)
void explicitPolicyDenyOverridesPublicWriteAcl() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + DENY_WRITE_ACL_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + DENY_WRITE_ACL_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.header("x-amz-acl", "public-read-write")
.when()
.put("/" + DENY_WRITE_ACL_BUCKET + "?acl")
@@ -1133,7 +1139,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(denyObjectActionPolicy(DENY_WRITE_ACL_BUCKET, "s3:PutObject"))
.when()
@@ -1168,7 +1174,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(40)
void publicWriteAclDoesNotAuthorizeObjectSubresources() {
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("subresource body")
.when()
.put("/" + PUBLIC_WRITE_ACL_BUCKET + "/acl-subresource.txt")
@@ -1197,10 +1203,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(41)
void deleteObjectPolicyGrantDoesNotAuthorizeVersionedDelete() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + DELETE_VERSION_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + DELETE_VERSION_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/xml")
.body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when()
@@ -1209,7 +1215,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
String versionId = given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("versioned delete target")
.when()
.put("/" + DELETE_VERSION_BUCKET + "/" + VERSION_KEY)
@@ -1218,7 +1224,7 @@ class S3AuthEnforcementIntegrationTest {
.extract().header("x-amz-version-id");
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(DELETE_VERSION_BUCKET, "s3:DeleteObject"))
.when()
@@ -1243,10 +1249,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(42)
void deleteObjectDoesNotBypassGovernanceRetentionWithoutDistinctPermission() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + BYPASS_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + BYPASS_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("locked candidate")
.when()
.put("/" + BYPASS_BUCKET + "/" + BYPASS_KEY)
@@ -1254,7 +1260,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(BYPASS_BUCKET, "s3:DeleteObject"))
.when()
@@ -1271,7 +1277,7 @@ class S3AuthEnforcementIntegrationTest {
.body("Error.Code", equalTo("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(deleteAndBypassGovernancePolicy(BYPASS_BUCKET))
.when()
@@ -1307,10 +1313,10 @@ class S3AuthEnforcementIntegrationTest {
// Floci has no request context to evaluate a Condition against, so a conditional Deny
// is treated as applying (fail closed) -- consistent with S3PublicAccessEvaluatorTest's
// conditionalDenyFailsClosedAndOverridesAllow, this must also override the ACL fallback.
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + CONDITIONAL_DENY_ACL_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + CONDITIONAL_DENY_ACL_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.header("x-amz-acl", "public-read-write")
.when()
.put("/" + CONDITIONAL_DENY_ACL_BUCKET + "?acl")
@@ -1318,7 +1324,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(conditionalDenyObjectActionPolicy(CONDITIONAL_DENY_ACL_BUCKET, "s3:PutObject"))
.when()
@@ -1338,10 +1344,10 @@ class S3AuthEnforcementIntegrationTest {
@Test
@Order(44)
void batchDeleteObjectsPolicyGrantDoesNotAuthorizeVersionedDelete() {
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + BATCH_DELETE_VERSION_BUCKET).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + BATCH_DELETE_VERSION_BUCKET).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/xml")
.body("<VersioningConfiguration><Status>Enabled</Status></VersioningConfiguration>")
.when()
@@ -1350,7 +1356,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
String versionId = given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("versioned batch delete target")
.when()
.put("/" + BATCH_DELETE_VERSION_BUCKET + "/" + VERSION_KEY)
@@ -1359,7 +1365,7 @@ class S3AuthEnforcementIntegrationTest {
.extract().header("x-amz-version-id");
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(BATCH_DELETE_VERSION_BUCKET, "s3:DeleteObject"))
.when()
@@ -1384,7 +1390,7 @@ class S3AuthEnforcementIntegrationTest {
.body(not(containsString("<Deleted>")));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + BATCH_DELETE_VERSION_BUCKET + "?versions&prefix=" + VERSION_KEY)
.then()
@@ -1398,10 +1404,10 @@ class S3AuthEnforcementIntegrationTest {
String bucket = "auth-batch-bypass-scope-bucket";
String unlockedKey = "unlocked.txt";
String lockedKey = "locked.txt";
- given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + bucket).then().statusCode(200);
+ given().filter(LOCAL_SIGNER).when().put("/" + bucket).then().statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("unlocked")
.when()
.put("/" + bucket + "/" + unlockedKey)
@@ -1409,7 +1415,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.header("x-amz-object-lock-mode", "GOVERNANCE")
.header("x-amz-object-lock-retain-until-date", "2030-01-01T00:00:00Z")
.body("locked")
@@ -1419,7 +1425,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(200);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicObjectActionPolicy(bucket, "s3:DeleteObject"))
.when()
@@ -1447,14 +1453,14 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("<Code>AccessDenied</Code>"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + bucket + "/" + unlockedKey)
.then()
.statusCode(404);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + bucket + "/" + lockedKey)
.then()
@@ -1514,11 +1520,6 @@ class S3AuthEnforcementIntegrationTest {
""".formatted(bucket, bucket);
}
- private static String authorizationHeader(String accessKeyId) {
- return "AWS4-HMAC-SHA256 Credential=" + credential(accessKeyId)
- + ", SignedHeaders=host;x-amz-date, Signature=test";
- }
-
private static String credential(String accessKeyId) {
return accessKeyId + "/" + SIGNING_DATE + "/us-east-1/s3/aws4_request";
}
@@ -1535,18 +1536,15 @@ class S3AuthEnforcementIntegrationTest {
null,
Instant.now().plusSeconds(3600),
null);
- String authorization = authorizationHeader(accessKeyId);
String path = "/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY;
- given().header("Authorization", authorization)
- .header("X-Amz-Security-Token", sessionToken)
+ given().filter(S3RequestSigner.signedAs(accessKeyId, "temp-key-material", sessionToken))
.when().get(path).then().statusCode(200).body(equalTo("private body"));
- given().header("Authorization", authorization)
+ given().filter(S3RequestSigner.signedAs(accessKeyId, "temp-key-material"))
.when().get(path).then().statusCode(403).body(containsString("InvalidAccessKeyId"));
- given().header("Authorization", authorization)
- .header("X-Amz-Security-Token", "other-session-token")
+ given().filter(S3RequestSigner.signedAs(accessKeyId, "temp-key-material", "other-session-token"))
.when().get(path).then().statusCode(403).body(containsString("InvalidAccessKeyId"));
}
@@ -1561,7 +1559,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.when()
.put("/" + BUCKET_CONFIG_BUCKET)
.then()
@@ -1569,14 +1567,14 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("InvalidAccessKeyId"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.head("/" + BUCKET_CONFIG_BUCKET)
.then()
.statusCode(404);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.put("/" + BUCKET_CONFIG_BUCKET)
.then()
@@ -1590,7 +1588,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.when()
.delete("/" + BUCKET_CONFIG_BUCKET)
.then()
@@ -1598,7 +1596,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("InvalidAccessKeyId"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.head("/" + BUCKET_CONFIG_BUCKET)
.then()
@@ -1609,7 +1607,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(47)
void unsignedRequestCannotRewriteBucketPolicyToExposePrivateObject() {
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.body("secret data")
.when()
.put("/" + BUCKET_CONFIG_BUCKET + "/" + PRIVATE_KEY)
@@ -1640,7 +1638,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + BUCKET_CONFIG_BUCKET + "?policy")
.then()
@@ -1681,7 +1679,7 @@ class S3AuthEnforcementIntegrationTest {
}
given()
- .header("Authorization", BAD_AUTH_HEADER)
+ .filter(BAD_KEY_SIGNER)
.contentType("application/xml")
.body("<Tagging><TagSet><Tag><Key>k</Key><Value>v</Value></Tag></TagSet></Tagging>")
.when()
@@ -1691,7 +1689,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("InvalidAccessKeyId"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + BUCKET_CONFIG_BUCKET + "?tagging")
.then()
@@ -1703,7 +1701,7 @@ class S3AuthEnforcementIntegrationTest {
@Order(49)
void bucketPolicyCanExplicitlyAllowAnonymousBucketConfigWrite() {
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(publicBucketActionPolicy(BUCKET_CONFIG_BUCKET, "s3:PutBucketTagging"))
.when()
@@ -1720,7 +1718,7 @@ class S3AuthEnforcementIntegrationTest {
.statusCode(204);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + BUCKET_CONFIG_BUCKET + "?tagging")
.then()
@@ -1738,7 +1736,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.delete("/" + BUCKET_CONFIG_BUCKET + "?policy")
.then()
@@ -1760,7 +1758,7 @@ class S3AuthEnforcementIntegrationTest {
}
""".formatted(BUCKET_CONFIG_BUCKET);
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.contentType("application/json")
.body(selfGrantingPolicy)
.when()
@@ -1785,7 +1783,7 @@ class S3AuthEnforcementIntegrationTest {
.body(containsString("AccessDenied"));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.get("/" + BUCKET_CONFIG_BUCKET + "?policy")
.then()
@@ -1794,13 +1792,182 @@ class S3AuthEnforcementIntegrationTest {
.body(not(containsString("s3:GetObject")));
given()
- .header("Authorization", LOCAL_AUTH_HEADER)
+ .filter(LOCAL_SIGNER)
.when()
.delete("/" + BUCKET_CONFIG_BUCKET + "?policy")
.then()
.statusCode(204);
}
+ @Test
+ @Order(51)
+ void forgedHeaderSignatureIsRejected() throws Exception {
+ String key = "forged-signature.txt";
+
+ given()
+ .filter(LOCAL_SIGNER.withSignature("deadbeef"))
+ .body("forged")
+ .when()
+ .put("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(403)
+ .body(containsString("SignatureDoesNotMatch"));
+
+ given()
+ .filter(S3RequestSigner.signedAs("test", "not-the-secret"))
+ .body("wrong secret")
+ .when()
+ .put("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(403)
+ .body(containsString("SignatureDoesNotMatch"));
+
+ given()
+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/" + SIGNING_DATE
+ + "/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature=deadbeef")
+ .header("x-amz-date", SIGNING_TIMESTAMP)
+ .header("x-amz-content-sha256", sha256Hex("forged"))
+ .body("forged")
+ .when()
+ .put("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(403)
+ .body(containsString("SignatureDoesNotMatch"));
+
+ given()
+ .filter(LOCAL_SIGNER)
+ .when()
+ .get("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(404);
+ }
+
+ @Test
+ @Order(52)
+ void headerSignatureMustBindHostAndPayload() throws Exception {
+ String key = "bound-signature.txt";
+
+ given()
+ .filter(LOCAL_SIGNER.withoutSignedHost())
+ .body("unbound host")
+ .when()
+ .put("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(403)
+ .body(containsString("SignatureDoesNotMatch"));
+
+ given()
+ .filter(LOCAL_SIGNER.withContentSha256(sha256Hex("the body that was signed")))
+ .body("a different body")
+ .when()
+ .put("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(400)
+ .body(containsString("XAmzContentSHA256Mismatch"));
+
+ given()
+ .filter(LOCAL_SIGNER)
+ .when()
+ .get("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(404);
+
+ given()
+ .filter(LOCAL_SIGNER.withContentSha256("UNSIGNED-PAYLOAD"))
+ .body("unsigned payload is allowed")
+ .when()
+ .put("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(200);
+
+ // The AWS CLI sends bucket configuration bodies and put-object --body without a
+ // Content-Type (RestAssured always adds one, hence the raw client); the declared hash
+ // must still be checked against the bytes that arrived.
+ byte[] suspend = "<VersioningConfiguration><Status>Suspended</Status></VersioningConfiguration>"
+ .getBytes(StandardCharsets.UTF_8);
+ assertThat(putWithoutContentType(WRITE_BUCKET + "?versioning", suspend, LOCAL_SIGNER).statusCode(),
+ equalTo(200));
+ HttpResponse<String> mismatch = putWithoutContentType(WRITE_BUCKET + "?versioning", suspend,
+ LOCAL_SIGNER.withContentSha256(sha256Hex("something else")));
+ assertThat(mismatch.statusCode(), equalTo(400));
+ assertThat(mismatch.body(), containsString("XAmzContentSHA256Mismatch"));
+
+ given()
+ .filter(LOCAL_SIGNER)
+ .when()
+ .get("/" + WRITE_BUCKET + "/" + key)
+ .then()
+ .statusCode(200)
+ .body(equalTo("unsigned payload is allowed"));
+ }
+
+ @Test
+ @Order(53)
+ void headerSignatureRequiresFreshDateAndContentHash() throws Exception {
+ String path = "/" + PRIVATE_BUCKET + "/" + PRIVATE_KEY;
+
+ given()
+ .filter(LOCAL_SIGNER.signedAt(Instant.now().minusSeconds(20 * 60)))
+ .when()
+ .get(path)
+ .then()
+ .statusCode(403)
+ .body(containsString("RequestTimeTooSkewed"));
+
+ given()
+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/" + SIGNING_DATE
+ + "/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature=abc")
+ .header("x-amz-date", SIGNING_TIMESTAMP)
+ .when()
+ .get(path)
+ .then()
+ .statusCode(400)
+ .body(containsString("InvalidRequest"))
+ .body(containsString("x-amz-content-sha256"));
+
+ given()
+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/" + SIGNING_DATE
+ + "/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-date, Signature=abc")
+ .header("x-amz-content-sha256", sha256Hex(""))
+ .when()
+ .get(path)
+ .then()
+ .statusCode(403)
+ .body(containsString("AccessDenied"))
+ .body(containsString("valid Date or x-amz-date"));
+
+ given()
+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/" + SIGNING_DATE
+ + "/us-east-1/s3/aws4_request")
+ .header("x-amz-date", SIGNING_TIMESTAMP)
+ .header("x-amz-content-sha256", sha256Hex(""))
+ .when()
+ .get(path)
+ .then()
+ .statusCode(400)
+ .body(containsString("AuthorizationHeaderMalformed"));
+
+ given()
+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=test/" + SIGNING_DATE
+ + "/us-east-1/sqs/aws4_request, SignedHeaders=host;x-amz-date, Signature=abc")
+ .header("x-amz-date", SIGNING_TIMESTAMP)
+ .header("x-amz-content-sha256", sha256Hex(""))
+ .when()
+ .get(path)
+ .then()
+ .statusCode(400)
+ .body(containsString("AuthorizationHeaderMalformed"));
+ }
+
+ private static HttpResponse<String> putWithoutContentType(String pathAndQuery, byte[] body,
+ S3RequestSigner signer) throws Exception {
+ URI uri = URI.create("http://localhost:" + io.restassured.RestAssured.port + "/" + pathAndQuery);
+ HttpRequest.Builder request = HttpRequest.newBuilder(uri)
+ .PUT(HttpRequest.BodyPublishers.ofByteArray(body));
+ signer.headersFor("PUT", uri, body).forEach(request::header);
+ return HttpClient.newHttpClient().send(request.build(), HttpResponse.BodyHandlers.ofString());
+ }
+
private static String presignedSignature(String method, String path,
String accessKeyId, String secretKey, String expires) {
return presignedSignature(method, path, accessKeyId, secretKey, expires, "UNSIGNED-PAYLOAD");
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java
index 4fe9acb70c..86337b4cb4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostAuthEnforcementIntegrationTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.s3;
+import io.github.hectorvent.floci.testutil.S3RequestSigner;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
@@ -49,7 +50,7 @@ class S3PresignedPostAuthEnforcementIntegrationTest {
@Order(1)
void createBucket() {
given()
- .header("Authorization", authorizationHeader(LEGACY_ACCESS_KEY_ID))
+ .filter(S3RequestSigner.signedAs(LEGACY_ACCESS_KEY_ID, LEGACY_SECRET_KEY))
.when()
.put("/" + BUCKET)
.then()
@@ -95,7 +96,7 @@ class S3PresignedPostAuthEnforcementIntegrationTest {
.body("Error.Code", org.hamcrest.Matchers.equalTo("SignatureDoesNotMatch"));
given()
- .header("Authorization", authorizationHeader(LEGACY_ACCESS_KEY_ID))
+ .filter(S3RequestSigner.signedAs(LEGACY_ACCESS_KEY_ID, LEGACY_SECRET_KEY))
.when()
.get("/" + BUCKET + "/" + key)
.then()
@@ -251,7 +252,7 @@ class S3PresignedPostAuthEnforcementIntegrationTest {
.header("ETag", notNullValue());
given()
- .header("Authorization", authorizationHeader(LEGACY_ACCESS_KEY_ID))
+ .filter(S3RequestSigner.signedAs(LEGACY_ACCESS_KEY_ID, LEGACY_SECRET_KEY))
.when()
.get("/" + BUCKET + "/" + key)
.then()
diff --git a/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSigner.java b/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSigner.java
new file mode 100644
index 0000000000..7ab8995d60
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/testutil/S3RequestSigner.java
@@ -0,0 +1,245 @@
+package io.github.hectorvent.floci.testutil;
+
+import io.restassured.filter.Filter;
+import io.restassured.filter.FilterContext;
+import io.restassured.response.Response;
+import io.restassured.specification.FilterableRequestSpecification;
+import io.restassured.specification.FilterableResponseSpecification;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.net.URI;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * A RestAssured filter that header-signs an S3 request with SigV4 the way an AWS SDK would, so
+ * tests can exercise {@code S3HeaderSignatureFilter} against real signatures over the request
+ * actually sent rather than against a fixture produced by the code under test.
+ *
+ * <p>Attach with {@code given().filter(S3RequestSigner.signedAs("test", "test"))} in place of a
+ * hand-written {@code Authorization} header. The signature covers the method, the raw path, the
+ * query string, {@code host}, {@code x-amz-content-sha256}, {@code x-amz-date} and, when a
+ * session token is given, {@code x-amz-security-token}; the content hash is the SHA-256 of the
+ * body the request carries. The {@code with*} methods return a copy that deliberately deviates
+ * (a stale timestamp, a forged signature, a wrong content hash) for the rejection cases.
+ */
+public final class S3RequestSigner implements Filter {
+
+ private static final String ALGORITHM = "AWS4-HMAC-SHA256";
+ private static final String SERVICE = "s3";
+ private static final DateTimeFormatter AMZ_DATE =
+ DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
+
+ private final String accessKeyId;
+ private final String secretKey;
+ private final String sessionToken;
+ private final String region;
+ private final Instant signedAt;
+ private final String signatureOverride;
+ private final String contentSha256Override;
+ private final boolean signHost;
+
+ private S3RequestSigner(String accessKeyId, String secretKey, String sessionToken, String region,
+ Instant signedAt, String signatureOverride, String contentSha256Override,
+ boolean signHost) {
+ this.accessKeyId = accessKeyId;
+ this.secretKey = secretKey;
+ this.sessionToken = sessionToken;
+ this.region = region;
+ this.signedAt = signedAt;
+ this.signatureOverride = signatureOverride;
+ this.contentSha256Override = contentSha256Override;
+ this.signHost = signHost;
+ }
+
+ public static S3RequestSigner signedAs(String accessKeyId, String secretKey) {
+ return new S3RequestSigner(accessKeyId, secretKey, null, "us-east-1", null, null, null, true);
+ }
+
+ public static S3RequestSigner signedAs(String accessKeyId, String secretKey, String sessionToken) {
+ return new S3RequestSigner(accessKeyId, secretKey, sessionToken, "us-east-1", null, null, null, true);
+ }
+
+ /** Signs as of a fixed instant instead of now; used to trip the clock-skew window. */
+ public S3RequestSigner signedAt(Instant instant) {
+ return new S3RequestSigner(accessKeyId, secretKey, sessionToken, region, instant,
+ signatureOverride, contentSha256Override, signHost);
+ }
+
+ /** Sends {@code signature} in place of the computed one, everything else intact. */
+ public S3RequestSigner withSignature(String signature) {
+ return new S3RequestSigner(accessKeyId, secretKey, sessionToken, region, signedAt,
+ signature, contentSha256Override, signHost);
+ }
+
+ /**
+ * Declares and signs {@code contentSha256} regardless of the body actually sent, so the
+ * signature verifies but the payload does not match its declared hash.
+ */
+ public S3RequestSigner withContentSha256(String contentSha256) {
+ return new S3RequestSigner(accessKeyId, secretKey, sessionToken, region, signedAt,
+ signatureOverride, contentSha256, signHost);
+ }
+
+ /** Leaves {@code host} out of {@code SignedHeaders}; no real signer does this. */
+ public S3RequestSigner withoutSignedHost() {
+ return new S3RequestSigner(accessKeyId, secretKey, sessionToken, region, signedAt,
+ signatureOverride, contentSha256Override, false);
+ }
+
+ @Override
+ public Response filter(FilterableRequestSpecification request,
+ FilterableResponseSpecification response, FilterContext ctx) {
+ try {
+ sign(request);
+ } catch (Exception e) {
+ throw new IllegalStateException("could not sign the request", e);
+ }
+ return ctx.next(request, response);
+ }
+
+ private void sign(FilterableRequestSpecification request) throws Exception {
+ headersFor(request.getMethod(), URI.create(request.getURI()), bodyBytes(request.getBody()))
+ .forEach(request::header);
+ }
+
+ /**
+ * The headers a signed request must carry ({@code Authorization}, {@code x-amz-date},
+ * {@code x-amz-content-sha256} and, with a session token, {@code X-Amz-Security-Token}), for
+ * tests that send with a client other than RestAssured.
+ */
+ public Map<String, String> headersFor(String method, URI uri, byte[] body) throws Exception {
+ Instant at = signedAt != null ? signedAt : Instant.now();
+ String amzDate = AMZ_DATE.format(at);
+ String scopeDate = amzDate.substring(0, 8);
+ String contentSha256 = contentSha256Override != null
+ ? contentSha256Override
+ : sha256Hex(body);
+ String host = uri.getPort() > 0 && uri.getPort() != 80 && uri.getPort() != 443
+ ? uri.getHost() + ":" + uri.getPort()
+ : uri.getHost();
+
+ List<String[]> headers = new ArrayList<>();
+ if (signHost) {
+ headers.add(new String[]{"host", host});
+ }
+ headers.add(new String[]{"x-amz-content-sha256", contentSha256});
+ headers.add(new String[]{"x-amz-date", amzDate});
+ if (sessionToken != null) {
+ headers.add(new String[]{"x-amz-security-token", sessionToken});
+ }
+ String signedHeaders = headers.stream().map(h -> h[0]).collect(Collectors.joining(";"));
+ String canonicalHeaders = headers.stream()
+ .map(h -> h[0] + ":" + h[1] + "\n")
+ .collect(Collectors.joining());
+
+ String rawPath = uri.getRawPath() == null || uri.getRawPath().isEmpty() ? "/" : uri.getRawPath();
+ String canonicalRequest = method + "\n"
+ + rawPath + "\n"
+ + canonicalQueryString(uri.getRawQuery()) + "\n"
+ + canonicalHeaders + "\n"
+ + signedHeaders + "\n"
+ + contentSha256;
+ String credentialScope = scopeDate + "/" + region + "/" + SERVICE + "/aws4_request";
+ String stringToSign = ALGORITHM + "\n"
+ + amzDate + "\n"
+ + credentialScope + "\n"
+ + sha256Hex(canonicalRequest.getBytes(StandardCharsets.UTF_8));
+ String signature = signatureOverride != null
+ ? signatureOverride
+ : hexEncode(hmacSha256(signingKey(scopeDate), stringToSign));
+
+ Map<String, String> result = new LinkedHashMap<>();
+ result.put("x-amz-date", amzDate);
+ result.put("x-amz-content-sha256", contentSha256);
+ if (sessionToken != null) {
+ result.put("X-Amz-Security-Token", sessionToken);
+ }
+ result.put("Authorization", ALGORITHM
+ + " Credential=" + accessKeyId + "/" + credentialScope
+ + ", SignedHeaders=" + signedHeaders
+ + ", Signature=" + signature);
+ return result;
+ }
+
+ private byte[] signingKey(String scopeDate) throws Exception {
+ byte[] kDate = hmacSha256(("AWS4" + secretKey).getBytes(StandardCharsets.UTF_8), scopeDate);
+ byte[] kRegion = hmacSha256(kDate, region);
+ byte[] kService = hmacSha256(kRegion, SERVICE);
+ return hmacSha256(kService, "aws4_request");
+ }
+
+ private static byte[] bodyBytes(Object body) {
+ if (body == null) {
+ return new byte[0];
+ }
+ if (body instanceof byte[] bytes) {
+ return bytes;
+ }
+ if (body instanceof String text) {
+ return text.getBytes(StandardCharsets.UTF_8);
+ }
+ throw new IllegalArgumentException("unsupported body type " + body.getClass().getName());
+ }
+
+ static String canonicalQueryString(String rawQuery) {
+ if (rawQuery == null || rawQuery.isEmpty()) {
+ return "";
+ }
+ List<String[]> pairs = new ArrayList<>();
+ for (String pair : rawQuery.split("&")) {
+ if (pair.isEmpty()) {
+ continue;
+ }
+ int equals = pair.indexOf('=');
+ String name = URLDecoder.decode(equals >= 0 ? pair.substring(0, equals) : pair, StandardCharsets.UTF_8);
+ String value = URLDecoder.decode(equals >= 0 ? pair.substring(equals + 1) : "", StandardCharsets.UTF_8);
+ pairs.add(new String[]{uriEncode(name), uriEncode(value)});
+ }
+ pairs.sort(Comparator.comparing((String[] p) -> p[0]).thenComparing(p -> p[1]));
+ return pairs.stream().map(p -> p[0] + "=" + p[1]).collect(Collectors.joining("&"));
+ }
+
+ static String uriEncode(String value) {
+ StringBuilder encoded = new StringBuilder();
+ for (byte b : value.getBytes(StandardCharsets.UTF_8)) {
+ int c = b & 0xFF;
+ if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
+ || c == '-' || c == '_' || c == '.' || c == '~') {
+ encoded.append((char) c);
+ } else {
+ encoded.append('%').append(String.format("%02X", c));
+ }
+ }
+ return encoded.toString();
+ }
+
+ private static byte[] hmacSha256(byte[] key, String data) throws Exception {
+ Mac mac = Mac.getInstance("HmacSHA256");
+ mac.init(new SecretKeySpec(key, "HmacSHA256"));
+ return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
+ }
+
+ private static String sha256Hex(byte[] input) throws Exception {
+ return hexEncode(MessageDigest.getInstance("SHA-256").digest(input));
+ }
+
+ private static String hexEncode(byte[] bytes) {
+ StringBuilder hex = new StringBuilder(bytes.length * 2);
+ for (byte b : bytes) {
+ hex.append(String.format("%02x", b));
+ }
+ return hex.toString();
+ }
+}