mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): allow resource wildcards in fixed-account ARN conditions under BlockPublicPolicy (#8500)
This commit is contained in:
1 parent
91943a061c
commit
c6faa73eb7
6 files changed
+91
-6
No files matched your search
@@ -270,10 +270,10 @@ final class DsqlClusterPolicies {
|
||||
return switch (operator) {
|
||||
case "StringEquals", "StringEqualsIgnoreCase" -> values.stream().noneMatch(value -> value.contains("${"));
|
||||
case "StringLike" -> values.stream().allMatch(value -> ARN_KEYS.contains(key)
|
||||
? PublicAccessConditions.pinsAccount(value) && !value.contains("${")
|
||||
? PublicAccessConditions.isFixedAccountArn(value)
|
||||
: PublicAccessConditions.isFixedValue(value));
|
||||
case "ArnEquals", "ArnLike" -> values.stream().allMatch(value -> value.split(":", 6).length < 6
|
||||
|| PublicAccessConditions.pinsAccount(value) && !value.contains("${"));
|
||||
|| PublicAccessConditions.isFixedAccountArn(value));
|
||||
case "Bool" -> PRINCIPAL_IS_AWS_SERVICE.equals(key) && values.stream().allMatch("true"::equalsIgnoreCase);
|
||||
default -> false;
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* What a resource policy's condition has to say for an {@code Allow} to everyone not to count as
|
||||
@@ -16,6 +17,7 @@ import java.util.Set;
|
||||
public final class PublicAccessConditions {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(PublicAccessConditions.class);
|
||||
private static final Pattern ACCOUNT_ID = Pattern.compile("[0-9]{12}");
|
||||
|
||||
/**
|
||||
* The trusted condition keys of IAM's block public access definition that Lambda and DSQL both
|
||||
@@ -51,9 +53,15 @@ public final class PublicAccessConditions {
|
||||
return value.indexOf('*') >= 0 || value.indexOf('?') >= 0;
|
||||
}
|
||||
|
||||
/** An ARN of six fields whose account has no wildcard. */
|
||||
/** An ARN of six fields with no wildcard, or with a 12-digit account when it has one. */
|
||||
public static boolean pinsAccount(String arn) {
|
||||
return AwsArnUtils.isArn(arn) && !hasWildcard(AwsArnUtils.parse(arn).accountId());
|
||||
return AwsArnUtils.isArn(arn)
|
||||
&& (!hasWildcard(arn) || ACCOUNT_ID.matcher(AwsArnUtils.parse(arn).accountId()).matches());
|
||||
}
|
||||
|
||||
/** An ARN that pins its account and has no policy variable in it. */
|
||||
public static boolean isFixedAccountArn(String arn) {
|
||||
return pinsAccount(arn) && !arn.contains("${");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -41,6 +41,7 @@ final class S3PublicAccessEvaluator {
|
||||
/** Recognised like the keys above, but the value additionally has to be a narrow range. */
|
||||
private static final String SOURCE_IP_CONDITION_KEY = "aws:sourceip";
|
||||
private static final String DATA_ACCESS_POINT_ARN_CONDITION_KEY = "s3:dataaccesspointarn";
|
||||
private static final String PRINCIPAL_ARN_CONDITION_KEY = "aws:principalarn";
|
||||
private static final Pattern BUCKET_POLICY_ACCESS_POINT_ARN = Pattern.compile(
|
||||
"arn:[a-z0-9-]+:s3:[a-z0-9-]+:[0-9]{12}:accesspoint/[a-zA-Z0-9*?._-]+");
|
||||
|
||||
@@ -187,7 +188,8 @@ final class S3PublicAccessEvaluator {
|
||||
* containing neither a wildcard nor an IAM policy variable. A single public statement makes
|
||||
* the whole policy public, which is what {@code RestrictPublicBuckets} keys off.
|
||||
*
|
||||
* <p>Bucket policies can use a wildcard in the access point name when the account is fixed.
|
||||
* <p>Bucket policies can use a wildcard in the resource part of {@code aws:PrincipalArn} or
|
||||
* the access point name of {@code s3:DataAccessPointArn} when the 12-digit account is fixed.
|
||||
* Floci does not model access points, so the different access-point-policy rule does not apply.
|
||||
*/
|
||||
static boolean policyIsPublic(ObjectMapper objectMapper, String policy) {
|
||||
@@ -244,7 +246,9 @@ final class S3PublicAccessEvaluator {
|
||||
}
|
||||
if (NON_PUBLIC_CONDITION_KEYS.contains(key)
|
||||
&& !operatorName.equalsIgnoreCase("IpAddress")
|
||||
&& allValuesFixed(entry.getValue(), PublicAccessConditions::isFixedValue)) {
|
||||
&& allValuesFixed(entry.getValue(), PRINCIPAL_ARN_CONDITION_KEY.equals(key)
|
||||
? PublicAccessConditions::isFixedAccountArn
|
||||
: PublicAccessConditions::isFixedValue)) {
|
||||
return true;
|
||||
}
|
||||
if (SOURCE_IP_CONDITION_KEY.equals(key)
|
||||
|
||||
@@ -24,14 +24,29 @@ class PublicAccessConditionsTest {
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
"arn:aws:iam::123456789012:role/app|true",
|
||||
"arn:aws:iam::123456789012:role/*|true",
|
||||
"arn:aws:s3:::my-bucket|true",
|
||||
"arn:aws:iam::*:role/app|false",
|
||||
"arn:aws:iam::12345678901?:root|false",
|
||||
"arn:aws:s3:::*|false",
|
||||
"arn:aws:iam::123456789012|false",
|
||||
})
|
||||
void anArnPinsItsAccountWhenTheAccountHasNoWildcard(String arn, boolean pins) {
|
||||
assertEquals(pins, PublicAccessConditions.pinsAccount(arn));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
"arn:aws:iam::123456789012:role/app|true",
|
||||
"arn:aws:iam::123456789012:role/*|true",
|
||||
"arn:aws:s3:::my-bucket|true",
|
||||
"arn:aws:iam::123456789012:role/${aws:username}|false",
|
||||
"arn:aws:iam::*:role/app|false",
|
||||
"arn:aws:s3:::*|false",
|
||||
})
|
||||
void aFixedAccountArnPinsItsAccountWithoutPolicyVariables(String arn, boolean fixed) {
|
||||
assertEquals(fixed, PublicAccessConditions.isFixedAccountArn(arn));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
"203.0.113.7|true",
|
||||
|
||||
@@ -69,6 +69,35 @@ class S3BlockPublicAccessIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicPolicyAcceptsFixedAccountPrincipalArnWildcard() {
|
||||
String bucket = createBucket();
|
||||
putBucketPublicAccessBlock(bucket, true, true, true, true);
|
||||
String fixedAccountWildcardPolicy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"AWS":"*"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*",
|
||||
"Condition":{"ArnLike":{"aws:PrincipalArn":
|
||||
"arn:aws:iam::123456789012:role/app-*"}}}]}
|
||||
""".formatted(bucket);
|
||||
String wildcardAccountPolicy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"AWS":"*"},"Action":"s3:GetObject","Resource":"arn:aws:s3:::%s/*",
|
||||
"Condition":{"ArnLike":{"aws:PrincipalArn":
|
||||
"arn:aws:iam::*:role/app-*"}}}]}
|
||||
""".formatted(bucket);
|
||||
|
||||
given().body(fixedAccountWildcardPolicy)
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(200);
|
||||
given().when().get("/" + bucket + "?policyStatus")
|
||||
.then().statusCode(200)
|
||||
.body(containsString("<IsPublic>false</IsPublic>"));
|
||||
given().body(wildcardAccountPolicy)
|
||||
.when().put("/" + bucket + "?policy")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void blockPublicPolicyAcceptsForAnyValueWithFixedPrincipalOrgPath() {
|
||||
String bucket = createBucket();
|
||||
|
||||
@@ -79,6 +79,35 @@ class S3PublicPolicyStatusTest {
|
||||
"""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void principalArnResourceWildcardWithFixedAccountIsNotPublic() {
|
||||
for (String condition : List.of(
|
||||
"\"ArnLike\":{\"aws:PrincipalArn\":\"arn:aws:iam::111122223333:role/app-*\"}",
|
||||
"\"StringLike\":{\"aws:PrincipalArn\":\"arn:aws:iam::111122223333:role/app-*\"}")) {
|
||||
assertFalse(isPublic("""
|
||||
{"Statement":[{"Principal":{"AWS":"*"},"Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:GetObject","Effect":"Allow","Condition":{%s}}]}
|
||||
""".formatted(condition)), condition);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void principalOrSourceArnWithWildcardAccountOrUnfixedSourceArnIsPublic() {
|
||||
for (String condition : List.of(
|
||||
"\"ArnLike\":{\"aws:PrincipalArn\":\"arn:aws:iam::*:role/app-*\"}",
|
||||
"\"ArnLike\":{\"aws:PrincipalArn\":\"arn:aws:iam::11112222333?:role/app\"}",
|
||||
"\"ArnLike\":{\"aws:PrincipalArn\":\"arn:aws:iam:::role/app-*\"}",
|
||||
"\"ArnLike\":{\"aws:PrincipalArn\":\"*\"}",
|
||||
"\"ArnLike\":{\"aws:PrincipalArn\":\"arn:aws:iam::111122223333:role/${aws:username}\"}",
|
||||
"\"ArnLike\":{\"aws:SourceArn\":\"arn:aws:s3:::*\"}",
|
||||
"\"ArnLike\":{\"aws:SourceArn\":\"arn:aws:lambda:us-east-1:111122223333:function:app-*\"}")) {
|
||||
assertTrue(isPublic("""
|
||||
{"Statement":[{"Principal":{"AWS":"*"},"Resource":"arn:aws:s3:::b/*",
|
||||
"Action":"s3:GetObject","Effect":"Allow","Condition":{%s}}]}
|
||||
""".formatted(condition)), condition);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardPrincipalPinnedToAFixedSourceAccountIsNotPublic() {
|
||||
assertFalse(isPublic("""
|
||||
|
||||
Reference in new issue
Block a user