mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+57
@@ -83,6 +83,63 @@ class S3AclPublicAccessEvaluatorTest {
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclAllowsPublicRead("<AccessControlPolicy>"));
|
||||
}
|
||||
|
||||
// Block Public Access asks a broader question than grant evaluation does: an ACL counts as
|
||||
// public when it grants ANY permission to AllUsers or to AuthenticatedUsers, so a
|
||||
// WRITE_ACP grant and an AuthenticatedUsers grant are both public here while neither
|
||||
// grants an anonymous caller a read.
|
||||
|
||||
@Test
|
||||
void allUsersReadAclIsPublic() {
|
||||
assertTrue(S3AclPublicAccessEvaluator.aclIsPublic(acl(
|
||||
groupGrant(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI, "READ"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authenticatedUsersAclIsPublic() {
|
||||
assertTrue(S3AclPublicAccessEvaluator.aclIsPublic(acl(
|
||||
groupGrant(AUTHENTICATED_USERS_GROUP_URI, "READ"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anyPermissionToAPublicGroupIsPublic() {
|
||||
assertTrue(S3AclPublicAccessEvaluator.aclIsPublic(acl(
|
||||
groupGrant(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI, "WRITE_ACP"))));
|
||||
assertTrue(S3AclPublicAccessEvaluator.aclIsPublic(acl(
|
||||
groupGrant(S3AclPublicAccessEvaluator.ALL_USERS_GROUP_URI, "READ_ACP"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void logDeliveryGroupAclIsNotPublic() {
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclIsPublic(acl(groupGrant(
|
||||
"http://acs.amazonaws.com/groups/s3/LogDelivery", "WRITE"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void canonicalUserOnlyAclIsNotPublic() {
|
||||
String acl = """
|
||||
<AccessControlPolicy>
|
||||
<AccessControlList>
|
||||
<Grant>
|
||||
<Grantee xsi:type="CanonicalUser"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<ID>000000000000</ID>
|
||||
</Grantee>
|
||||
<Permission>FULL_CONTROL</Permission>
|
||||
</Grant>
|
||||
</AccessControlList>
|
||||
</AccessControlPolicy>
|
||||
""";
|
||||
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclIsPublic(acl));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidAclIsNotPublic() {
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclIsPublic(null));
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclIsPublic(""));
|
||||
assertFalse(S3AclPublicAccessEvaluator.aclIsPublic("<AccessControlPolicy>"));
|
||||
}
|
||||
|
||||
private static String acl(String... grants) {
|
||||
return """
|
||||
<AccessControlPolicy>
|
||||
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
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;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* The read-time half of Block Public Access: {@code RestrictPublicBuckets} withholds the access a
|
||||
* public bucket policy grants, and {@code IgnorePublicAcls} withholds the access a public ACL
|
||||
* grants. Both act on an existing policy or ACL rather than on the write that created it, so
|
||||
* they only have meaning once anonymous authorization runs, which is what {@code enforce-auth}
|
||||
* turns on.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(S3BlockPublicAccessEnforcementIntegrationTest.EnforcementProfile.class)
|
||||
class S3BlockPublicAccessEnforcementIntegrationTest {
|
||||
|
||||
private static final S3RequestSigner OWNER = S3RequestSigner.signedAs("test", "test");
|
||||
|
||||
private static final String PUBLIC_READ_POLICY = """
|
||||
{"Version":"2012-10-17","Statement":[{"Sid":"PublicRead","Effect":"Allow",
|
||||
"Principal":"*","Action":["s3:GetObject","s3:ListBucket"],
|
||||
"Resource":["arn:aws:s3:::%1$s","arn:aws:s3:::%1$s/*"]}]}
|
||||
""";
|
||||
|
||||
public static final class EnforcementProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.s3.enforce-auth", "true");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anonymousGetSucceedsOnAPublicPolicyWithoutRestrictPublicBuckets() {
|
||||
String bucket = bucketWithPublicPolicy();
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(200).body(equalTo("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restrictPublicBucketsWithholdsAnonymousGetOnAPublicPolicy() {
|
||||
String bucket = bucketWithPublicPolicy();
|
||||
putBucketPublicAccessBlock(bucket, false, false, false, true);
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restrictPublicBucketsWithholdsAnonymousListBucketOnAPublicPolicy() {
|
||||
String bucket = bucketWithPublicPolicy();
|
||||
putBucketPublicAccessBlock(bucket, false, false, false, true);
|
||||
|
||||
given().when().get("/" + bucket)
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restrictPublicBucketsLeavesTheOwnersSignedAccessAlone() {
|
||||
String bucket = bucketWithPublicPolicy();
|
||||
putBucketPublicAccessBlock(bucket, false, false, false, true);
|
||||
|
||||
given().filter(OWNER).when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(200).body(equalTo("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restrictPublicBucketsDoesNotAffectABucketWhosePolicyIsNotPublic() {
|
||||
String bucket = createBucket();
|
||||
putObject(bucket, "object.txt");
|
||||
putBucketPolicy(bucket, """
|
||||
{"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));
|
||||
putBucketPublicAccessBlock(bucket, false, false, false, true);
|
||||
|
||||
given().filter(OWNER).when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(200).body(equalTo("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theAccountLevelRestrictPublicBucketsWithholdsAnonymousGet() {
|
||||
String bucket = bucketWithPublicPolicy();
|
||||
putAccountPublicAccessBlock(false, false, false, true);
|
||||
try {
|
||||
given().when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
} finally {
|
||||
deleteAccountPublicAccessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anonymousGetSucceedsOnAPublicObjectAclWithoutIgnorePublicAcls() {
|
||||
String bucket = bucketWithPublicObjectAcl();
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(200).body(equalTo("hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignorePublicAclsWithholdsAnonymousGetOnAPublicObjectAcl() {
|
||||
String bucket = bucketWithPublicObjectAcl();
|
||||
putBucketPublicAccessBlock(bucket, false, true, false, false);
|
||||
|
||||
given().when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignorePublicAclsWithholdsAnonymousListBucketOnAPublicBucketAcl() {
|
||||
String bucket = createBucket();
|
||||
putObject(bucket, "object.txt");
|
||||
given().filter(OWNER).header("x-amz-acl", "public-read")
|
||||
.when().put("/" + bucket + "?acl")
|
||||
.then().statusCode(200);
|
||||
|
||||
given().when().get("/" + bucket).then().statusCode(200);
|
||||
|
||||
putBucketPublicAccessBlock(bucket, false, true, false, false);
|
||||
|
||||
given().when().get("/" + bucket)
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignorePublicAclsLeavesTheOwnersSignedAccessAlone() {
|
||||
String bucket = bucketWithPublicObjectAcl();
|
||||
putBucketPublicAccessBlock(bucket, false, true, false, false);
|
||||
|
||||
given().filter(OWNER).when().get("/" + bucket + "/object.txt")
|
||||
.then().statusCode(200).body(equalTo("hello"));
|
||||
}
|
||||
|
||||
private static String bucketWithPublicPolicy() {
|
||||
String bucket = createBucket();
|
||||
putObject(bucket, "object.txt");
|
||||
putBucketPolicy(bucket, PUBLIC_READ_POLICY.formatted(bucket));
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static String bucketWithPublicObjectAcl() {
|
||||
String bucket = createBucket();
|
||||
putObject(bucket, "object.txt");
|
||||
given().filter(OWNER).header("x-amz-acl", "public-read")
|
||||
.when().put("/" + bucket + "/object.txt?acl")
|
||||
.then().statusCode(200);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static String createBucket() {
|
||||
String bucket = "bpa-enforce-" + UUID.randomUUID();
|
||||
given().filter(OWNER).when().put("/" + bucket).then().statusCode(200);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static void putObject(String bucket, String key) {
|
||||
given().filter(OWNER).body("hello")
|
||||
.when().put("/" + bucket + "/" + key)
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void putBucketPolicy(String bucket, String policy) {
|
||||
given().filter(OWNER).body(policy)
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void putBucketPublicAccessBlock(String bucket, boolean blockPublicAcls,
|
||||
boolean ignorePublicAcls, boolean blockPublicPolicy,
|
||||
boolean restrictPublicBuckets) {
|
||||
given().filter(OWNER)
|
||||
.body(configuration(blockPublicAcls, ignorePublicAcls, blockPublicPolicy, restrictPublicBuckets))
|
||||
.when().put("/" + bucket + "?publicAccessBlock")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void putAccountPublicAccessBlock(boolean blockPublicAcls, boolean ignorePublicAcls,
|
||||
boolean blockPublicPolicy, boolean restrictPublicBuckets) {
|
||||
given().filter(OWNER).header("x-amz-account-id", "000000000000")
|
||||
.body(configuration(blockPublicAcls, ignorePublicAcls, blockPublicPolicy, restrictPublicBuckets))
|
||||
.when().put("/v20180820/configuration/publicAccessBlock")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void deleteAccountPublicAccessBlock() {
|
||||
given().filter(OWNER).header("x-amz-account-id", "000000000000")
|
||||
.when().delete("/v20180820/configuration/publicAccessBlock")
|
||||
.then().statusCode(204);
|
||||
}
|
||||
|
||||
private static String configuration(boolean blockPublicAcls, boolean ignorePublicAcls,
|
||||
boolean blockPublicPolicy, boolean restrictPublicBuckets) {
|
||||
return """
|
||||
<PublicAccessBlockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<BlockPublicAcls>%s</BlockPublicAcls>
|
||||
<IgnorePublicAcls>%s</IgnorePublicAcls>
|
||||
<BlockPublicPolicy>%s</BlockPublicPolicy>
|
||||
<RestrictPublicBuckets>%s</RestrictPublicBuckets>
|
||||
</PublicAccessBlockConfiguration>
|
||||
""".formatted(blockPublicAcls, ignorePublicAcls, blockPublicPolicy, restrictPublicBuckets);
|
||||
}
|
||||
}
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
/**
|
||||
* The write-time half of Block Public Access: {@code BlockPublicPolicy} and
|
||||
* {@code BlockPublicAcls} reject the call that would introduce public access.
|
||||
*
|
||||
* <p>AWS applies both whoever the caller is, so these run under the default configuration with
|
||||
* no {@code enforce-auth} and no IAM enforcement. The read-time half
|
||||
* ({@code RestrictPublicBuckets}, {@code IgnorePublicAcls}) only has meaning once anonymous
|
||||
* authorization runs at all, and is covered by
|
||||
* {@link S3BlockPublicAccessEnforcementIntegrationTest}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class S3BlockPublicAccessIntegrationTest {
|
||||
|
||||
private static final String PUBLIC_POLICY = """
|
||||
{"Version":"2012-10-17","Statement":[{"Sid":"PublicRead","Effect":"Allow",
|
||||
"Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*"}]}
|
||||
""";
|
||||
|
||||
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/*"}]}
|
||||
""";
|
||||
|
||||
@Test
|
||||
void blockPublicPolicyRejectsAPublicBucketPolicy() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, false, false, true, false);
|
||||
|
||||
given().body(PUBLIC_POLICY.formatted(bucket))
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicPolicyLeavesANonPublicBucketPolicyAlone() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, false, false, true, false);
|
||||
|
||||
given().body(ACCOUNT_SCOPED_POLICY.formatted(bucket))
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicPolicyAcceptsFixedAccountAccessPointWildcard() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, false, false, true, false);
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*",
|
||||
"Condition":{"ArnLike":{"s3:DataAccessPointArn":
|
||||
"arn:aws:s3:us-west-2:123456789012:accesspoint/*"}}}]}
|
||||
""".formatted(bucket);
|
||||
|
||||
given().body(policy)
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicPolicyAcceptsForAnyValueWithFixedPrincipalOrgPath() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, false, false, true, false);
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":"*","Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*",
|
||||
"Condition":{"ForAnyValue:StringLike":{"aws:PrincipalOrgPaths":
|
||||
["o-a1b2c3d4e5/r-ab12/ou-ab12-11111111/"]}}}]}
|
||||
""".formatted(bucket);
|
||||
|
||||
given().body(policy)
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPublicBucketPolicyIsAcceptedWithoutBlockPublicPolicy() {
|
||||
String bucket = createBucket();
|
||||
|
||||
given().body(PUBLIC_POLICY.formatted(bucket))
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicPolicyDoesNotRemoveAPolicyStoredBeforeItWasSet() {
|
||||
String bucket = createBucket();
|
||||
given().body(PUBLIC_POLICY.formatted(bucket))
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(200);
|
||||
|
||||
putBucketPublicAccessBlock(bucket, false, false, true, false);
|
||||
|
||||
given().when().get("/" + bucket + "?policy")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("PublicRead"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theAccountLevelSettingBlocksAPublicPolicyOnItsOwn() {
|
||||
String bucket = createBucket();
|
||||
putAccountPublicAccessBlock(false, false, true, false);
|
||||
try {
|
||||
given().body(PUBLIC_POLICY.formatted(bucket))
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
} finally {
|
||||
deleteAccountPublicAccessBlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicAclsRejectsAPublicBucketAcl() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, true, false, false, false);
|
||||
|
||||
given().header("x-amz-acl", "public-read")
|
||||
.when().put("/" + bucket + "?acl")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicAclsLeavesAPrivateBucketAclAlone() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, true, false, false, false);
|
||||
|
||||
given().header("x-amz-acl", "private")
|
||||
.when().put("/" + bucket + "?acl")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicAclsRejectsAPublicObjectAcl() {
|
||||
String bucket = createBucket();
|
||||
given().body("hello").when().put("/" + bucket + "/object.txt").then().statusCode(200);
|
||||
putBucketPublicAccessBlock(bucket, true, false, false, false);
|
||||
|
||||
given().header("x-amz-acl", "public-read")
|
||||
.when().put("/" + bucket + "/object.txt?acl")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicAclsRejectsAPutObjectCarryingAPublicAcl() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, true, false, false, false);
|
||||
|
||||
given().header("x-amz-acl", "public-read").body("hello")
|
||||
.when().put("/" + bucket + "/public.txt")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicAclsLeavesAPutObjectWithoutAPublicAclAlone() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, true, false, false, false);
|
||||
|
||||
given().body("hello")
|
||||
.when().put("/" + bucket + "/private.txt")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAuthenticatedUsersAclCountsAsPublic() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, true, false, false, false);
|
||||
|
||||
given().header("x-amz-acl", "authenticated-read")
|
||||
.when().put("/" + bucket + "?acl")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
private static String createBucket() {
|
||||
String bucket = "bpa-" + UUID.randomUUID();
|
||||
given().when().put("/" + bucket).then().statusCode(200);
|
||||
return bucket;
|
||||
}
|
||||
|
||||
private static void putBucketPublicAccessBlock(String bucket, boolean blockPublicAcls,
|
||||
boolean ignorePublicAcls, boolean blockPublicPolicy,
|
||||
boolean restrictPublicBuckets) {
|
||||
given().body(configuration(blockPublicAcls, ignorePublicAcls, blockPublicPolicy, restrictPublicBuckets))
|
||||
.when().put("/" + bucket + "?publicAccessBlock")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void putAccountPublicAccessBlock(boolean blockPublicAcls, boolean ignorePublicAcls,
|
||||
boolean blockPublicPolicy, boolean restrictPublicBuckets) {
|
||||
given().header("x-amz-account-id", "000000000000")
|
||||
.body(configuration(blockPublicAcls, ignorePublicAcls, blockPublicPolicy, restrictPublicBuckets))
|
||||
.when().put("/v20180820/configuration/publicAccessBlock")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void deleteAccountPublicAccessBlock() {
|
||||
given().header("x-amz-account-id", "000000000000")
|
||||
.when().delete("/v20180820/configuration/publicAccessBlock")
|
||||
.then().statusCode(204);
|
||||
}
|
||||
|
||||
private static String configuration(boolean blockPublicAcls, boolean ignorePublicAcls,
|
||||
boolean blockPublicPolicy, boolean restrictPublicBuckets) {
|
||||
return """
|
||||
<PublicAccessBlockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<BlockPublicAcls>%s</BlockPublicAcls>
|
||||
<IgnorePublicAcls>%s</IgnorePublicAcls>
|
||||
<BlockPublicPolicy>%s</BlockPublicPolicy>
|
||||
<RestrictPublicBuckets>%s</RestrictPublicBuckets>
|
||||
</PublicAccessBlockConfiguration>
|
||||
""".formatted(blockPublicAcls, ignorePublicAcls, blockPublicPolicy, restrictPublicBuckets);
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class S3BlockPublicAccessSettingsTest {
|
||||
|
||||
@Test
|
||||
void parsesAllFourFlags() {
|
||||
S3BlockPublicAccessSettings settings = S3BlockPublicAccessSettings.parse(configuration(
|
||||
true, true, true, true));
|
||||
|
||||
assertTrue(settings.blockPublicAcls());
|
||||
assertTrue(settings.ignorePublicAcls());
|
||||
assertTrue(settings.blockPublicPolicy());
|
||||
assertTrue(settings.restrictPublicBuckets());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesFlagsIndependently() {
|
||||
S3BlockPublicAccessSettings settings = S3BlockPublicAccessSettings.parse(configuration(
|
||||
false, true, false, true));
|
||||
|
||||
assertFalse(settings.blockPublicAcls());
|
||||
assertTrue(settings.ignorePublicAcls());
|
||||
assertFalse(settings.blockPublicPolicy());
|
||||
assertTrue(settings.restrictPublicBuckets());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOmittedFlagIsOff() {
|
||||
S3BlockPublicAccessSettings settings = S3BlockPublicAccessSettings.parse("""
|
||||
<PublicAccessBlockConfiguration>
|
||||
<BlockPublicPolicy>true</BlockPublicPolicy>
|
||||
</PublicAccessBlockConfiguration>
|
||||
""");
|
||||
|
||||
assertTrue(settings.blockPublicPolicy());
|
||||
assertFalse(settings.blockPublicAcls());
|
||||
assertFalse(settings.ignorePublicAcls());
|
||||
assertFalse(settings.restrictPublicBuckets());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAbsentConfigurationBlocksNothing() {
|
||||
assertEquals(S3BlockPublicAccessSettings.NONE, S3BlockPublicAccessSettings.parse(null));
|
||||
assertEquals(S3BlockPublicAccessSettings.NONE, S3BlockPublicAccessSettings.parse(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedXmlBlocksNothing() {
|
||||
assertEquals(S3BlockPublicAccessSettings.NONE,
|
||||
S3BlockPublicAccessSettings.parse("<PublicAccessBlockConfiguration"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mostRestrictiveCombinesFlagsFromBothLevels() {
|
||||
S3BlockPublicAccessSettings bucket = S3BlockPublicAccessSettings.parse(configuration(
|
||||
true, false, false, false));
|
||||
S3BlockPublicAccessSettings account = S3BlockPublicAccessSettings.parse(configuration(
|
||||
false, false, true, false));
|
||||
|
||||
S3BlockPublicAccessSettings effective = bucket.mostRestrictive(account);
|
||||
|
||||
assertTrue(effective.blockPublicAcls());
|
||||
assertTrue(effective.blockPublicPolicy());
|
||||
assertFalse(effective.ignorePublicAcls());
|
||||
assertFalse(effective.restrictPublicBuckets());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mostRestrictiveNeverRelaxesAFlag() {
|
||||
S3BlockPublicAccessSettings all = S3BlockPublicAccessSettings.parse(configuration(
|
||||
true, true, true, true));
|
||||
|
||||
assertEquals(all, all.mostRestrictive(S3BlockPublicAccessSettings.NONE));
|
||||
assertEquals(all, S3BlockPublicAccessSettings.NONE.mostRestrictive(all));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mostRestrictiveTreatsAMissingConfigurationAsNoFlags() {
|
||||
S3BlockPublicAccessSettings bucket = S3BlockPublicAccessSettings.parse(configuration(
|
||||
false, true, false, false));
|
||||
|
||||
assertEquals(bucket, bucket.mostRestrictive(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anyFlagSetIsReported() {
|
||||
assertFalse(S3BlockPublicAccessSettings.NONE.blocksAnything());
|
||||
assertTrue(S3BlockPublicAccessSettings.parse(configuration(false, false, false, true))
|
||||
.blocksAnything());
|
||||
}
|
||||
|
||||
private static String configuration(boolean blockPublicAcls, boolean ignorePublicAcls,
|
||||
boolean blockPublicPolicy, boolean restrictPublicBuckets) {
|
||||
return """
|
||||
<PublicAccessBlockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<BlockPublicAcls>%s</BlockPublicAcls>
|
||||
<IgnorePublicAcls>%s</IgnorePublicAcls>
|
||||
<BlockPublicPolicy>%s</BlockPublicPolicy>
|
||||
<RestrictPublicBuckets>%s</RestrictPublicBuckets>
|
||||
</PublicAccessBlockConfiguration>
|
||||
""".formatted(blockPublicAcls, ignorePublicAcls, blockPublicPolicy, restrictPublicBuckets);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* The "meaning of public" rules S3 Block Public Access applies to a bucket policy. S3 assumes a
|
||||
* policy is public and then looks for a reason it is not: a wildcard principal is public unless
|
||||
* the statement pins one of the recognised condition keys to a fixed, wildcard-free value.
|
||||
*/
|
||||
class S3PublicPolicyStatusTest {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalAllowIsPublic() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:PutObject","Effect":"Allow"}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardAwsPrincipalAllowIsPublic() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":{"AWS":"*"},"Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:GetObject","Effect":"Allow"}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalDenyIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:*","Effect":"Deny"}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fixedPrincipalAllowIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":{"AWS":"arn:aws:iam::111122223333:root"},
|
||||
"Resource":"arn:aws:s3:::b/*","Action":"s3:GetObject","Effect":"Allow"}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void servicePrincipalAllowIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":{"Service":"cloudtrail.amazonaws.com"},
|
||||
"Resource":"arn:aws:s3:::b/*","Action":"s3:PutObject","Effect":"Allow"}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalPinnedToAFixedSourceVpcIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:PutObject","Effect":"Allow",
|
||||
"Condition":{"StringEquals":{"aws:SourceVpc":"vpc-91237329"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalWithAWildcardSourceVpcIsPublic() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:PutObject","Effect":"Allow",
|
||||
"Condition":{"StringLike":{"aws:SourceVpc":"vpc-*"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalPinnedToAFixedPrincipalArnIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"ArnEquals":{"aws:PrincipalArn":"arn:aws:iam::111122223333:role/app"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalPinnedToAFixedSourceAccountIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"StringEquals":{"aws:SourceAccount":"111122223333"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void forAnyValueWithFixedPrincipalOrgPathIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"ForAnyValue:StringLike":{"aws:PrincipalOrgPaths":
|
||||
["o-a1b2c3d4e5/r-ab12/ou-ab12-11111111/"]}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void forAllValuesOrWildcardOrgPathDoesNotNarrowPublicAccess() {
|
||||
for (String operator : new String[] {"ForAllValues:StringLike", "ForAnyValue:StringLike"}) {
|
||||
String path = operator.startsWith("ForAllValues")
|
||||
? "o-a1b2c3d4e5/r-ab12/ou-ab12-11111111/"
|
||||
: "o-a1b2c3d4e5/r-ab12/ou-ab12-11111111/*";
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"%s":{"aws:PrincipalOrgPaths":["%s"]}}}]}
|
||||
""".formatted(operator, path)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void accessPointNameWildcardWithFixedAccountIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"ArnLike":{"s3:DataAccessPointArn":
|
||||
"arn:aws:s3:us-west-2:123456789012:accesspoint/*"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void accessPointArnWithWildcardAccountIsPublic() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"ArnLike":{"s3:DataAccessPointArn":
|
||||
"arn:aws:s3:us-west-2:*:accesspoint/*"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void negatedOrOptionalConditionDoesNotMakePolicyNonPublic() {
|
||||
for (String operator : new String[] {"StringNotEquals", "StringEqualsIfExists", "IpAddress"}) {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*",
|
||||
"Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"%s":{"aws:SourceAccount":"123456789012"}}}]}
|
||||
""".formatted(operator)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnrecognisedConditionKeyDoesNotMakeAWildcardPrincipalPrivate() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"StringEquals":{"s3:x-amz-acl":"bucket-owner-full-control"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPolicyVariableIsNotAFixedValue() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"StringEquals":{"aws:userid":"${aws:userid}"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNarrowSourceIpRangeIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"IpAddress":{"aws:SourceIp":"203.0.113.0/24"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVeryBroadSourceIpRangeIsPublic() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow",
|
||||
"Condition":{"IpAddress":{"aws:SourceIp":"0.0.0.0/1"}}}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void onePublicStatementMakesTheWholePolicyPublic() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[
|
||||
{"Principal":{"Service":"cloudtrail.amazonaws.com"},"Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:PutObject","Effect":"Allow"},
|
||||
{"Principal":{"AWS":"arn:aws:iam::444455556666:root"},"Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:GetObject","Effect":"Allow"},
|
||||
{"Principal":"*","Resource":"arn:aws:s3:::b/*","Action":"s3:GetObject","Effect":"Allow"}
|
||||
]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removingThePublicStatementMakesThePolicyNonPublic() {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[
|
||||
{"Principal":{"Service":"cloudtrail.amazonaws.com"},"Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:PutObject","Effect":"Allow"},
|
||||
{"Principal":{"AWS":"arn:aws:iam::444455556666:root"},"Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:GetObject","Effect":"Allow"}
|
||||
]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notPrincipalAllowIsPublic() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"NotPrincipal":{"AWS":"arn:aws:iam::111122223333:root"},
|
||||
"Resource":"*","Action":"s3:GetObject","Effect":"Allow"}]}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSingleStatementObjectIsEvaluated() {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":{"Principal":"*","Resource":"*","Action":"s3:GetObject","Effect":"Allow"}}
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAbsentOrUnparseablePolicyIsNotPublic() {
|
||||
assertFalse(isPublic(null));
|
||||
assertFalse(isPublic(""));
|
||||
assertFalse(isPublic("not json"));
|
||||
}
|
||||
|
||||
private boolean isPublic(String policy) {
|
||||
return S3PublicAccessEvaluator.policyIsPublic(objectMapper, policy);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user