fix(bedrock-agentcore): validate tag resource request maps (#9076)

AgentCore TagResource accepted any tags map. It now validates the map
before dispatch with the same rules create and update use: at most 50
entries, keys of 1 to 128 characters and values of 0 to 256 in the
AgentCore tag pattern. The handler opts into the strict tag payload
parser, so a malformed or non-object body is refused without changing
stored tags, while an empty map and an empty untag request stay valid.

Closes #7894
This commit is contained in:
galsuchetzky-act authored and GitHub committed 2026-11-20 07:18:43 -04:00
1 parent 704b5f1c3b
commit a3c45a4ce2
6 files changed
+264 -13

No files matched your search

@@ -43,6 +43,16 @@ public class BedrockAgentCoreTagHandler implements TagHandler {
return "bedrock-agentcore";
}
@Override
public boolean strictTagValidation() {
return true;
}
@Override
public boolean allowEmptyTagKeys() {
return true;
}
@Override
public Map<String, String> listTags(String region, String arn) {
return switch (kind(region, arn)) {
@@ -56,7 +66,9 @@ public class BedrockAgentCoreTagHandler implements TagHandler {
@Override
public void tagResource(String region, String arn, Map<String, String> tags) {
switch (kind(region, arn)) {
ResourceKind resourceKind = kind(region, arn);
BedrockAgentCoreTagValidation.validateTagMap(tags);
switch (resourceKind) {
case GATEWAY -> gatewayService.tagByArn(region, arn, tags);
case MEMORY -> memoryService.tagByArn(region, arn, tags);
case RUNTIME -> runtimeService.tagByArn(region, arn, tags);
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.BedrockTagKeyValueRules;
import java.util.Map;
/**
* AgentCore's tag key/value validation rules, shared by the service classes under this package
* that accept a {@code tags} field on create/update requests. {@code BedrockAgentCoreCredentialProviderService}
@@ -12,6 +14,8 @@ import io.floci.aws.core.common.BedrockTagKeyValueRules;
*/
final class BedrockAgentCoreTagValidation {
private static final int MAX_TAGS = 50;
private BedrockAgentCoreTagValidation() {
}
@@ -19,22 +23,44 @@ final class BedrockAgentCoreTagValidation {
if (tags == null || tags.isNull()) {
return;
}
if (!tags.isObject() || tags.size() > 50) {
throw new AwsException("ValidationException", "tags must be an object with at most 50 entries", 400);
if (!tags.isObject() || tags.size() > MAX_TAGS) {
throw new AwsException("ValidationException",
"tags must be an object with at most " + MAX_TAGS + " entries", 400);
}
tags.fields().forEachRemaining(entry -> {
String key = entry.getKey();
JsonNode rawValue = entry.getValue();
if (!BedrockTagKeyValueRules.validKey(key)) {
throw new AwsException("ValidationException", "tag key does not satisfy AgentCore constraints", 400);
}
validateKey(key);
if (!rawValue.isTextual()) {
throw new AwsException("ValidationException", "tag value must be a string", 400);
}
String value = rawValue.asText();
if (!BedrockTagKeyValueRules.validValue(value)) {
throw new AwsException("ValidationException", "tag value does not satisfy AgentCore constraints", 400);
}
validateValue(rawValue.asText());
});
}
static void validateTagMap(Map<String, String> tags) {
if (tags == null || tags.size() > MAX_TAGS) {
throw new AwsException("ValidationException",
"tags must be an object with at most " + MAX_TAGS + " entries", 400);
}
tags.forEach((key, value) -> {
validateKey(key);
validateValue(value);
});
}
private static void validateKey(String key) {
if (key == null || !BedrockTagKeyValueRules.validKey(key)) {
throw new AwsException("ValidationException", "tag key does not satisfy AgentCore constraints", 400);
}
}
private static void validateValue(String value) {
if (value == null) {
throw new AwsException("ValidationException", "tag value must be a string", 400);
}
if (!BedrockTagKeyValueRules.validValue(value)) {
throw new AwsException("ValidationException", "tag value does not satisfy AgentCore constraints", 400);
}
}
}