mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
1 parent
4a70910c53
commit
f9ed48de2a
10 files changed
+286
-41
No files matched your search
@@ -2586,9 +2586,9 @@ AWS checks as `dynamodb:GetItem`, is checked item by item, as described under
|
||||
|
||||
When `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED` is active, Floci also queries registered `ResourcePolicyProvider` SPI implementations (such as S3 bucket policies) during request authorization:
|
||||
|
||||
- Resource policy statements are matched against the caller (`Principal` and `NotPrincipal` clauses), each principal type naming only its own kind of caller, as on AWS: `"*"` matches anyone; `{"AWS": "*"}` matches IAM identities and AWS services; any other `AWS` entry (a user, role, account id or account root) matches IAM identities only, and a role session matches a `Principal` naming its role through the role's own ARN, path included; `{"Service": "<name>"}` matches that service exactly. `{"CanonicalUser": "<id>"}`, which S3 bucket policies accept, names an account by its S3 canonical user ID and matches that account's IAM identities, as an account principal does; Floci derives that ID from the account ID as its SHA-256 hex digest, the 64-hex value its S3 ACLs report, and still accepts the account ID itself for policies written before it derived one. `{"Service": "*"}`, which AWS does not accept, matches nothing, and `Federated` entries never match an IAM caller.
|
||||
- Resource policy statements are matched against the caller (`Principal` and `NotPrincipal` clauses), each principal type naming only its own kind of caller, as on AWS: `"*"` matches anyone; `{"AWS": "*"}` matches IAM identities and AWS services; any other `AWS` entry (a user, role, account id or account root) matches IAM identities only, and a role session matches a `Principal` naming its role through the role's own ARN, path included; `{"Service": "<name>"}` matches that service exactly. `{"CanonicalUser": "<id>"}`, which S3 bucket policies accept, names an account by its S3 canonical user ID and matches that account's IAM identities, as an account principal does; Floci derives that ID from the account ID as its SHA-256 hex digest, the 64-hex value its S3 ACLs report, and still accepts the account ID itself for policies written before it derived one. `{"Service": "*"}`, which AWS does not accept, matches nothing, and `Federated` entries never match an IAM caller. Each entry is written as a string, so a principal element whose entries are no strings names nobody at all, and a statement whose principal element names nobody matches no caller. A statement carrying both elements is a shape the grammar does not describe, and the `Principal` it names decides it either way, one naming nobody included.
|
||||
- An explicit **Deny** in a resource policy overrides any allows.
|
||||
- A Deny statement's `NotPrincipal` spares an IAM caller only when it names the caller's account as well as the caller itself, the user, role or role session (the account alone for the account root), and never spares a caller that has a permissions boundary attached, as the IAM User Guide's "AWS JSON policy elements: NotPrincipal" describes: without the account, AWS "might deny access to the entire account", and with a boundary it "will always deny". An Allow with `NotPrincipal`, which Access Analyzer warns can grant to anonymous callers, grants everyone it does not name.
|
||||
- A Deny statement's `NotPrincipal` spares an IAM caller only when it names the caller's account as well as the caller itself, the user, role or role session (the account alone for the account root), and never spares a caller that has a permissions boundary attached, as the IAM User Guide's "AWS JSON policy elements: NotPrincipal" describes: without the account, AWS "might deny access to the entire account", and with a boundary it "will always deny". An Allow with `NotPrincipal`, which Access Analyzer warns can grant to anonymous callers, grants everyone it does not name, and one that names nobody spares nobody and so covers nobody, rather than covering every caller there is; a Deny written that way denies nobody for the same reason.
|
||||
- In cross-account scenarios or resource-controlled access, an explicit **Allow** in a resource policy grants access to the principal.
|
||||
- A statement that names the caller only through its account, by its id, its root ARN or its S3 canonical user ID, delegates to the account, as the IAM User Guide's "AWS account principals" describes: across accounts it is the resource owner's half of the grant, and within the account it grants nothing on its own, so the caller's identity policies have to allow the request. A statement naming the user, role or role session itself, or `"*"`, grants on its own within the account.
|
||||
- For detailed S3 bucket policy behavior and configuration, see [S3 Bucket Policy Enforcement](s3.md#bucket-policy-enforcement).
|
||||
@@ -2878,7 +2878,10 @@ account key carries no identity policies of its own.
|
||||
resolved from the request context.
|
||||
- **Effects**: `Allow` and `Deny`. A statement whose `Effect` names neither, because it is absent,
|
||||
is not a string, or is some other word, states nothing: it grants nothing, denies nothing, and
|
||||
leaves the request to the rest of the document.
|
||||
leaves the request to the rest of the document. The two words are read as written, the IAM User
|
||||
Guide saying of the element that "The `Effect` value is case sensitive", so `"Effect": "allow"`
|
||||
states nothing here, as it is also refused by a write of this package, and a `Deny` spelled in
|
||||
another case overrides nothing.
|
||||
|
||||
#### Policy variables
|
||||
|
||||
|
||||
+3
-1
@@ -450,7 +450,9 @@ Bucket policy statements can specify principals using `Principal` or `NotPrincip
|
||||
- **AWS Account:** `{"AWS": "<12-digit-account-id>"}` or `{"AWS": "arn:aws:iam::<account>:root"}` matches any principal belonging to the specified account.
|
||||
- **Canonical user:** `{"CanonicalUser": "<canonical-id>"}` names an account by its S3 canonical user ID and matches any principal belonging to that account, as an account principal does. Floci derives the canonical ID deterministically from the account ID as its SHA-256 hex digest, a 64-character lowercase hex string, and reports it wherever S3 identifies an owner: the `Owner` of `ListBuckets`, `ListParts` and both ACLs, the `Initiator` of `ListParts`, and every `CanonicalUser` grantee of an ACL Floci generates. The account ID itself still matches, for policies written when Floci reported the account as the canonical ID.
|
||||
- **Service Principal:** `{"Service": "<service>.amazonaws.com"}`.
|
||||
- **NotPrincipal:** Inverts matching so the statement applies to any principal not matching the specified patterns. On a `Deny`, an IAM caller is spared only when the list names its account as well as the caller, and never when it has a permissions boundary attached; see [Resource-based policies](iam.md#resource-based-policies).
|
||||
- **NotPrincipal:** Inverts matching so the statement applies to any principal not matching the specified patterns. On a `Deny`, an IAM caller is spared only when the list names its account as well as the caller, and never when it has a permissions boundary attached; see [Resource-based policies](iam.md#resource-based-policies). Each entry is written as a string, so a `NotPrincipal` whose entries are no strings, an empty list among them, names nobody: it carves nobody out, so the statement covers nobody rather than everyone, on an anonymous request and a signed one alike. Block Public Access reads it the other way round, and deliberately: S3 "begins by assuming that the policy is public" and calls a policy non-public only when it grants "only to fixed values" of a principal or of one of the condition keys below, so a `NotPrincipal` leaves the policy public whether it names anybody or not.
|
||||
|
||||
Every check against a bucket policy, the anonymous paths and the Block Public Access status included, holds a statement's `Effect` to `Allow` or `Deny` as written, the IAM User Guide saying that "The `Effect` value is case sensitive", so a statement spelling either word another way states nothing and one bucket policy answers a signed request and an unsigned one the same way.
|
||||
|
||||
The same matching applies to every check S3 makes against a bucket policy, including the source of `CopyObject` and `UploadPartCopy`.
|
||||
|
||||
|
||||
@@ -506,7 +506,7 @@ public class IamPolicyEvaluator {
|
||||
return false;
|
||||
}
|
||||
for (PolicyStatement stmt : parseAll(List.of(policyDocument))) {
|
||||
if (!"Allow".equalsIgnoreCase(stmt.getEffect())) {
|
||||
if (!stmt.isAllow()) {
|
||||
continue;
|
||||
}
|
||||
if (stmt.getActions() != null) {
|
||||
@@ -543,7 +543,7 @@ public class IamPolicyEvaluator {
|
||||
public List<String> servicesGrantedBy(List<String> policyDocuments) {
|
||||
Set<String> granted = new TreeSet<>();
|
||||
for (PolicyStatement stmt : parseAll(policyDocuments)) {
|
||||
if (!"Allow".equalsIgnoreCase(stmt.getEffect())) {
|
||||
if (!stmt.isAllow()) {
|
||||
continue;
|
||||
}
|
||||
if (stmt.getNotActions() != null) {
|
||||
@@ -1057,18 +1057,24 @@ public class IamPolicyEvaluator {
|
||||
return match;
|
||||
}
|
||||
|
||||
/** A {@code NotPrincipal} statement applies to every caller it does not spare. */
|
||||
/**
|
||||
* A {@code NotPrincipal} statement applies to every caller it does not spare. A {@code
|
||||
* Principal} the statement carries decides it instead, naming nobody included, and an element
|
||||
* that names nobody carves nobody out, so the statement covers no caller.
|
||||
*/
|
||||
private PrincipalMatch principalMatch(PolicyStatement stmt, RequestPrincipal principal) {
|
||||
if (stmt.getPrincipals() != null) {
|
||||
return matchAnyPrincipal(stmt.getPrincipals(), principal);
|
||||
Map<String, List<String>> principals = stmt.getPrincipals();
|
||||
if (principals != null) {
|
||||
return matchAnyPrincipal(principals, principal);
|
||||
}
|
||||
if (stmt.getNotPrincipals() != null) {
|
||||
boolean spared = stmt.isDeny()
|
||||
? sparedFromDeny(stmt.getNotPrincipals(), principal)
|
||||
: matchAnyPrincipal(stmt.getNotPrincipals(), principal) != PrincipalMatch.NONE;
|
||||
return spared ? PrincipalMatch.NONE : PrincipalMatch.CALLER;
|
||||
Map<String, List<String>> notPrincipals = stmt.getNotPrincipals();
|
||||
if (notPrincipals == null || notPrincipals.isEmpty()) {
|
||||
return PrincipalMatch.NONE;
|
||||
}
|
||||
return PrincipalMatch.NONE;
|
||||
boolean spared = stmt.isDeny()
|
||||
? sparedFromDeny(notPrincipals, principal)
|
||||
: matchAnyPrincipal(notPrincipals, principal) != PrincipalMatch.NONE;
|
||||
return spared ? PrincipalMatch.NONE : PrincipalMatch.CALLER;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1884,7 +1890,10 @@ public class IamPolicyEvaluator {
|
||||
// nothing. An Effect that is absent, is not a string, or names neither is read as neither:
|
||||
// the statement grants nothing, denies nothing, and leaves the request to the rest of the
|
||||
// document. Reading an absent Effect as an Allow would grant on a statement that describes
|
||||
// no permission.
|
||||
// no permission. The two words are the words themselves, "The Effect value is case
|
||||
// sensitive" (IAM User Guide, "AWS JSON policy elements: Effect"), which is the comparison
|
||||
// IamPolicyDocuments makes of a document being written, so "allow" states nothing here
|
||||
// rather than granting what no write of this package would have stored.
|
||||
String effect = stmt.path("Effect").isTextual() ? stmt.get("Effect").asText() : "";
|
||||
Map<String, List<String>> principals = parsePrincipals(stmt.get("Principal"));
|
||||
Map<String, List<String>> notPrincipals = parsePrincipals(stmt.get("NotPrincipal"));
|
||||
@@ -1908,29 +1917,47 @@ public class IamPolicyEvaluator {
|
||||
stmt.path("Sid").isTextual() ? stmt.get("Sid").asText() : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The callers a {@code Principal} or {@code NotPrincipal} element names, by principal type.
|
||||
* Each of them is written as a {@code <principal_id_string>}, so an entry that is no string
|
||||
* names nobody, as {@link #patternValues} reads the action and resource patterns beside it. An
|
||||
* element naming nobody at all is no principal element rather than one naming an empty list,
|
||||
* which the typed spelling already came to by dropping a type whose entries named nobody and
|
||||
* the string and list spellings are read the same way here.
|
||||
*
|
||||
* <p>Where it tells is {@code NotPrincipal}, the element whose statement covers every caller it
|
||||
* does not name: one naming nobody spared nobody, so an {@code Allow} whose {@code
|
||||
* NotPrincipal} was an empty list, or a list of entries that are no strings, granted to anyone
|
||||
* the statement otherwise covered. A statement whose principal element names nobody matches no
|
||||
* caller now, which is how a {@code NotAction} or {@code NotResource} naming nothing already
|
||||
* leaves a statement matching neither.
|
||||
*
|
||||
* <p>An element that is written and names nobody is still the element the statement carries, so
|
||||
* this answers an empty map rather than null and only an absent element answers null. A
|
||||
* statement carrying both elements is a shape the grammar does not describe, and the {@code
|
||||
* Principal} it names decides it either way, so writing that one as an empty list leaves the
|
||||
* statement matching nobody instead of handing the decision to the {@code NotPrincipal} beside
|
||||
* it.
|
||||
*/
|
||||
private Map<String, List<String>> parsePrincipals(JsonNode node) {
|
||||
if (node == null || node.isNull() || node.isMissingNode()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, List<String>> result = new LinkedHashMap<>();
|
||||
if (node.isTextual()) {
|
||||
result.put("*", List.of(node.asText()));
|
||||
return result;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
result.put("*", patternValues(node));
|
||||
return result;
|
||||
}
|
||||
if (node.isObject()) {
|
||||
if (node.isTextual() || node.isArray()) {
|
||||
List<String> values = patternValues(node);
|
||||
if (!values.isEmpty()) {
|
||||
result.put("*", values);
|
||||
}
|
||||
} else if (node.isObject()) {
|
||||
node.fields().forEachRemaining(entry -> {
|
||||
List<String> values = patternValues(entry.getValue());
|
||||
if (!values.isEmpty()) {
|
||||
result.put(entry.getKey(), values);
|
||||
}
|
||||
});
|
||||
return result.isEmpty() ? null : result;
|
||||
}
|
||||
return null;
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Map<String, List<String>>> parseConditions(JsonNode condNode) {
|
||||
|
||||
@@ -5379,7 +5379,11 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
return arn != null && "root".equals(AwsArnUtils.parse(arn).resource());
|
||||
}
|
||||
|
||||
/** One policy document holding only the Deny statements of a user's policies and boundary. */
|
||||
/**
|
||||
* One policy document holding only the Deny statements of a user's policies and boundary. A
|
||||
* statement is a Deny when its {@code Effect} is that word as written, which is the reading
|
||||
* the policy evaluator makes of the same element.
|
||||
*/
|
||||
private String denyStatementsOf(String userName) {
|
||||
List<String> documents = new ArrayList<>();
|
||||
List<String> userPolicies = collectUserPolicies(userName);
|
||||
@@ -5395,7 +5399,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
try {
|
||||
JsonNode statements = POLICY_MAPPER.readTree(document).path("Statement");
|
||||
for (JsonNode statement : statements.isArray() ? statements : List.of(statements)) {
|
||||
if ("Deny".equalsIgnoreCase(statement.path("Effect").asText())) {
|
||||
if ("Deny".equals(statement.path("Effect").asText())) {
|
||||
denies.add(statement);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,13 @@ public class PolicyStatement {
|
||||
public String getVersion() { return version; }
|
||||
public String getSid() { return sid; }
|
||||
|
||||
public boolean isDeny() { return "Deny".equalsIgnoreCase(effect); }
|
||||
public boolean isAllow() { return "Allow".equalsIgnoreCase(effect); }
|
||||
/**
|
||||
* Whether the statement states that effect. The two words are compared as written, the IAM
|
||||
* User Guide saying of the element that "The {@code Effect} value is case sensitive", so a
|
||||
* statement whose effect is spelled in another case names neither of them and states nothing.
|
||||
* It is the comparison the write validator of this package makes of a document on its way in.
|
||||
*/
|
||||
public boolean isDeny() { return "Deny".equals(effect); }
|
||||
|
||||
public boolean isAllow() { return "Allow".equals(effect); }
|
||||
}
|
||||
@@ -53,6 +53,62 @@ final class S3PublicAccessEvaluator {
|
||||
private S3PublicAccessEvaluator() {
|
||||
}
|
||||
|
||||
/**
|
||||
* The effect a statement states, or the empty string when it states neither of them. The two
|
||||
* words are the words as written, the IAM User Guide saying of the element that "The {@code
|
||||
* Effect} value is case sensitive", which is the reading the policy evaluator makes of the same
|
||||
* element, so a signed request and an unsigned one answer one bucket policy alike.
|
||||
*/
|
||||
private static String effectOf(JsonNode statement) {
|
||||
String effect = statement.path("Effect").asText("");
|
||||
return "Allow".equals(effect) || "Deny".equals(effect) ? effect : "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a principal element names anybody at all. Each entry is written as a string, so an
|
||||
* element whose entries are no strings names nobody, an empty list and a typed map whose types
|
||||
* name nothing among them. An exclusion that names nobody carves nobody out, so a statement
|
||||
* whose {@code NotPrincipal} names nobody covers nobody rather than everyone, which is again
|
||||
* the policy evaluator's reading of the element.
|
||||
*
|
||||
* <p>This gates the readers that decide a request. The Block Public Access status does not ask
|
||||
* it, that one answering S3's own rule rather than who the statement covers; see
|
||||
* {@link #grantsToEveryone(JsonNode)}.
|
||||
*/
|
||||
private static boolean namesAnyPrincipal(JsonNode principal) {
|
||||
if (principal == null || principal.isMissingNode() || principal.isNull()) {
|
||||
return false;
|
||||
}
|
||||
if (principal.isObject()) {
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = principal.fields();
|
||||
while (fields.hasNext()) {
|
||||
if (namesPrincipalValue(fields.next().getValue())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return namesPrincipalValue(principal);
|
||||
}
|
||||
|
||||
/** Whether one principal entry, a string or a list of them, names anybody. */
|
||||
private static boolean namesPrincipalValue(JsonNode value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
if (value.isTextual()) {
|
||||
return true;
|
||||
}
|
||||
if (value.isArray()) {
|
||||
for (JsonNode item : value) {
|
||||
if (item.isTextual()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean publicPolicyAllows(ObjectMapper objectMapper, String policy, String action, String resourceArn) {
|
||||
return publicPolicyDecision(objectMapper, policy, action, resourceArn) == PublicAccessDecision.ALLOW;
|
||||
}
|
||||
@@ -66,14 +122,14 @@ final class S3PublicAccessEvaluator {
|
||||
boolean allowed = false;
|
||||
Iterable<JsonNode> iterable = statements.isArray() ? statements : List.of(statements);
|
||||
for (JsonNode statement : iterable) {
|
||||
String effect = statement.path("Effect").asText("");
|
||||
if (!"Allow".equalsIgnoreCase(effect) && !"Deny".equalsIgnoreCase(effect)) {
|
||||
String effect = effectOf(statement);
|
||||
if (effect.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (!statementMatchesPublicPrincipalActionResource(statement, action, resourceArn)) {
|
||||
continue;
|
||||
}
|
||||
if ("Deny".equalsIgnoreCase(effect)) {
|
||||
if ("Deny".equals(effect)) {
|
||||
return PublicAccessDecision.DENY;
|
||||
}
|
||||
if (statement.hasNonNull("Condition")) {
|
||||
@@ -104,8 +160,8 @@ final class S3PublicAccessEvaluator {
|
||||
boolean allowed = false;
|
||||
Iterable<JsonNode> iterable = statements.isArray() ? statements : List.of(statements);
|
||||
for (JsonNode statement : iterable) {
|
||||
String effect = statement.path("Effect").asText("");
|
||||
if (!"Allow".equalsIgnoreCase(effect) && !"Deny".equalsIgnoreCase(effect)) {
|
||||
String effect = effectOf(statement);
|
||||
if (effect.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (!principalMatches(statement, principalType, principalValue)
|
||||
@@ -116,7 +172,7 @@ final class S3PublicAccessEvaluator {
|
||||
if (!conditionsMatch(statement.path("Condition"), context)) {
|
||||
continue;
|
||||
}
|
||||
if ("Deny".equalsIgnoreCase(effect)) {
|
||||
if ("Deny".equals(effect)) {
|
||||
return PublicAccessDecision.DENY;
|
||||
}
|
||||
allowed = true;
|
||||
@@ -161,7 +217,7 @@ final class S3PublicAccessEvaluator {
|
||||
if (statement == null || !statement.isObject()) {
|
||||
return false;
|
||||
}
|
||||
if (!"Allow".equalsIgnoreCase(statement.path("Effect").asText(""))) {
|
||||
if (!"Allow".equals(effectOf(statement))) {
|
||||
return false;
|
||||
}
|
||||
if (!grantsToEveryone(statement)) {
|
||||
@@ -174,7 +230,13 @@ final class S3PublicAccessEvaluator {
|
||||
if (statement.hasNonNull("Principal")) {
|
||||
return hasPublicPrincipal(statement.path("Principal"));
|
||||
}
|
||||
// An Allow on NotPrincipal grants to everyone the statement does not name.
|
||||
// An Allow on NotPrincipal grants to everyone the statement does not name. Block Public
|
||||
// Access asks a different question from the readers that decide a request: 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 (S3 User Guide,
|
||||
// "The meaning of public"). A NotPrincipal pins no principal to a fixed value whether it
|
||||
// names anybody or not, so the status stays public here even where the grant readers below
|
||||
// decide the statement covers nobody.
|
||||
return statement.hasNonNull("NotPrincipal")
|
||||
&& !hasPublicPrincipal(statement.path("NotPrincipal"));
|
||||
}
|
||||
@@ -273,7 +335,7 @@ final class S3PublicAccessEvaluator {
|
||||
if (statement.hasNonNull("Principal")) {
|
||||
return hasPublicPrincipal(statement.path("Principal"));
|
||||
}
|
||||
if (statement.hasNonNull("NotPrincipal")) {
|
||||
if (namesAnyPrincipal(statement.path("NotPrincipal"))) {
|
||||
return !hasPublicPrincipal(statement.path("NotPrincipal"));
|
||||
}
|
||||
return false;
|
||||
@@ -284,7 +346,7 @@ final class S3PublicAccessEvaluator {
|
||||
if (statement.hasNonNull("Principal")) {
|
||||
return principalNodeMatches(statement.path("Principal"), principalType, principalValue);
|
||||
}
|
||||
if (statement.hasNonNull("NotPrincipal")) {
|
||||
if (namesAnyPrincipal(statement.path("NotPrincipal"))) {
|
||||
return !principalNodeMatches(
|
||||
statement.path("NotPrincipal"), principalType, principalValue);
|
||||
}
|
||||
|
||||
@@ -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("""
|
||||
|
||||
Reference in new issue
Block a user