fix(iam): validate RoleName in CreateRole (#5374)

Follow-up to #5289.

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-09 23:00:28 -04:00
1 parent 477e3ef813
commit cb673802e0
3 files changed
+85

No files matched your search

@@ -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<String, String> 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