Skip to content

Commit

Permalink
Merge pull request #3362 from semgrep/inkz/xmlinputfactory-fix-2
Browse files Browse the repository at this point in the history
Fix Java xmlinputfactory rules (again)
  • Loading branch information
philipturnbull authored Apr 25, 2024
2 parents 27f35ab + f3da09a commit fc2cb1f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ rules:
- pattern-either:
- pattern: (javax.xml.stream.XMLInputFactory $XMLFACTORY).setProperty("javax.xml.stream.isSupportingExternalEntities", true);
- pattern: (javax.xml.stream.XMLInputFactory $XMLFACTORY).setProperty(javax.xml.stream.XMLInputFactory.SUPPORT_DTD, true);
- pattern: (javax.xml.stream.XMLInputFactory $XMLFACTORY).setProperty("javax.xml.stream.isSupportingExternalEntities", Boolean.TRUE);
- pattern: (javax.xml.stream.XMLInputFactory $XMLFACTORY).setProperty(javax.xml.stream.XMLInputFactory.SUPPORT_DTD, Boolean.TRUE);
languages:
- java
12 changes: 12 additions & 0 deletions java/lang/security/xmlinputfactory-possible-xxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public GoodConstXMLInputFactory() {
}
}

class GoodConstXMLInputFactory1 {
public GoodConstXMLInputFactory1() {
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();

// See
// https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.md#xmlinputfactory-a-stax-parser
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
// ok
xmlInputFactory.setProperty(IS_SUPPORTING_EXTERNAL_ENTITIES, false);
}
}

class BadXMLInputFactory1 {
public BadXMLInputFactory1() {
// ruleid:xmlinputfactory-possible-xxe
Expand Down
12 changes: 12 additions & 0 deletions java/lang/security/xmlinputfactory-possible-xxe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ rules:
$XMLFACTORY.setProperty(javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
...
}
- pattern-not-inside: |
$METHOD(...) {
...
$XMLFACTORY.setProperty("javax.xml.stream.isSupportingExternalEntities", Boolean.FALSE);
...
}
- pattern-not-inside: |
$METHOD(...) {
...
$XMLFACTORY.setProperty(javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
...
}
- pattern-either:
- pattern: javax.xml.stream.XMLInputFactory.newFactory(...)
- pattern: new XMLInputFactory(...)
Expand Down

0 comments on commit fc2cb1f

Please sign in to comment.