fix(iam): read declared constraints before the lookup they precede (#6402)

The validation AWS runs ahead of an operation never reaches its store, so a
request that breaks a declared constraint is refused whether or not the thing it
names exists. Four places here had the order the other way round, and each one
answered a 404 for a request AWS answers with a 400.

The three Access Advisor and Organizations report readers looked the job up
before reading JobId, MaxItems and Marker, so an unknown job carrying a
MaxItems of 0 answered NoSuchEntity, and a JobId of the wrong length was never
held to its shape at all. jobIDType is min 36 and max 36, and the fixed length
reports as one constraint, "Member must have length equal to 36", which is the
form lifecycleActionToken already uses for the same shape in Auto Scaling. The
MaxItems check moves out of paginate so a reader can run it early, and paginate
still runs it, being reached from readers that look nothing up.

GenerateServiceLastAccessedDetails had the same order on its Granularity enum,
reading it inside the generate that sits behind the ARN resolution, so a
granularity AWS refuses answered NoSuchEntity for an ARN that named nothing.
The check is its own method now and the handler runs it first.

PolicySourceArn is arnType, min 20, and the simulators resolved the entity out
of whatever followed the last slash, so ":user/bob" simulated against a user
named bob. A value too short to be any ARN is now the length ValidationError
IAM answers for a short ARN member. A value that clears 20 characters and still
parses as no ARN is left alone: the member publishes no pattern, and no source
records what AWS answers for one.

The service-specific credential id reported both of its length bounds in one
violation, so a short id was told about a 128-character ceiling it was nowhere
near. Each message names the one bound the value broke, which is how every
recorded IAM length violation reads: "Member must have length less than or
equal to 64" for a user name over 64, and "Member must have length greater than
or equal to 20" for an ARN under 20.

Seven tests. Four over the wire on the readers: a JobId of the wrong length
refused before any lookup on both the last-accessed reader and the entities
reader, the same on the Organizations reader with no organization built at all,
and an unknown job carrying a bad MaxItems, a bad Marker and then nothing wrong,
which pins the 400s and the surviving 404. One on the generator, where the
granularity is refused for an ARN that names nothing. One on both simulators for
a PolicySourceArn under 20. And the credential id's two bounds, each asserting
the other bound is absent from the message, with the credential itself still
listed once the refusals are done: a validation error that deleted something on
its way out would read the same from the response alone.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-24 08:38:33 -05:00
1 parent 1174922522
commit 62c202f073
8 files changed
+350 -60

No files matched your search

@@ -555,6 +555,47 @@ class IamIntegrationTest {
contains("aws:CurrentTime", "aws:username"));
}
/**
* {@code PolicySourceArn} is {@code arnType}, min 20, and the simulator used to resolve the
* entity out of whatever followed the last slash, so a value far too short to be an ARN
* simulated against a user named after its tail. IAM answers that minimum with a
* {@code ValidationError} naming the length, which is what both principal readers answer now.
*
* <p>A value that clears 20 characters and still parses as no ARN is deliberately untouched:
* the member publishes no pattern and nothing records what AWS answers for one.
*/
@Test
@Order(88)
void aPolicySourceArnUnderItsDeclaredLengthIsAValidationError() {
String expected = "1 validation error detected: Value at 'policySourceArn' failed to "
+ "satisfy constraint: Member must have length greater than or equal to 20";
given()
.formParam("Action", "SimulatePrincipalPolicy")
.formParam("PolicySourceArn", ":user/bob")
.formParam("ActionNames.member.1", "s3:GetObject")
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(expected));
given()
.formParam("Action", "GetContextKeysForPrincipalPolicy")
.formParam("PolicySourceArn", ":role/admin")
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(expected));
}
@Test
@Order(41)
void getContextKeysForPrincipalPolicyMergesAttachedAndExtraPolicies() {
@@ -460,4 +460,38 @@ class OrganizationsAccessReportIntegrationTest {
assertEquals("OrganizationalUnitNotFoundException",
report.getString(result("ErrorDetails.Code")));
}
/**
* The ordering DRIFT-3 was about, on this reader. {@code jobIDType} is a fixed 36 characters,
* so the shape is refused before anything is resolved: no organization is built here, and none
* has to be, which is the point.
*/
@Test
void aJobIdOutsideItsDeclaredLengthIsRefusedWithoutAnOrganization() {
iam(MANAGEMENT_ACCOUNT, "GetOrganizationsAccessReport").formParam("JobId", "nope")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value at 'jobId' failed to satisfy constraint: "
+ "Member must have length equal to 36"));
}
/**
* A well-formed id no job carries, with a {@code MaxItems} the model refuses: the paging
* members are read before the job lookup, so this is a 400 rather than the 404 the missing job
* would answer first.
*/
@Test
void anUnknownJobWithAMalformedMaxItemsIsAValidationErrorRatherThanNotFound() {
iam(MANAGEMENT_ACCOUNT, "GetOrganizationsAccessReport")
.formParam("JobId", "00000000-0000-4000-8000-000000000000")
.formParam("MaxItems", "1001")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value '1001' at 'maxItems' failed to satisfy "
+ "constraint: Member must have value less than or equal to 1000"));
}
}
@@ -39,6 +39,9 @@ class ServiceLastAccessedIntegrationTest {
return given().header("Authorization", IAM_AUTH).formParam("Action", action);
}
/** Well formed at the 36 characters jobIDType declares, and carried by no job. */
private static final String UNKNOWN_JOB_ID = "00000000-0000-4000-8000-000000000000";
private static String suffix() {
return Long.toString(System.nanoTime(), 36);
}
@@ -1090,9 +1093,8 @@ class ServiceLastAccessedIntegrationTest {
*/
@Test
void theRemainingEnumReportsAlsoOpenWithTheCount() {
// GenerateServiceLastAccessedDetails: the Granularity enum. The ARN has to resolve first,
// because the handler looks the target up before the granularity is checked, so a
// nonexistent one answers NoSuchEntity and never reaches it.
// GenerateServiceLastAccessedDetails: the Granularity enum, on an ARN that resolves. The
// enum is read ahead of that lookup now, which the test below pins on an ARN that does not.
String arn = createUser("granularity-" + suffix());
iam("GenerateServiceLastAccessedDetails")
.formParam("Arn", arn)
@@ -1109,4 +1111,79 @@ class ServiceLastAccessedIntegrationTest {
.body(containsString("1 validation error detected"))
.body(containsString("Member must satisfy enum value set"));
}
/**
* The ordering DRIFT-3 was about. {@code jobIDType} is a fixed 36 characters, so an id of the
* wrong length is a bad request on its shape and never reaches the store: AWS validates ahead
* of the operation, and the store it would have been looked up in is behind it.
*/
@Test
void aJobIdOutsideItsDeclaredLengthIsRefusedBeforeTheLookup() {
iam("GetServiceLastAccessedDetails").formParam("JobId", "nope")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value at 'jobId' failed to satisfy constraint: "
+ "Member must have length equal to 36"))
.body(not(containsString("NoSuchEntity")));
}
/** The same shape on the entities reader, which pages the same job through the same id. */
@Test
void theWithEntitiesReaderHoldsTheSameJobIdShape() {
iam("GetServiceLastAccessedDetailsWithEntities")
.formParam("JobId", "nope").formParam("ServiceNamespace", "s3")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", containsString("Member must have length equal to 36"));
}
/**
* The other half of the ordering: a well-formed id that no job carries, with a {@code MaxItems}
* the model refuses. The paging members are read before the job is looked up, so this is the
* 400 AWS answers rather than the 404 the missing job would otherwise produce first.
*/
@Test
void anUnknownJobWithAMalformedMaxItemsIsAValidationErrorRatherThanNotFound() {
iam("GetServiceLastAccessedDetails")
.formParam("JobId", UNKNOWN_JOB_ID).formParam("MaxItems", "0")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", containsString("at 'maxItems'"))
.body(not(containsString("NoSuchEntity")));
// And a marker that is no marker at all, on the same unknown job.
iam("GetServiceLastAccessedDetails")
.formParam("JobId", UNKNOWN_JOB_ID).formParam("Marker", "not-a-marker")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidInput"));
// The unknown job on its own is still a 404, so the ordering is what changed and not the
// answer for a request that breaks nothing.
iam("GetServiceLastAccessedDetails").formParam("JobId", UNKNOWN_JOB_ID)
.when().post("/").then()
.statusCode(404)
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"));
}
/**
* The same rule on the generator: {@code Granularity} is a declared enum, so it is refused
* whether or not the ARN names anything. It used to be read inside the generate, behind the
* lookup that resolves the target.
*/
@Test
void theGranularityIsRefusedWhateverTheArnNames() {
iam("GenerateServiceLastAccessedDetails")
.formParam("Arn", "arn:aws:iam::000000000000:user/laa-nobody-" + suffix())
.formParam("Granularity", "HOURLY")
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", containsString("Member must satisfy enum value set"))
.body(not(containsString("NoSuchEntity")));
}
}
@@ -646,22 +646,33 @@ class ServiceSpecificCredentialIntegrationTest {
* The id's other published constraint is a length of 20 to 128. A short id satisfies
* {@code [\w]+} and still fails, and one that breaks both constraints is reported as two
* validation errors rather than one, which is what the counted prefix is for.
*
* <p>Each message names the one bound the value broke. Every recorded IAM length violation
* reads that way, and a short id told about a 128-character ceiling would be refused for a
* constraint it did not break.
*/
@Test
void anIdOutsideItsPublishedLengthIsAValidationError() {
String userName = user();
create(userName, CODECOMMIT);
String credentialId = create(userName, CODECOMMIT);
for (String id : List.of("tooshort", "W".repeat(129))) {
iam("DeleteServiceSpecificCredential")
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId", id)
.when().post("/").then().statusCode(400)
.body(containsString("1 validation error detected"))
.body(containsString("Member must have length less than or equal to 128 and "
+ "greater than or equal to 20"))
.body(not(containsString("NoSuchEntity")));
}
iam("DeleteServiceSpecificCredential")
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId", "tooshort")
.when().post("/").then().statusCode(400)
.body(containsString("1 validation error detected"))
.body(containsString("Member must have length greater than or equal to 20"))
.body(not(containsString("less than or equal to 128")))
.body(not(containsString("NoSuchEntity")));
iam("DeleteServiceSpecificCredential")
.formParam("UserName", userName)
.formParam("ServiceSpecificCredentialId", "W".repeat(129))
.when().post("/").then().statusCode(400)
.body(containsString("1 validation error detected"))
.body(containsString("Member must have length less than or equal to 128"))
.body(not(containsString("greater than or equal to 20")))
.body(not(containsString("NoSuchEntity")));
// Short and outside the pattern at once: both constraints are reported.
iam("DeleteServiceSpecificCredential")
@@ -679,6 +690,12 @@ class ServiceSpecificCredentialIntegrationTest {
.formParam("ServiceSpecificCredentialId", "A".repeat(20))
.when().post("/").then().statusCode(404)
.body(containsString("NoSuchEntity"));
// The refusals deleted nothing. The validation runs ahead of the delete it precedes, so
// the credential created above is still the user's.
iam("ListServiceSpecificCredentials").formParam("UserName", userName)
.when().post("/").then().statusCode(200)
.body(containsString(credentialId));
}
@Test