fix(iam): read an effect as written and a principal element that names nobody (#8402)

A statement's Effect was compared without regard to case, so "Effect":"allow"
granted and "Effect":"deny" overrode an allow, on a document no write of this
package would have stored: the write validator holds that element to the two
words the language defines, compared literally. The guide is explicit about the
element, "The Effect value is case sensitive", so the two words are the words
themselves here too, and a statement that spells one of them another way states
nothing, which is how an absent effect and one that is no string are already
read. The two service-reachability readers ask the statement for its effect
rather than spelling the word a third time, and denyStatementsOf, the one other
reader of the element in this package, collects a Deny written as Deny.

The principal half is an element that names nobody. A Principal or NotPrincipal
written as a string or a list came to a map carrying whatever the list named, an
empty one included, where the typed spelling already dropped a type whose
entries named nobody. It tells on NotPrincipal, the element whose statement
covers every caller it does not name: "NotPrincipal": [], or a list whose only
entry is no string, spared nobody and so covered everyone the statement
otherwise covered, an Allow written that way granting to any caller there is.
Each entry is written as a <principal_id_string>, so an element whose entries
are no strings names nobody, and a statement whose principal element names
nobody matches no caller now, which is how a NotAction or NotResource naming
nothing already leaves a statement matching neither. An element that is written
and names nobody is still the element the statement carries, so a statement
carrying both of them is decided by its Principal either way, that being the one
the grammar gives precedence, rather than by the NotPrincipal beside it.

S3 reads the same two elements in a pass of its own, the one that decides an
unsigned request, and it read both the old way: an effect without regard to
case, and any NotPrincipal that was there at all as an exclusion. Its readers
take the effect through one place that compares the words as written now, and a
NotPrincipal has to name somebody before it carves anybody out, so one bucket
policy answers a signed request and an anonymous one alike.

The Block Public Access status keeps the old question, and on purpose. It asks
what a policy could grant rather than whom a statement covers: S3 "begins by
assuming that the policy is public" and calls it non-public only when it grants
"only to fixed values" of a principal or of one of the recognised condition
keys. A NotPrincipal pins no principal to a fixed value whether it names
anybody or not, so a statement written that way leaves the policy public there
while granting nobody anything in the readers that decide a request. The effect
is the one element that reads alike everywhere, the guide's case rule being
about the element itself.

Every half reads a document already stored or supplied rather than checking one
being written: no code and no status moves, and nothing is refused that was not
refused before. A Deny whose NotPrincipal names nobody denies nobody for the
same reason an Allow written that way grants nobody, which is this emulator's
own reading of an element AWS leaves undefined.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 00:00:10 -04:00
1 parent 4a70910c53
commit f9ed48de2a
10 files changed
+286 -41

No files matched your search

@@ -1064,6 +1064,55 @@ class IamPolicyEvaluatorTest {
assertEquals(ResourcePolicyDecision.ALLOW, resourceDecision(allButAlice, RequestPrincipal.anonymous()));
}
@ParameterizedTest
@ValueSource(strings = {
"\"NotPrincipal\":[]",
"\"NotPrincipal\":[123]",
"\"NotPrincipal\":{}",
"\"NotPrincipal\":{\"AWS\":[]}",
"\"NotPrincipal\":{\"AWS\":123}",
})
void aNotPrincipalThatNamesNobodyCoversNobody(String notPrincipal) {
// A principal is written as a string, so an element whose entries are no strings names
// nobody, as a NotAction or NotResource naming nothing excludes nothing and so matches
// nothing. An Allow sparing nobody would otherwise cover every caller there is.
RequestPrincipal alice = RequestPrincipal.iam("arn:aws:iam::123456789012:user/alice");
assertEquals(ResourcePolicyDecision.NEUTRAL,
resourceDecision(resourcePolicy("Allow", notPrincipal), alice));
assertEquals(ResourcePolicyDecision.NEUTRAL,
resourceDecision(resourcePolicy("Allow", notPrincipal), RequestPrincipal.anonymous()));
assertEquals(ResourcePolicyDecision.NEUTRAL,
resourceDecision(resourcePolicy("Deny", notPrincipal), alice));
}
@Test
void aPrincipalThatNamesNobodyDecidesTheStatementItIsWrittenIn() {
// A statement carrying both elements is a shape the grammar does not describe, and the
// Principal decides it either way, so one naming nobody leaves the statement matching
// nobody rather than handing the decision to the NotPrincipal beside it.
String both = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":[],"
+ "\"NotPrincipal\":{\"AWS\":\"arn:aws:iam::123456789012:user/alice\"},"
+ "\"Action\":\"*\",\"Resource\":\"*\"}]}";
assertEquals(ResourcePolicyDecision.NEUTRAL,
resourceDecision(both, RequestPrincipal.iam("arn:aws:iam::123456789012:user/alice")));
assertEquals(ResourcePolicyDecision.NEUTRAL,
resourceDecision(both, RequestPrincipal.iam("arn:aws:iam::123456789012:user/bob")));
assertEquals(ResourcePolicyDecision.NEUTRAL, resourceDecision(both, RequestPrincipal.anonymous()));
}
@Test
void thePrincipalsNamedBesideAnEntryThatIsNoStringAreStillNamed() {
String allButAlice = resourcePolicy("Allow",
"\"NotPrincipal\":{\"AWS\":[123,\"arn:aws:iam::123456789012:user/alice\"]}");
assertEquals(ResourcePolicyDecision.NEUTRAL,
resourceDecision(allButAlice, RequestPrincipal.iam("arn:aws:iam::123456789012:user/alice")));
assertEquals(ResourcePolicyDecision.ALLOW,
resourceDecision(allButAlice, RequestPrincipal.iam("arn:aws:iam::123456789012:user/bob")));
}
@Test
void evaluatesResourcePolicyConditionsAndIdentityParity() {
String condPolicy = """
@@ -102,4 +102,39 @@ class IamStatementEffectTest {
assertEquals(List.of("s3"), reachable);
}
@ParameterizedTest
@ValueSource(strings = {"allow", "ALLOW", "aLLoW"})
void anAllowSpelledInAnotherCaseStatesNothing(String effect) {
// IAM User Guide, "AWS JSON policy elements: Effect": "The Effect value is case
// sensitive". The write validator of this package compares the word as written too, so a
// document carrying another spelling was never stored through a write here.
String document = identityStatement("\"Effect\":\"" + effect + "\",");
assertEquals(Decision.DENY, evaluator.evaluate(
CallerContext.of(List.of(document)), null, "s3:GetObject", "arn:aws:s3:::b/k", null));
assertTrue(evaluator.servicesGrantedBy(List.of(document)).isEmpty());
assertTrue(evaluator.servicesAllowedByScpLevels(List.of(List.of(document))).isEmpty());
}
@ParameterizedTest
@ValueSource(strings = {"deny", "DENY", "dENy"})
void aDenySpelledInAnotherCaseOverridesNothing(String effect) {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":["
+ "{\"Effect\":\"Allow\",\"Action\":\"s3:*\",\"Resource\":\"*\"},"
+ "{\"Effect\":\"" + effect + "\",\"Action\":\"s3:*\",\"Resource\":\"*\"}]}";
assertEquals(Decision.ALLOW, evaluator.evaluate(
CallerContext.of(List.of(document)), null, "s3:GetObject", "arn:aws:s3:::b/k", null));
}
@Test
void aResourcePolicyStatementIsHeldToTheSameTwoSpellings() {
String bucketPolicy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"allow\","
+ "\"Principal\":\"*\",\"Action\":\"s3:*\",\"Resource\":\"*\"}]}";
assertEquals(ResourcePolicyDecision.NEUTRAL, evaluator.evaluateResourcePolicy(
List.of(bucketPolicy), "arn:aws:iam::123456789012:user/alice", "s3:GetObject",
"arn:aws:s3:::b/k", null));
}
}
@@ -3,6 +3,7 @@ package io.floci.aws.services.s3;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static io.floci.aws.services.s3.S3PublicAccessEvaluator.PublicAccessDecision.ALLOW;
@@ -196,6 +197,38 @@ class S3PublicAccessEvaluatorTest {
"arn:aws:cloudfront::000000000000:distribution/EBLOCKED")));
}
@Test
void anEffectSpelledInAnotherCaseDoesNotAllowAnonymousRead() {
String policy = """
{"Version":"2012-10-17","Statement":{
"Effect":"allow",
"Principal":"*",
"Action":"s3:GetObject",
"Resource":"arn:aws:s3:::public-bucket/*"
}}""";
assertEquals(NEUTRAL, S3PublicAccessEvaluator.publicPolicyDecision(
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN));
assertEquals(NEUTRAL, S3PublicAccessEvaluator.principalPolicyDecision(
OBJECT_MAPPER, policy, "AWS", "arn:aws:iam::123456789012:user/alice",
"s3:GetObject", OBJECT_ARN, Map.of()));
}
@Test
void aNotPrincipalThatNamesNobodyDoesNotAllowAnonymousRead() {
for (String notPrincipal : List.of("[]", "[123]", "{}", "{\"AWS\":[]}")) {
String policy = "{\"Version\":\"2012-10-17\",\"Statement\":{\"Effect\":\"Allow\","
+ "\"NotPrincipal\":" + notPrincipal + ","
+ "\"Action\":\"s3:GetObject\",\"Resource\":\"arn:aws:s3:::public-bucket/*\"}}";
assertEquals(NEUTRAL, S3PublicAccessEvaluator.publicPolicyDecision(
OBJECT_MAPPER, policy, "s3:GetObject", OBJECT_ARN), notPrincipal);
assertEquals(NEUTRAL, S3PublicAccessEvaluator.principalPolicyDecision(
OBJECT_MAPPER, policy, "AWS", "arn:aws:iam::123456789012:user/alice",
"s3:GetObject", OBJECT_ARN, Map.of()), notPrincipal);
}
}
@Test
void missingEffectDoesNotAllowAnonymousRead() {
String policy = """
@@ -3,6 +3,8 @@ package io.floci.aws.services.s3;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -203,6 +205,27 @@ class S3PublicPolicyStatusTest {
"""));
}
@Test
void anAllowSpelledInAnotherCaseIsNotPublic() {
assertFalse(isPublic("""
{"Statement":[{"Principal":"*","Resource":"*","Action":"s3:PutObject","Effect":"allow"}]}
"""));
}
@Test
void aNotPrincipalThatNamesNobodyIsStillPublic() {
// S3 "begins by assuming that the policy is public" and calls it non-public only when it
// grants "only to fixed values" of a principal or a recognised condition key (S3 User
// Guide, "The meaning of public"). A NotPrincipal pins no principal to a fixed value, so
// the status stays public even where the readers that decide a request, held to who the
// statement covers, grant nobody anything.
for (String notPrincipal : List.of("[]", "[123]", "{}", "{\"AWS\":[]}")) {
assertTrue(isPublic("{\"Statement\":[{\"NotPrincipal\":" + notPrincipal
+ ",\"Resource\":\"*\",\"Action\":\"s3:GetObject\",\"Effect\":\"Allow\"}]}"),
notPrincipal);
}
}
@Test
void notPrincipalWildcardIsNotPublic() {
assertFalse(isPublic("""