fix(s3): allow resource wildcards in fixed-account ARN conditions under BlockPublicPolicy (#8500)

This commit is contained in:
Koushik Dutta authored and GitHub committed 2026-11-17 09:16:56 -04:00
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)