fix(ssm): normalize names across parameter operations (#8278)

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Jackson Price <jxnprice@amazon.com>
Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-14 11:44:37 -04:00
1 parent 3e4d867de3
commit c97f913f5e
7 files changed
+900 -10

No files matched your search

@@ -164,7 +164,7 @@ public class SsmJsonHandler {
}
private Response handleGetParameter(JsonNode request, String region, String authorization) {
String name = request.path("Name").asText();
String name = normalizeParameterName(request.path("Name").asText(), region);
boolean withDecryption = request.path("WithDecryption").asBoolean(false);
authorizeSecretReads(List.of(name), withDecryption, region, authorization);
Parameter param = ssmService.getParameter(name, withDecryption, region);
@@ -175,9 +175,30 @@ public class SsmJsonHandler {
return Response.ok(response).build();
}
private String normalizeParameterName(String name, String region) {
String normalized = SsmService.withoutSurroundingSpaces(name);
if (normalized.indexOf(' ') >= 0 && !ssmService.hasStoredParameter(normalized, region)) {
throw parameterNameSpaceError();
}
return normalized;
}
private String normalizeParameterPath(String path, String region) {
String normalized = SsmService.withoutSurroundingSpaces(path);
if (normalized.indexOf(' ') >= 0 && !ssmService.hasStoredParametersUnderPath(normalized, region)) {
throw parameterNameSpaceError();
}
return normalized;
}
private static AwsException parameterNameSpaceError() {
return new AwsException("ValidationException",
"Parameter names cannot contain spaces between characters.", 400);
}
private Response handleGetParameters(JsonNode request, String region, String authorization) {
List<String> names = new ArrayList<>();
request.path("Names").forEach(n -> names.add(n.asText()));
request.path("Names").forEach(n -> names.add(normalizeParameterName(n.asText(), region)));
boolean withDecryption = request.path("WithDecryption").asBoolean(false);
authorizeSecretReads(names, withDecryption, region, authorization);
@@ -242,7 +263,7 @@ public class SsmJsonHandler {
throw SmithyValidationErrors.validationException(SmithyValidationErrors.notNull("path"));
}
PageRequest pageRequest = pageRequest(request, GET_PARAMETERS_BY_PATH_MAX_RESULTS);
String path = request.path("Path").asText();
String path = normalizeParameterPath(request.path("Path").asText(), region);
boolean recursive = request.path("Recursive").asBoolean(false);
boolean withDecryption = request.path("WithDecryption").asBoolean(false);
@@ -303,7 +324,7 @@ public class SsmJsonHandler {
private Response handleGetParameterHistory(JsonNode request, String region) {
PageRequest pageRequest = pageRequest(request, GET_PARAMETER_HISTORY_MAX_RESULTS);
String name = request.path("Name").asText();
String name = normalizeParameterName(request.path("Name").asText(), region);
boolean withDecryption = request.path("WithDecryption").asBoolean(false);
List<ParameterHistory> history = ssmService.getParameterHistory(name, region);
@@ -1154,7 +1175,7 @@ public class SsmJsonHandler {
}
private Response handleLabelParameterVersion(JsonNode request, String region) {
String name = request.path("Name").asText();
String name = normalizeParameterName(request.path("Name").asText(), region);
Long parameterVersion = request.hasNonNull("ParameterVersion")
? request.path("ParameterVersion").asLong()
: null;
@@ -1204,8 +1204,7 @@ public class SsmService implements ResourceProvider, StoredResourceArns {
}
/**
* The name without its leading and trailing spaces, as {@code PutParameter} stores it. Only the space character
* is removed; a tab, newline or other whitespace stays and fails name validation.
* Removes leading and trailing ASCII spaces. Other whitespace is preserved for the operation's validation.
*/
public static String withoutSurroundingSpaces(String name) {
if (name == null) {
@@ -1408,6 +1407,33 @@ public class SsmService implements ResourceProvider, StoredResourceArns {
return parameterName(nameOrArn, region, true);
}
/** Whether a read names a stored parameter in the caller's scope, including a version or label selector. */
boolean hasStoredParameter(String nameOrArn, String region) {
if (nameOrArn.startsWith(SECRET_REFERENCE_PREFIX)) {
return false;
}
String name = parameterReadName(nameOrArn, region);
if (AwsArnUtils.looksLikeArn(name)) {
return false;
}
if (parameterStore.get(regionKey(region, name)).isPresent()) {
return true;
}
int separator = name.lastIndexOf(':');
if (separator <= 0 || separator == name.length() - 1
|| name.substring(separator + 1).indexOf(' ') >= 0) {
return false;
}
return parameterStore.get(regionKey(region, name.substring(0, separator))).isPresent();
}
/** A legacy hierarchy is readable only while the caller's account and region contain a parameter below it. */
boolean hasStoredParametersUnderPath(String path, String region) {
String normalizedPath = path.endsWith("/") ? path : path + "/";
String prefix = regionKey(region, normalizedPath);
return parameterStore.keys().stream().anyMatch(key -> key.startsWith(prefix));
}
/**
* The stored name a tagging {@code ResourceId} addresses. A legacy {@code parameter<name>} ARN of the request's
* partition, region and account is read as {@code parameter/<name>}; see {@link #withLegacyParameterSlash}.