diff --git a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java index 9d4ddbf342..828986dd02 100644 --- a/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java +++ b/src/main/java/io/floci/aws/services/cloudfront/CloudFrontController.java @@ -1289,6 +1289,7 @@ public class CloudFrontController { @Path("/key-value-store") public Response createKeyValueStore(String body) { try { + validateRequestXml(body, "CreateKeyValueStoreRequest"); String name = XmlParser.extractFirst(body, "Name", null); String comment = XmlParser.extractFirst(body, "Comment", null); boolean hasImportSource = XmlParser.extractElementTree(body, "ImportSource") != null; @@ -1351,6 +1352,7 @@ public class CloudFrontController { @HeaderParam("If-Match") String ifMatch, String body) { try { + validateRequestXml(body, "UpdateKeyValueStoreRequest"); KeyValueStore kvs = service.updateKeyValueStore(name, ifMatch, XmlParser.extractFirst(body, "Comment", null)); return Response.ok(xmlKeyValueStore(kvs), XML) .header("ETag", kvs.getEtag()) @@ -4183,6 +4185,12 @@ public class CloudFrontController { return oai; } + private static void validateRequestXml(String body, String root) { + if (!root.equals(XmlParser.rootElementName(body))) { + throw new AwsException("InvalidArgument", "The request XML is malformed or has an invalid root element.", 400); + } + } + private CloudFrontFunction parseFunction(String body) { CloudFrontFunction fn = new CloudFrontFunction(); fn.setName(XmlParser.extractFirst(body, "Name", null)); diff --git a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreIntegrationTest.java index 9afc184ffe..3390de5e30 100644 --- a/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudfront/CloudFrontKeyValueStoreIntegrationTest.java @@ -5,6 +5,8 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -90,6 +92,54 @@ class CloudFrontKeyValueStoreIntegrationTest { Base64.getEncoder().encodeToString(FUNCTION_CODE.getBytes(StandardCharsets.UTF_8))); } + @ParameterizedTest + @ValueSource(strings = { + "%s", + "%s<", + "%s" + }) + void invalidCreateXmlDoesNotCreateStore(String requestXml) { + String name = uniqueName(); + String body = requestXml.formatted(name); + try { + given().contentType(XML).body(body).when().post(CLOUDFRONT + "key-value-store") + .then().statusCode(400).body(hasXPath("//*[local-name()='Code']", equalTo("InvalidArgument"))); + given().get(CLOUDFRONT + "key-value-store/" + name).then().statusCode(404); + } finally { + deleteStoreIfPresent(name); + } + } + + @ParameterizedTest + @ValueSource(strings = { + "changed", + "changed<", + "changed" + }) + void invalidUpdateXmlPreservesCommentAndEtag(String body) { + Store store = createStore(uniqueName(), "original"); + try { + given().contentType(XML).header("If-Match", store.etag()).body(body) + .put(CLOUDFRONT + "key-value-store/" + store.name()).then().statusCode(400) + .body(hasXPath("//*[local-name()='Code']", equalTo("InvalidArgument"))); + given().get(CLOUDFRONT + "key-value-store/" + store.name()).then() + .statusCode(200).header("ETag", equalTo(store.etag())) + .body(hasXPath("//*[local-name()='Comment']", equalTo("original"))); + } finally { + deleteStoreIfPresent(store.name()); + } + } + + private static void deleteStoreIfPresent(String name) { + Response current = given().get(CLOUDFRONT + "key-value-store/" + name); + if (current.statusCode() == 200) { + given().header("If-Match", current.header("ETag")).delete(CLOUDFRONT + "key-value-store/" + name) + .then().statusCode(204); + } else { + current.then().statusCode(404); + } + } + // ── Control plane ───────────────────────────────────────────────────────── @Test