fix(iam): refuse an action no vendor prefaces on the way in (#8428)

No write read the values a statement's action elements name, so a document
naming a bare verb was stored and only told apart later, by whatever tried to
decide with it. AWS refuses that document before it stores anything, with
MalformedPolicyDocument at 400 and the clause's own sentence,
"Actions/Conditions must be prefaced by a vendor, e.g., iam, sdb, ec2, etc.",
fixed text with no request value in it. The resource elements beside them have
been read value by value for a while, so this is the action half of the same
clause check.

The policy grammar writes an action as a service namespace, a colon and the
name of an action, and an action element may name the bare wildcard instead,
which stands for every action there is. So a value passes when it is "*" or
carries a namespace before a colon, whatever the name after it spells: "s3:*"
and "iam:*AccessKey*" are the reference's own examples of a wildcard inside the
name. A value that is no string is held to the text it spells, as the Version,
the Effect and the resource values already are, so "Action": 123 is refused
where "Action": "123" was; an element carrying null still names nothing, which
is the element's own sentence rather than a value to read.

The namespace is asked for rather than looked up. No sentence anywhere says AWS
refuses an action whose vendor it does not publish, so reading these values
against a catalog of services would refuse documents AWS stores and would make
an action of a service this emulator has yet to model impossible to write. What
is read is the shape the grammar gives, nothing more.

Both identity validators word this clause alike, the effect being the one clause
of an identity document whose sentence follows the validator that read it. The
actions are read before the resources, which is the order the elements a
statement must carry are already read in, so a document missing both answers the
action sentence first. No code and no status moves: the refusal is
MalformedPolicyDocument at 400, as every other sentence of this reader is. AWS
words the clause for actions and condition keys together and only the actions
are read here, a condition key being a value neither validator reads yet.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 08:13:41 -04:00
1 parent 31c86c05e9
commit a8c34f6326
4 files changed
+133 -14

No files matched your search

+12 -2
View File
@@ -504,8 +504,9 @@ on `PutUserPolicy`, `PutGroupPolicy` and `PutRolePolicy`, is held to that gramma
validator's one sentence for both cases, `Syntax errors in policy.`, and to the values the policy
language defines: a `Version` outside `2012-10-17` and `2008-10-17` answers that same sentence, a
statement whose `Effect` is not literally `Allow` or `Deny` answers `The policy failed legacy
parsing`, and a resource named neither by ARN nor by `*` answers `Resource <value> must be in ARN
format or "*".` A value that is no string names the text of that value, as the `Version` and the
parsing`, an action that no vendor prefaces answers `Actions/Conditions must be prefaced by a
vendor, e.g., iam, sdb, ec2, etc.`, and a resource named neither by ARN nor by `*` answers
`Resource <value> must be in ARN format or "*".` A value that is no string names the text of that value, as the `Version` and the
`Effect` already do, so `"Resource": 123` answers that sentence exactly as `"Resource": "123"`
does; an element carrying null names nothing, which is the element's own case rather than a value
to read. A managed policy, on `CreatePolicy` and `CreatePolicyVersion`, is held to those and
@@ -515,6 +516,15 @@ rather than a missing element. One clause the two validators do not word alike:
language does not define answers `The policy failed legacy parsing` on an inline write and
`Syntax errors in policy.` on a managed one, so the sentence follows the validator that read it.
An action is prefaced when it carries a service namespace before a colon, whatever the name after it
spells: `s3:*` and `iam:*AccessKey*` are the reference's own examples, and the bare `*`, which
stands for every action there is, is admitted on its own. The namespace is asked for rather than
looked up, since no sentence says AWS refuses an action whose vendor it does not publish, so an
action naming a service Floci has yet to model is stored as written. The clause is read before the
resource one, which is the order the required elements are already read in, and AWS words it for
actions and condition keys together while only the actions are read here, a condition key being a
value neither validator reads yet.
A document carrying `Statement` with nothing in it is still a policy, one that grants nothing, and a
member the request leaves out is still answered as absent by the constraints ahead of the operation.
None of these sentences is published and none is captured: they are the ones AWS's own emulator
+10 -1
View File
@@ -108,7 +108,16 @@ Editing a permission set marks its provisioned copies stale. Explicit reprovisio
the role without changing its ARN or role ID, removes policies no longer configured, and
preserves the previous role configuration if that account's desired policies cannot be
validated. A missing policy or invalid inline document reports a `FAILED` provisioning or
assignment operation with a failure reason. Failed assignment creation leaves no assignment.
assignment operation with a failure reason. What makes an inline document invalid is the reading IAM
gives it, the role being where it lands: `PutInlinePolicyToPermissionSet` holds the document to its
size and its non-whitespace quota, as AWS documents a syntax error there, and the policy language's
own values are read when the document is applied, so one naming an action that no vendor prefaces, a
resource that is neither an ARN nor `*`, an effect the language does not define or a version it does
not publish is attached and then reported in that failure reason. See
[IAM](iam.md#inline-policies) for the sentences each clause is refused with. A permission set keeps
whatever document it was attached, so one stored before a clause was read reports that failure
reason on its next provisioning, refresh or assignment rather than when it was attached, and
provisions again once the document is replaced. Failed assignment creation leaves no assignment.
`ALL_PROVISIONED_ACCOUNTS` refreshes the permission set's currently provisioned accounts;
an operation can fail after an earlier account was refreshed.
@@ -28,8 +28,9 @@ import java.util.List;
* required.";</li>
* <li>the <em>values</em> the elements declare, which an identity-based document is held to: the
* version it declares has to be one the language defines, a statement's {@code Effect} has to
* be {@code Allow} or {@code Deny} compared literally, and a resource it names has to be
* named by ARN or by the wildcard.</li>
* be {@code Allow} or {@code Deny} compared literally, an action it names has to be prefaced
* by a vendor or be the bare wildcard, and a resource it names has to be named by ARN or by
* the wildcard.</li>
* </ul>
*
* <p>A managed policy's document is held to its statements' own elements as well, each statement
@@ -80,6 +81,16 @@ final class IamPolicyDocuments {
*/
private static final String POLICY_PARSE_FAILED = "The policy failed legacy parsing";
/**
* What a statement naming an action that no vendor prefaces is refused with. AWS words the
* clause for actions and condition keys together, and only the actions are read here, a
* condition key being a value neither validator reads yet.
*/
private static final String ACTION_WITHOUT_VENDOR =
"Actions/Conditions must be prefaced by a vendor, e.g., iam, sdb, ec2, etc.";
private static final String ANY_ACTION = "*";
private static final String ANY_RESOURCE = "*";
private static final String ARN_PREFIX = "arn:";
@@ -187,7 +198,12 @@ final class IamPolicyDocuments {
if (effect != null && !EFFECTS.contains(effect.asText())) {
throw malformed(unreadableEffect);
}
for (String resource : resourceValues(statement)) {
for (String action : elementValues(statement, "Action", "NotAction")) {
if (!prefacedByVendor(action)) {
throw malformed(ACTION_WITHOUT_VENDOR);
}
}
for (String resource : elementValues(statement, "Resource", "NotResource")) {
if (!resource.startsWith(ARN_PREFIX) && !ANY_RESOURCE.equals(resource)) {
throw malformed("Resource " + resource + " must be in ARN format or \""
+ ANY_RESOURCE + "\".");
@@ -267,16 +283,17 @@ final class IamPolicyDocuments {
}
/**
* Every value a statement's resource elements name, in the order the document carries them. A
* value that is no string names the text of that value, which is how the {@code Version} and
* the {@code Effect} above are already read: a number or a boolean is held to naming a resource
* rather than passed over, so {@code 123} and {@code "123"} are refused alike. A null names
* nothing, as {@link #namesSomething} reads it, and a list or an object is no value of this
* element at all.
* Every value a statement names under {@code spellings}, in the order the document carries
* them, which is how both pattern elements are read: the actions a statement is about and the
* resources they apply to. A value that is no string names the text of that value, which is
* how the {@code Version} and the {@code Effect} above are already read: a number or a boolean
* is held to naming a value of its element rather than passed over, so {@code 123} and
* {@code "123"} are refused alike. A null names nothing, as {@link #namesSomething} reads it,
* and a list or an object is no value of these elements at all.
*/
private static List<String> resourceValues(JsonNode statement) {
private static List<String> elementValues(JsonNode statement, String... spellings) {
List<String> values = new ArrayList<>();
for (String spelling : new String[] {"Resource", "NotResource"}) {
for (String spelling : spellings) {
JsonNode element = statement.get(spelling);
if (element == null) {
continue;
@@ -290,6 +307,20 @@ final class IamPolicyDocuments {
return values;
}
/**
* Whether an action value names the vendor the language asks for. The grammar writes an action
* as "a service namespace, a colon, and the name of an action", and an action element may name
* the bare wildcard instead, which stands for every action there is. So a value is prefaced
* when it carries a namespace before a colon, whatever the name after it spells, the wildcards
* the reference admits in an action name included ({@code s3:*} and {@code iam:*AccessKey*} are
* its own examples). The namespace is asked for rather than looked up: no sentence says AWS
* refuses an action whose vendor it does not publish, and refusing one here would turn a
* service this emulator has yet to model into a document nobody can store.
*/
private static boolean prefacedByVendor(String action) {
return ANY_ACTION.equals(action) || action.indexOf(':') > 0;
}
private static JsonNode parse(String document) {
if (document == null) {
return null;
@@ -34,6 +34,9 @@ class IamPolicyDocumentWriteIntegrationTest {
private static final String MALFORMED = "MalformedPolicyDocument";
private static final String ACTIONS_WANT_A_VENDOR =
"Actions/Conditions must be prefaced by a vendor, e.g., iam, sdb, ec2, etc.";
/** Everything these tests create outlives the class otherwise: the emulator is shared. */
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@@ -383,4 +386,70 @@ class IamPolicyDocumentWriteIntegrationTest {
"The policy failed legacy parsing");
}
}
/**
* An action is written as a service namespace, a colon and the name of an action, so one that
* no vendor prefaces is refused with the clause's own sentence, which both identity validators
* word alike. A value that is no string is held to the rule its text would be, as the resource
* values beside it are.
*/
@Test
void anActionNoVendorPrefacesIsRefusedByBothValidators() {
String userName = createUser("write-action-vendor");
String arn = createManagedPolicy("write-action-vendor");
String bare = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":\"GetObject\",\"Resource\":\"*\"}]}";
String inAList = "{\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":[\"s3:GetObject\",\"ListAllMyBuckets\"],\"Resource\":\"*\"}]}";
String notAction = "{\"Statement\":[{\"Effect\":\"Allow\",\"NotAction\":\"ChangePassword\","
+ "\"Resource\":\"*\"}]}";
String noString = "{\"Statement\":[{\"Effect\":\"Allow\",\"Action\":123,\"Resource\":\"*\"}]}";
for (String document : new String[] {bare, inAList, notAction, noString}) {
expectMalformed(inlinePolicyWrite(userName, "write-action-vendor", document),
ACTIONS_WANT_A_VENDOR);
expectMalformed(managedPolicyWrite("write-action-vendor-managed", document),
ACTIONS_WANT_A_VENDOR);
expectMalformed(iam("CreatePolicyVersion")
.formParam("PolicyArn", arn)
.formParam("PolicyDocument", document),
ACTIONS_WANT_A_VENDOR);
}
// An action element naming nothing is the element's own sentence rather than this one.
expectMalformed(managedPolicyWrite("write-action-none",
"{\"Statement\":[{\"Effect\":\"Allow\",\"Action\":null,\"Resource\":\"*\"}]}"),
"Policy statement must contain actions.");
}
/**
* What the rule admits: the bare wildcard, which stands for every action there is, and a
* namespace before a colon whatever the name after it spells, the wildcards the reference
* writes into an action name included. The vendor is asked for rather than looked up, so an
* action of a service this emulator does not model is stored as written.
*/
@Test
void anActionThatNamesAVendorIsStoredWhateverTheNameSpells() {
String userName = createUser("write-action-admitted");
for (String action : new String[] {"\"*\"", "[\"*\"]", "\"s3:*\"", "\"iam:*AccessKey\"",
"\"*:GetObject\"", "\"nosuchservice:DoThing\"", "[\"s3:Get*\",\"s3:List*\"]"}) {
String document = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":" + action + ",\"Resource\":\"*\"}]}";
String policyName = unique("write-action-admitted");
inlinePolicyWrite(userName, policyName, document)
.when().post("/").then().statusCode(200);
iam("GetUserPolicy")
.formParam("UserName", userName)
.formParam("PolicyName", policyName)
.when().post("/").then().statusCode(200);
// The holder has to be deletable afterwards, and a user keeping an inline policy is
// not, so each case takes its own back out.
iam("DeleteUserPolicy")
.formParam("UserName", userName)
.formParam("PolicyName", policyName)
.when().post("/").then().statusCode(200);
}
}
}