Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): verify header and presigned POST signatures under validate-signatures (#4786)
Abhigya8f904afa5a85
diff --git a/docs/configuration/advanced/application-yml.md b/docs/configuration/advanced/application-yml.mdindex b1c786c755..dceddea543 100644--- a/docs/configuration/advanced/application-yml.md+++ b/docs/configuration/advanced/application-yml.md@@ -104,7 +104,7 @@ floci:spoof-aws-endpoints: falseauth:- validate-signatures: false # Set to true to verify S3 presigned URL signatures+ validate-signatures: false # Set to true to verify S3 SigV4 signatures (header, presigned URL, presigned POST)presign-secret: local-emulator-secret # HMAC secret for S3 pre-signed URL verificationtls:diff --git a/docs/configuration/docker-compose.md b/docs/configuration/docker-compose.mdindex 0192bcd89f..a9e19da401 100644--- a/docs/configuration/docker-compose.md+++ b/docs/configuration/docker-compose.md@@ -173,7 +173,7 @@ The most frequently set variables when running Floci as a Docker image:| `FLOCI_STORAGE_MODE` | `memory` | `memory`, `persistent`, `hybrid`, or `wal` || `FLOCI_STORAGE_PERSISTENT_PATH` | `./data` | Directory for persistent storage || `FLOCI_SERVICES_DOCKER_NETWORK` | _(none)_ | Docker network for spawned containers (Lambda, ElastiCache, RDS, OpenSearch, MSK) |-| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify S3 presigned URL signatures |+| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify S3 SigV4 signatures without evaluating bucket policies || `FLOCI_SERVICES_LAMBDA_EPHEMERAL` | `false` | Remove Lambda containers after each invocation |For the complete list of every `FLOCI_*` variable, see [Environment Variables Reference](./environment-variables.md).diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.mdindex 01ca1d3d50..f126cb25cf 100644--- a/docs/configuration/environment-variables.md+++ b/docs/configuration/environment-variables.md@@ -23,7 +23,7 @@ Floci is configured exclusively through environment variables. Every option belo| Variable | Default | Description ||---|---|---|-| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | When `true`, verifies S3 presigned URL signatures |+| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | When `true`, verifies S3 SigV4 signatures (`Authorization` header, presigned URL and presigned POST) without evaluating bucket policies || `FLOCI_AUTH_PRESIGN_SECRET` | `local-emulator-secret` | Secret used to sign and verify pre-signed URLs |## Network Exposurediff --git a/docs/configuration/multi-account.md b/docs/configuration/multi-account.mdindex 49ceccb736..a8ecede986 100644--- a/docs/configuration/multi-account.md+++ b/docs/configuration/multi-account.md@@ -167,16 +167,18 @@ bucket in one account and reads it from another (see [S3](../services/s3.md#glob## Signature Validation-Floci does not currently perform general SigV4 validation for `Authorization` headers. Only the access key ID matters for account resolution, so the secret access key can be any non-empty string.+Outside S3, Floci does not perform general SigV4 validation for `Authorization` headers. Only the access key ID matters for account resolution, so the secret access key can be any non-empty string.-`FLOCI_AUTH_VALIDATE_SIGNATURES` currently applies only to S3 presigned URL validation. It does not authenticate general service requests or protect IAM and STS account routing. To validate S3 presigned URLs:+`FLOCI_AUTH_VALIDATE_SIGNATURES` applies only to S3. It verifies the SigV4 signature of every signed S3 request (`Authorization` header, presigned URL and presigned POST) without evaluating bucket policies, and leaves unsigned requests alone (see [S3 Signature Verification](../services/s3.md#signature-verification)). It does not authenticate other services or protect IAM and STS account routing. To verify S3 signatures:```bashFLOCI_AUTH_VALIDATE_SIGNATURES=trueFLOCI_AUTH_PRESIGN_SECRET=your-secret # for pre-signed URL verification```-When `validate-signatures` is `false` (the default), S3 presigned URL signatures are not verified. Account routing remains AKID-based regardless of this setting.+With `FLOCI_AUTH_VALIDATE_SIGNATURES` or `FLOCI_SERVICES_S3_ENFORCE_AUTH` enabled, header-signed S3 requests using a synthetic 12-digit account key fail with `403 InvalidAccessKeyId`, while presigned POSTs using that key fail with `403 SignatureDoesNotMatch`. Sign S3 requests with `test`/`test` or credentials created through Floci IAM or STS (including the session token for STS credentials). For multi-account S3 access, use IAM or STS credentials for the target account; `test`/`test` routes to `FLOCI_DEFAULT_ACCOUNT_ID` (see [S3 Signature Verification](../services/s3.md#signature-verification)).++When `validate-signatures` is `false` (the default), S3 signatures are verified only under `FLOCI_SERVICES_S3_ENFORCE_AUTH`. Account routing remains AKID-based regardless of this setting.## Persistence and Account Isolation@@ -188,4 +190,4 @@ Storage keys are namespaced per account at the persistence layer. When using `pe|---|---|---|| `FLOCI_DEFAULT_ACCOUNT_ID` | `000000000000` | Account ID used when the AKID does not resolve directly or through a stored credential || `FLOCI_DEFAULT_REGION` | `us-east-1` | Region used when not derivable from the `Authorization` header |-| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify S3 presigned URL signatures |+| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify S3 SigV4 signatures without evaluating bucket policies |diff --git a/docs/services/s3.md b/docs/services/s3.mdindex 032c386912..e344b1a8b0 100644--- a/docs/services/s3.md+++ b/docs/services/s3.md@@ -227,11 +227,36 @@ CDK asset bucket) is created in one account and read from another.**Why the flag exists, and what it trades away.** Floci partitions every resource by the caller's account, which is the right model for services whose names are account-scoped in AWS. S3 bucket names are not: they are unique across all accounts, a bucket lives in exactly one owning account, and whether another account may touch it is decided by policy rather than by the name being unreachable. A hard per-account partition therefore lets two accounts hold different buckets under one name — a state AWS cannot reach — and makes a legitimate cross-account call fail with `NoSuchBucket` instead of being authorized or denied on its merits. With the flag on, a caller that names another account's bucket resolves it and only IAM and bucket-policy evaluation stand between the caller and the object: AWS-faithful, but stricter than the implicit isolation Floci gives you elsewhere — **provided both `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED` (identity-policy checks, applied before the request reaches S3) and `FLOCI_SERVICES_S3_ENFORCE_AUTH` (the S3-specific flag that gates bucket-policy evaluation itself, in `S3Service.authorizeS3Read`/`authorizeS3Write`) are also `true`**. Both default to `false`, and bucket-policy evaluation in particular is a no-op while `FLOCI_SERVICES_S3_ENFORCE_AUTH` is off — regardless of the IAM flag — so in the default configuration enabling the global namespace does not trade per-account isolation for a policy-gated boundary — it removes the only isolation Floci gives S3 for that bucket and replaces it with nothing. Mutations are written back to the bucket's owning account rather than forked into the caller's, and `ListBuckets`/`ListObjects` stay owner-scoped, so the flag never reassigns ownership or leaks a bucket inventory. It is off by default — turn it on when emulating a multi-account estate whose real behaviour depends on the global namespace, and leave it off when the per-account partition is the isolation you are relying on. Account-scoped S3 state, notably the account-level Block Public Access configuration, is never widened by this flag. Note also that retrofitting a global namespace onto state that was previously partitioned per account means that if two accounts already hold a bucket under the same name — a state real AWS can't reach, but Floci's prior per-account isolation could — resolution picks whichever account's copy the backend happens to iterate to first; this is unlikely to be reachable on a fresh LZA-driven account structure, but is worth knowing if you enable the flag on an estate with pre-existing same-name buckets.+## Signature Verification++AWS treats authentication and authorization as two steps. S3 first verifies the SigV4 signature of a signed request and refuses one that does not verify, whatever the bucket policy says. Only then does it decide, from IAM policies, the bucket policy and ACLs, whether the caller may perform the operation. An unsigned request is anonymous, and is allowed only when a policy or ACL grants access to everyone.++Floci verifies signatures when either of two flags is on:++| | `FLOCI_AUTH_VALIDATE_SIGNATURES` | `FLOCI_SERVICES_S3_ENFORCE_AUTH` |+|---|---|---|+| `Authorization` header signatures | Verified | Verified |+| Presigned URL signatures (`X-Amz-Signature`) | Verified | Verified |+| Presigned POST signatures (browser form) | Verified when the form carries signature fields | Verified, and required |+| Bucket policy and ACL evaluation | Not evaluated | Evaluated |+| Unsigned (anonymous) requests | Allowed | Allowed only when a policy or ACL grants public access |++For a signed request, verification rebuilds the canonical request from the wire path, the query string, the headers named in `SignedHeaders` and the declared payload hash, derives the signing key from the access key's secret, and compares the signatures in constant time. It rejects:++- a signature that does not verify, or one computed for another key or method, with `403 SignatureDoesNotMatch`+- an access key that is neither `test` nor registered in Floci IAM (`CreateAccessKey` keys, and STS keys with their session token), with `403 InvalidAccessKeyId`. Under `FLOCI_AUTH_VALIDATE_SIGNATURES` alone, a presigned URL from such a key falls back to the older `FLOCI_AUTH_PRESIGN_SECRET` check instead, which an SDK-made URL fails with `403 SignatureDoesNotMatch`+- a header-signed request more than 15 minutes from server time, with `403 RequestTimeTooSkewed`+- a body that does not match a declared `x-amz-content-sha256` digest, with `400 XAmzContentSHA256Mismatch`++`FLOCI_AUTH_VALIDATE_SIGNATURES` is authentication alone: use it when forged or stale credentials must fail but buckets should stay reachable without policies. `FLOCI_SERVICES_S3_ENFORCE_AUTH` adds authorization, so it also decides anonymous access the way AWS does: a bucket that anonymous clients read needs a public-read bucket policy or ACL.++A workload must sign with credentials Floci can verify once either flag is on. A container Floci launches without a role, such as a Lambda function with no execution role, is given placeholder credentials that no flag accepts, so give it a role, or sign with `test`/`test`.+## Bucket Policy EnforcementFloci evaluates S3 bucket policies for authenticated (signed) requests when policy enforcement is enabled. Two flags control this evaluation:-- `FLOCI_SERVICES_S3_ENFORCE_AUTH`: gates bucket policy authorization directly within `S3Service` for bucket read, object write, and bucket write operations.+- `FLOCI_SERVICES_S3_ENFORCE_AUTH`: gates bucket policy authorization directly within `S3Service` for bucket read, object write, and bucket write operations. It also turns on [signature verification](#signature-verification).- `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED`: activates the global IAM request filter to evaluate identity-based policies and resource policies before requests reach the service layer.Both flags default to `false` for backward compatibility.@@ -332,7 +357,8 @@ These AWS S3 features have no handler in Floci. Calls will return an error (typi| `FLOCI_SERVICES_S3_ENABLED` | `true` | Enable or disable the service || `FLOCI_SERVICES_S3_DEFAULT_PRESIGN_EXPIRY_SECONDS` | `3600` | Default pre-signed URL expiry (1 hour) || `FLOCI_AUTH_PRESIGN_SECRET` | `local-emulator-secret` | Secret used to sign pre-signed URLs |-| `FLOCI_SERVICES_S3_ENFORCE_AUTH` | `false` | Enable bucket policy evaluation for authenticated callers |+| `FLOCI_SERVICES_S3_ENFORCE_AUTH` | `false` | Verify SigV4 signatures and evaluate bucket policies and ACLs, including for anonymous callers (see [Signature Verification](#signature-verification)) |+| `FLOCI_AUTH_VALIDATE_SIGNATURES` | `false` | Verify SigV4 signatures without evaluating bucket policies or ACLs (see [Signature Verification](#signature-verification)) || `FLOCI_SERVICES_S3_GLOBAL_BUCKET_NAMESPACE` | `false` | Enable cross-account bucket and object resolution |### Storagediff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.javaindex 00717d838f..5955450e20 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java@@ -71,6 +71,7 @@ import java.util.Map;import java.util.Optional;import java.util.Set;import java.util.function.Function;+import java.util.stream.Stream;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;@@ -3502,7 +3503,7 @@ public class S3Controller {S3PublicAccessEvaluator.objectArn(s3Service.bucketPartition(bucket), bucket, key));}- if (s3Service.isAuthEnforced()) {+ if (s3Service.isAuthEnforced() || (config.auth().validateSignatures() && isSignedPost(lcFields))) {validatePresignedPostAuth(lcFields, bucket, key, fileData.length);} else {// Validate policy conditions if present@@ -3571,6 +3572,17 @@ public class S3Controller {}}+ /**+ * Whether a browser POST carries any SigV4 form field. Under {@code floci.auth.validate-signatures}+ * such a POST must verify, while one with none of them is anonymous and is left to+ * {@code enforce-auth}, which decides anonymous access from the bucket policy and ACL.+ */+ private static boolean isSignedPost(Map<String, String> fields) {+ return Stream.of("x-amz-algorithm", "x-amz-credential", "x-amz-signature")+ .map(fields::get)+ .anyMatch(value -> value != null && !value.isEmpty());+ }+/*** Enforces real S3 presigned-POST auth: a {@code policy} field must be present and* SigV4-signed by a known secret key referenced through {@code x-amz-credential}, matchingdiff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.javaindex 04b87915ed..ec7479aad3 100644--- a/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java+++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3HeaderSignatureFilter.java@@ -28,7 +28,7 @@ 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.+ * {@code floci.services.s3.enforce-auth} or {@code floci.auth.validate-signatures} 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@@ -59,7 +59,12 @@ import java.util.Optional;* {@link PreSignedUrlFilter} and {@link S3PostPolicySigner}.</li>* </ul>*- * <p>Nothing here runs with the flag off: the filter returns before reading a single header, so+ * <p>Signature verification is authentication only. Whether the verified caller, or an anonymous+ * one, may perform the operation is authorization, which runs in {@link S3Service} under+ * {@code enforce-auth} alone. {@code validate-signatures} therefore rejects a forged or unknown+ * credential without evaluating bucket policies or ACLs, and lets unsigned requests through.+ *+ * <p>Nothing here runs with both flags off: the filter returns before reading a single header, so* the default configuration keeps accepting any well-formed {@code Authorization} header exactly* as before.*/@@ -77,6 +82,7 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);private final S3Service s3Service;+ private final PreSignedUrlGenerator presignGenerator;private final IamService iamService;private final CurrentVertxRequest currentVertxRequest;@@ -84,16 +90,17 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {ResourceInfo resourceInfo;@Inject- public S3HeaderSignatureFilter(S3Service s3Service, IamService iamService,- CurrentVertxRequest currentVertxRequest) {+ public S3HeaderSignatureFilter(S3Service s3Service, PreSignedUrlGenerator presignGenerator,+ IamService iamService, CurrentVertxRequest currentVertxRequest) {this.s3Service = s3Service;+ this.presignGenerator = presignGenerator;this.iamService = iamService;this.currentVertxRequest = currentVertxRequest;}@Overridepublic void filter(ContainerRequestContext ctx) throws IOException {- if (!s3Service.isAuthEnforced() || !routedToS3()) {+ if (!verifiesSignatures() || !routedToS3()) {return;}String authorization = ctx.getHeaderString("Authorization");@@ -181,6 +188,10 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {}}+ private boolean verifiesSignatures() {+ return s3Service.isAuthEnforced() || presignGenerator.shouldValidateSignatures();+ }+private boolean routedToS3() {return resourceInfo != null && S3Controller.class.equals(resourceInfo.getResourceClass());}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/PreSignedUrlIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/PreSignedUrlIntegrationTest.javaindex 7543ef9f7d..87fce171ab 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/PreSignedUrlIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/PreSignedUrlIntegrationTest.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;@@ -34,8 +35,7 @@ class PreSignedUrlIntegrationTest {private static final String BUCKET = "presign-test-bucket";- private static final String AUTH_HEADER =- "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/s3/aws4_request";+ private static final S3RequestSigner LOCAL_SIGNER = S3RequestSigner.signedAs("test", "test");@InjectPreSignedUrlGenerator presignGenerator;@@ -220,7 +220,7 @@ class PreSignedUrlIntegrationTest {// Must be a signed request per AWS spec (response-* params require Authorization or presigned URL).given().urlEncodingEnabled(false)- .header("Authorization", AUTH_HEADER)+ .filter(LOCAL_SIGNER).when().get("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22file.txt%22").then()@@ -233,7 +233,7 @@ class PreSignedUrlIntegrationTest {void getObjectAppliesAllResponseOverrides() {given().urlEncodingEnabled(false)- .header("Authorization", AUTH_HEADER)+ .filter(LOCAL_SIGNER).when().get("/" + BUCKET + "/disposition-file.txt"+ "?response-content-type=application%2Fpdf"@@ -269,7 +269,7 @@ class PreSignedUrlIntegrationTest {void headObjectAppliesResponseContentDispositionOverride() {given().urlEncodingEnabled(false)- .header("Authorization", AUTH_HEADER)+ .filter(LOCAL_SIGNER).when().head("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22head.txt%22").then()@@ -295,7 +295,7 @@ class PreSignedUrlIntegrationTest {void rangeRequestAppliesResponseContentDispositionOverride() {given().urlEncodingEnabled(false)- .header("Authorization", AUTH_HEADER)+ .filter(LOCAL_SIGNER).header("Range", "bytes=0-3").when().get("/" + BUCKET + "/disposition-file.txt?response-content-disposition=attachment%3B%20filename%3D%22range.txt%22")diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ValidateSignaturesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ValidateSignaturesIntegrationTest.javanew file mode 100644index 0000000000..489d4cdd1a--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ValidateSignaturesIntegrationTest.java@@ -0,0 +1,253 @@+package io.github.hectorvent.floci.services.s3;++import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;+import io.github.hectorvent.floci.testutil.S3RequestSigner;+import io.quarkus.test.junit.QuarkusTest;+import io.quarkus.test.junit.TestProfile;+import io.restassured.path.xml.XmlPath;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import java.nio.charset.StandardCharsets;+import java.time.Instant;+import java.time.ZoneOffset;+import java.time.format.DateTimeFormatter;+import java.util.Base64;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;++/**+ * {@code floci.auth.validate-signatures} authenticates every signed S3 request, whichever+ * placement carries the signature, without authorizing it: a forged or unknown credential is+ * refused, while bucket policies are not evaluated and unsigned requests pass, as they do with+ * {@code enforce-auth} off. Shares {@link PreSignedUrlIntegrationTest}'s profile, which covers the+ * presigned query-string placement.+ */+@QuarkusTest+@TestProfile(PreSignedUrlIntegrationTest.PresignValidationProfile.class)+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class S3ValidateSignaturesIntegrationTest {++ private static final String BUCKET = "validate-signatures-bucket";+ private static final String KEY = "object.txt";+ private static final String CREDENTIAL_DATE = "20260101";+ private static final String AMZ_DATE = CREDENTIAL_DATE + "T000000Z";+ private static final S3RequestSigner LOCAL_SIGNER = S3RequestSigner.signedAs("test", "test");+ private static final String DENY_ALL_POLICY = """+ {"Version":"2012-10-17","Statement":[{"Effect":"Deny","Principal":"*",+ "Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*"}]}+ """.formatted(BUCKET);++ private static String userAccessKeyId;+ private static String userSecretKey;++ @Test+ @Order(1)+ void headerSignedRequestsWithAKnownKeyAreAccepted() {+ given().filter(LOCAL_SIGNER).when().put("/" + BUCKET).then().statusCode(200);+ given()+ .filter(LOCAL_SIGNER)+ .body("signed body")+ .when()+ .put("/" + BUCKET + "/" + KEY)+ .then()+ .statusCode(200);+ given()+ .filter(LOCAL_SIGNER)+ .when()+ .get("/" + BUCKET + "/" + KEY)+ .then()+ .statusCode(200)+ .body(equalTo("signed body"));+ }++ @Test+ @Order(2)+ void headerSignatureThatDoesNotVerifyIsRejected() {+ given()+ .filter(LOCAL_SIGNER.withSignature("0".repeat(64)))+ .when()+ .get("/" + BUCKET + "/" + KEY)+ .then()+ .statusCode(403)+ .body("Error.Code", equalTo("SignatureDoesNotMatch"));+ }++ @Test+ @Order(3)+ void headerSignedRequestWithAnUnknownKeyIsRejected() {+ given()+ .filter(S3RequestSigner.signedAs("AKIAUNKNOWNACCESSKEY", "some-secret"))+ .when()+ .get("/" + BUCKET + "/" + KEY)+ .then()+ .statusCode(403)+ .body("Error.Code", equalTo("InvalidAccessKeyId"));+ }++ @Test+ @Order(4)+ void headerSignedBodyThatDoesNotMatchItsHashIsRejected() {+ given()+ .filter(LOCAL_SIGNER.withContentSha256("0".repeat(64)))+ .body("tampered body")+ .when()+ .put("/" + BUCKET + "/tampered.txt")+ .then()+ .statusCode(400)+ .body("Error.Code", equalTo("XAmzContentSHA256Mismatch"));+ }++ @Test+ @Order(5)+ void unsignedRequestsAreLeftToEnforceAuth() {+ given().body("anonymous body").when().put("/" + BUCKET + "/anonymous.txt").then().statusCode(200);+ given().when().get("/" + BUCKET + "/anonymous.txt").then().statusCode(200).body(equalTo("anonymous body"));+ }++ @Test+ @Order(10)+ void verifiedCallerIsNotAuthorizedAgainstTheBucketPolicy() {+ createIamUser("validate-signatures-user");+ given()+ .filter(LOCAL_SIGNER)+ .body(DENY_ALL_POLICY)+ .when()+ .put("/" + BUCKET + "?policy")+ .then()+ .statusCode(200);+ given()+ .filter(LOCAL_SIGNER)+ .when()+ .get("/" + BUCKET + "?policy")+ .then()+ .statusCode(200)+ .body(containsString("\"Deny\""));++ given()+ .filter(S3RequestSigner.signedAs(userAccessKeyId, userSecretKey))+ .when()+ .get("/" + BUCKET + "/" + KEY)+ .then()+ .statusCode(200)+ .body(equalTo("signed body"));+ }++ @Test+ @Order(20)+ void presignedPostWithAGenuineSignatureIsAccepted() {+ String key = "post-genuine.txt";+ String policy = postPolicy(key);+ String credential = "test/" + CREDENTIAL_DATE + "/us-east-1/s3/aws4_request";++ given()+ .multiPart("key", key)+ .multiPart("policy", policy)+ .multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")+ .multiPart("x-amz-credential", credential)+ .multiPart("x-amz-date", AMZ_DATE)+ .multiPart("x-amz-signature", signPolicy(policy, "test"))+ .multiPart("file", key, "posted".getBytes(StandardCharsets.UTF_8), "text/plain")+ .when()+ .post("/" + BUCKET)+ .then()+ .statusCode(204);+ }++ @Test+ @Order(21)+ void presignedPostWithAForgedSignatureIsRejected() {+ String key = "post-forged.txt";+ String policy = postPolicy(key);++ given()+ .multiPart("key", key)+ .multiPart("policy", policy)+ .multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")+ .multiPart("x-amz-credential", "test/" + CREDENTIAL_DATE + "/us-east-1/s3/aws4_request")+ .multiPart("x-amz-date", AMZ_DATE)+ .multiPart("x-amz-signature", "0".repeat(64))+ .multiPart("file", key, "forged".getBytes(StandardCharsets.UTF_8), "text/plain")+ .when()+ .post("/" + BUCKET)+ .then()+ .statusCode(403)+ .body("Error.Code", equalTo("SignatureDoesNotMatch"));++ given().filter(LOCAL_SIGNER).when().get("/" + BUCKET + "/" + key).then().statusCode(404);+ }++ @Test+ @Order(22)+ void presignedPostWithASignatureButNoPolicyIsRejected() {+ given()+ .multiPart("key", "post-partial.txt")+ .multiPart("x-amz-signature", "0".repeat(64))+ .multiPart("file", "post-partial.txt", "partial".getBytes(StandardCharsets.UTF_8), "text/plain")+ .when()+ .post("/" + BUCKET)+ .then()+ .statusCode(403)+ .body("Error.Code", equalTo("AccessDenied"));+ }++ @Test+ @Order(23)+ void unsignedPostIsLeftToEnforceAuth() {+ given()+ .multiPart("key", "post-anonymous.txt")+ .multiPart("file", "post-anonymous.txt", "anonymous".getBytes(StandardCharsets.UTF_8), "text/plain")+ .when()+ .post("/" + BUCKET)+ .then()+ .statusCode(204);+ }++ private static void createIamUser(String userName) {+ String authorization = "AWS4-HMAC-SHA256 Credential=test/" + CREDENTIAL_DATE+ + "/us-east-1/iam/aws4_request, SignedHeaders=host, Signature=unused";+ given()+ .formParam("Action", "CreateUser")+ .formParam("UserName", userName)+ .header("Authorization", authorization)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ XmlPath key = given()+ .formParam("Action", "CreateAccessKey")+ .formParam("UserName", userName)+ .header("Authorization", authorization)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract()+ .xmlPath();+ userAccessKeyId = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");+ userSecretKey = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey");+ }++ private static String postPolicy(String key) {+ String expiration = DateTimeFormatter.ISO_INSTANT.format(+ Instant.now().plusSeconds(3600).atZone(ZoneOffset.UTC));+ String policy = """+ {"expiration": "%s", "conditions": [{"bucket": "%s"}, {"key": "%s"}]}+ """.formatted(expiration, BUCKET, key);+ return Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8));+ }++ private static String signPolicy(String policy, String secretKey) {+ try {+ byte[] signingKey = SigV4RequestValidator.deriveSigningKey(secretKey, CREDENTIAL_DATE, "us-east-1", "s3");+ return SigV4RequestValidator.hexEncode(SigV4RequestValidator.hmacSha256(signingKey, policy));+ } catch (Exception e) {+ throw new IllegalStateException("could not sign the POST policy", e);+ }+ }+}