Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(wafv2): enforce the 50-tag limit and key/value constraints on tags (#4118)

Matheus André93f8d1421acc
diff --git a/docs/services/wafv2.md b/docs/services/wafv2.md
index 0c844964b6..33392a86c0 100644
--- a/docs/services/wafv2.md
+++ b/docs/services/wafv2.md
@@ -56,6 +56,10 @@ WAF v2 resources are partitioned by `Scope`: `REGIONAL` (ALB, API Gateway, AppSy
`CreateIPSet` and `UpdateIPSet` require every entry in `Addresses` to be in CIDR notation matching the set's `IPAddressVersion`: an IPv4 address with a `/1`-`/32` prefix, or an IPv6 address with a `/1`-`/128` prefix (a `/0` prefix is rejected, matching AWS). A bare address with no prefix, a prefix outside that range, or an address that does not match the declared `IPAddressVersion` is rejected with `WAFInvalidParameterException`.
+## Tags
+
+Every WAF v2 resource accepts at most 50 tags. `TagResource`, `CreateWebACL`, `CreateIPSet`, `CreateRegexPatternSet` and `CreateRuleGroup` reject a request that would leave the resource with more than 50 tags with `WAFLimitsExceededException`; overwriting an existing key does not count toward the limit. Tag keys must be 1-128 characters and values 0-256 characters, both limited to letters, numbers, spaces and `_ . : / = + - @`. An invalid key or value is rejected with `WAFInvalidParameterException` (`Field: TAGS`, or `TAG_KEYS` for `UntagResource`).
+
## Example
```bash
diff --git a/src/main/java/io/github/hectorvent/floci/services/wafv2/WafV2Service.java b/src/main/java/io/github/hectorvent/floci/services/wafv2/WafV2Service.java
index 70615c733e..701e768709 100644
--- a/src/main/java/io/github/hectorvent/floci/services/wafv2/WafV2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/wafv2/WafV2Service.java
@@ -22,6 +22,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
+import java.util.regex.Pattern;
/**
* WAF v2 management-plane business logic. Resources are stored by the
@@ -32,6 +33,11 @@ import java.util.UUID;
@ApplicationScoped
public class WafV2Service {
+ private static final int MAX_TAGS = 50;
+ private static final int MAX_TAG_KEY_LENGTH = 128;
+ private static final int MAX_TAG_VALUE_LENGTH = 256;
+ private static final Pattern TAG_PATTERN = Pattern.compile("^[\\p{L}\\p{Z}\\p{N}_.:/=+\\-@]*$");
+
private final StorageBackend<String, WebAcl> webAclStore;
private final StorageBackend<String, IpSet> ipSetStore;
private final StorageBackend<String, RegexPatternSet> regexStore;
@@ -73,6 +79,7 @@ public class WafV2Service {
"AWS WAF couldn't perform the operation because some resource "
+ "in your request is a duplicate of an existing one.", 400);
}
+ validateTags(acl.getTags());
acl.setId(UUID.randomUUID().toString());
acl.setName(name);
acl.setScope(scope);
@@ -133,6 +140,7 @@ public class WafV2Service {
if (findByName(ipSetStore, scope, name) != null) {
throw new AwsException("WAFDuplicateItemException", "Duplicate IPSet name: " + name, 400);
}
+ validateTags(ipSet.getTags());
validateAddresses(ipSet.getAddresses(), ipSet.getIpAddressVersion());
ipSet.setId(UUID.randomUUID().toString());
ipSet.setName(name);
@@ -177,6 +185,7 @@ public class WafV2Service {
if (findByName(regexStore, scope, name) != null) {
throw new AwsException("WAFDuplicateItemException", "Duplicate RegexPatternSet name: " + name, 400);
}
+ validateTags(set.getTags());
set.setId(UUID.randomUUID().toString());
set.setName(name);
set.setScope(scope);
@@ -219,6 +228,7 @@ public class WafV2Service {
if (findByName(ruleGroupStore, scope, name) != null) {
throw new AwsException("WAFDuplicateItemException", "Duplicate RuleGroup name: " + name, 400);
}
+ validateTags(group.getTags());
group.setId(UUID.randomUUID().toString());
group.setName(name);
group.setScope(scope);
@@ -337,12 +347,18 @@ public class WafV2Service {
}
public void tagResource(String resourceArn, Map<String, String> tags) {
+ validateTags(tags);
Object resource = taggable(resourceArn);
- tagsOf(resource).putAll(tags);
+ Map<String, String> current = tagsOf(resource);
+ Map<String, String> merged = new LinkedHashMap<>(current);
+ merged.putAll(tags);
+ requireTagCount(merged.size());
+ current.putAll(tags);
persistTagged(resource);
}
public void untagResource(String resourceArn, List<String> keys) {
+ keys.forEach(key -> requireValidTagKey(key, "TAG_KEYS"));
Object resource = taggable(resourceArn);
keys.forEach(tagsOf(resource)::remove);
persistTagged(resource);
@@ -495,6 +511,41 @@ public class WafV2Service {
}
}
+ /**
+ * Enforces the WAFv2 tag contract: at most {@value #MAX_TAGS} tags per resource, keys of
+ * 1-{@value #MAX_TAG_KEY_LENGTH} characters, values of up to {@value #MAX_TAG_VALUE_LENGTH}
+ * characters, both restricted to letters, numbers, spaces and {@code _ . : / = + - @}.
+ */
+ private void validateTags(Map<String, String> tags) {
+ if (tags == null) {
+ return;
+ }
+ for (Map.Entry<String, String> tag : tags.entrySet()) {
+ requireValidTagKey(tag.getKey(), "TAGS");
+ String value = tag.getValue();
+ if (value != null
+ && (value.length() > MAX_TAG_VALUE_LENGTH || !TAG_PATTERN.matcher(value).matches())) {
+ throw invalidParameter("TAGS", tag.getKey(), "ILLEGAL_ARGUMENT");
+ }
+ }
+ requireTagCount(tags.size());
+ }
+
+ private void requireValidTagKey(String key, String field) {
+ if (key == null || key.isEmpty() || key.length() > MAX_TAG_KEY_LENGTH
+ || !TAG_PATTERN.matcher(key).matches()) {
+ throw invalidParameter(field, key == null ? "" : key, "INVALID_TAG_KEY");
+ }
+ }
+
+ private void requireTagCount(int count) {
+ if (count > MAX_TAGS) {
+ throw new AwsException("WAFLimitsExceededException",
+ "AWS WAF couldn't perform the operation because you exceeded your resource limit. "
+ + "A resource can have at most " + MAX_TAGS + " tags.", 400);
+ }
+ }
+
/**
* Validates that every entry in an IPSet's {@code Addresses} is a CIDR block matching
* the declared {@code IPAddressVersion}, per the {@code CreateIPSet}/{@code UpdateIPSet}
diff --git a/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2TagLimitsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2TagLimitsIntegrationTest.java
new file mode 100644
index 0000000000..63b3e4507c
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/wafv2/WafV2TagLimitsIntegrationTest.java
@@ -0,0 +1,207 @@
+package io.github.hectorvent.floci.services.wafv2;
+
+import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.Response;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Validates that WAFv2 tag mutations enforce the AWS tag contract: at most 50 tags per
+ * resource, keys of 1-128 characters and values of up to 256 characters, restricted to the
+ * documented character set (issue #4035).
+ */
+@QuarkusTest
+class WafV2TagLimitsIntegrationTest {
+
+ private static final String CT = "application/x-amz-json-1.1";
+ private static final String PREFIX = "AWSWAF_20190729.";
+
+ @BeforeAll
+ static void configure() {
+ RestAssuredJsonUtils.configureAwsContentTypes();
+ }
+
+ private static Response call(String action, String body) {
+ return given().contentType(CT).header("X-Amz-Target", PREFIX + action)
+ .body(body).when().post("/");
+ }
+
+ private static String createIpSet(String name) {
+ Response created = call("CreateIPSet",
+ "{\"Name\":\"" + name + "\",\"Scope\":\"REGIONAL\",\"IPAddressVersion\":\"IPV4\","
+ + "\"Addresses\":[]}");
+ created.then().statusCode(200).body("Summary.ARN", notNullValue());
+ return created.jsonPath().getString("Summary.ARN");
+ }
+
+ private static String tags(int from, int toExclusive) {
+ return IntStream.range(from, toExclusive)
+ .mapToObj(i -> "{\"Key\":\"key-" + i + "\",\"Value\":\"value-" + i + "\"}")
+ .collect(Collectors.joining(","));
+ }
+
+ private static String tagRequest(String arn, String tagList) {
+ return "{\"ResourceARN\":\"" + arn + "\",\"Tags\":[" + tagList + "]}";
+ }
+
+ @Test
+ void tagResourceAllowsUpToFiftyTags() {
+ String arn = createIpSet("tag-limits-fifty");
+
+ call("TagResource", tagRequest(arn, tags(0, 50))).then().statusCode(200);
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + arn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.TagList", hasSize(50));
+ }
+
+ @Test
+ void tagResourceRejectsMoreThanFiftyTagsInOneRequest() {
+ String arn = createIpSet("tag-limits-fifty-one");
+
+ call("TagResource", tagRequest(arn, tags(0, 51)))
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFLimitsExceededException"));
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + arn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.TagList", hasSize(0));
+ }
+
+ @Test
+ void tagResourceRejectsAccumulatingPastFiftyTags() {
+ String arn = createIpSet("tag-limits-accumulate");
+
+ call("TagResource", tagRequest(arn, tags(0, 50))).then().statusCode(200);
+
+ call("TagResource", tagRequest(arn, tags(50, 51)))
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFLimitsExceededException"));
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + arn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.TagList", hasSize(50));
+ }
+
+ @Test
+ void tagResourceOverwritingExistingKeysDoesNotCountTwice() {
+ String arn = createIpSet("tag-limits-overwrite");
+
+ call("TagResource", tagRequest(arn, tags(0, 50))).then().statusCode(200);
+
+ call("TagResource", tagRequest(arn, "{\"Key\":\"key-0\",\"Value\":\"updated\"}"))
+ .then().statusCode(200);
+
+ call("ListTagsForResource", "{\"ResourceARN\":\"" + arn + "\"}")
+ .then().statusCode(200)
+ .body("TagInfoForResource.TagList", hasSize(50))
+ .body("TagInfoForResource.TagList.find { it.Key == 'key-0' }.Value", equalTo("updated"));
+ }
+
+ @Test
+ void tagResourceRejectsKeyLongerThan128Characters() {
+ String arn = createIpSet("tag-limits-long-key");
+ String longKey = "k".repeat(129);
+
+ call("TagResource", tagRequest(arn, "{\"Key\":\"" + longKey + "\",\"Value\":\"v\"}"))
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFInvalidParameterException"))
+ .body("Field", equalTo("TAGS"))
+ .body("Parameter", equalTo(longKey))
+ .body("Reason", equalTo("INVALID_TAG_KEY"));
+
+ call("TagResource", tagRequest(arn, "{\"Key\":\"" + "k".repeat(128) + "\",\"Value\":\"v\"}"))
+ .then().statusCode(200);
+ }
+
+ @Test
+ void tagResourceRejectsEmptyKey() {
+ String arn = createIpSet("tag-limits-empty-key");
+
+ call("TagResource", tagRequest(arn, "{\"Key\":\"\",\"Value\":\"v\"}"))
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFInvalidParameterException"))
+ .body("Field", equalTo("TAGS"))
+ .body("Reason", equalTo("INVALID_TAG_KEY"));
+ }
+
+ @Test
+ void tagResourceRejectsKeyWithDisallowedCharacters() {
+ String arn = createIpSet("tag-limits-bad-key");
+
+ call("TagResource", tagRequest(arn, "{\"Key\":\"bad!key\",\"Value\":\"v\"}"))
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFInvalidParameterException"))
+ .body("Field", equalTo("TAGS"))
+ .body("Parameter", equalTo("bad!key"))
+ .body("Reason", equalTo("INVALID_TAG_KEY"));
+ }
+
+ @Test
+ void tagResourceRejectsValueLongerThan256Characters() {
+ String arn = createIpSet("tag-limits-long-value");
+
+ call("TagResource", tagRequest(arn, "{\"Key\":\"k\",\"Value\":\"" + "v".repeat(257) + "\"}"))
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFInvalidParameterException"))
+ .body("Field", equalTo("TAGS"))
+ .body("Parameter", equalTo("k"))
+ .body("Reason", equalTo("ILLEGAL_ARGUMENT"));
+
+ call("TagResource", tagRequest(arn, "{\"Key\":\"k\",\"Value\":\"" + "v".repeat(256) + "\"}"))
+ .then().statusCode(200);
+
+ call("TagResource", tagRequest(arn, "{\"Key\":\"empty\",\"Value\":\"\"}"))
+ .then().statusCode(200);
+ }
+
+ @Test
+ void untagResourceRejectsInvalidKeys() {
+ String arn = createIpSet("tag-limits-untag");
+
+ call("UntagResource", "{\"ResourceARN\":\"" + arn + "\",\"TagKeys\":[\"" + "k".repeat(129) + "\"]}")
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFInvalidParameterException"))
+ .body("Field", equalTo("TAG_KEYS"))
+ .body("Reason", equalTo("INVALID_TAG_KEY"));
+
+ call("UntagResource", "{\"ResourceARN\":\"" + arn + "\",\"TagKeys\":[\"\"]}")
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFInvalidParameterException"))
+ .body("Field", equalTo("TAG_KEYS"));
+
+ call("UntagResource", "{\"ResourceARN\":\"" + arn + "\",\"TagKeys\":[\"absent\"]}")
+ .then().statusCode(200);
+ }
+
+ @Test
+ void createRejectsMoreThanFiftyTags() {
+ call("CreateIPSet",
+ "{\"Name\":\"tag-limits-create\",\"Scope\":\"REGIONAL\",\"IPAddressVersion\":\"IPV4\","
+ + "\"Addresses\":[],\"Tags\":[" + tags(0, 51) + "]}")
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFLimitsExceededException"));
+
+ call("CreateIPSet",
+ "{\"Name\":\"tag-limits-create\",\"Scope\":\"REGIONAL\",\"IPAddressVersion\":\"IPV4\","
+ + "\"Addresses\":[],\"Tags\":[{\"Key\":\"bad!key\",\"Value\":\"v\"}]}")
+ .then().statusCode(400)
+ .body("__type", equalTo("WAFInvalidParameterException"))
+ .body("Field", equalTo("TAGS"));
+
+ call("CreateIPSet",
+ "{\"Name\":\"tag-limits-create\",\"Scope\":\"REGIONAL\",\"IPAddressVersion\":\"IPV4\","
+ + "\"Addresses\":[],\"Tags\":[" + tags(0, 50) + "]}")
+ .then().statusCode(200)
+ .body("Summary.Id", notNullValue());
+ }
+}