fix(iam): read a policy value that is no string as naming nothing (#8313)

An action, a resource and a principal are written as strings: the policy grammar
spells each one out, as an <action_string>, a <resource_string> and a
<principal_id_string>. The evaluator read an element that was no string as
naming no value when it stood on its own and as the text of that value when it
sat in a list, so "Resource": 123 named no resource while "Resource": [123]
named the pattern 123.

A list is read like the bare element now, and an entry that is no string names
nothing. It tells where the element carves an exception out, since a statement
matches on whichever of the two elements it names: a NotAction or a NotResource
whose only entry was a number excluded that one text and so covered every action
or every resource there is, which made an Allow a grant over everything and a
Deny a refusal of everything. Naming no value leaves the statement matching
neither.

Condition values keep the reading they had. The grammar's JSON rules say
quotation marks "are optional for numeric and Boolean values", a lone number or
boolean was read as the text of the value already, and a list of them is read
that way still, so the two readings sit under names that say which is which.

Naming no value is a shape the access report had to learn as well.
deniesEveryRequest read a Deny that names no resource as one that bites on
every request, where matchesResource reads the same statement as matching
nothing, so an allow-everything beside a Deny whose Resource carries only a
number took the service off the Organizations access report although every
request still reached it. The report reads a statement the way the decision
does now: a Deny covers every request only when its Resource names *.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 15:18:10 -04:00
1 parent 66bec3b89b
commit bbcc58f03b
3 files changed
+177 -12

No files matched your search

@@ -791,7 +791,14 @@ public class IamPolicyEvaluator {
return false;
}
/** Whether a {@code Deny} applies to every request, rather than to some of them. */
/**
* Whether a {@code Deny} applies to every request, rather than to some of them. A statement
* reaches through whichever resource element it names, so one that names neither reaches no
* request at all: {@link #matchesResource} answers no for it, and this answers the same way
* rather than reading the absence as every resource. Taking an entry that is no string for no
* value is what leaves that shape, so the two readings have to agree, or a report would drop a
* service over a {@code Deny} that the decision it reports never applies.
*/
private static boolean deniesEveryRequest(PolicyStatement stmt) {
if (stmt.getConditions() != null && !stmt.getConditions().isEmpty()) {
return false;
@@ -800,7 +807,7 @@ public class IamPolicyEvaluator {
return false;
}
List<String> resources = stmt.getResources();
return resources == null || resources.isEmpty() || resources.contains("*");
return resources != null && resources.contains("*");
}
/** Appends the key name inside every {@code ${key}} policy variable found in {@code values}. */
@@ -1883,10 +1890,10 @@ public class IamPolicyEvaluator {
Map<String, List<String>> notPrincipals = parsePrincipals(stmt.get("NotPrincipal"));
// Action names are case-insensitive on AWS, so they are lowercased once here instead of
// on every request. Resource ARNs are case-sensitive on AWS and are kept as written.
List<String> actions = lowercaseAll(nodeToList(stmt.get("Action")));
List<String> notActions = lowercaseAll(nodeToList(stmt.get("NotAction")));
List<String> resources = nodeToList(stmt.get("Resource"));
List<String> notResources= nodeToList(stmt.get("NotResource"));
List<String> actions = lowercaseAll(patternValues(stmt.get("Action")));
List<String> notActions = lowercaseAll(patternValues(stmt.get("NotAction")));
List<String> resources = patternValues(stmt.get("Resource"));
List<String> notResources= patternValues(stmt.get("NotResource"));
Map<String, Map<String, List<String>>> conditions = parseConditions(stmt.get("Condition"));
return new PolicyStatement(
effect,
@@ -1911,12 +1918,12 @@ public class IamPolicyEvaluator {
return result;
}
if (node.isArray()) {
result.put("*", nodeToList(node));
result.put("*", patternValues(node));
return result;
}
if (node.isObject()) {
node.fields().forEachRemaining(entry -> {
List<String> values = nodeToList(entry.getValue());
List<String> values = patternValues(entry.getValue());
if (!values.isEmpty()) {
result.put(entry.getKey(), values);
}
@@ -1936,7 +1943,7 @@ public class IamPolicyEvaluator {
opEntry.getValue().fields().forEachRemaining(kvEntry -> {
JsonNode value = kvEntry.getValue();
kvMap.put(kvEntry.getKey(), value.isBoolean() || value.isNumber()
? List.of(value.asText()) : nodeToList(value));
? List.of(value.asText()) : conditionValues(value));
});
result.put(opEntry.getKey(), kvMap);
});
@@ -1951,7 +1958,43 @@ public class IamPolicyEvaluator {
return lowered;
}
private List<String> nodeToList(JsonNode node) {
/**
* The values a pattern element names. The grammar writes each of them as a string: an action is
* an {@code <action_string>}, a resource a {@code <resource_string>} and a principal a
* {@code <principal_id_string>}, so an entry that is no string names no value and is left out.
* A bare non-string element was read that way already, while a list carrying one took its text
* for a pattern, so {@code "Resource": 123} named no resource and {@code "Resource": [123]}
* named the pattern {@code 123}. The list is read like the bare element now.
*
* <p>It tells most where a pattern carves an exception out, since the element the statement
* matches on is whichever of the two it names: a {@code NotAction} or {@code NotResource} whose
* entries name nothing excludes nothing, and a statement excluding nothing covers every action
* or every resource there is. Naming no value leaves the statement matching neither instead.
*/
private List<String> patternValues(JsonNode node) {
List<String> values = new ArrayList<>();
if (node == null) {
return values;
}
if (node.isTextual()) {
values.add(node.asText());
} else if (node.isArray()) {
for (JsonNode item : node) {
if (item.isTextual()) {
values.add(item.asText());
}
}
}
return values;
}
/**
* The values a condition element names, where a number and a boolean are values of their own:
* the grammar's JSON rules say quotation marks "are optional for numeric and Boolean values",
* and an operator reads what it is given as the text of the value either way. A lone number or
* boolean is read by the caller, so what is left here is the list a key may carry.
*/
private List<String> conditionValues(JsonNode node) {
List<String> list = new ArrayList<>();
if (node == null) {
return list;