fix(iam): read the documents a request supplies, and hold each to being a policy (#8164)

`SimulatePrincipalPolicy` read neither `PolicyInputList` nor
`PermissionsBoundaryPolicyInputList`, so a caller testing a policy it has not
attached, or a boundary other than the stored one, was answered about the
principal as it stands: a decision about a question nobody asked. Both members
have published behaviour, and `SimulateCustomPolicy` already read both, the
documents being its whole input, so this is the principal simulator dropping two
of its own.

A `PolicyInputList` document is "an optional list of additional policy documents
to include in the simulation", so each is evaluated with the weight of a policy
the principal holds: an allow in one allows an action the attached policies do
not, which is what the operation's own example in the API Reference turns on, and
a deny in one stops an action they allow. A `PermissionsBoundaryPolicyInputList`
document is the boundary that simulation runs with in place of the stored one,
"if a permissions boundary is attached to an entity and you pass in a different
permissions boundary policy using this parameter, then the new permissions
boundary policy is used for the simulation", and it is the boundary when the
entity carries none, the member being "the IAM permissions boundary policy to
simulate". The one-document limit both simulators are held to is read in one
place now rather than two.

Neither document is attached to anything, so no `PolicyExclusionList` identifier
reaches either: that member names the policies "actually attached to the
principal", where these are the request's own. A simulation leaving out every
part a `PolicyType` can name therefore still carries what the request handed in,
and a boundary handed in still holds the simulation down where the stored one was
left out.

Reading a document means being able to read it, so each supplied document is held
to the grammar before anything is decided from it. Four operations take
`PolicyInputList`, the two simulators and the two context-key readers, and each
takes "the complete, valid JSON text of an IAM policy". A text that does not
parse as a JSON object, and an object carrying no `Statement`, "the main element
for a policy. This element is required.", are both refused with the modelled
`InvalidInput`, 400, naming the entry by the index the request numbered it with.
A supplied permissions boundary carries the sentence that names no entry, the
member taking one document. Without that, an unreadable document was answered as
though the request had carried nothing at all: an unreadable identity document
left an attached allow still allowing, and an unreadable boundary was worse,
becoming a ceiling that allows nothing and turning an allow into an implicit
deny.

The 500 `PolicyEvaluation` the two simulators model is not that answer. It
carries no sender fault where `InvalidInput` does, and the two context-key
readers take this same member and model no `PolicyEvaluation` at all, so one
defect on one member would otherwise be answered two ways across the four
operations that take it.

A document already stored is read as it always was: it contributes no statements
and the decision is made on the rest, because a policy the account accepted once
must not make every later call fail. The supplied documents are read ahead of the
principal lookup now, which is where this package answers an input.

Either supplied document is also held to the shape the model declares, so one
past 131072 characters is a `ValidationError` naming the member under the index
the request numbered it with. The boundary list was read by the constraint walk
already; `PolicyInputList` was carried by hand for both simulators, which only
`SimulateCustomPolicy` needs: it requires the member and answers an empty list in
its own wording, "At least one PolicyInputList member is required.", where
`SimulatePrincipalPolicy` leaves the member optional and so has nothing of its
own in front of the walk. So the generator's exemption drops that operation and
the vendored shapes are regenerated from the pinned model, which adds the one
member to `SimulatePrincipalPolicyRequest`.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-12 20:59:23 -04:00
1 parent 40ec27593e
commit 623bb15283
9 files changed
+766 -24

No files matched your search

@@ -1,5 +1,6 @@
package io.floci.aws.services.iam;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.AwsArnUtils;
@@ -1677,6 +1678,32 @@ public class IamPolicyEvaluator {
// Policy document parsing
// -----------------------------------------------------------------------
/**
* Whether {@code document} reads as an IAM policy document at all: a value that parses as a
* JSON object and carries the element the grammar requires of every policy, {@code Statement},
* "the main element for a policy. This element is required."
*
* <p>Only a document a request SUPPLIED is asked this. An operation taking one takes "the
* complete, valid JSON text of an IAM policy", so a value that is not one is an input the
* operation answers rather than a document the evaluation reads nothing out of. A document
* already stored is read by {@link #parseAll} instead, which contributes no statements for one
* it cannot parse and leaves the decision to the rest: a policy the account accepted once must
* not make every later call fail.
*/
public boolean readsAsPolicyDocument(String document) {
if (document == null) {
return false;
}
try {
JsonNode root = objectMapper.readTree(document);
return root.isObject() && root.has("Statement");
} catch (JsonProcessingException unreadable) {
// Not JSON, so no policy document either, and the caller words the refusal. A stored
// document carrying the same text is logged where it is parsed, in parseUncached.
return false;
}
}
private List<PolicyStatement> parseAll(List<String> documents) {
return parseAllTracked(documents).statements();
}
@@ -2165,15 +2165,18 @@ public class IamQueryHandler {
}
/**
* The principal's own policies, less whatever {@code PolicyExclusionList} asked to leave out.
* The identifiers are request inputs, so they are read and refused ahead of the lookup, which
* is where this package answers an input; a simulation that excludes nothing resolves the
* context it always did.
* The principal's own policies, less whatever {@code PolicyExclusionList} asked to leave out
* and beside whatever documents the request handed in. The identifiers are request inputs, so
* they are read and refused ahead of the lookup, which is where this package answers an input;
* a simulation that excludes nothing and hands nothing in resolves the context it always did.
*/
private Response handleSimulatePrincipalPolicy(MultivaluedMap<String, String> params) {
String policySourceArn = validatedPolicySourceArn(params);
PolicyExclusions exclusions = PolicyExclusions.from(params);
CallerContext caller = iamService.resolvePrincipalContext(policySourceArn, exclusions);
List<String> inputDocuments = readablePolicyInputList(params);
String inputBoundary = inputBoundaryDocument(params);
CallerContext caller = withInputDocuments(
iamService.resolvePrincipalContext(policySourceArn, exclusions), inputDocuments, inputBoundary);
List<String> actionNames = requireActionNames(params);
List<String> resourceArns = extractResourceArnsOrWildcard(params);
// Keyed without regard to case, as the evaluator compares condition key names: a value
@@ -2202,14 +2205,85 @@ public class IamQueryHandler {
List<String> actionNames = requireActionNames(params);
List<String> resourceArns = extractResourceArnsOrWildcard(params);
Map<String, List<String>> context = extractContextEntries(params);
// AWS accepts only one permissions boundary document per simulation; a request that
// provides more than one is not modeled and only the first is applied.
List<String> boundaryInputList = getMemberList(params, "PermissionsBoundaryPolicyInputList");
String boundaryDocument = boundaryInputList.isEmpty() ? null : boundaryInputList.get(0);
CallerContext caller = new CallerContext(policyInputList, null, boundaryDocument);
CallerContext caller = new CallerContext(policyInputList, null, inputBoundaryDocument(params));
return simulationResults("SimulateCustomPolicy", params, caller, actionNames, resourceArns, context);
}
/**
* The documents a principal simulation was handed beside the principal's own.
* {@code PolicyInputList} is "an optional list of additional policy documents to include in
* the simulation", so each is evaluated like a policy the principal holds: AWS's own example
* for this operation allows an action the attached policies do not, on the strength of an
* input document alone. A permissions boundary handed in "is used for the simulation" in place
* of the stored one, which is what the member says of an entity carrying one, and is the
* boundary when the entity carries none.
*
* <p>Neither document is attached to anything, so no {@code PolicyExclusionList} identifier
* reaches either: that member names the policies "actually attached to the principal", where
* these are the request's own. Both are read before the principal is resolved, which is where
* this package answers an input, so a document the operation cannot read is refused whether or
* not the ARN names anybody.
*/
private CallerContext withInputDocuments(CallerContext caller, List<String> documents,
String boundaryDocument) {
CallerContext simulated = caller.withIdentityPolicies(documents);
return boundaryDocument == null ? simulated : simulated.withBoundaryPolicy(boundaryDocument);
}
/**
* The one permissions boundary document a simulation runs with, read the same way for both
* simulators. AWS accepts only one, "You can input only one permissions boundary when you pass
* a policy to this operation", so a request carrying more is not modelled and only the first
* is applied.
*/
private String inputBoundaryDocument(MultivaluedMap<String, String> params) {
List<String> boundaryInputList = getMemberList(params, "PermissionsBoundaryPolicyInputList");
if (boundaryInputList.isEmpty()) {
return null;
}
String boundary = boundaryInputList.get(0);
if (!policyEvaluator.readsAsPolicyDocument(boundary)) {
// The member carries one document, so the refusal identifies none: this is the fixed
// sentence AWS's own emulator answers for every supplied document but an entry of
// PolicyInputList, which is named by its index instead.
throw new AwsException("InvalidInput", "Syntax errors in policy.", 400);
}
return boundary;
}
/**
* The {@code PolicyInputList} documents a request supplied, each held to being a policy
* document before anything is decided from it. The member takes "the complete, valid JSON text
* of an IAM policy", so a value that is no policy document is an invalid input value rather
* than a document the evaluation reads nothing out of, and the modelled {@code InvalidInput} is
* what this package answers a value it cannot read as the member's type at all, the declared
* format and length being answered by the constraints ahead of the operation.
*
* <p>The 500 {@code PolicyEvaluation} the two simulators model is not that answer: it carries
* no sender fault where {@code InvalidInput} does, and the two context-key readers take this
* same member and model no {@code PolicyEvaluation} at all, so one defect on one member would
* otherwise be answered two ways across the four operations that take it.
*
* <p>No wording for the refusal is published. This is the sentence AWS's own emulator answers,
* naming the offending entry by the index it arrived under, the member carrying several
* documents and the caller needing to know which of them was refused.
*/
private List<String> readablePolicyInputList(MultivaluedMap<String, String> params) {
List<String> documents = new ArrayList<>();
for (AwsQueryLists.IndexedMember member : AwsQueryLists.members(params, "PolicyInputList", null)) {
String document = params.getFirst(member.prefix());
if (document == null) {
continue;
}
if (!policyEvaluator.readsAsPolicyDocument(document)) {
throw new AwsException("InvalidInput",
"Policy input list item " + member.index() + " has invalid content", 400);
}
documents.add(document);
}
return documents;
}
private List<String> requireActionNames(MultivaluedMap<String, String> params) {
List<String> actionNames = getMemberList(params, "ActionNames");
if (actionNames.isEmpty()) {
@@ -2219,7 +2293,7 @@ public class IamQueryHandler {
}
private List<String> requirePolicyInputList(MultivaluedMap<String, String> params) {
List<String> policyInputList = getMemberList(params, "PolicyInputList");
List<String> policyInputList = readablePolicyInputList(params);
if (policyInputList.isEmpty()) {
throw new AwsException("ValidationError", "At least one PolicyInputList member is required.", 400);
}
@@ -2271,9 +2345,10 @@ public class IamQueryHandler {
// entity, and any additional policies that you provide, are included": a grant the account
// applies without attaching it is therefore not read here, though a simulation evaluates
// with it.
List<String> inputDocuments = readablePolicyInputList(params);
CallerContext caller = iamService.resolveAttachedPrincipalContext(policySourceArn);
List<String> allDocuments = new ArrayList<>(caller.identityPolicies());
allDocuments.addAll(getMemberList(params, "PolicyInputList"));
allDocuments.addAll(inputDocuments);
List<String> keys = policyEvaluator.contextKeysReferencedIn(allDocuments);
return Response.ok(AwsQueryResponse.envelope("GetContextKeysForPrincipalPolicy", AwsNamespaces.IAM,
contextKeyNamesXml(keys))).build();
@@ -50,11 +50,28 @@ public record CallerContext(
/** Copy of this context with one more identity policy, evaluated like the caller's own. */
public CallerContext withIdentityPolicy(String policyDocument) {
return withIdentityPolicies(List.of(policyDocument));
}
/**
* Copy of this context with more identity policies, each evaluated like the caller's own. A
* simulation handed documents of its own adds them this way, so what the request brought and
* what the principal holds carry the same weight.
*/
public CallerContext withIdentityPolicies(List<String> policyDocuments) {
if (policyDocuments.isEmpty()) {
return this;
}
List<String> policies = new ArrayList<>(identityPolicies);
policies.add(policyDocument);
policies.addAll(policyDocuments);
return new CallerContext(policies, sessionPolicyDocument, boundaryPolicyDocument, scpLevels, principalArn);
}
/** Copy of this context with the permissions boundary document a caller handed in instead. */
public CallerContext withBoundaryPolicy(String policyDocument) {
return new CallerContext(identityPolicies, sessionPolicyDocument, policyDocument, scpLevels, principalArn);
}
/** Copy of this context with the caller principal ARN attached. */
public CallerContext withPrincipalArn(String principalArn) {
return new CallerContext(identityPolicies, sessionPolicyDocument, boundaryPolicyDocument, scpLevels, principalArn);