CVE-2023-41034 (published 2023-08-31T18:15Z)

Eclipse Leshan is a device management server and client Java implementation. In affected versions DDFFileParser` and `DefaultDDFFileValidator` (and so `ObjectLoader`) are vulnerable to `XXE Attacks`. A DDF file is a LWM2M format used to store LWM2M object description. Leshan users are impacted only if they parse untrusted DDF files (e.g. if they let external users provide their own model), in that case they MUST upgrade to fixed version. If you parse only trusted DDF file and validate only with trusted xml schema, upgrading is not mandatory.  This issue has been fixed in versions 1.5.0 and 2.0.0-M13. Users are advised to upgrade. There are no known workarounds for this vulnerability.

CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-611
cwe_name: Improper Restriction of XML External Entity Reference
description: The product processes an XML document that can contain XML entities with URIs that resolve to documents outside of the intended sphere of control, causing the product to embed incorrect documents into its output.
extended_description: 
            XML documents optionally contain a Document Type Definition (DTD), which, among other features, enables the definition of XML entities. It is possible to define an entity by providing a substitution string in the form of a URI. The XML parser can access the contents of this URI and embed these contents back into the XML document for further processing.
            By submitting an XML file that defines an external entity with a file:// URI, an attacker can cause the processing application to read the contents of a local file. For example, a URI such as "file:///c:/winnt/win.ini" designates (in Windows) the file C:\Winnt\win.ini, or file:///etc/passwd designates the password file in Unix-based systems. Using URIs with other schemes such as http://, the attacker can force the application to make outgoing requests to servers that the attacker cannot reach directly, which can be used to bypass firewall restrictions or hide the source of attacks such as port scanning.
            Once the content of the URI is read, it is fed back into the application that is processing the XML. This application may echo back the data (e.g. in an error message), thereby exposing the file contents.
         
         

Fix information:
repo_url: https://github.com/eclipse-leshan/leshan
commit_details: repo_url: https://github.com/eclipse-leshan/leshan
author: Simon Bernard
author_date: 2023-08-18 16:47:58+02:00
msg: Make DDFFileParser and DefaultDDFFileValidator safer.
repository: repo_url: https://github.com/eclipse-leshan/leshan
repo_name: eclipse-leshan/leshan
description: Java Library for LWM2M
repo_language: Java
owner: eclipse-leshan
file_changes: filename: DDFFileParser.java
change_type: ModificationType.MODIFY
diff: @@ -24,6 +24,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.xml.XMLConstants;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
@@ -75,12 +76,39 @@ public class DDFFileParser {
     }
 
     private DDFFileParser(DDFFileValidator ddfValidator, DDFFileValidatorFactory ddfFileValidatorFactory) {
-        factory = DocumentBuilderFactory.newInstance();
-        factory.setNamespaceAware(true);
+        this.factory = createDocumentBuilderFactory();
         this.ddfValidator = ddfValidator;
         this.ddfValidatorFactory = ddfFileValidatorFactory;
     }
 
+    protected DocumentBuilderFactory createDocumentBuilderFactory() {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        try {
+            // Create Safe DocumentBuilderFactory (not vulnerable to XXE Attacks)
+            // -----------------------------------------------------------------
+            // There is several recommendation from different source we try to apply all, even if some are maybe
+            // redundant.
+
+            // from :
+            // https://semgrep.dev/docs/cheat-sheets/java-xxe/
+            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+
+            // from :
+            // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#jaxp-documentbuilderfactory-saxparserfactory-and-dom4j
+            factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); // Disable DTDs
+            factory.setXIncludeAware(false); // Disable XML Inclusions
+
+            // from :
+            // https://community.veracode.com/s/article/Java-Remediation-Guidance-for-XXE
+            factory.setExpandEntityReferences(false); // disable expand entity reference nodes
+
+        } catch (ParserConfigurationException e) {
+            throw new IllegalStateException("Unable to create DocumentBuilderFactory", e);
+        }
+        factory.setNamespaceAware(true);
+        return factory;
+    }
+
     /**
      * Parse a DDF file.
      *

programming_language: Java
filename: DefaultDDFFileValidator.java
change_type: ModificationType.MODIFY
diff: @@ -30,6 +30,8 @@ import org.eclipse.leshan.core.LwM2m.LwM2mVersion;
 import org.eclipse.leshan.core.util.Validate;
 import org.w3c.dom.Node;
 import org.xml.sax.SAXException;
+import org.xml.sax.SAXNotRecognizedException;
+import org.xml.sax.SAXNotSupportedException;
 
 /**
  * A DDF File Validator.
@@ -95,7 +97,30 @@ public class DefaultDDFFileValidator implements DDFFileValidator {
     protected Schema getEmbeddedLwM2mSchema() throws SAXException {
         InputStream inputStream = DDFFileValidator.class.getResourceAsStream(schema);
         Source source = new StreamSource(inputStream);
-        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        SchemaFactory schemaFactory = createSchemaFactory();
         return schemaFactory.newSchema(source);
     }
+
+    protected SchemaFactory createSchemaFactory() {
+        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+        try {
+            // Create Safe SchemaFactory (not vulnerable to XXE Attacks)
+            // --------------------------------------------------------
+            // There is several recommendation from different source we try to apply all, even if some are maybe
+            // redundant.
+
+            // from :
+            // https://semgrep.dev/docs/cheat-sheets/java-xxe/
+            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+
+            // from :
+            // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html#schemafactory
+            factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
+            factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
+
+        } catch (SAXNotRecognizedException | SAXNotSupportedException e) {
+            throw new IllegalStateException("Unable to create SchemaFactory", e);
+        }
+        return factory;
+    }
 }

programming_language: Java