diff --git a/docs/services/iam.md b/docs/services/iam.md index 0b4876763b..f716f718a4 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -449,6 +449,44 @@ violation moves the quoted-pattern entry in `tools/aws/regen_iam_shapes.py`, the `src/main/resources/aws/iam-request-shapes.json` a regeneration writes from it, which that generator's own test holds the entry to, the test that pins the comma, and this paragraph. +### What a malformed policy ARN answers + +`PolicyArn` is `arnType` on every operation that takes one, 20 to 2048 characters and no pattern, +so the declared check refuses a shorter value and nothing holds the rest to parsing as an ARN. What +AWS answers beyond that length is recorded rather than published, and it is two sentences rather +than one, both `ValidationError` with a 400 and a sender fault. + +- Attaching names the value: `ARN is not valid.` Recorded on `AttachRolePolicy` for a bare + policy name on 2026-11-03, and on `AttachUserPolicy`, `AttachGroupPolicy` and `AttachRolePolicy` + for a value that is no ARN at all on 2026-11-06. +- Everything else answers `Invalid ARN: Could not be parsed!`, which is wholly fixed: the slot the + value would fill renders empty, so two spaces follow the colon and the request's value never + appears in it. Recorded on the three `Detach*Policy` operations on 2026-11-06, on + `ListEntitiesForPolicy` on 2026-11-03 and on `GetPolicy` on 2026-10-29. + +The remaining operations that take the member, the tag and version families among them, carry no +recording and answer the parse sentence with those eight: one member read by one parser answers one +way, and the attach side is the exception the recordings name. That is this emulator's reading +under the standing decision rather than a measurement. A value that parses as an ARN and names no +policy is unchanged by all of this, still the 404 `NoSuchEntity` each operation already answered. + +An older recording disagrees about the attach side and is no longer followed. LocalStack's +AWS-validated snapshots have `AttachUserPolicy` and `AttachRolePolicy` answering the parse sentence +for a 21-character value that is no ARN and for a bare policy name, validated 2025-03-06 and +2025-06-19, where all three 2026 recordings have the attach side naming the value. Two +AWS-validated sources disagree on one operation and one class of input, seventeen months apart, so +the reading here is that AWS reworded that side between them and the newer recordings are what this +emulator follows. A recording that shows otherwise moves this paragraph and `IamArnShapes`. + +`PolicySourceArn`, which the two principal simulators resolve an entity from, carries the same type +and one more recorded case: a value of `*` answers `InvalidInput` 400, `Invalid Entity Arn: * does +not clearly define entity type and name.`, where the member's declared minimum is 20 and that value +is one character, so AWS's entity parse runs ahead of its length check for it. Recorded on +`SimulatePrincipalPolicy` on 2026-11-03. `GetContextKeysForPrincipalPolicy` takes the same member +through the same resolver and answers it the same way, which is a reading rather than a recording. +A value that clears 20 characters and parses as no ARN keeps the sentence its own capture settled, +`Invalid ARN provided in the request: `, and a value under 20 keeps the declared length. + ### Permission Boundaries | Action | Description | diff --git a/src/main/java/io/floci/aws/services/iam/IamArnShapes.java b/src/main/java/io/floci/aws/services/iam/IamArnShapes.java new file mode 100644 index 0000000000..164fd62943 --- /dev/null +++ b/src/main/java/io/floci/aws/services/iam/IamArnShapes.java @@ -0,0 +1,92 @@ +package io.floci.aws.services.iam; + +import io.floci.aws.core.common.AwsArnUtils; +import io.floci.aws.core.common.AwsException; +import jakarta.ws.rs.core.MultivaluedMap; + +import java.util.Set; + +/** + * The refusals IAM answers for an ARN member whose value parses as no ARN. The model declares a + * length for those members and no pattern, so what AWS answers beyond the length is recorded + * rather than published, and this is where the recordings are read. + * + *

{@code PolicyArn} is {@code arnType} on every operation that takes one, and + * {@link IamRequestShapes} holds it to that length ahead of this check, which is the order #6402 + * settled. A value that clears 20 characters and is still no ARN is refused here rather than + * reaching the lookup: a 400 {@code ValidationError} on both sides of an attachment, in two + * sentences. Attaching names the value, {@code ARN is not valid.}, recorded for a bare + * policy name on 2026-11-03 and for a value that is no ARN at all on all three attach operations + * on 2026-11-06. Everything else answers {@code Invalid ARN: Could not be parsed!}, wholly fixed, + * the value slot rendering empty so two spaces follow the colon: recorded on the three detach + * operations on 2026-11-06, on {@code ListEntitiesForPolicy} on 2026-11-03 and on + * {@code GetPolicy} on 2026-10-29. + * + *

The other operations that take the member carry no recording of their own and answer the + * parse sentence with the eight that do, which is this emulator's reading under the standing + * decision rather than a measurement: one member is read by one parser, and the attach side is the + * exception the recordings name. An older recording disagrees about that side and is no longer + * followed. LocalStack's AWS-validated snapshots have {@code AttachUserPolicy} and + * {@code AttachRolePolicy} answering the parse sentence for the same two values, validated + * 2025-03-06 and 2025-06-19, where all three 2026 recordings have the attach side naming the + * value, so AWS looks to have reworded it between. {@code docs/services/iam.md} carries both and + * says which is followed. + * + *

{@code PolicySourceArn} is the same type on the two operations that resolve a principal from + * one, and a value of {@code *} is refused ahead of the declared length rather than by it: AWS + * answers that value with an entity sentence of its own where the length it publishes for the + * member is 20, so its entity parse runs first. Recorded on {@code SimulatePrincipalPolicy} on + * 2026-11-03; {@code GetContextKeysForPrincipalPolicy} takes the same member through the same + * resolver and answers it the same way, which is a reading rather than a recording. + */ +final class IamArnShapes { + + /** The attach side, which AWS words with the value in it. */ + private static final Set ATTACH_POLICY = Set.of( + "AttachUserPolicy", "AttachGroupPolicy", "AttachRolePolicy"); + + /** What IAM answers wherever it reports nothing it could parse, the value slot left empty. */ + private static final String UNPARSEABLE_ARN = "Invalid ARN: Could not be parsed!"; + + /** The member the two principal simulators resolve an entity from. */ + private static final String POLICY_SOURCE_ARN = "PolicySourceArn"; + + private static final String POLICY_ARN = "PolicyArn"; + + private IamArnShapes() { + } + + /** + * Refuses a {@code PolicyArn} that parses as no ARN, on every operation whose generated input + * shape declares the member. Runs after the declared constraints, so a value too short to be + * any ARN keeps answering the length it breaks. + */ + static void checkPolicyArn(String action, MultivaluedMap params) { + if (!IamRequestShapes.hasMember(action, POLICY_ARN)) { + return; + } + String value = params.getFirst(POLICY_ARN); + if (value == null || AwsArnUtils.isArn(value)) { + return; + } + throw ATTACH_POLICY.contains(action) + ? new AwsException("ValidationError", "ARN " + value + " is not valid.", 400) + : new AwsException("ValidationError", UNPARSEABLE_ARN, 400); + } + + /** + * Refuses a {@code PolicySourceArn} of {@code *}, which names no entity and is answered ahead + * of the member's declared length. Every other value is left to the length and then to the + * resolver, which refuses what parses as no ARN in the operation's own sentence. + */ + static void checkPolicySourceArn(String action, MultivaluedMap params) { + if (!"SimulatePrincipalPolicy".equals(action) + && !"GetContextKeysForPrincipalPolicy".equals(action)) { + return; + } + if ("*".equals(params.getFirst(POLICY_SOURCE_ARN))) { + throw new AwsException("InvalidInput", "Invalid Entity Arn: *" + + " does not clearly define entity type and name.", 400); + } + } +} diff --git a/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java b/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java index 5c1ad0ac01..f91c3bdc8e 100644 --- a/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java +++ b/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java @@ -138,7 +138,9 @@ public class IamQueryHandler { LOG.debugv("IAM action: {0}", action); try { + IamArnShapes.checkPolicySourceArn(action, params); IamRequestShapes.check(action, params); + IamArnShapes.checkPolicyArn(action, params); return switch (action) { // Users case "CreateUser" -> handleCreateUser(params); diff --git a/src/test/java/io/floci/aws/services/iam/IamMalformedPolicyArnIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamMalformedPolicyArnIntegrationTest.java new file mode 100644 index 0000000000..fa774991ba --- /dev/null +++ b/src/test/java/io/floci/aws/services/iam/IamMalformedPolicyArnIntegrationTest.java @@ -0,0 +1,213 @@ +package io.floci.aws.services.iam; + +import io.floci.aws.testing.PartitionCleanup; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.util.Locale; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +/** + * A {@code PolicyArn} that parses as no ARN is refused on its shape rather than looked up, in the + * two sentences AWS words it with: the attach side names the value, everything else reports + * nothing it could parse. The cases here are the recorded ones, the order against the member's + * declared length, and the controls that keep the refusal from reaching a value that is an ARN. + * + *

{@code docs/services/iam.md} carries the recordings and the one they supersede. + */ +@QuarkusTest +class IamMalformedPolicyArnIntegrationTest { + + private static final String IAM_AUTH = + "AWS4-HMAC-SHA256 Credential=test/20261118/us-east-1/iam/aws4_request"; + + private static final String CODE = "ErrorResponse.Error.Code"; + private static final String MESSAGE = "ErrorResponse.Error.Message"; + private static final String TYPE = "ErrorResponse.Error.Type"; + + /** The check's own value, long enough to clear the member's declared minimum of 20. */ + private static final String NOT_AN_ARN = "not-a-valid-policy-arn"; + + /** What AWS answers wherever it reports nothing it could parse, the value slot left empty. */ + private static final String UNPARSEABLE = "Invalid ARN: Could not be parsed!"; + + /** The prefix AWS puts on a constraint violation, which the declared check already carries. */ + private static final String ONE_VIOLATION = "1 validation error detected: "; + + /** An AWS-managed policy every account carries, for the controls. */ + private static final String READ_ONLY = "arn:aws:iam::aws:policy/ReadOnlyAccess"; + + @RegisterExtension + final PartitionCleanup cleanup = new PartitionCleanup(); + + private static RequestSpecification iam(String action) { + return given().header("Authorization", IAM_AUTH).formParam("Action", action); + } + + private static String unique(String prefix) { + return prefix + "-" + System.nanoTime(); + } + + /** The trust policy a role needs to exist at all, which is the only extra any of these take. */ + private static final String TRUST_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":" + + "[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"ec2.amazonaws.com\"}," + + "\"Action\":\"sts:AssumeRole\"}]}"; + + /** + * A principal of {@code kind}, registered for removal. All three take their name under + * {@code Name} and are created and deleted by {@code Create} and + * {@code Delete}, so only the extras differ. + */ + private String createPrincipal(String kind, String... extras) { + String name = unique("arn-shape-" + kind.toLowerCase(Locale.ROOT)); + RequestSpecification create = iam("Create" + kind).formParam(kind + "Name", name); + for (int i = 0; i + 1 < extras.length; i += 2) { + create = create.formParam(extras[i], extras[i + 1]); + } + create.when().post("/").then().statusCode(200); + cleanup.register(() -> iam("Delete" + kind).formParam(kind + "Name", name) + .when().post("/").then().statusCode(200)); + return name; + } + + /** The one kind that carries an extra, named so the cases stay one line each. */ + private String createRole() { + return createPrincipal("Role", "AssumeRolePolicyDocument", TRUST_POLICY); + } + + private static void expectValidationError(RequestSpecification request, String message) { + request.when().post("/").then().statusCode(400) + .body(CODE, equalTo("ValidationError")) + .body(TYPE, equalTo("Sender")) + .body(MESSAGE, equalTo(message)); + } + + /** + * The attach side, recorded on all three operations: the sentence carries the value exactly as + * the request sent it. + */ + @Test + void attachingAMalformedPolicyArnNamesTheValue() { + String message = "ARN " + NOT_AN_ARN + " is not valid."; + expectValidationError(iam("AttachUserPolicy") + .formParam("UserName", createPrincipal("User")).formParam("PolicyArn", NOT_AN_ARN), message); + expectValidationError(iam("AttachGroupPolicy") + .formParam("GroupName", createPrincipal("Group")).formParam("PolicyArn", NOT_AN_ARN), message); + expectValidationError(iam("AttachRolePolicy") + .formParam("RoleName", createRole()) + .formParam("PolicyArn", NOT_AN_ARN), message); + } + + /** + * The detach side, recorded on all three: the value never appears, and the slot it would fill + * leaves two spaces after the colon. + */ + @Test + void detachingAMalformedPolicyArnReportsNothingItCouldParse() { + expectValidationError(iam("DetachUserPolicy") + .formParam("UserName", createPrincipal("User")).formParam("PolicyArn", NOT_AN_ARN), UNPARSEABLE); + expectValidationError(iam("DetachGroupPolicy") + .formParam("GroupName", createPrincipal("Group")).formParam("PolicyArn", NOT_AN_ARN), UNPARSEABLE); + expectValidationError(iam("DetachRolePolicy") + .formParam("RoleName", createRole()) + .formParam("PolicyArn", NOT_AN_ARN), UNPARSEABLE); + } + + /** The two reads the same sentence is recorded on. */ + @Test + void theReadsThatCarryARecordingAnswerTheSameSentence() { + expectValidationError(iam("GetPolicy").formParam("PolicyArn", NOT_AN_ARN), UNPARSEABLE); + expectValidationError(iam("ListEntitiesForPolicy") + .formParam("PolicyArn", NOT_AN_ARN), UNPARSEABLE); + } + + /** + * The operations no recording covers answer with them, which is the reading this emulator + * documents: one member, one parser, one answer, with the attach side as the exception. + */ + @Test + void theOperationsWithNoRecordingFollowTheParseSentence() { + expectValidationError(iam("DeletePolicy").formParam("PolicyArn", NOT_AN_ARN), UNPARSEABLE); + expectValidationError(iam("ListPolicyVersions") + .formParam("PolicyArn", NOT_AN_ARN), UNPARSEABLE); + expectValidationError(iam("TagPolicy").formParam("PolicyArn", NOT_AN_ARN) + .formParam("Tags.member.1.Key", "team") + .formParam("Tags.member.1.Value", "iam"), UNPARSEABLE); + } + + /** + * The declared length still answers first, which is the order #6402 settled: a value too short + * to be any ARN breaks the constraint the model publishes and never reaches the parse check. + */ + @Test + void aValueUnderTheDeclaredMinimumStillAnswersTheLength() { + expectValidationError(iam("AttachUserPolicy") + .formParam("UserName", createPrincipal("User")).formParam("PolicyArn", "short"), + ONE_VIOLATION + "Value at 'policyArn' failed to satisfy constraint: " + + "Member must have length greater than or equal to 20"); + expectValidationError(iam("GetPolicy").formParam("PolicyArn", "short"), + ONE_VIOLATION + "Value at 'policyArn' failed to satisfy constraint: " + + "Member must have length greater than or equal to 20"); + } + + /** + * The control that keeps the check narrow: a value that parses as an ARN and names no policy + * is the 404 each operation already answered, and one that names a real policy still attaches. + */ + @Test + void anArnThatNamesNoPolicyIsStillNotFoundAndARealOneStillAttaches() { + String absent = "arn:aws:iam::000000000000:policy/arn-shape-absent"; + iam("GetPolicy").formParam("PolicyArn", absent).when().post("/").then().statusCode(404) + .body(CODE, equalTo("NoSuchEntity")); + + String userName = createPrincipal("User"); + iam("AttachUserPolicy").formParam("UserName", userName).formParam("PolicyArn", absent) + .when().post("/").then().statusCode(404).body(CODE, equalTo("NoSuchEntity")); + iam("AttachUserPolicy").formParam("UserName", userName).formParam("PolicyArn", READ_ONLY) + .when().post("/").then().statusCode(200); + iam("DetachUserPolicy").formParam("UserName", userName).formParam("PolicyArn", READ_ONLY) + .when().post("/").then().statusCode(200); + } + + /** + * A principal simulation handed a wildcard where an entity ARN belongs is refused on the + * entity rather than on the member's length, which is what the recording shows for a value of + * one character against a declared minimum of 20. + */ + @Test + void aWildcardPolicySourceArnIsRefusedOnTheEntity() { + String message = "Invalid Entity Arn: * does not clearly define entity type and name."; + iam("SimulatePrincipalPolicy").formParam("PolicySourceArn", "*") + .formParam("ActionNames.member.1", "s3:GetObject") + .when().post("/").then().statusCode(400) + .body(CODE, equalTo("InvalidInput")) + .body(TYPE, equalTo("Sender")) + .body(MESSAGE, equalTo(message)); + iam("GetContextKeysForPrincipalPolicy").formParam("PolicySourceArn", "*") + .when().post("/").then().statusCode(400) + .body(CODE, equalTo("InvalidInput")) + .body(MESSAGE, equalTo(message)); + } + + /** + * The other two answers for that member are unchanged: a value that clears 20 characters and + * parses as no ARN keeps the sentence its own capture settled, and a shorter one the length. + */ + @Test + void theOtherPolicySourceArnAnswersAreUnchanged() { + String unparseable = "arn:aws:iam:user/bob-and-then-some"; + iam("SimulatePrincipalPolicy").formParam("PolicySourceArn", unparseable) + .formParam("ActionNames.member.1", "s3:GetObject") + .when().post("/").then().statusCode(400) + .body(CODE, equalTo("InvalidInput")) + .body(MESSAGE, equalTo("Invalid ARN provided in the request: " + unparseable)); + expectValidationError(iam("SimulatePrincipalPolicy").formParam("PolicySourceArn", "short") + .formParam("ActionNames.member.1", "s3:GetObject"), + ONE_VIOLATION + "Value at 'policySourceArn' failed to satisfy constraint: " + + "Member must have length greater than or equal to 20"); + } +}