2023-05-03 11:06:20 +02:00
== Why is this an issue?
2022-01-24 15:54:34 +01:00
include::../description.adoc[]
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2022-01-24 15:54:34 +01:00
For https://docs.oracle.com/javase/9/docs/api/javax/xml/parsers/DocumentBuilderFactory.html[DocumentBuilder], https://docs.oracle.com/javase/9/docs/api/javax/xml/parsers/SAXParserFactory.html[SAXParser] and https://docs.oracle.com/javase/9/docs/api/javax/xml/validation/SchemaFactory.html[Schema] JAPX factories:
2022-02-04 17:28:24 +01:00
[source,java]
2022-01-24 15:54:34 +01:00
----
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true); // Noncompliant
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true); // Noncompliant
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true); // Noncompliant
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true); // Noncompliant
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true); // Noncompliant
----
For https://dom4j.github.io/[Dom4j] library:
2022-02-04 17:28:24 +01:00
[source,java]
2022-01-24 15:54:34 +01:00
----
SAXReader xmlReader = new SAXReader(); // Noncompliant
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true); // Noncompliant
----
For http://www.jdom.org/[Jdom2] library:
2022-02-04 17:28:24 +01:00
[source,java]
2022-01-24 15:54:34 +01:00
----
SAXBuilder builder = new SAXBuilder();
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true); // Noncompliant
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2022-01-24 15:54:34 +01:00
For https://docs.oracle.com/javase/9/docs/api/javax/xml/parsers/DocumentBuilderFactory.html[DocumentBuilder], https://docs.oracle.com/javase/9/docs/api/javax/xml/parsers/SAXParserFactory.html[SAXParser] and https://docs.oracle.com/javase/9/docs/api/javax/xml/validation/SchemaFactory.html[Schema] JAPX factories:
2022-02-04 17:28:24 +01:00
[source,java]
2022-01-24 15:54:34 +01:00
----
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
----
For https://dom4j.github.io/[Dom4j] library:
2022-02-04 17:28:24 +01:00
[source,java]
2022-01-24 15:54:34 +01:00
----
SAXReader xmlReader = new SAXReader(); // Noncompliant
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
----
For http://www.jdom.org/[Jdom2] library:
2022-02-04 17:28:24 +01:00
[source,java]
2022-01-24 15:54:34 +01:00
----
SAXBuilder builder = new SAXBuilder();
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
----
2023-05-03 11:06:20 +02:00
=== Exceptions
2022-01-24 15:54:34 +01:00
This rules does not raise an issue when an `EntityResolver` is set.
2023-05-25 14:18:12 +02:00
[source,java]
2022-01-24 15:54:34 +01:00
----
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new MyEntityResolver());
SAXBuilder builder = new SAXBuilder();
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);
builder.setEntityResolver(new EntityResolver());
----
2023-05-03 11:06:20 +02:00
== Resources
2022-01-24 15:54:34 +01:00
* https://docs.oracle.com/en/java/javase/13/security/java-api-xml-processing-jaxp-security-guide.html#GUID-8CD65EF5-D113-4D5C-A564-B875C8625FAC[Oracle Java Documentation] - XML External Entity Injection Attack
2024-01-15 17:15:56 +01:00
* OWASP - https://owasp.org/www-project-top-ten/2017/A4_2017-XML_External_Entities_(XXE)[Top 10 2017 Category A4 - XML External Entities (XXE)]
2022-01-24 15:54:34 +01:00
* https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#java[OWASP XXE Prevention Cheat Sheet]
2024-01-15 17:15:56 +01:00
* CWE - https://cwe.mitre.org/data/definitions/611[CWE-611 - Information Exposure Through XML External Entity Reference]
* CWE - https://cwe.mitre.org/data/definitions/827[CWE-827 - Improper Control of Document Type Definition]
2022-01-24 15:54:34 +01:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Disable loading of external schemas in XML parsing.
2022-01-24 15:54:34 +01:00
'''
2022-01-25 13:38:33 +01:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 25 Jan 2022, 10:34:00 Quentin Jaquier wrote:
Quick fixes (for Java): even if it is technically possible to provide a fix that would result in compliant code, it does not sound wise to set properties blindly, as it can have side effects. Fixing the issue requires a careful and good understanding of the overall context of the code.
2022-01-24 15:54:34 +01:00
endif::env-github,rspecator-view[]