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 <Name> 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
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-11-01 18:26:49 -04:00
1 parent 601b4d132d
commit 5680ec4b8f
3 files changed
+120 -9

No files matched your search

@@ -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<XmlElement> 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<XmlElement> children, String rawText) {
public XmlElement child(String childName) {
return children.stream()
.filter(child -> childName.equals(child.name()))
@@ -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;
}