Light, fluffy, and always free - The AWS Local Emulator alternative
fix(iam): validate the values each ReplacementValues entry carries (#6478)
Priamo Germosen68e7b313a806
diff --git a/docs/services/iam.md b/docs/services/iam.mdindex 5bad7292fb..345f9fe258 100644--- a/docs/services/iam.md+++ b/docs/services/iam.md@@ -114,8 +114,18 @@ question and is not read here either.The declared constraints are read before the template is looked for, which is the order AWSvalidates in: `TemplateArn` is held to `arnType`'s 20 to 2048 characters, `MinorVersion` and`TemplateMinorVersion` are held to being integers and answer `InvalidInput` for a value that is-not a number, and `ReplacementValues` is held to the 30 entries it declares. A request malformed-in any of those is a 400 rather than the 404.+not a number, and `ReplacementValues` is held to the 30 entries it declares and each of those+entries to the `Values` it is required to carry, between 1 and 20 of them. A request malformed in+any of those is a 400 rather than the 404, and one malformed in several is answered with every+violation at once, the map's own length ahead of its entries. An entry's values are counted by the+member being present rather than by what it holds, the list's members publishing no constraint of+their own, so a replacement value sent empty is a value.++The message for a broken entry names it by its key, as `replacementValues.Department.values`, which+is how IAM reports a bad value in its account properties map. No recorded AWS violation on a map+member is on hand to confirm that spelling, so it is Floci's reading of it: a capture would either+keep it or replace it with the nested form, where the path is the map itself and the constraint+reads "Map value must satisfy constraint".### Policiesdiff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.javaindex 133a1dfe1c..73d2dda296 100644--- a/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamQueryHandler.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.iam;import io.github.hectorvent.floci.core.common.*;+import io.github.hectorvent.floci.core.common.AwsQueryLists;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.SmithyValidationErrors;import io.github.hectorvent.floci.services.iam.model.AccessKey;@@ -50,6 +51,7 @@ import java.util.Optional;import java.util.Set;import java.util.TreeMap;import java.util.function.Function;+import java.util.regex.Matcher;import java.util.regex.Pattern;import java.util.stream.Stream;@@ -81,6 +83,17 @@ public class IamQueryHandler {private static final Pattern USER_NAME_PATTERN = Pattern.compile("[\\w+=,.@-]+");/** {@code mapStringReplacementValueEntry} on {@code AcquireRole}: at most 30 entries. */private static final int REPLACEMENT_VALUES_MAX_ENTRIES = 30;+ /** {@code replacementValueListType}, the values one entry carries: 1 to 20 of them. */+ private static final int REPLACEMENT_VALUES_MAX_PER_ENTRY = 20;+ /** The Query spelling of a {@code ReplacementValues} entry, which carries a key and a value. */+ private static final String REPLACEMENT_VALUES_ENTRY = "ReplacementValues.entry.";+ /**+ * One entry's key, capturing the prefix the rest of that entry is read back under: an index is+ * taken as the request spells it, and ordered by the number it reads as, an index that is no+ * number sorting after every index that is one.+ */+ private static final Pattern REPLACEMENT_VALUES_ENTRY_KEY =+ Pattern.compile("(" + Pattern.quote(REPLACEMENT_VALUES_ENTRY) + "[^.]+)\\.key");/** {@code sortKeyType} on {@code GetOrganizationsAccessReport}. */private static final Set<String> SORT_KEYS = Set.of("SERVICE_NAMESPACE_ASCENDING","SERVICE_NAMESPACE_DESCENDING", "LAST_AUTHENTICATED_TIME_ASCENDING",@@ -1567,24 +1580,67 @@ public class IamQueryHandler {private Response handleAcquireRole(MultivaluedMap<String, String> params) {String templateArn = requiredArn(params, "TemplateArn");requireIntegerIfPresent(params, "TemplateMinorVersion");- requireReplacementValuesWithinDeclaredSize(params);+ requireReplacementValuesWithinDeclaredShape(params);throw noSuchRoleTemplate(templateArn);}/**- * {@code ReplacementValues} is declared with at most 30 entries, and a Query map arrives as- * {@code ReplacementValues.entry.N.key}, so the keys are counted rather than the indices- * walked: a caller numbering them sparsely still sends as many values as it sends.+ * {@code ReplacementValues} is declared with at most 30 entries, and each entry holds a+ * {@code ReplacementValueEntry}, which declares {@code Values} required and bounds+ * {@code replacementValueListType} at 1 to 20 values. The API Reference spells the same thing+ * out, "Map Entries: Maximum number of 30 items" on the map and, on the entry,+ * "Array Members: Minimum number of 1 item. Maximum number of 20 items" with "Required: Yes".+ * All of it is read ahead of the template lookup, so a request that breaks any of it is the 400+ * the validation layer answers rather than a 404 that ignored the map it was handed.+ *+ * <p>The map's own length is reported before its entries, which is the order the shape is+ * walked, and every entry that is broken is reported rather than only the first: the validation+ * layer answers with each member it found broken. An entry is read off its key, as the map's+ * own count has always been, so a value sent under no key is not an entry the map could hold+ * and is not one here either, and an entry is read back under the index the request spelled+ * rather than under the number that index reads as, the parameters that carry it being named+ * after the spelling. The count takes every key, as it has since the cap landed, so two+ * spellings of one number are two entries to it and each is answered on its own.+ *+ * <p>An entry's values arrive as {@code ReplacementValues.entry.N.value.Values.member.M}, read+ * through the helper the other Query services read a list with, and they are counted by the+ * member being present rather than by what it holds: the list's members publish no constraint+ * of their own, so a replacement value sent empty is a value, where reading it as absent would+ * answer the required-member violation for a list that has one. A list of no members at all is+ * how an absent member reaches a Query service, so the lower bound of 1 is never reached on its+ * own, and an entry carrying nothing is the required member reading as null. The violation names+ * the entry by its key, the way the account properties map names one, rather than by whatever+ * index the request numbered it with.*/- private static void requireReplacementValuesWithinDeclaredSize(+ private static void requireReplacementValuesWithinDeclaredShape(MultivaluedMap<String, String> params) {- long entries = params.keySet().stream()- .filter(name -> name.startsWith("ReplacementValues.entry.") && name.endsWith(".key"))- .count();- if (entries > REPLACEMENT_VALUES_MAX_ENTRIES) {- throw SmithyValidationErrors.validationError("Value at 'replacementValues' failed to "- + "satisfy constraint: Member must have length less than or equal to "- + REPLACEMENT_VALUES_MAX_ENTRIES);+ List<String> entries = params.keySet().stream()+ .map(REPLACEMENT_VALUES_ENTRY_KEY::matcher)+ .filter(Matcher::matches)+ .map(matcher -> matcher.group(1))+ .sorted(Comparator.<String>comparingInt(AwsQueryLists::memberIndex)+ .thenComparing(Comparator.naturalOrder()))+ .toList();+ List<String> violations = new ArrayList<>();+ if (entries.size() > REPLACEMENT_VALUES_MAX_ENTRIES) {+ violations.add("Value at 'replacementValues' failed to satisfy constraint: Member "+ + "must have length less than or equal to " + REPLACEMENT_VALUES_MAX_ENTRIES);+ }+ for (String entry : entries) {+ String path = "replacementValues." + params.getFirst(entry + ".key") + ".values";+ Pattern members = AwsQueryLists.memberKeys(entry + ".value.Values", null);+ long values = params.keySet().stream()+ .filter(name -> members.matcher(name).matches())+ .count();+ if (values == 0) {+ violations.add(SmithyValidationErrors.notNull(path));+ } else if (values > REPLACEMENT_VALUES_MAX_PER_ENTRY) {+ violations.add("Value at '" + path + "' failed to satisfy constraint: Member must "+ + "have length less than or equal to " + REPLACEMENT_VALUES_MAX_PER_ENTRY);+ }+ }+ if (!violations.isEmpty()) {+ throw SmithyValidationErrors.validationError(violations);}}diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/RoleTemplateIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/RoleTemplateIntegrationTest.javaindex 7ba1584d2b..ed13f69877 100644--- a/src/test/java/io/github/hectorvent/floci/services/iam/RoleTemplateIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/iam/RoleTemplateIntegrationTest.java@@ -161,4 +161,128 @@ class RoleTemplateIntegrationTest {.statusCode(404).body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"));}++ /**+ * Each entry holds a {@code ReplacementValueEntry}, which declares {@code Values} required.+ * A Query list arrives as {@code ...value.Values.member.M}, so an entry sending none of those+ * is the required member reading as null, reported against the key the entry was sent under.+ */+ @Test+ void anEntryCarryingNoValuesIsTheRequiredMemberRatherThanThe404() {+ iam("AcquireRole")+ .formParam("TemplateArn", TEMPLATE_ARN)+ .formParam("ReplacementValues.entry.1.key", "Department")+ .when().post("/").then()+ .statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("ValidationError"))+ .body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value null "+ + "at 'replacementValues.Department.values' failed to satisfy constraint: "+ + "Member must not be null"))+ .body(not(containsString("NoSuchEntity")));+ }++ /**+ * {@code replacementValueListType} is 1 to 20, so a 21st value in one entry is a length+ * violation ahead of the lookup, while 20 of them reach the 404 the ARN earns.+ */+ @Test+ void moreValuesInOneEntryThanTheDeclaredMaximumIsAValidationError() {+ acquireWithValues(21).when().post("/").then()+ .statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("ValidationError"))+ .body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value at "+ + "'replacementValues.Department.values' failed to satisfy constraint: Member "+ + "must have length less than or equal to 20"));++ acquireWithValues(20).when().post("/").then()+ .statusCode(404)+ .body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"));+ }++ private static RequestSpecification acquireWithValues(int values) {+ RequestSpecification request = iam("AcquireRole")+ .formParam("TemplateArn", TEMPLATE_ARN)+ .formParam("ReplacementValues.entry.1.key", "Department");+ for (int value = 1; value <= values; value++) {+ request.formParam("ReplacementValues.entry.1.value.Values.member." + value, "v" + value);+ }+ return request;+ }++ /**+ * The list's members publish no constraint of their own, so a value sent empty is still a+ * value: the entry carries one, the required member is satisfied and the request reaches the+ * 404. That is the same reading {@code GetMFADevice} gives a blank {@code UserName}, where what+ * the request carries is the member being present rather than what it holds.+ */+ @Test+ void anEntryWhoseOnlyValueIsEmptyStillCarriesAValue() {+ iam("AcquireRole")+ .formParam("TemplateArn", TEMPLATE_ARN)+ .formParam("ReplacementValues.entry.1.key", "Department")+ .formParam("ReplacementValues.entry.1.value.Values.member.1", "")+ .when().post("/").then()+ .statusCode(404)+ .body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))+ .body(not(containsString("ValidationError")));+ }++ /**+ * A Query index is read as the request spells it, the parameters that carry an entry being+ * named after that spelling rather than after the number the spelling reads as. An entry+ * numbered {@code 01} holds its key and its values under {@code entry.01}, so that is where+ * they are read from and the entry is whole: reading it as entry 1 would look for parameters+ * nothing sent and answer the required member against a key it could not find either.+ *+ * <p>The 404 alone would not hold that, an entry nothing read at all reaching it too, so the+ * same entry left empty is sent as well: the violation naming {@code Department} is what says+ * the key was read under the spelling it arrived in rather than under {@code 1}.+ */+ @Test+ void anEntryNumberedWithALeadingZeroIsReadUnderTheSpellingItWasSent() {+ iam("AcquireRole")+ .formParam("TemplateArn", TEMPLATE_ARN)+ .formParam("ReplacementValues.entry.01.key", "Department")+ .formParam("ReplacementValues.entry.01.value.Values.member.1", "Engineering")+ .when().post("/").then()+ .statusCode(404)+ .body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))+ .body(not(containsString("ValidationError")));++ iam("AcquireRole")+ .formParam("TemplateArn", TEMPLATE_ARN)+ .formParam("ReplacementValues.entry.01.key", "Department")+ .when().post("/").then()+ .statusCode(400)+ .body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value null "+ + "at 'replacementValues.Department.values' failed to satisfy constraint: "+ + "Member must not be null"));+ }++ /**+ * The validation layer answers with every member it found broken, so a request that sends 31+ * entries and leaves two of them empty is told all three things at once: the map's own length+ * first, the shape being walked from the outside in, then the entries in the order their+ * numbers read rather than the order the parameters happen to be held in.+ */+ @Test+ void theMapsLengthAndEveryEmptyEntryAreReportedTogether() {+ RequestSpecification request = iam("AcquireRole").formParam("TemplateArn", TEMPLATE_ARN);+ for (int entry = 1; entry <= 31; entry++) {+ request.formParam("ReplacementValues.entry." + entry + ".key", "Parameter" + entry);+ if (entry != 2 && entry != 10) {+ request.formParam("ReplacementValues.entry." + entry + ".value.Values.member.1", "v");+ }+ }++ request.when().post("/").then()+ .statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("ValidationError"))+ .body("ErrorResponse.Error.Message", equalTo("3 validation errors detected: Value at "+ + "'replacementValues' failed to satisfy constraint: Member must have length "+ + "less than or equal to 30; Value null at 'replacementValues.Parameter2."+ + "values' failed to satisfy constraint: Member must not be null; Value null "+ + "at 'replacementValues.Parameter10.values' failed to satisfy constraint: "+ + "Member must not be null"));+ }}