mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
@@ -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 = """
|
||||
<!DOCTYPE root [ <!ENTITY x "expanded"> ]>
|
||||
<Root><Value>plain</Value></Root>
|
||||
""";
|
||||
|
||||
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 = """
|
||||
<!DOCTYPE root [ <!ENTITY x "expanded"> ]>
|
||||
<Root><Value>&x;</Value></Root>
|
||||
""";
|
||||
|
||||
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 = """
|
||||
<!DOCTYPE root [ <!ENTITY secret SYSTEM "file:///nonexistent/secret"> ]>
|
||||
<Root><Value>&secret;</Value></Root>
|
||||
""";
|
||||
|
||||
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 = "<Root xmlns=\"http://example.com/ns\"><Item id=\"7\" name=\"x\"/></Root>";
|
||||
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user