mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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.
This commit is contained in:
1 parent
82eaaaeee7
commit
278d9ec83e
5 files changed
+102
-44
No files matched your search
@@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
|
||||
+7
-19
@@ -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<String, Object> 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();
|
||||
|
||||
@@ -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<ResourceRecord> currentRecords = null;
|
||||
@@ -1078,7 +1067,7 @@ public class Route53Controller {
|
||||
List<String> 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();
|
||||
|
||||
@@ -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())) {
|
||||
|
||||
Reference in new issue
Block a user