mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(s3): enforce Block Public Access on the data plane (#4253)
* feat(s3): enforce Block Public Access on the data plane The four Block Public Access flags were stored and returned but never read by any authorization path, so put-public-access-block was accepted and changed nothing: a bucket left public by a policy or an ACL stayed readable. Enforce all four, combining a bucket's configuration with its owner account's and taking the most restrictive of the two, as AWS does. BlockPublicPolicy and BlockPublicAcls reject the write that would introduce public access, which AWS applies whoever the caller is, so they do not sit behind enforce-auth. RestrictPublicBuckets and IgnorePublicAcls suppress access an existing policy or ACL grants, which only has meaning once anonymous authorization runs, so they take effect under enforce-auth. Deciding what counts as public follows the documented AWS rules: an ACL is public when it grants any permission to AllUsers or AuthenticatedUsers, and a policy is assumed public unless a wildcard-principal statement pins a recognised condition key to a fixed value. A single public statement makes the whole policy public, so RestrictPublicBuckets also withholds the cross-account delegation a separate non-public statement grants. Enabling a setting never rewrites stored state; an existing public policy or ACL is left exactly as written. * test(s3): pin the Block Public Access contract at the wire Cover the write-time flags under the default configuration and the read-time flags under enforce-auth, including the reproduction that motivated the fix: put-public-access-block followed by an anonymous GetObject on a bucket whose policy is public. Each enforcing assertion is paired with a negative control that must keep passing without the fix, so a test cannot pass vacuously: the without-BPA case still returns 200, and the owner's signed access is never affected. Reverting the S3Service change fails the 11 enforcing assertions and leaves the 10 controls green. Unit tests cover the settings record's parsing and most-restrictive merge, the ACL public-status rule, and the policy public-status rule against the examples in the AWS documentation. * test(s3): validate Block Public Access through the AWS SDK Drives BlockPublicPolicy and BlockPublicAcls with S3Client so the rejection is seen the way an SDK caller sees it: a 403 carrying the AccessDenied error code, not just a status line. Both apply without enforce-auth, which is the configuration this suite runs against. * test(s3): drop the em-dash from the Block Public Access display name * fix(s3): recognize scoped access point ARN in bucket policies * fix(s3): honor ForAnyValue in public policy checks
This commit is contained in:
1 parent
72231c09cf
commit
bae7e0316a
12 files changed
+1435
-14
No files matched your search
+166
@@ -0,0 +1,166 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
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 software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.BucketAlreadyOwnedByYouException;
|
||||
import software.amazon.awssdk.services.s3.model.BucketCannedACL;
|
||||
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetBucketPolicyRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetPublicAccessBlockRequest;
|
||||
import software.amazon.awssdk.services.s3.model.GetPublicAccessBlockResponse;
|
||||
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
|
||||
import software.amazon.awssdk.services.s3.model.PublicAccessBlockConfiguration;
|
||||
import software.amazon.awssdk.services.s3.model.PutBucketAclRequest;
|
||||
import software.amazon.awssdk.services.s3.model.PutBucketPolicyRequest;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.PutPublicAccessBlockRequest;
|
||||
import software.amazon.awssdk.services.s3.model.S3Exception;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.catchThrowableOfType;
|
||||
|
||||
/**
|
||||
* S3 Block Public Access, driven through the real AWS SDK.
|
||||
*
|
||||
* <p>Covers the half AWS enforces whoever the caller is: {@code BlockPublicPolicy} rejecting a
|
||||
* public bucket policy and {@code BlockPublicAcls} rejecting a public ACL. Both apply without
|
||||
* {@code enforce-auth}, which is the configuration this suite runs against. The read-time flags
|
||||
* ({@code RestrictPublicBuckets}, {@code IgnorePublicAcls}) need anonymous authorization to be
|
||||
* running and are covered by the in-tree integration tests instead.
|
||||
*/
|
||||
@DisplayName("S3 Block Public Access: BlockPublicPolicy / BlockPublicAcls")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class S3BlockPublicAccessTest {
|
||||
|
||||
private static S3Client s3;
|
||||
|
||||
private static final String BUCKET = "compat-bpa-bucket";
|
||||
|
||||
private static final String PUBLIC_POLICY = """
|
||||
{"Version":"2012-10-17","Statement":[{"Sid":"PublicRead","Effect":"Allow",
|
||||
"Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*"}]}
|
||||
""".formatted(BUCKET);
|
||||
|
||||
private static final String ACCOUNT_SCOPED_POLICY = """
|
||||
{"Version":"2012-10-17","Statement":[{"Sid":"OneAccount","Effect":"Allow",
|
||||
"Principal":{"AWS":"arn:aws:iam::111122223333:root"},"Action":"s3:GetObject",
|
||||
"Resource":"arn:aws:s3:::%s/*"}]}
|
||||
""".formatted(BUCKET);
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
s3 = TestFixtures.s3Client();
|
||||
try {
|
||||
s3.createBucket(CreateBucketRequest.builder().bucket(BUCKET).build());
|
||||
} catch (BucketAlreadyOwnedByYouException expected) {
|
||||
// A previous run of this suite left the bucket behind; reuse it.
|
||||
}
|
||||
s3.putPublicAccessBlock(PutPublicAccessBlockRequest.builder()
|
||||
.bucket(BUCKET)
|
||||
.publicAccessBlockConfiguration(PublicAccessBlockConfiguration.builder()
|
||||
.blockPublicAcls(true)
|
||||
.ignorePublicAcls(false)
|
||||
.blockPublicPolicy(true)
|
||||
.restrictPublicBuckets(false)
|
||||
.build())
|
||||
.build());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void teardown() {
|
||||
if (s3 != null) {
|
||||
s3.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
@DisplayName("GetPublicAccessBlock returns the flags PutPublicAccessBlock stored")
|
||||
void publicAccessBlockRoundTrips() {
|
||||
GetPublicAccessBlockResponse response = s3.getPublicAccessBlock(
|
||||
GetPublicAccessBlockRequest.builder().bucket(BUCKET).build());
|
||||
|
||||
PublicAccessBlockConfiguration configuration = response.publicAccessBlockConfiguration();
|
||||
assertThat(configuration.blockPublicAcls()).isTrue();
|
||||
assertThat(configuration.blockPublicPolicy()).isTrue();
|
||||
assertThat(configuration.ignorePublicAcls()).isFalse();
|
||||
assertThat(configuration.restrictPublicBuckets()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
@DisplayName("BlockPublicPolicy rejects PutBucketPolicy with AccessDenied")
|
||||
void blockPublicPolicyRejectsAPublicPolicy() {
|
||||
S3Exception exception = catchThrowableOfType(
|
||||
() -> s3.putBucketPolicy(PutBucketPolicyRequest.builder()
|
||||
.bucket(BUCKET)
|
||||
.policy(PUBLIC_POLICY)
|
||||
.build()),
|
||||
S3Exception.class);
|
||||
|
||||
assertThat(exception).isNotNull();
|
||||
assertThat(exception.statusCode()).isEqualTo(403);
|
||||
assertThat(exception.awsErrorDetails().errorCode()).isEqualTo("AccessDenied");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
@DisplayName("BlockPublicPolicy leaves a policy scoped to one account alone")
|
||||
void aNonPublicPolicyIsStillAccepted() {
|
||||
s3.putBucketPolicy(PutBucketPolicyRequest.builder()
|
||||
.bucket(BUCKET)
|
||||
.policy(ACCOUNT_SCOPED_POLICY)
|
||||
.build());
|
||||
|
||||
assertThat(s3.getBucketPolicy(GetBucketPolicyRequest.builder().bucket(BUCKET).build()).policy())
|
||||
.contains("OneAccount");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
@DisplayName("BlockPublicAcls rejects a public bucket ACL with AccessDenied")
|
||||
void blockPublicAclsRejectsAPublicBucketAcl() {
|
||||
S3Exception exception = catchThrowableOfType(
|
||||
() -> s3.putBucketAcl(PutBucketAclRequest.builder()
|
||||
.bucket(BUCKET)
|
||||
.acl(BucketCannedACL.PUBLIC_READ)
|
||||
.build()),
|
||||
S3Exception.class);
|
||||
|
||||
assertThat(exception).isNotNull();
|
||||
assertThat(exception.statusCode()).isEqualTo(403);
|
||||
assertThat(exception.awsErrorDetails().errorCode()).isEqualTo("AccessDenied");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
@DisplayName("BlockPublicAcls rejects a PutObject carrying a public ACL")
|
||||
void blockPublicAclsRejectsAPublicObjectAcl() {
|
||||
assertThatThrownBy(() -> s3.putObject(
|
||||
PutObjectRequest.builder()
|
||||
.bucket(BUCKET)
|
||||
.key("public.txt")
|
||||
.acl(ObjectCannedACL.PUBLIC_READ)
|
||||
.build(),
|
||||
RequestBody.fromString("hello")))
|
||||
.isInstanceOf(S3Exception.class)
|
||||
.satisfies(thrown -> assertThat(((S3Exception) thrown).statusCode()).isEqualTo(403));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@DisplayName("A PutObject without a public ACL is unaffected")
|
||||
void aPrivatePutObjectIsUnaffected() {
|
||||
s3.putObject(
|
||||
PutObjectRequest.builder().bucket(BUCKET).key("private.txt").build(),
|
||||
RequestBody.fromString("hello"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user