mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user