From 5680ec4b8fd3d8aaa820142ec7b45c929f318421 Mon Sep 17 00:00:00 2001 From: Allen Sanborn Date: Sun, 1 Nov 2026 16:26:49 -0600 Subject: [PATCH] fix(cloudfront): read policy Name and Comment from the config root only (#7252) * fix(cloudfront): read policy Name and Comment from the config root only Cache and origin request policy configs nest elements (cookie, header and query-string items). A flat first-match lookup stored the first item name as the policy name when the SDK serialised those blocks before the top-level Name. * fix(cloudfront): keep policy Name and Comment text as sent The direct-child lookup for cache, origin-request and response-headers policy Name and Comment read XmlElement.text(), which the element tree trims. A Comment of " note " came back as "note". XmlElement now also carries the untrimmed text as rawText(), which directChildText uses; text() stays trimmed for the S3 and CloudFront callers that match it against enum values. * test(cloudfront): cover padded policy Name and newline-padded Comment --- .../floci/core/common/XmlParser.java | 11 ++- .../cloudfront/CloudFrontController.java | 24 +++-- .../CloudFrontManagementProtocolTest.java | 94 +++++++++++++++++++ 3 files changed, 120 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java b/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java index ae13247d7c..8e151bddee 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java @@ -497,14 +497,19 @@ public final class XmlParser { || event == XMLStreamConstants.CDATA) { text.append(r.getText()); } else if (event == XMLStreamConstants.END_ELEMENT) { - return new XmlElement(name, text.toString().trim(), List.copyOf(children)); + String raw = text.toString(); + return new XmlElement(name, raw.trim(), List.copyOf(children), raw); } } throw new XMLStreamException("Unexpected end of XML while reading " + name); } - /** A namespace-agnostic XML element used by {@link #extractElementTree(String, String)}. */ - public record XmlElement(String name, String text, List children) { + /** + * A namespace-agnostic XML element used by {@link #extractElementTree(String, String)}. + * {@code text} is trimmed for enum and number fields; {@code rawText} keeps a free-form + * value such as a name or comment exactly as the client sent it. + */ + public record XmlElement(String name, String text, List children, String rawText) { public XmlElement child(String childName) { return children.stream() .filter(child -> childName.equals(child.name())) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java index 630b5e3ff1..1d0893495b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontController.java @@ -3529,26 +3529,38 @@ public class CloudFrontController { return inv; } + /** + * Text of a direct child of the config root. Policy configs nest other {@code Name} elements + * (cookie, header and query-string items) that a flat first-match lookup would pick up. + */ + private static String directChildText(XmlParser.XmlElement root, String name) { + XmlParser.XmlElement child = root == null ? null : root.child(name); + return child == null ? null : child.rawText(); + } + private CachePolicy parseCachePolicy(String body) { CachePolicy policy = new CachePolicy(); - policy.setName(XmlParser.extractFirst(body, "Name", null)); - policy.setComment(XmlParser.extractFirst(body, "Comment", null)); + XmlParser.XmlElement root = XmlParser.parseDocumentElementTree(body); + policy.setName(directChildText(root, "Name")); + policy.setComment(directChildText(root, "Comment")); policy.setConfig(CloudFrontPolicyConfigCodec.parseCachePolicy(body)); return policy; } private OriginRequestPolicy parseOriginRequestPolicy(String body) { OriginRequestPolicy policy = new OriginRequestPolicy(); - policy.setName(XmlParser.extractFirst(body, "Name", null)); - policy.setComment(XmlParser.extractFirst(body, "Comment", null)); + XmlParser.XmlElement root = XmlParser.parseDocumentElementTree(body); + policy.setName(directChildText(root, "Name")); + policy.setComment(directChildText(root, "Comment")); policy.setConfig(CloudFrontPolicyConfigCodec.parseOriginRequestPolicy(body)); return policy; } private ResponseHeadersPolicy parseResponseHeadersPolicy(String body) { ResponseHeadersPolicy policy = new ResponseHeadersPolicy(); - policy.setName(XmlParser.extractFirst(body, "Name", null)); - policy.setComment(XmlParser.extractFirst(body, "Comment", null)); + XmlParser.XmlElement root = XmlParser.parseDocumentElementTree(body); + policy.setName(directChildText(root, "Name")); + policy.setComment(directChildText(root, "Comment")); policy.setConfig(ResponseHeadersPolicyConfigCodec.parse(body)); return policy; } diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java index 042fd0fa8a..09eb3b73e0 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java @@ -439,6 +439,100 @@ class CloudFrontManagementProtocolTest { .body(hasXPath("//*[local-name()='Code']/text()", equalTo("InvalidArgument"))); } + @ParameterizedTest(name = "{0} name is the root Name, not an item Name") + @CsvSource({"cache-policy, CachePolicy", "origin-request-policy, OriginRequestPolicy"}) + void policyNameIgnoresItemNamesThatPrecedeIt(String endpoint, String type) { + String name = "name-order-" + UUID.randomUUID(); + String items = """ + 1Session-ID"""; + String params = """ + whitelist + 1X-Hdr + whitelist + %s + whitelist + %s + """.formatted(items, items); + String config = endpoint.equals("cache-policy") + ? "" + + "false" + params + + "0" + : params; + String body = "<" + type + "Config xmlns=\"http://cloudfront.amazonaws.com/doc/2020-05-31/\">" + + config + "real-comment" + name + ""; + Response created = given().contentType("application/xml").body(body) + .when().post(API + endpoint); + created.then().statusCode(201); + String id = XmlParser.extractFirst(created.asString(), "Id", null); + String etag = created.header("ETag"); + try { + given().when().get(API + endpoint + "/" + id + "/config").then().statusCode(200) + .body(hasXPath("/*/*[local-name()='Name']/text()", equalTo(name))) + .body(hasXPath("/*/*[local-name()='Comment']/text()", equalTo("real-comment"))); + given().when().get(API + endpoint).then().statusCode(200) + .body(hasXPath("//*[local-name()='" + type + "Summary']/*[local-name()='" + type + + "']/*[local-name()='" + type + "Config']/*[local-name()='Name' and text()='" + + name + "']")); + } finally { + given().header("If-Match", etag).when().delete(API + endpoint + "/" + id) + .then().statusCode(204); + } + } + + @ParameterizedTest(name = "{0} keeps the Comment's surrounding whitespace") + @ValueSource(strings = {"cache-policy", "origin-request-policy", "response-headers-policy"}) + void policyCommentRoundTripsWithSurroundingWhitespace(String endpoint) { + String name = "padded-" + UUID.randomUUID(); + String comment = "\n padded comment \n"; + String body = switch (endpoint) { + case "cache-policy" -> cachePolicyBody(name, comment, "3600", "session"); + case "origin-request-policy" -> originRequestPolicyBody(name, comment, "X-Hdr"); + default -> "" + + "" + name + "" + comment + ""; + }; + Response created = given().contentType("application/xml").body(body) + .when().post(API + endpoint); + created.then().statusCode(201); + String id = XmlParser.extractFirst(created.asString(), "Id", null); + try { + given().when().get(API + endpoint + "/" + id + "/config").then().statusCode(200) + .body(hasXPath("/*/*[local-name()='Comment']/text()", equalTo(comment))); + } finally { + given().header("If-Match", created.header("ETag")).when().delete(API + endpoint + "/" + id) + .then().statusCode(204); + } + } + + @ParameterizedTest(name = "{0} keeps the Name's surrounding whitespace") + @ValueSource(strings = {"cache-policy", "origin-request-policy"}) + void policyNameRoundTripsWithSurroundingWhitespace(String endpoint) { + String name = " padded-" + UUID.randomUUID() + " "; + String body = endpoint.equals("cache-policy") + ? cachePolicyBody(name, "c", "3600", "session") + : originRequestPolicyBody(name, "c", "X-Hdr"); + Response created = given().contentType("application/xml").body(body) + .when().post(API + endpoint); + created.then().statusCode(201); + String id = XmlParser.extractFirst(created.asString(), "Id", null); + try { + given().when().get(API + endpoint + "/" + id + "/config").then().statusCode(200) + .body(hasXPath("/*/*[local-name()='Name']/text()", equalTo(name))); + } finally { + given().header("If-Match", created.header("ETag")).when().delete(API + endpoint + "/" + id) + .then().statusCode(204); + } + } + + @Test + void responseHeadersPolicyRejectsNameWithSurroundingWhitespace() { + // Name pattern [A-Za-z0-9_-]{1,128} has no spaces; trimming would wrongly accept it. + given().contentType("application/xml") + .body("" + + " padded-" + UUID.randomUUID() + " ") + .when().post(API + "response-headers-policy") + .then().statusCode(400); + } + @Test void cachePolicyReadFillsAwsTtlDefaults() { String name = "default-ttl-cache-policy-" + UUID.randomUUID();