fix(epub): support safe doctypes in OPF (#2196)

This commit is contained in:
James Ward
2026-08-07 16:26:01 -04:00
committed by GitHub
parent 72a27c57cd
commit f0cedee137
2 changed files with 107 additions and 12 deletions
@@ -7,6 +7,7 @@ import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.util.List;
@Slf4j
@UtilityClass
@@ -16,24 +17,48 @@ public class SecureXmlUtils {
private static final DocumentBuilderFactory NS_AWARE_FACTORY;
private static final DocumentBuilderFactory NON_NS_AWARE_FACTORY;
private static final List<String> DISABLED_FEATURE_FLAGS = List.of(
// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-general-entities
// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-general-entities
// JDK7+ - http://xml.org/sax/features/external-general-entities
//This feature has to be used together with the following one, otherwise it will not protect you from XXE for sure
"http://xml.org/sax/features/external-general-entities",
// Xerces 1 - http://xerces.apache.org/xerces-j/features.html#external-parameter-entities
// Xerces 2 - http://xerces.apache.org/xerces2-j/features.html#external-parameter-entities
// JDK7+ - http://xml.org/sax/features/external-parameter-entities
//This feature has to be used together with the previous one, otherwise it will not protect you from XXE for sure
"http://xml.org/sax/features/external-parameter-entities",
// Disable external DTDs as well
"http://apache.org/xml/features/nonvalidating/load-external-dtd"
);
static {
try {
NS_AWARE_FACTORY = buildFactory(true);
NON_NS_AWARE_FACTORY = buildFactory(false);
} catch (ParserConfigurationException e) {
throw new ExceptionInInitializerError(e);
}
NS_AWARE_FACTORY = buildFactory(true);
NON_NS_AWARE_FACTORY = buildFactory(false);
}
private static DocumentBuilderFactory buildFactory(boolean namespaceAware) throws ParserConfigurationException {
private static DocumentBuilderFactory buildFactory(boolean namespaceAware) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(namespaceAware);
// Prevent XXE attacks
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
// We cannot explicitly disable DTDs but we can disable a number of feature flags that allow
// users to exploit issues & enable feature flags to protect users.
for (var featureFlag : DISABLED_FEATURE_FLAGS) {
try {
factory.setFeature(featureFlag, false);
} catch (ParserConfigurationException e) {
log.error("Feature {} is not supported by your XML processor.", featureFlag, e);
}
}
try {
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (ParserConfigurationException e) {
log.error("Feature FEATURE_SECURE_PROCESSING is not supported by your XML processor.", e);
}
factory.setXIncludeAware(false);
factory.setExpandEntityReferences(false);
@@ -1468,5 +1468,75 @@ class EpubMetadataExtractorTest {
assertThat(metadata.getContentRating()).isEqualTo(rating);
}
@Test
void invalidDocTypeIsIgnored() throws IOException {
String opf = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>Root OPF</dc:title>
<dc:creator opf:role="aut">Root Author</dc:creator>
<dc:date>2020</dc:date>
</metadata>
<manifest/>
</package>""";
File epub = createEpub(opf, "OEBPS/content.opf", null);
BookMetadata metadata = extractor.extractMetadata(epub);
assertThat(metadata.getTitle()).isEqualTo("Root OPF");
assertThat(metadata.getAuthors()).containsExactly("Root Author");
assertThat(metadata.getPublishedDate()).isEqualTo(LocalDate.of(2020, 1, 1));
}
@Test
void xxePreventsDataAccess() throws IOException {
// https://owasp.org/www-community/vulnerabilities/XML_External_Entity_(XXE)_Processing
String opf = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>&xxe;</dc:title>
</metadata>
<manifest/>
</package>""";
File epub = createEpub(opf, "OEBPS/content.opf", null);
BookMetadata metadata = extractor.extractMetadata(epub);
// Test is the name of the file.
assertThat(metadata.getTitle()).isEqualTo("test");
}
@Test
void xxePreventsBillionLaughs() throws IOException {
// https://en.wikipedia.org/wiki/Billion_laughs_attack
String opf = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lolz [
<!ELEMENT lolz (#PCDATA)>
<!ENTITY lol1 "lollollollollollollollollollol">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
<!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;">
<!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;">
<!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;">
<!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;">
<!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;">
]>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title><lolz>&lol9;</lolz></dc:title>
</metadata>
<manifest/>
</package>""";
File epub = createEpub(opf, "OEBPS/content.opf", null);
BookMetadata metadata = extractor.extractMetadata(epub);
// Test is the name of the file.
assertThat(metadata.getTitle()).isEqualTo("test");
}
}
}