Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion functions/http/parse-xml/src/main/java/functions/ParseXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@
import org.xml.sax.SAXException;

public class ParseXml implements HttpFunction {
private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
private static DocumentBuilderFactory dbFactory;

static {
dbFactory = DocumentBuilderFactory.newInstance();
try {
// Prevent XXE attacks (see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html)
dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-medium medium

To improve the robustness and maintainability of the XML parser configuration, consider making the dbFactory field final and adding additional defense-in-depth features as recommended by the OWASP XXE Prevention Cheat Sheet. While disallow-doctype-decl is highly effective, these extra settings provide layered security against various XXE vectors.

  private static final DocumentBuilderFactory dbFactory;

  static {
    dbFactory = DocumentBuilderFactory.newInstance();
    try {
      // Prevent XXE attacks (see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html)
      dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
      dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
      dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
      dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
      dbFactory.setXIncludeAware(false);
      dbFactory.setExpandEntityReferences(false);
    } catch (ParserConfigurationException e) {
      throw new RuntimeException(e);
    }
  }


// Parses a HTTP request in XML format
// (Responds with a 400 error if the HTTP request isn't valid XML.)
Expand Down