fix(cloudfront): reject malformed key value store request XML (#9695)

Check the XML document and its root before creating a store or updating
its comment. A rejected update keeps the existing comment and ETag.

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Jackson Price <jxnprice@amazon.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-28 20:26:53 -04:00
1 parent 72983e8282
commit e1f4588d2f
2 files changed
+58

No files matched your search

@@ -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));
@@ -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 = {
"<CreateKeyValueStoreRequest><Name>%s</Name>",
"<CreateKeyValueStoreRequest><Name>%s</Name></CreateKeyValueStoreRequest><",
"<OtherRequest><Name>%s</Name></OtherRequest>"
})
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 = {
"<UpdateKeyValueStoreRequest><Comment>changed</Comment>",
"<UpdateKeyValueStoreRequest><Comment>changed</Comment></UpdateKeyValueStoreRequest><",
"<OtherRequest><Comment>changed</Comment></OtherRequest>"
})
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