fix(iam): match IAM's constraint-violation message shape (#4613)

The MaxItems and ServiceNamespaces validation messages added in #4543 did
not read the way IAM's do. AWS leads with the error count, quotes the
offending value, and names only the bound that was actually broken, so a
MaxItems of 1001 says "Member must have value less than or equal to 1000"
and nothing about the minimum.

Both messages now go through the helpers this handler already had for that
shape. tagValidationError builds the scalar form, so it is renamed
validationError now that it serves more than the tag paths, and MaxItems
reports whichever side of the range it fell outside rather than reciting
both. checkListLength builds the list form, which deliberately does not
quote the value, and takes its limit as an argument so ServiceNamespaces
can use it instead of echoing 201 namespaces back in an error message.

The tests asserted only that the response contained "ValidationError",
which is what let the wrong shape through in the first place. They now
assert the whole message, on the parsed Error.Message element rather than
the raw body, since the XML escapes the quotes AWS puts around the value.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-28 22:02:28 -05:00
1 parent 26146a3f94
commit 240fd865f0
2 files changed
+48 -21

No files matched your search

@@ -1663,11 +1663,7 @@ public class IamQueryHandler {
throw new AwsException("InvalidInput",
"The request must include at least one service namespace.", 400);
}
if (namespaces.size() > MAX_SERVICE_NAMESPACES) {
throw new AwsException("ValidationError",
"Value at 'serviceNamespaces' failed to satisfy constraint: Member must have "
+ "length less than or equal to " + MAX_SERVICE_NAMESPACES, 400);
}
checkListLength(namespaces.size(), "serviceNamespaces", MAX_SERVICE_NAMESPACES);
List<GrantingPolicy> candidates = policiesForIdentity(accountId, arn);
// The response list carries one entry per requested namespace, so that is what a Marker
// walks through.
@@ -1737,11 +1733,13 @@ public class IamQueryHandler {
throw new AwsException("InvalidInput",
"The value " + raw + " at 'maxItems' is not a number.", 400);
}
if (limit < 1 || limit > MAX_MAX_ITEMS) {
throw new AwsException("ValidationError",
"Value at 'maxItems' failed to satisfy constraint: Member must have "
+ "value greater than or equal to 1 and less than or equal to "
+ MAX_MAX_ITEMS, 400);
if (limit < 1) {
throw validationError(raw, "maxItems",
"Member must have value greater than or equal to 1");
}
if (limit > MAX_MAX_ITEMS) {
throw validationError(raw, "maxItems",
"Member must have value less than or equal to " + MAX_MAX_ITEMS);
}
}
}
@@ -2196,14 +2194,14 @@ public class IamQueryHandler {
String at = "tags." + (i + 1) + ".member";
String keyViolation = tagKeyViolation(key);
if (keyViolation != null) {
throw tagValidationError(key, at + ".key", keyViolation);
throw validationError(key, at + ".key", keyViolation);
}
if (value.codePointCount(0, value.length()) > MAX_TAG_VALUE_LENGTH) {
throw tagValidationError(value, at + ".value",
throw validationError(value, at + ".value",
"Member must have length less than or equal to " + MAX_TAG_VALUE_LENGTH);
}
if (!TAG_VALUE_PATTERN.matcher(value).matches()) {
throw tagValidationError(value, at + ".value",
throw validationError(value, at + ".value",
"Member must satisfy regular expression pattern: " + TAG_VALUE_PATTERN.pattern());
}
if (caseInsensitiveKeys) {
@@ -2225,10 +2223,18 @@ public class IamQueryHandler {
}
private static void checkListLength(int members, String param) {
if (members > MAX_TAG_LIST_MEMBERS) {
checkListLength(members, param, MAX_TAG_LIST_MEMBERS);
}
/**
* IAM's constraint-violation shape for a list member. Unlike the scalar form it does not quote
* the value, matching how AWS reports an over-long list rather than echoing every member back.
*/
private static void checkListLength(int members, String param, int limit) {
if (members > limit) {
throw new AwsException("ValidationError",
"1 validation error detected: Value at '" + param + "' failed to satisfy constraint: "
+ "Member must have length less than or equal to " + MAX_TAG_LIST_MEMBERS, 400);
+ "Member must have length less than or equal to " + limit, 400);
}
}
@@ -2246,7 +2252,12 @@ public class IamQueryHandler {
return null;
}
private static AwsException tagValidationError(String value, String at, String constraint) {
/**
* IAM's constraint-violation shape for a scalar member, which leads with the error count and
* quotes the offending value. Only the bound actually broken is named, as AWS does: a value
* outside a range reports the side it fell outside, not the whole range.
*/
private static AwsException validationError(String value, String at, String constraint) {
return new AwsException("ValidationError",
"1 validation error detected: Value '" + value + "' at '" + at + "' failed to satisfy constraint: "
+ constraint, 400);
@@ -2273,7 +2284,7 @@ public class IamQueryHandler {
for (String key : keys) {
String violation = tagKeyViolation(key);
if (violation != null) {
throw tagValidationError(keys.toString(), "tagKeys",
throw validationError(keys.toString(), "tagKeys",
"Member must satisfy constraint: [" + violation + "]");
}
}
@@ -801,7 +801,10 @@ class ServiceLastAccessedIntegrationTest {
.formParam("JobId", jobId).formParam("Marker", "999").formParam("MaxItems", "0")
.when().post("/").then()
.statusCode(400)
.body(containsString("ValidationError"));
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value '0' at 'maxItems' failed to satisfy "
+ "constraint: Member must have value greater than or equal to 1"));
}
@Test
@@ -1006,7 +1009,13 @@ class ServiceLastAccessedIntegrationTest {
.formParam("JobId", generateJobFor(arn)).formParam("MaxItems", "1001")
.when().post("/").then()
.statusCode(400)
.body(containsString("ValidationError"));
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
// IAM leads with the count, quotes the offending value, and names only the bound that
// broke: an over-large MaxItems says nothing about the minimum. Asserted on the parsed
// element because the raw body escapes the quotes as &apos;.
.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"));
}
/** serviceNamespaceListType allows at most 200 members. */
@@ -1018,9 +1027,16 @@ class ServiceLastAccessedIntegrationTest {
for (int i = 1; i <= 201; i++) {
request.formParam("ServiceNamespaces.member." + i, "svc" + i);
}
request.when().post("/").then()
String body = request.when().post("/").then()
.statusCode(400)
.body(containsString("ValidationError"));
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo(
"1 validation error detected: Value at 'serviceNamespaces' failed to satisfy "
+ "constraint: Member must have length less than or equal to 200"))
.extract().asString();
// The list form does not quote the value, so a 201-member request must not echo them back.
assertTrue(!body.contains("svc201"), "expected the members not to be echoed, in: " + body);
}
/** An empty report is a valid answer, not a request that fails its own page-size check. */