fix(iam): populate PrincipalIsAWSService for signed requests (#4695)

* fix(iam): set PrincipalIsAWSService for signed IAM requests

* fix(iam): apply global conditions to additional resources

* fix(iam): accept JSON booleans in Bool conditions
This commit is contained in:
XuJian authored and GitHub committed 2026-09-29 20:37:36 -04:00
1 parent 43ed79d15b
commit e6894119f4
11 files changed
+255 -15

No files matched your search

@@ -101,7 +101,9 @@ public class IamConditionContextResolver {
putIfPresent(conditions, "aws:ResourceAccount", resourceAccount);
putIfPresent(conditions, "aws:PrincipalAccount", accountId);
putIfPresent(conditions, "aws:RequestedRegion", region);
return conditions.isEmpty() ? null : conditions;
// Enforcement evaluates signed IAM credentials, not direct AWS service principals.
conditions.put("aws:PrincipalIsAWSService", List.of("false"));
return conditions;
}
private static String accountFromArn(String arn) {
@@ -727,22 +727,24 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
conditionContext.put("aws:PrincipalArn", List.of(principalArn.get()));
}
Map<String, List<String>> effectiveContext = conditionContext;
ResourcePolicyDecision effectiveDecision = resourcePolicyDecision;
String effectiveOwnerAccountId = resourceOwnerAccountId;
List<String> policyDocs = null;
if (effectiveDecision == null) {
List<ResourcePolicyProvider.ResourcePolicy> resourcePolicies = resolveResourcePolicies(
credentialScope, resource);
effectiveOwnerAccountId = resourcePolicies.isEmpty()
? null : resourcePolicies.getFirst().ownerAccountId();
List<String> policyDocs = resourcePolicies.stream()
policyDocs = resourcePolicies.stream()
.map(ResourcePolicyProvider.ResourcePolicy::policyDocument)
.filter(doc -> doc != null && !doc.isBlank())
.toList();
String region = requestContext.getRegion() == null
? config.defaultRegion() : requestContext.getRegion();
effectiveContext = IamConditionContextResolver.withGlobalContext(
conditionContext, resource, region, accountId, effectiveOwnerAccountId);
}
String region = requestContext.getRegion() == null
? config.defaultRegion() : requestContext.getRegion();
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
conditionContext, resource, region, accountId, effectiveOwnerAccountId);
if (effectiveDecision == null) {
effectiveDecision = evaluator.evaluateResourcePolicy(
policyDocs.isEmpty() ? null : policyDocs,
caller.principalArn(), action, resource, effectiveContext);
@@ -1230,8 +1230,12 @@ public class IamPolicyEvaluator {
Map<String, Map<String, List<String>>> result = new LinkedHashMap<>();
condNode.fields().forEachRemaining(opEntry -> {
Map<String, List<String>> kvMap = new LinkedHashMap<>();
opEntry.getValue().fields().forEachRemaining(kvEntry ->
kvMap.put(kvEntry.getKey(), nodeToList(kvEntry.getValue())));
boolean boolOperator = "Bool".equals(parseOperator(opEntry.getKey()).baseOp());
opEntry.getValue().fields().forEachRemaining(kvEntry -> {
JsonNode value = kvEntry.getValue();
kvMap.put(kvEntry.getKey(), boolOperator && value.isBoolean()
? List.of(value.asText()) : nodeToList(value));
});
result.put(opEntry.getKey(), kvMap);
});
return result.isEmpty() ? null : result;
@@ -481,14 +481,17 @@ final class S3PublicAccessEvaluator {
|| "ArnLike".equalsIgnoreCase(operator.getKey());
boolean exact = "StringEquals".equalsIgnoreCase(operator.getKey())
|| "ArnEquals".equalsIgnoreCase(operator.getKey());
if ((!glob && !exact) || !operator.getValue().isObject()) {
boolean bool = "Bool".equalsIgnoreCase(operator.getKey());
if ((!glob && !exact && !bool) || !operator.getValue().isObject()) {
return false;
}
Iterator<Map.Entry<String, JsonNode>> entries = operator.getValue().fields();
while (entries.hasNext()) {
Map.Entry<String, JsonNode> entry = entries.next();
String actual = contextValue(context, entry.getKey());
if (actual == null || !conditionValueMatches(entry.getValue(), actual, glob)) {
if (actual == null || !(bool
? booleanConditionMatches(entry.getValue(), actual)
: conditionValueMatches(entry.getValue(), actual, glob))) {
return false;
}
}
@@ -508,6 +511,25 @@ final class S3PublicAccessEvaluator {
return null;
}
private static boolean booleanConditionMatches(JsonNode expected, String actual) {
if (!"true".equalsIgnoreCase(actual) && !"false".equalsIgnoreCase(actual)) {
return false;
}
if (expected.isBoolean() || expected.isTextual()) {
String value = expected.asText();
return ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value))
&& value.equalsIgnoreCase(actual);
}
if (expected.isArray()) {
for (JsonNode item : expected) {
if (booleanConditionMatches(item, actual)) {
return true;
}
}
}
return false;
}
private static boolean conditionValueMatches(JsonNode expected, String actual, boolean glob) {
if (expected == null || expected.isNull()) {
return false;
@@ -1082,8 +1082,9 @@ public class S3Service implements Resettable, ResourceProvider {
}
Map<String, List<String>> conditionCtx = principalArn != null
? Map.of("aws:PrincipalArn", List.of(principalArn))
: Map.of();
? Map.of("aws:PrincipalArn", List.of(principalArn),
"aws:PrincipalIsAWSService", List.of("false"))
: Map.of("aws:PrincipalIsAWSService", List.of("false"));
ResourcePolicyDecision decision = policyEvaluator.evaluateResourcePolicy(
List.of(policy),
@@ -1164,7 +1165,8 @@ public class S3Service implements Resettable, ResourceProvider {
principalArn.get(),
action,
resourceArn,
Map.of("aws:PrincipalArn", principalArn.get()));
Map.of("aws:PrincipalArn", principalArn.get(),
"aws:PrincipalIsAWSService", "false"));
ResourcePolicyDecision decision = switch (policyEvaluation.decision()) {
case ALLOW -> policyEvaluation.directPrincipalAllow() && principalArn.get().contains(":user/")
? ResourcePolicyDecision.ALLOW_DIRECT_IAM_USER