From 8b625f7e30fd976453002e7e3c1a6ef5f93b9516 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:02:40 -0400 Subject: [PATCH] fix(iam): hold a blank required member to its shape rather than reading it as absent (#8058) A required member the request carried empty was answered as a member it never carried: `requireParam` threw the null violation for a blank value as well as for an absent one, and `validatedPolicySourceArn` handed a blank to the resolver that answers an absent `PolicySourceArn`. On this protocol an empty value is a member the request carries, so what a blank breaks is the shape declared for it, and that is the constraint AWS's validation layer reports. For nearly every member nothing moves, because the constraints read ahead of the operation already answer a blank against the minimum, the pattern or the value set its shape declares: `RoleName=` breaks its pattern, `Status=` its value set, `PolicyDocument=` both its minimum and its pattern. Of every member read through `requireParam`, only `serviceName` on `CreateServiceSpecificCredential` declares none of the three. A blank there reaches the operation now, where a service name nothing supports is `NoSuchEntity` with the name echoed as the caller sent it, which is what LocalStack's AWS-validated snapshots record for a malformed name as well as for an unsupported one. `PolicySourceArn` is the one `arnType` member this package reads by hand, the simulators answering an absent one in a sentence of their own. A blank is held to the 20 characters the member declares now, which is the answer `GetContextKeysForPrincipalPolicy` gives for the same member through the model, so the two readers of it agree on the same input. One left out keeps that sentence. `docs/services/iam.md` records the split beside the wording an absent member answers with, and four tests in the constraints class pin both halves on both members. One assertion in the service-specific credential tests pinned the old reading, a blank name said never to reach the service check, and it moves with the fix: the blank reaches it and the absent name is what the required-parameter check answers. --- docs/services/iam.md | 14 +++ .../aws/services/iam/IamQueryHandler.java | 55 ++++++-- .../IamRequestConstraintsIntegrationTest.java | 119 ++++++++++++++++++ ...viceSpecificCredentialIntegrationTest.java | 11 +- 4 files changed, 187 insertions(+), 12 deletions(-) diff --git a/docs/services/iam.md b/docs/services/iam.md index 2fa10370b6..de4f9ecf7b 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -379,6 +379,20 @@ one edit rather than across the service, and a capture of some other omitted mem or split `Status` from the rest in one edit too. The services that do quote their values, STS among them, still write `Value null at ''`. +A member the request carries empty is a value rather than a member it left out. An empty value +arrives on this protocol as the parameter spelled with nothing after it, so what it breaks is the +shape declared for it, answered ahead of the operation: `RoleName=` breaks its pattern, `Status=` +its value set, `VersionId=` its pattern, and `PolicyDocument=` both its minimum length and its +pattern, reported together. Only a member the request never carried reads as absent. + +The two members read by hand rather than through the model follow the same split. A blank +`PolicySourceArn` is held to the 20 characters `arnType` declares, which is the answer +`GetContextKeysForPrincipalPolicy` gives for the same member through the model, while one left out +keeps the sentence the simulators answer it with, `PolicySourceArn is required.`. `ServiceName` on +`CreateServiceSpecificCredential` declares no length, no pattern and no value set, so nothing ahead +of the operation holds a blank: it reaches the operation, where a service name nothing supports is +`NoSuchEntity` with the name echoed as the caller sent it. + ### Two answers the model carries since a capture settled them `statusType` and `CreateServiceLinkedRole`'s `AWSServiceName` were answered by hand here until 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 aeb10a6659..b14d65fcb6 100644 --- a/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java +++ b/src/main/java/io/floci/aws/services/iam/IamQueryHandler.java @@ -1422,10 +1422,32 @@ public class IamQueryHandler { private Response handleUpdateAssumeRolePolicy(MultivaluedMap params) { iamService.updateAssumeRolePolicy(requireParam(params, "RoleName"), - requireParam(params, "PolicyDocument")); + requireTrustPolicyDocument(params)); return Response.ok(AwsQueryResponse.envelopeNoResult("UpdateAssumeRolePolicy", AwsNamespaces.IAM)).build(); } + /** + * The trust policy this operation writes, which a blank is not. {@code policyDocumentType} + * declares a minimum of 1 beside a pattern that admits the space, tab, line feed and carriage + * return characters, so the shape does not exclude a blank, and this write replaces the policy + * a role is already assumed under: the trust evaluator reads a blank document as matching + * nothing, which would leave a role that could be assumed unassumable. + * + *

So the one value the shape lets through here is answered with the modelled + * {@code MalformedPolicyDocument}, worded as IAM's trust-policy validator words a document that + * does not parse, which is what that validator answers rather than the sentence an identity + * policy's gets. Nothing further is read: every other document that is no policy is taken by + * this operation and by every other write, which is one gap of its own rather than this + * member's. + */ + private String requireTrustPolicyDocument(MultivaluedMap params) { + String document = requireParam(params, "PolicyDocument"); + if (document.isBlank()) { + throw new AwsException("MalformedPolicyDocument", "This policy contains invalid Json", 400); + } + return document; + } + private Response handleTagRole(MultivaluedMap params) { iamService.tagRole(getParam(params, "RoleName"), extractTags(params, true)); return Response.ok(AwsQueryResponse.envelopeNoResult("TagRole", AwsNamespaces.IAM)).build(); @@ -2427,17 +2449,19 @@ public class IamQueryHandler { * {@code PolicySourceArn} held to the 20 to 2048 characters {@code arnType} declares, before * the entity it names is resolved. IAM answers that minimum on a sibling ARN member with a * {@code ValidationError} naming the length, so a value too short to be any ARN is refused on - * its shape rather than resolved to whatever follows its last slash. + * its shape rather than resolved to whatever follows its last slash. A blank is such a value: + * the request carried it, so it is held to the same minimum, which is the answer + * {@code GetContextKeysForPrincipalPolicy} gives for the same member through the walk. * *

A value that clears 20 characters and still parses as no ARN is left to the resolver * below, which refuses it as {@code InvalidInput} on a capture of its own: the member publishes - * a length and no pattern, so there is no shape to hold it to here. An absent or blank value - * is left there too, that resolver reporting it. + * a length and no pattern, so there is no shape to hold it to here. An absent member is left + * there too, that resolver answering it in the operation's own sentence. */ private String validatedPolicySourceArn(MultivaluedMap params) { String arn = getParam(params, "PolicySourceArn"); - if (arn == null || arn.isBlank()) { - return arn; + if (arn == null) { + return null; } return arnOfDeclaredLength(arn, "policySourceArn"); } @@ -3617,14 +3641,23 @@ public class IamQueryHandler { * malformed request read as a valid empty answer, so anything the model marks required is read * through here instead. * - *

A blank value is a value the request carries, and the constraints read ahead of this - * answer it against the minimum length its shape declares. What is left here is a member the - * model gives no minimum and an operation the vendored file does not carry, where a blank is - * still read as a member that never arrived rather than passed to the operation. + *

Only an absent member is reported as absent. A blank value is a value the request + * carries on this protocol, so what it breaks is the shape declared for it: the constraints + * read ahead of this answer every member declaring a minimum, a pattern or a value set. + * + *

Satisfying a shape is not the same as being usable, and this reader does not stand in for + * the rules above it. Fourteen required members declare a shape a blank satisfies: a minimum of + * 1 beside a pattern admitting the space, tab, line feed and carriage return characters, or + * none of the three at all. Seven of those are policy documents, and one that is no policy + * document reaches the operation and is stored, because nothing here holds a policy document to + * being one on a write; that gap takes a bare {@code +} the same way, so it is wider than a + * blank and is not this reader's to close. {@code serviceName} on + * {@code CreateServiceSpecificCredential} declares none of the three and answers a service name + * nothing supports. */ private String requireParam(MultivaluedMap params, String name) { String value = params.getFirst(name); - if (value == null || value.isBlank()) { + if (value == null) { throw IamValidationErrors.notNull(memberPath(name)); } return value; diff --git a/src/test/java/io/floci/aws/services/iam/IamRequestConstraintsIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/IamRequestConstraintsIntegrationTest.java index 82eb48db1f..f3a341ad50 100644 --- a/src/test/java/io/floci/aws/services/iam/IamRequestConstraintsIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamRequestConstraintsIntegrationTest.java @@ -17,6 +17,10 @@ import static org.hamcrest.Matchers.not; * layer runs ahead of the operation and answers {@code ValidationError}, which is the ordering * #6402 settled for this package. * + *

A member the request carries empty is a value, not a member it left out: what a blank breaks + * is the shape declared for it, and only an absent member is reported as absent. That holds for the + * two members this package still reads by hand as well as for the ones the walk reads. + * *

{@code AttachRolePolicy}'s {@code RoleName} is the parity case rather than a change: * {@code IamService.validateRoleName} answers it from the same bounds and the same pattern, and * both write the message IAM writes, which names the member with no value beside it: that is @@ -383,4 +387,119 @@ class IamRequestConstraintsIntegrationTest { + " 'permissions.parameters.1.member.name' failed to satisfy constraint:" + " Member must have length greater than or equal to 5")); } + + /** + * {@code PolicySourceArn} is read by hand on the simulators and through the walk on + * {@code GetContextKeysForPrincipalPolicy}. A blank is a value on either, so both hold it to + * the 20 characters {@code arnType} declares and answer in the same words. + */ + @Test + void aBlankArnReadByHandBreaksItsLengthRatherThanReadingAsAbsent() { + String refusal = "1 validation error detected: Value at 'policySourceArn' failed to satisfy" + + " constraint: Member must have length greater than or equal to 20"; + + iam("SimulatePrincipalPolicy") + .formParam("PolicySourceArn", "") + .formParam("ActionNames.member.1", "s3:GetObject") + .when().post("/").then().statusCode(400) + .body(CODE, equalTo("ValidationError")) + .body(MESSAGE, equalTo(refusal)); + + iam("GetContextKeysForPrincipalPolicy") + .formParam("PolicySourceArn", "") + .when().post("/").then().statusCode(400) + .body(CODE, equalTo("ValidationError")) + .body(MESSAGE, equalTo(refusal)); + } + + /** + * The member left out keeps the sentence the operation answers it with, which is the answer + * those two actions already carried and which no capture moves. + */ + @Test + void thatArnLeftOutKeepsTheOperationsOwnSentence() { + iam("SimulatePrincipalPolicy") + .formParam("ActionNames.member.1", "s3:GetObject") + .when().post("/").then().statusCode(400) + .body(CODE, equalTo("ValidationError")) + .body(MESSAGE, equalTo("PolicySourceArn is required.")); + } + + /** + * {@code serviceName} declares no length, no pattern and no value set, so nothing ahead of the + * operation holds a blank and the operation answers it: a service name nothing supports is + * {@code NoSuchEntity}, which is what LocalStack's AWS-validated snapshots record for a + * malformed name as well as for an unsupported one. The name is echoed as the caller sent it, + * which is why nothing stands between the two spaces. + * + *

The user is created so the blank name is the only thing wrong with the request. IAM reads + * the user ahead of the service name and answers a user that is not there first, which + * LocalStack's snapshot of a request carrying neither records, so naming a user that was never + * created would be asking for an answer a blank name is not what earns. + */ + @Test + void aBlankValueTheModelConstrainsInNoWayReachesTheOperation() { + String userName = "constraints-blank-service-" + System.nanoTime(); + iam("CreateUser").formParam("UserName", userName).when().post("/").then().statusCode(200); + try { + iam("CreateServiceSpecificCredential") + .formParam("UserName", userName) + .formParam("ServiceName", "") + .when().post("/").then().statusCode(404) + .body(CODE, equalTo("NoSuchEntity")) + .body(MESSAGE, equalTo("No such service is supported for Service Specific" + + " Credentials")); + } finally { + iam("DeleteUser").formParam("UserName", userName).when().post("/"); + } + } + + /** + * The one member a blank reaches the store through: this write replaces the policy a role is + * assumed under, and a blank trust policy matches nothing, so the role it was written to could + * no longer be assumed. The shape admits the value, a minimum of 1 beside a pattern that takes + * whitespace, so the operation answers it with the modelled {@code MalformedPolicyDocument} and + * the stored policy stays as it was. + */ + @Test + void aBlankTrustPolicyIsRefusedAndLeavesTheStoredOneStanding() { + String roleName = "constraints-blank-trust-" + System.nanoTime(); + iam("CreateRole") + .formParam("RoleName", roleName) + .formParam("AssumeRolePolicyDocument", TRUST_POLICY) + .when().post("/").then().statusCode(200); + try { + iam("UpdateAssumeRolePolicy") + .formParam("RoleName", roleName) + .formParam("PolicyDocument", " ") + .when().post("/").then().statusCode(400) + .body(CODE, equalTo("MalformedPolicyDocument")) + .body(MESSAGE, equalTo("This policy contains invalid Json")); + + iam("GetRole") + .formParam("RoleName", roleName) + .when().post("/").then().statusCode(200) + .body("GetRoleResponse.GetRoleResult.Role.AssumeRolePolicyDocument", + containsString("sts:AssumeRole")); + + // A document the member's shape admits and this operation writes is still written. + iam("UpdateAssumeRolePolicy") + .formParam("RoleName", roleName) + .formParam("PolicyDocument", TRUST_POLICY) + .when().post("/").then().statusCode(200); + } finally { + iam("DeleteRole").formParam("RoleName", roleName).when().post("/"); + } + } + + /** The same member left out is still a member the request never carried. */ + @Test + void thatValueLeftOutIsStillReportedAsAbsent() { + iam("CreateServiceSpecificCredential") + .formParam("UserName", "constraints-absent-service") + .when().post("/").then().statusCode(400) + .body(CODE, equalTo("ValidationError")) + .body(MESSAGE, equalTo("1 validation error detected: Value at 'serviceName' failed to" + + " satisfy constraint: Member must not be null")); + } } diff --git a/src/test/java/io/floci/aws/services/iam/ServiceSpecificCredentialIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/ServiceSpecificCredentialIntegrationTest.java index cfbabe89a5..b03e517f4b 100644 --- a/src/test/java/io/floci/aws/services/iam/ServiceSpecificCredentialIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/ServiceSpecificCredentialIntegrationTest.java @@ -310,9 +310,18 @@ class ServiceSpecificCredentialIntegrationTest { .body(not(containsString("NotSupportedService"))); } - // An absent or blank name never reaches that check: it fails the required-parameter one. + // A blank name is a value the request carried, and `serviceName` declares no length, + // pattern or value set for anything ahead of the operation to hold it to, so it reaches + // the same check with nothing standing where the name goes. Only an absent one fails the + // required-parameter check. iam("CreateServiceSpecificCredential") .formParam("UserName", userName).formParam("ServiceName", "") + .when().post("/").then().statusCode(404) + .body(containsString("NoSuchEntity")) + .body(containsString("No such service is supported for Service Specific Credentials")); + + iam("CreateServiceSpecificCredential") + .formParam("UserName", userName) .when().post("/").then().statusCode(400).body(containsString("ValidationError")); }