Skip to content

Commit

Permalink
add test and additional disallow-doctype-decl to disallow it on parsing
Browse files Browse the repository at this point in the history
- fixes: SIRI-1037
  • Loading branch information
ymo-sci committed Dec 9, 2024
1 parent 68fa48d commit 53b8d66
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/sirius/kernel/xml/XMLReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ public void parse(InputStream stream, Function<String, InputStream> resourceLoca
try (stream) {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
factory.setXIncludeAware(false);
SAXParser saxParser = factory.newSAXParser();
org.xml.sax.XMLReader reader = saxParser.getXMLReader();
reader.setEntityResolver(new EntityResolver() {
Expand Down
22 changes: 22 additions & 0 deletions src/test/kotlin/sirius/kernel/xml/XmlReaderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
package sirius.kernel.xml

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import sirius.kernel.SiriusExtension
import sirius.kernel.commons.ValueHolder
import sirius.kernel.health.Counter
import java.io.ByteArrayInputStream
import java.io.IOException
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
Expand Down Expand Up @@ -138,4 +140,24 @@ internal class XmlReaderTest {
assertEquals(0, attributes.size)
assertEquals("", attribute.get())
}

@Test
fun `Reading an external entity is not allowed`() {
val readString = ValueHolder.of<String?>(null)
val reader = XMLReader()
reader.addHandler("root") { node: StructuredNode ->
readString.set(node.queryString("."))
}
assertThrows<IOException> {
reader.parse(
ByteArrayInputStream(//language=xml
"""
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [<!ENTITY xxe SYSTEM "file:///etc/hosts">]>
<root>&xxe;</root>
""".trimIndent().toByteArray()
)
)
}
}
}

0 comments on commit 53b8d66

Please sign in to comment.