From cb673802e03511e7fbb09b609f45c314eb562fd1 Mon Sep 17 00:00:00 2001 From: Matheus Oliveira <99829531+omatheusmesmo@users.noreply.github.com> Date: Sat, 10 Oct 2026 00:00:28 -0300 Subject: [PATCH] fix(iam): validate RoleName in CreateRole (#5374) Follow-up to #5289. Signed-off-by: Matheus Oliveira --- docs/services/iam.md | 2 + .../floci/services/iam/IamService.java | 22 +++++++ .../services/iam/IamIntegrationTest.java | 61 +++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/docs/services/iam.md b/docs/services/iam.md index 7dce59ef1b..b1f7420be1 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -65,6 +65,8 @@ type. | UntagRole | Removes tags from an IAM role. | | ListRoleTags | Lists tags stored for an IAM role. | +`CreateRole` checks `RoleName` as AWS does: 1-64 characters of `[\w+=,.@-]`, otherwise `ValidationError`. A colon is never valid, so no role can take the name of the EC2 instance identity role, `aws:ec2-instance`. + ### Policies | Action | Description | diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java index 6010235bc9..997d9c8e1d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/IamService.java @@ -167,6 +167,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider { /** CustomSuffix as AWS constrains it: 1-64 characters of {@code [\w+=,.@-]}. */ private static final Pattern CUSTOM_SUFFIX_PATTERN = Pattern.compile("[\\w+=,.@-]{1,64}"); private static final int ROLE_NAME_MAX_LENGTH = 64; + /** roleNameType's character set. Excluding {@code :} is what reserves names such as {@code aws:ec2-instance} for AWS. */ + private static final Pattern ROLE_NAME_PATTERN = Pattern.compile("[\\w+=,.@-]+"); /** groupNameType / instanceProfileNameType: 1-128 characters of {@code [\w+=,.@-]}. */ private static final Pattern IAM_RESOURCE_NAME_PATTERN = Pattern.compile("[\\w+=,.@-]{1,128}"); /** @@ -1011,6 +1013,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { public IamRole createRole(String roleName, String path, String assumeRolePolicyDocument, String description, int maxSessionDuration, Map tags, String permissionsBoundaryArn) { + validateRoleName(roleName); if (permissionsBoundaryArn != null) { requirePolicy(permissionsBoundaryArn); // validate before anything is created } @@ -1056,6 +1059,25 @@ public class IamService implements SessionAccountLookup, ResourceProvider { return roles.get(roleName); } + /** roleNameType: 1-64 characters of {@code [\w+=,.@-]}, reported the way AWS reports a model constraint. */ + private static void validateRoleName(String roleName) { + String constraint; + if (roleName == null) { + constraint = "Member must not be null"; + } else if (roleName.isEmpty()) { + constraint = "Member must have length greater than or equal to 1"; + } else if (roleName.length() > ROLE_NAME_MAX_LENGTH) { + constraint = "Member must have length less than or equal to " + ROLE_NAME_MAX_LENGTH; + } else if (!ROLE_NAME_PATTERN.matcher(roleName).matches()) { + constraint = "Member must satisfy regular expression pattern: " + ROLE_NAME_PATTERN.pattern(); + } else { + return; + } + String value = roleName == null ? "null" : "'" + roleName + "'"; + throw new AwsException("ValidationError", "1 validation error detected: Value " + value + + " at 'roleName' failed to satisfy constraint: " + constraint, 400); + } + /** * AWS publishes UnmodifiableEntity on twelve role actions, and its message names the linked * service the caller has to go through instead. This guards the eleven of them the emulator diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java index da8e73294b..caf1ba1828 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java @@ -9,6 +9,8 @@ import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import java.util.Map; + import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.*; @@ -1560,4 +1562,63 @@ class IamIntegrationTest { .when().post("/").then() .statusCode(404); } + + @Test + @Order(86) + void createRoleRejectsANameOutsideTheRoleNamePattern() { + String pattern = "Member must satisfy regular expression pattern: [\\w+=,.@-]+"; + Map constraints = Map.of( + "aws:ec2-instance", pattern, + "bad/name", pattern, + "x".repeat(65), "Member must have length less than or equal to 64"); + for (Map.Entry entry : constraints.entrySet()) { + String name = entry.getKey(); + given() + .formParam("Action", "CreateRole") + .formParam("RoleName", name) + .formParam("AssumeRolePolicyDocument", TRUST_POLICY) + .header("Authorization", IAM_AUTH) + .when().post("/").then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("ValidationError")) + .body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value '" + name + + "' at 'roleName' failed to satisfy constraint: " + entry.getValue())); + + given() + .formParam("Action", "GetRole") + .formParam("RoleName", name) + .header("Authorization", IAM_AUTH) + .when().post("/").then() + .statusCode(404); + } + + given() + .formParam("Action", "CreateRole") + .formParam("AssumeRolePolicyDocument", TRUST_POLICY) + .header("Authorization", IAM_AUTH) + .when().post("/").then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("ValidationError")) + .body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value null" + + " at 'roleName' failed to satisfy constraint: Member must not be null")); + + given() + .formParam("Action", "CreateRole") + .formParam("RoleName", "") + .formParam("AssumeRolePolicyDocument", TRUST_POLICY) + .header("Authorization", IAM_AUTH) + .when().post("/").then() + .statusCode(400) + .body("ErrorResponse.Error.Code", equalTo("ValidationError")) + .body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value ''" + + " at 'roleName' failed to satisfy constraint: Member must have length greater than or equal to 1")); + + given() + .formParam("Action", "CreateRole") + .formParam("RoleName", "Ok_+=,.@-" + "x".repeat(55)) + .formParam("AssumeRolePolicyDocument", TRUST_POLICY) + .header("Authorization", IAM_AUTH) + .when().post("/").then() + .statusCode(200); + } }