From 278d9ec83e90d5e8eaa9172df2f21af0a8fc4903 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Thu, 10 Sep 2026 06:43:29 -0500 Subject: [PATCH] refactor(core): parse XML through one hardened factory, not four (#3337) CloudFront, Route 53 and S3 each stood up a private XMLInputFactory and each restated the same three hardening properties: namespace-aware, DTDs off, external entities off. XmlParser already had a fourth copy. Four copies of security-relevant configuration is three too many, and a copy that one day forgets a property is an XXE hole with nothing to catch it. XmlParser gains newStreamReader, exposing the factory it already holds, for callers whose parsing goes past the extraction helpers because they read attributes. The three controllers drop their own factories and their ten call sites go through it. Behaviour is unchanged by construction: the four factories were byte-identical before this, same properties and same values, so no test needed touching. That was the check, not an afterthought. Tests pin what is being centralised, which nothing covered before. Worth recording what they found: SUPPORT_DTD=false does not reject a document type declaration, it silently skips it. The protection is that nothing declared inside takes effect, so an entity referenced from it fails as undeclared rather than expanding, and an external one is never read. The tests assert that behaviour rather than the refusal I first assumed. --- .../floci/core/common/XmlParser.java | 12 +++ .../cloudfront/CloudFrontController.java | 26 ++---- .../services/route53/Route53Controller.java | 15 +--- .../floci/services/s3/S3Controller.java | 13 +-- .../floci/core/common/XmlParserTest.java | 80 +++++++++++++++++++ 5 files changed, 102 insertions(+), 44 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 16c55f4188..0151fbfa45 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 @@ -42,6 +42,18 @@ public final class XmlParser { private XmlParser() {} + /** + * A stream reader over {@code xml} using this class's hardened settings: namespace-aware, + * with DTDs and external entities disabled. + * + *

For callers whose parsing goes beyond the extraction helpers here, typically because + * they read attributes. Reaching for {@link XMLInputFactory} directly means restating the + * hardening, and a copy that forgets a property is an XXE hole that nothing would catch. + */ + public static XMLStreamReader newStreamReader(String xml) throws XMLStreamException { + return FACTORY.createXMLStreamReader(new StringReader(xml)); + } + /** * Parses XML into a namespace-aware DOM document with external entities and * DTD processing disabled. DOM is required by the JDK XML-DSig API. 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 c3c9d0dfcf..195edea19a 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 @@ -11,10 +11,8 @@ import jakarta.ws.rs.*; import jakarta.ws.rs.core.Response; import org.jboss.logging.Logger; -import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamReader; -import java.io.StringReader; import java.net.URI; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -42,15 +40,6 @@ public class CloudFrontController { private static final String DEFAULT_GEO_RESTRICTION_TYPE = "none"; private static final int EMPTY_QUANTITY = 0; - private static final XMLInputFactory XML_FACTORY; - - static { - XML_FACTORY = XMLInputFactory.newInstance(); - XML_FACTORY.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); - XML_FACTORY.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); - XML_FACTORY.setProperty(XMLInputFactory.SUPPORT_DTD, false); - } - private final CloudFrontService service; @Inject @@ -2500,8 +2489,7 @@ public class CloudFrontController { return values; } try { - XMLStreamReader reader = XML_FACTORY.createXMLStreamReader( - new StringReader(body)); + XMLStreamReader reader = XmlParser.newStreamReader(body); boolean inDistributionConfig = false; int nestedDepth = 0; while (reader.hasNext()) { @@ -2574,7 +2562,7 @@ public class CloudFrontController { return result; } try { - XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body)); + XMLStreamReader r = XmlParser.newStreamReader(body); boolean inBlock = false; boolean inItem = false; Map current = null; @@ -2641,7 +2629,7 @@ public class CloudFrontController { return result; } try { - XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body)); + XMLStreamReader r = XmlParser.newStreamReader(body); boolean inOrigins = false; boolean inOrigin = false; boolean inS3OriginConfig = false; @@ -2999,7 +2987,7 @@ public class CloudFrontController { return dcb; } try { - XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body)); + XMLStreamReader r = XmlParser.newStreamReader(body); boolean inDcb = false; boolean inAllowedMethods = false; boolean inCachedMethods = false; @@ -3282,7 +3270,7 @@ public class CloudFrontController { return result; } try { - XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body)); + XMLStreamReader r = XmlParser.newStreamReader(body); boolean inCacheBehaviors = false; boolean inCacheBehavior = false; boolean inAllowedMethods = false; @@ -3666,7 +3654,7 @@ public class CloudFrontController { return result; } try { - XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body)); + XMLStreamReader r = XmlParser.newStreamReader(body); boolean inAliases = false; while (r.hasNext()) { int event = r.next(); @@ -3695,7 +3683,7 @@ public class CloudFrontController { return result; } try { - XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body)); + XMLStreamReader r = XmlParser.newStreamReader(body); boolean inVc = false; while (r.hasNext()) { int event = r.next(); diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java index 2679c3b737..500ddfd532 100644 --- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53Controller.java @@ -23,10 +23,8 @@ import jakarta.ws.rs.PathParam; import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.Response; -import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamReader; -import java.io.StringReader; import java.net.URI; import java.time.Instant; import java.util.ArrayList; @@ -63,15 +61,6 @@ public class Route53Controller { "af-south-1", "eu-south-1", "eu-south-2", "il-central-1", "mx-central-1"); - private static final XMLInputFactory XML_FACTORY; - - static { - XML_FACTORY = XMLInputFactory.newInstance(); - XML_FACTORY.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); - XML_FACTORY.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); - XML_FACTORY.setProperty(XMLInputFactory.SUPPORT_DTD, false); - } - @Inject Route53Service service; @@ -913,7 +902,7 @@ public class Route53Controller { if (body == null || body.isEmpty()) return result; try { - XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body)); + XMLStreamReader r = XmlParser.newStreamReader(body); String currentAction = null; ResourceRecordSet currentRrs = null; List currentRecords = null; @@ -1078,7 +1067,7 @@ public class Route53Controller { List keys = new ArrayList<>(); if (body == null || body.isEmpty()) return keys; try { - XMLStreamReader r = XML_FACTORY.createXMLStreamReader(new StringReader(body)); + XMLStreamReader r = XmlParser.newStreamReader(body); boolean inRemove = false; while (r.hasNext()) { int event = r.next(); diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index fb05ade41a..132d19291e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java @@ -42,7 +42,6 @@ import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.StreamingOutput; import jakarta.ws.rs.core.UriInfo; import java.io.ByteArrayOutputStream; -import java.io.StringReader; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.time.Instant; @@ -59,7 +58,6 @@ import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.Set; -import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; @@ -83,17 +81,8 @@ public class S3Controller { private static final DateTimeFormatter RFC_822 = DateTimeFormatter .ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.US) .withZone(ZoneId.of("GMT")); - private static final XMLInputFactory NOTIFICATION_XML_FACTORY; - private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); - static { - NOTIFICATION_XML_FACTORY = XMLInputFactory.newInstance(); - NOTIFICATION_XML_FACTORY.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); - NOTIFICATION_XML_FACTORY.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); - NOTIFICATION_XML_FACTORY.setProperty(XMLInputFactory.SUPPORT_DTD, false); - } - private final S3Service s3Service; private final S3SelectService s3SelectService; private final RegionResolver regionResolver; @@ -1760,7 +1749,7 @@ public class S3Controller { return result; } try { - XMLStreamReader reader = NOTIFICATION_XML_FACTORY.createXMLStreamReader(new StringReader(xml)); + XMLStreamReader reader = XmlParser.newStreamReader(xml); while (reader.hasNext()) { int event = reader.next(); if (event == XMLStreamConstants.START_ELEMENT && groupElement.equals(reader.getLocalName())) { diff --git a/src/test/java/io/github/hectorvent/floci/core/common/XmlParserTest.java b/src/test/java/io/github/hectorvent/floci/core/common/XmlParserTest.java index abc671aee6..cdeae3fa0b 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/XmlParserTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/XmlParserTest.java @@ -2,6 +2,10 @@ package io.github.hectorvent.floci.core.common; import org.junit.jupiter.api.Test; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + import java.util.List; import java.util.Map; @@ -9,6 +13,82 @@ import static org.junit.jupiter.api.Assertions.*; class XmlParserTest { + // --- newStreamReader: the hardening this centralises --- + + /** + * The reason three controllers stopped standing up their own XMLInputFactory. Each restated + * these settings, and a copy that forgot one would be an XXE hole nothing would catch. + */ + @Test + void newStreamReaderIgnoresEntitiesDeclaredInADoctype() throws Exception { + // The DTD is skipped rather than rejected, so the document itself still parses. What + // protects us is that nothing in it takes effect: an entity it declares stays undefined, + // so any reference to one fails instead of expanding. + String declaredButUnused = """ + ]> + plain + """; + + XMLStreamReader r = XmlParser.newStreamReader(declaredButUnused); + StringBuilder text = new StringBuilder(); + while (r.hasNext()) { + if (r.next() == XMLStreamConstants.CHARACTERS) { + text.append(r.getText()); + } + } + assertEquals("plain", text.toString().trim(), "the document parses, the DTD is ignored"); + + String referenced = """ + ]> + &x; + """; + + XMLStreamException e = assertThrows(XMLStreamException.class, () -> { + XMLStreamReader r2 = XmlParser.newStreamReader(referenced); + while (r2.hasNext()) { + r2.next(); + } + }, "an entity from the skipped DTD must not expand"); + assertTrue(e.getMessage().contains("not declared"), + "should fail because the DTD never took effect, was: " + e.getMessage()); + } + + @Test + void newStreamReaderDoesNotExpandAnExternalEntity() { + // Pointing at a path that does not exist: if the entity were resolved this would fail + // by trying to read it. Refusing the DTD outright is the behaviour we want. + String xml = """ + ]> + &secret; + """; + + XMLStreamException e = assertThrows(XMLStreamException.class, () -> { + XMLStreamReader r = XmlParser.newStreamReader(xml); + while (r.hasNext()) { + r.next(); + } + }, "an external entity must never be resolved"); + assertTrue(e.getMessage().contains("not declared"), + "must fail without reading the file at all, was: " + e.getMessage()); + } + + @Test + void newStreamReaderIsNamespaceAwareAndReadsAttributes() throws Exception { + // Attributes are why a caller reaches for a raw reader instead of the helpers here. + String xml = ""; + + XMLStreamReader r = XmlParser.newStreamReader(xml); + String id = null; + while (r.hasNext()) { + if (r.next() == XMLStreamConstants.START_ELEMENT && "Item".equals(r.getLocalName())) { + id = r.getAttributeValue(null, "id"); + assertEquals("http://example.com/ns", r.getNamespaceURI(), + "the reader must be namespace-aware"); + } + } + assertEquals("7", id); + } + // --- extractGroupsMulti: nested element resilience --- @Test