diff --git a/backend/src/main/java/org/booklore/util/SecureXmlUtils.java b/backend/src/main/java/org/booklore/util/SecureXmlUtils.java index e2a021942..c5967ed8a 100644 --- a/backend/src/main/java/org/booklore/util/SecureXmlUtils.java +++ b/backend/src/main/java/org/booklore/util/SecureXmlUtils.java @@ -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 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); diff --git a/backend/src/test/java/org/booklore/service/metadata/extractor/EpubMetadataExtractorTest.java b/backend/src/test/java/org/booklore/service/metadata/extractor/EpubMetadataExtractorTest.java index f93ad87e5..7d4992797 100644 --- a/backend/src/test/java/org/booklore/service/metadata/extractor/EpubMetadataExtractorTest.java +++ b/backend/src/test/java/org/booklore/service/metadata/extractor/EpubMetadataExtractorTest.java @@ -1468,5 +1468,75 @@ class EpubMetadataExtractorTest { assertThat(metadata.getContentRating()).isEqualTo(rating); } + + @Test + void invalidDocTypeIsIgnored() throws IOException { + String opf = """ + + + + + Root OPF + Root Author + 2020 + + + """; + 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 = """ + + ]> + + + &xxe; + + + """; + 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 = """ + + + + + + + + + + + + ]> + + + &lol9; + + + """; + 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"); + } } }