CVE-2023-41034 (Java, published 2023-08-31T18:15Z)
Description: value: 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.
Severity: nan (CVSS v3 base score: 9.8)
Weakness: CWE-611 \u2014 Improper Restriction of XML External Entity Reference
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.
Repository: https://github.com/eclipse-leshan/leshan
Files changed:
leshan-core/src/main/java/org/eclipse/leshan/core/model/DDFFileParser.java
leshan-core/src/main/java/org/eclipse/leshan/core/model/DefaultDDFFileValidator.java
Commit message: Make DDFFileParser and DefaultDDFFileValidator safer.


Vulnerable code:
        factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Fixed code:
import javax.xml.XMLConstants;
        this.factory = createDocumentBuilderFactory();
    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;
    }

import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
        SchemaFactory schemaFactory = createSchemaFactory();

    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;
    }