Skip to content

Commit fcf5208

Browse files
authored
fix: harden XML parser against XXE in Cloud Functions sample (#10236)
* fix: harden XML parser against XXE in Cloud Functions sample DocumentBuilderFactory.newInstance() with default settings allows external entity resolution. This enables XXE attacks (file read, SSRF) when parsing untrusted XML input. Add disallow-doctype-decl feature per OWASP XXE Prevention Cheat Sheet. * address review: add defense-in-depth features, make field final, fix pom.xml target - Make dbFactory final per review suggestion - Add OWASP-recommended defense-in-depth features: external-general-entities, external-parameter-entities, load-external-dtd, XIncludeAware, ExpandEntityReferences - Fix pom.xml functionTarget: ParseContentType -> ParseXml
1 parent 42d5952 commit fcf5208

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

functions/http/parse-xml/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
<artifactId>function-maven-plugin</artifactId>
9595
<version>0.11.0</version>
9696
<configuration>
97-
<functionTarget>functions.ParseContentType</functionTarget>
97+
<functionTarget>functions.ParseXml</functionTarget>
9898
</configuration>
9999
</plugin>
100100
<plugin>

functions/http/parse-xml/src/main/java/functions/ParseXml.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,22 @@
3232
import org.xml.sax.SAXException;
3333

3434
public class ParseXml implements HttpFunction {
35-
private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
35+
private static final DocumentBuilderFactory dbFactory;
36+
37+
static {
38+
dbFactory = DocumentBuilderFactory.newInstance();
39+
try {
40+
// Prevent XXE attacks (see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html)
41+
dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
42+
dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
43+
dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
44+
dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
45+
dbFactory.setXIncludeAware(false);
46+
dbFactory.setExpandEntityReferences(false);
47+
} catch (ParserConfigurationException e) {
48+
throw new RuntimeException(e);
49+
}
50+
}
3651

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

0 commit comments

Comments
 (0)