mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): verify the SigV4 header signature when enforce-auth is enabled (#3573)
With FLOCI_SERVICES_S3_ENFORCE_AUTH=true, a request signed in the Authorization header, which is how every AWS SDK and the CLI sign ordinary calls, was authenticated by access key existence alone: isKnownAccessKey checked that the key id was registered and the Signature value was never recomputed. The presigned placements already had real verifiers (#1842 for the query string, #3081 for browser POST); this was the remaining one, so a fabricated signature naming a known key was accepted for any request. Add S3HeaderSignatureFilter beside PreSignedUrlFilter. It runs only with the flag on, for requests routed to S3Controller, and rebuilds the canonical request from the request as it arrived: raw path, canonical query string, the SignedHeaders with host resolved through S3VirtualHostFilter, and the signed x-amz-content-sha256. The signature is derived from the resolved secret and compared in constant time; when the declared hash is a real digest the body is hashed too and handed back unchanged. Rejections mirror S3's codes and order: AuthorizationHeaderMalformed for a structurally bad header or scope, AccessDenied for a missing date, InvalidRequest for a missing content hash, InvalidAccessKeyId for an unknown key or a temporary key without its session token, RequestTimeTooSkewed beyond fifteen minutes, SignatureDoesNotMatch for an unsigned host or a signature that does not verify, and XAmzContentSHA256Mismatch for a body that differs from its declared hash. PreSignedUrlFilter's crypto helpers go package-private so the new filter shares them. The entity stream is read directly rather than gated on hasEntity(), which RESTEasy Reactive implements as "has a Content-Type"; the AWS CLI sends bucket configuration bodies and put-object --body without one, and the first cut rejected those as hash mismatches. The enforce-auth tests all sent a fabricated Signature=test, which is the bug running green. A new testutil S3RequestSigner signs each request at send time the way an SDK does; the 103 fabricated headers become filter attachments with no assertion changed, and three new tests pin the forged, wrong-secret, unbound-host, tampered-body, stale-date, missing-hash, missing-date and malformed-header cases. Verified live with the flag on against the unmodified AWS CLI 2.36 (including an 8 MB multipart round trip) and the AWS Java SDK v2 compat suite, 143/143 across every S3 class. Closes #2332
This commit is contained in:
1 parent
126af73141
commit
bfe2b71587
5 files changed
+827
-123
No files matched your search
+281
-114
@@ -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");
|
||||
|
||||
+4
-3
@@ -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()
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user