diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 056ab0d8d1..c148c2163c 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 @@ -259,34 +259,45 @@ public class S3Controller { byte[] body) { try { validateRawUri(); + S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired( + s3Service.isAuthEnforced(), httpHeaders, uriInfo); if (hasQueryParam(uriInfo, "notification")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketNotification", authorization); return handlePutBucketNotification(bucket, body); } if (hasQueryParam(uriInfo, "versioning")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketVersioning", authorization); return handlePutBucketVersioning(bucket, body); } if (hasQueryParam(uriInfo, "tagging")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketTagging", authorization); return handlePutBucketTagging(bucket, body); } if (hasQueryParam(uriInfo, "object-lock")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketObjectLockConfiguration", authorization); return handlePutObjectLockConfiguration(bucket, body); } if (hasQueryParam(uriInfo, "website")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketWebsite", authorization); return handlePutBucketWebsite(bucket, body); } if (hasQueryParam(uriInfo, "logging")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketLogging", authorization); s3Service.putBucketLogging(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } if (hasQueryParam(uriInfo, "policy")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketPolicy", authorization); s3Service.putBucketPolicy(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } if (hasQueryParam(uriInfo, "cors")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketCORS", authorization); s3Service.putBucketCors(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } if (hasQueryParam(uriInfo, "lifecycle")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutLifecycleConfiguration", authorization); String requestedSize = httpHeaders.getHeaderString("x-amz-transition-default-minimum-object-size"); String storedSize = s3Service.putBucketLifecycle(bucket, new String(body, StandardCharsets.UTF_8), requestedSize); @@ -295,6 +306,7 @@ public class S3Controller { .build(); } if (hasQueryParam(uriInfo, "acl")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketAcl", authorization); s3Service.putBucketAcl(bucket, new String(body, StandardCharsets.UTF_8), httpHeaders.getHeaderString("x-amz-acl"), httpHeaders.getHeaderString("x-amz-grant-read"), @@ -305,57 +317,59 @@ public class S3Controller { return Response.ok().build(); } if (hasQueryParam(uriInfo, "encryption")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutEncryptionConfiguration", authorization); s3Service.putBucketEncryption(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } if (hasQueryParam(uriInfo, "publicAccessBlock")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketPublicAccessBlock", authorization); s3Service.putPublicAccessBlock(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } if (hasQueryParam(uriInfo, "ownershipControls")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketOwnershipControls", authorization); s3Service.putBucketOwnershipControls(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } if (hasQueryParam(uriInfo, "requestPayment")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketRequestPayment", authorization); s3Service.putBucketRequestPayment(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } if (hasQueryParam(uriInfo, "accelerate")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutAccelerateConfiguration", authorization); s3Service.putBucketAccelerateConfiguration(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } if (hasQueryParam(uriInfo, "replication")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutReplicationConfiguration", authorization); s3Service.putBucketReplication(bucket, new String(body, StandardCharsets.UTF_8)); return Response.ok().build(); } // Must be handled here: an unmatched subresource falls through to CreateBucket below, // which answers a metrics call with BucketAlreadyOwnedByYou. if (hasQueryParam(uriInfo, "metrics")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutMetricsConfiguration", authorization); return handlePutBucketMetricsConfiguration(bucket, uriInfo, body); } // Same fall-through hazard as metrics: an intelligent-tiering PUT must not become a // CreateBucket. if (hasQueryParam(uriInfo, "intelligent-tiering")) { - S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired( - s3Service.isAuthEnforced(), httpHeaders, uriInfo); s3Service.authorizeBucketWrite(bucket, "s3:PutIntelligentTieringConfiguration", authorization); return handlePutBucketIntelligentTieringConfiguration(bucket, uriInfo, body); } // Same fall-through hazard as metrics: an analytics or inventory PUT must not become // a CreateBucket. if (hasQueryParam(uriInfo, "analytics")) { - S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired( - s3Service.isAuthEnforced(), httpHeaders, uriInfo); s3Service.authorizeBucketWrite(bucket, "s3:PutAnalyticsConfiguration", authorization); return handlePutBucketAnalyticsConfiguration(bucket, uriInfo, body); } if (hasQueryParam(uriInfo, "inventory")) { - S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired( - s3Service.isAuthEnforced(), httpHeaders, uriInfo); s3Service.authorizeBucketWrite(bucket, "s3:PutInventoryConfiguration", authorization); return handlePutBucketInventoryConfiguration(bucket, uriInfo, body); } + s3Service.authorizeCreateBucket(authorization); String locationConstraint = null; if (body != null && body.length > 0) { locationConstraint = XmlParser.extractFirst(new String(body, StandardCharsets.UTF_8), @@ -401,51 +415,61 @@ public class S3Controller { @Context HttpHeaders httpHeaders) { try { validateRawUri(); + S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired( + s3Service.isAuthEnforced(), httpHeaders, uriInfo); if (hasQueryParam(uriInfo, "tagging")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketTagging", authorization); s3Service.deleteBucketTagging(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "website")) { + s3Service.authorizeBucketWrite(bucket, "s3:DeleteBucketWebsite", authorization); s3Service.deleteBucketWebsite(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "policy")) { + s3Service.authorizeBucketWrite(bucket, "s3:DeleteBucketPolicy", authorization); s3Service.deleteBucketPolicy(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "cors")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketCORS", authorization); s3Service.deleteBucketCors(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "lifecycle")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutLifecycleConfiguration", authorization); s3Service.deleteBucketLifecycle(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "encryption")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutEncryptionConfiguration", authorization); s3Service.deleteBucketEncryption(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "publicAccessBlock")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketPublicAccessBlock", authorization); s3Service.deletePublicAccessBlock(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "ownershipControls")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutBucketOwnershipControls", authorization); s3Service.deleteBucketOwnershipControls(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "replication")) { + s3Service.authorizeBucketWrite(bucket, "s3:PutReplicationConfiguration", authorization); s3Service.deleteBucketReplication(bucket); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "metrics")) { // Likewise this must not fall through to deleting the bucket. + s3Service.authorizeBucketWrite(bucket, "s3:PutMetricsConfiguration", authorization); s3Service.deleteBucketMetricsConfiguration(bucket, requireMetricsId(uriInfo)); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "intelligent-tiering")) { // Likewise this must not fall through to deleting the bucket. - S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired( - s3Service.isAuthEnforced(), httpHeaders, uriInfo); s3Service.authorizeBucketWrite(bucket, "s3:PutIntelligentTieringConfiguration", authorization); s3Service.deleteBucketIntelligentTieringConfiguration(bucket, requireIntelligentTieringId(uriInfo)); @@ -453,16 +477,12 @@ public class S3Controller { } if (hasQueryParam(uriInfo, "analytics")) { // Likewise this must not fall through to deleting the bucket. - S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired( - s3Service.isAuthEnforced(), httpHeaders, uriInfo); s3Service.authorizeBucketWrite(bucket, "s3:PutAnalyticsConfiguration", authorization); s3Service.deleteBucketAnalyticsConfiguration(bucket, requireAnalyticsId(uriInfo)); return Response.noContent().build(); } if (hasQueryParam(uriInfo, "inventory")) { // Likewise this must not fall through to deleting the bucket. - S3Service.RequestAuthorization authorization = S3RequestAuthorizationParser.parseIfRequired( - s3Service.isAuthEnforced(), httpHeaders, uriInfo); s3Service.authorizeBucketWrite(bucket, "s3:PutInventoryConfiguration", authorization); s3Service.deleteBucketInventoryConfiguration(bucket, requireInventoryId(uriInfo)); return Response.noContent().build(); @@ -473,6 +493,7 @@ public class S3Controller { throw new AwsException("MethodNotAllowed", "The specified method is not allowed against this resource.", 405); } + s3Service.authorizeBucketWrite(bucket, "s3:DeleteBucket", authorization); s3Service.deleteBucket(bucket); return Response.noContent().build(); } catch (AwsException e) { diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index 51a070dc8b..1061e8d8d3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java @@ -764,6 +764,20 @@ public class S3Service implements Resettable, ResourceProvider { authorizeS3Read(bucketName, null, null, action, bucketArn, authorization); } + /** + * CreateBucket is never anonymous on AWS: there is no bucket policy to consult yet, so an + * unsigned request is denied outright and a signed one only needs a known access key. + */ + void authorizeCreateBucket(RequestAuthorization authorization) { + if (!enforceAuth) { + return; + } + authorizeSignedRequest(authorization); + if (isUnsignedRequest(authorization)) { + throw new AwsException("AccessDenied", "Access Denied", 403); + } + } + void authorizeBucketWrite(String bucketName, String action, RequestAuthorization authorization) { if (!enforceAuth) { return; @@ -780,6 +794,12 @@ public class S3Service implements Resettable, ResourceProvider { Bucket bucket = bucketStore.get(bucketName) .orElseThrow(() -> new AwsException("NoSuchBucket", "The specified bucket does not exist.", 404)); + // AWS lets only an identity in the bucket owner's account manage the bucket policy; the + // policy itself can never grant PutBucketPolicy or DeleteBucketPolicy to an anonymous caller. + if (isBucketPolicyAction(action)) { + throw new AwsException("AccessDenied", "Access Denied", 403); + } + String bucketArn = S3PublicAccessEvaluator.bucketArn(bucketName); S3PublicAccessEvaluator.PublicAccessDecision policyDecision = S3PublicAccessEvaluator.publicPolicyDecision(objectMapper, bucket.getPolicy(), action, bucketArn); @@ -931,6 +951,10 @@ public class S3Service implements Resettable, ResourceProvider { return "s3:PutObject".equals(action); } + private static boolean isBucketPolicyAction(String action) { + return "s3:PutBucketPolicy".equals(action) || "s3:DeleteBucketPolicy".equals(action); + } + private static boolean isUnsignedRequest(RequestAuthorization authorization) { return authorization == null || !authorization.signed(); } 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 ce86b7dfd6..bdd94e1e57 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 @@ -52,6 +52,7 @@ class S3AuthEnforcementIntegrationTest { private static final String BYPASS_BUCKET = "auth-bypass-governance-bucket"; private static final String BYPASS_KEY = "bypass-target.txt"; private static final String CONDITIONAL_DENY_ACL_BUCKET = "auth-conditional-deny-acl-bucket"; + private static final String BUCKET_CONFIG_BUCKET = "auth-bucket-config-bucket"; private static final String PUBLIC_KEY = "public.txt"; private static final String PRIVATE_KEY = "private.txt"; private static final String ERROR_KEY = "error.html"; @@ -71,8 +72,8 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(1) void createBucketsAndObjects() { - given().when().put("/" + PUBLIC_BUCKET).then().statusCode(200); - given().when().put("/" + PRIVATE_BUCKET).then().statusCode(200); + 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() .header("Authorization", LOCAL_AUTH_HEADER) @@ -91,6 +92,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicReadPolicy(PUBLIC_BUCKET)) .when() @@ -245,7 +247,7 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(12) void websiteRootAuthorizesIndexObjectReadNotBucketList() { - given().when().put("/" + WEBSITE_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + WEBSITE_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -257,6 +259,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/xml") .body(websiteConfiguration()) .when() @@ -265,6 +268,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicGetObjectPolicy(WEBSITE_BUCKET)) .when() @@ -292,7 +296,7 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(13) void websiteRootUsesErrorDocumentForDeniedIndexObject() { - given().when().put("/" + WEBSITE_ERROR_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + WEBSITE_ERROR_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -313,6 +317,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/xml") .body(websiteConfiguration(ERROR_KEY)) .when() @@ -321,6 +326,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicGetObjectPolicy(WEBSITE_ERROR_BUCKET, ERROR_KEY)) .when() @@ -342,7 +348,7 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(14) void bucketAclPublicReadAllowsUnsignedList() { - given().when().put("/" + BUCKET_ACL_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + BUCKET_ACL_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -353,6 +359,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/xml") .body(publicReadAcl()) .when() @@ -371,7 +378,7 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(15) void explicitBucketPolicyDenyOverridesPublicObjectAcl() { - given().when().put("/" + DENY_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + DENY_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -383,6 +390,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(denyGetObjectPolicy(DENY_BUCKET)) .when() @@ -484,9 +492,10 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(20) void missingObjectRequiresListBucketToReturnNoSuchKey() { - given().when().put("/" + GET_ONLY_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + GET_ONLY_BUCKET).then().statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicGetObjectPolicy(GET_ONLY_BUCKET)) .when() @@ -512,9 +521,10 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(21) void versionedObjectReadRequiresGetObjectVersion() { - given().when().put("/" + VERSION_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + VERSION_BUCKET).then().statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/xml") .body("Enabled") .when() @@ -532,6 +542,7 @@ class S3AuthEnforcementIntegrationTest { .extract().header("x-amz-version-id"); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicObjectActionPolicy(VERSION_BUCKET, "s3:GetObject")) .when() @@ -547,6 +558,7 @@ class S3AuthEnforcementIntegrationTest { .body("Error.Code", equalTo("AccessDenied")); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicObjectActionPolicy(VERSION_BUCKET, "s3:GetObjectVersion")) .when() @@ -703,7 +715,7 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(29) void unsignedRequestCannotPutObject() { - given().when().put("/" + WRITE_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + WRITE_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -779,9 +791,10 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(32) void bucketPolicyCanExplicitlyAllowAnonymousWrite() { - given().when().put("/" + PUBLIC_WRITE_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + PUBLIC_WRITE_BUCKET).then().statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicObjectActionPolicy(PUBLIC_WRITE_BUCKET, "s3:PutObject")) .when() @@ -797,6 +810,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicObjectActionPolicy(PUBLIC_WRITE_BUCKET, "s3:DeleteObject")) .when() @@ -1064,7 +1078,7 @@ 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().when().put("/" + PUBLIC_WRITE_ACL_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + PUBLIC_WRITE_ACL_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -1108,7 +1122,7 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(39) void explicitPolicyDenyOverridesPublicWriteAcl() { - given().when().put("/" + DENY_WRITE_ACL_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + DENY_WRITE_ACL_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -1119,6 +1133,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(denyObjectActionPolicy(DENY_WRITE_ACL_BUCKET, "s3:PutObject")) .when() @@ -1182,9 +1197,10 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(41) void deleteObjectPolicyGrantDoesNotAuthorizeVersionedDelete() { - given().when().put("/" + DELETE_VERSION_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + DELETE_VERSION_BUCKET).then().statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/xml") .body("Enabled") .when() @@ -1202,6 +1218,7 @@ class S3AuthEnforcementIntegrationTest { .extract().header("x-amz-version-id"); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicObjectActionPolicy(DELETE_VERSION_BUCKET, "s3:DeleteObject")) .when() @@ -1226,7 +1243,7 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(42) void deleteObjectDoesNotBypassGovernanceRetentionWithoutDistinctPermission() { - given().when().put("/" + BYPASS_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + BYPASS_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -1237,6 +1254,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicObjectActionPolicy(BYPASS_BUCKET, "s3:DeleteObject")) .when() @@ -1253,6 +1271,7 @@ class S3AuthEnforcementIntegrationTest { .body("Error.Code", equalTo("AccessDenied")); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(deleteAndBypassGovernancePolicy(BYPASS_BUCKET)) .when() @@ -1288,7 +1307,7 @@ 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().when().put("/" + CONDITIONAL_DENY_ACL_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + CONDITIONAL_DENY_ACL_BUCKET).then().statusCode(200); given() .header("Authorization", LOCAL_AUTH_HEADER) @@ -1299,6 +1318,7 @@ class S3AuthEnforcementIntegrationTest { .statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(conditionalDenyObjectActionPolicy(CONDITIONAL_DENY_ACL_BUCKET, "s3:PutObject")) .when() @@ -1318,9 +1338,10 @@ class S3AuthEnforcementIntegrationTest { @Test @Order(44) void batchDeleteObjectsPolicyGrantDoesNotAuthorizeVersionedDelete() { - given().when().put("/" + BATCH_DELETE_VERSION_BUCKET).then().statusCode(200); + given().header("Authorization", LOCAL_AUTH_HEADER).when().put("/" + BATCH_DELETE_VERSION_BUCKET).then().statusCode(200); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/xml") .body("Enabled") .when() @@ -1338,6 +1359,7 @@ class S3AuthEnforcementIntegrationTest { .extract().header("x-amz-version-id"); given() + .header("Authorization", LOCAL_AUTH_HEADER) .contentType("application/json") .body(publicObjectActionPolicy(BATCH_DELETE_VERSION_BUCKET, "s3:DeleteObject")) .when() @@ -1458,6 +1480,257 @@ class S3AuthEnforcementIntegrationTest { .when().get(path).then().statusCode(403).body(containsString("InvalidAccessKeyId")); } + @Test + @Order(46) + void unsignedRequestCannotCreateOrDeleteBucket() { + given() + .when() + .put("/" + BUCKET_CONFIG_BUCKET) + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .header("Authorization", BAD_AUTH_HEADER) + .when() + .put("/" + BUCKET_CONFIG_BUCKET) + .then() + .statusCode(403) + .body(containsString("InvalidAccessKeyId")); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .head("/" + BUCKET_CONFIG_BUCKET) + .then() + .statusCode(404); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .put("/" + BUCKET_CONFIG_BUCKET) + .then() + .statusCode(200); + + given() + .when() + .delete("/" + BUCKET_CONFIG_BUCKET) + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .header("Authorization", BAD_AUTH_HEADER) + .when() + .delete("/" + BUCKET_CONFIG_BUCKET) + .then() + .statusCode(403) + .body(containsString("InvalidAccessKeyId")); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .head("/" + BUCKET_CONFIG_BUCKET) + .then() + .statusCode(200); + } + + @Test + @Order(47) + void unsignedRequestCannotRewriteBucketPolicyToExposePrivateObject() { + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .body("secret data") + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "/" + PRIVATE_KEY) + .then() + .statusCode(200); + + given() + .when() + .get("/" + BUCKET_CONFIG_BUCKET + "/" + PRIVATE_KEY) + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .contentType("application/json") + .body(publicGetObjectPolicy(BUCKET_CONFIG_BUCKET)) + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .when() + .get("/" + BUCKET_CONFIG_BUCKET + "/" + PRIVATE_KEY) + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .get("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(404) + .body(containsString("NoSuchBucketPolicy")); + + given() + .when() + .delete("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + } + + @Test + @Order(48) + void unsignedRequestCannotWriteBucketSubresources() { + for (String subresource : new String[] { + "acl", "cors", "lifecycle", "versioning", "website", "notification", "logging", + "encryption", "publicAccessBlock", "ownershipControls", "requestPayment", "tagging", + "accelerate", "replication", "object-lock", "metrics&id=anon"}) { + given() + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "?" + subresource) + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + } + for (String subresource : new String[] { + "cors", "lifecycle", "website", "encryption", "publicAccessBlock", + "ownershipControls", "tagging", "replication", "metrics&id=anon"}) { + given() + .when() + .delete("/" + BUCKET_CONFIG_BUCKET + "?" + subresource) + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + } + + given() + .header("Authorization", BAD_AUTH_HEADER) + .contentType("application/xml") + .body("kv") + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "?tagging") + .then() + .statusCode(403) + .body(containsString("InvalidAccessKeyId")); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .get("/" + BUCKET_CONFIG_BUCKET + "?tagging") + .then() + .statusCode(200) + .body(not(containsString("k"))); + } + + @Test + @Order(49) + void bucketPolicyCanExplicitlyAllowAnonymousBucketConfigWrite() { + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .contentType("application/json") + .body(publicBucketActionPolicy(BUCKET_CONFIG_BUCKET, "s3:PutBucketTagging")) + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(200); + + given() + .contentType("application/xml") + .body("anongranted") + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "?tagging") + .then() + .statusCode(204); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .get("/" + BUCKET_CONFIG_BUCKET + "?tagging") + .then() + .statusCode(200) + .body(containsString("anon")); + + given() + .contentType("application/xml") + .body("*" + + "GET") + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "?cors") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .delete("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(204); + } + + @Test + @Order(50) + void bucketPolicyCannotGrantAnonymousBucketPolicyWrites() { + String selfGrantingPolicy = """ + { + "Version": "2012-10-17", + "Statement": { + "Effect": "Allow", + "Principal": "*", + "Action": ["s3:PutBucketPolicy", "s3:DeleteBucketPolicy"], + "Resource": "arn:aws:s3:::%s" + } + } + """.formatted(BUCKET_CONFIG_BUCKET); + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .contentType("application/json") + .body(selfGrantingPolicy) + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(200); + + given() + .contentType("application/json") + .body(publicGetObjectPolicy(BUCKET_CONFIG_BUCKET)) + .when() + .put("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .when() + .delete("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(403) + .body(containsString("AccessDenied")); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .get("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(200) + .body(containsString("s3:DeleteBucketPolicy")) + .body(not(containsString("s3:GetObject"))); + + given() + .header("Authorization", LOCAL_AUTH_HEADER) + .when() + .delete("/" + BUCKET_CONFIG_BUCKET + "?policy") + .then() + .statusCode(204); + } + private static String presignedSignature(String method, String path, String accessKeyId, String secretKey, String expires) { return presignedSignature(method, path, accessKeyId, secretKey, expires, "UNSIGNED-PAYLOAD"); @@ -1587,6 +1860,20 @@ class S3AuthEnforcementIntegrationTest { """.formatted(action, bucket, key); } + private static String publicBucketActionPolicy(String bucket, String action) { + return """ + { + "Version": "2012-10-17", + "Statement": { + "Effect": "Allow", + "Principal": "*", + "Action": "%s", + "Resource": "arn:aws:s3:::%s" + } + } + """.formatted(action, bucket); + } + private static String denyGetObjectPolicy(String bucket) { return """ { 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 480a86011a..4fe9acb70c 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 @@ -49,6 +49,7 @@ class S3PresignedPostAuthEnforcementIntegrationTest { @Order(1) void createBucket() { given() + .header("Authorization", authorizationHeader(LEGACY_ACCESS_KEY_ID)) .when() .put("/" + BUCKET) .then()