Skip to content

Commit

Permalink
Fix Java xmlinputfactory rules (semgrep#3347)
Browse files Browse the repository at this point in the history
* fix java xmlinputfactory rules

* fix java xmlinputfactory rules

* fix java xmlinputfactory rules
  • Loading branch information
inkz authored Apr 8, 2024
1 parent 144ad6e commit 4b0ac74
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
22 changes: 22 additions & 0 deletions java/lang/security/xmlinputfactory-external-entities-enabled.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package example;

import javax.xml.stream.XMLInputFactory;
import static javax.xml.stream.XMLInputFactory.SUPPORT_DTD;

class GoodXMLInputFactory {
public GoodXMLInputFactory() {
Expand All @@ -16,10 +17,31 @@ public GoodXMLInputFactory() {
}
}

class GoodXMLInputFactory1 {
public GoodXMLInputFactory1() {
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
// ok:xmlinputfactory-external-entities-enabled
xmlInputFactory.setProperty(SUPPORT_DTD, false);
}
}

class BadXMLInputFactory {
public BadXMLInputFactory() {
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
// ruleid:xmlinputfactory-external-entities-enabled
xmlInputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", true);
}
}

class BadXMLInputFactory1 {
public BadXMLInputFactory1() {
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
// ruleid:xmlinputfactory-external-entities-enabled
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ rules:
to XML external entity
attacks. Disable external entities by setting "javax.xml.stream.isSupportingExternalEntities"
to false.
pattern: $XMLFACTORY.setProperty("javax.xml.stream.isSupportingExternalEntities", true);
patterns:
- 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);
languages:
- java
20 changes: 14 additions & 6 deletions java/lang/security/xmlinputfactory-possible-xxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
package example;

import javax.xml.stream.XMLInputFactory;
import static java.xml.stream.XMLFactoryInput.IS_SUPPORTING_EXTERNAL_ENTITIES;
import static javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES;

class GoodXMLInputFactory {
public void Blah() {
public void blah() {
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();

// See
Expand All @@ -18,7 +18,7 @@ public void Blah() {
}

class GoodConstXMLInputFactory {
public void Blah() {
public GoodConstXMLInputFactory() {
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();

// See
Expand All @@ -29,16 +29,24 @@ public void Blah() {
}
}

class BadXMLInputFactory {
public Blah() {
class BadXMLInputFactory1 {
public BadXMLInputFactory1() {
// ruleid:xmlinputfactory-possible-xxe
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
xmlInputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", true);
}
}

class BadXMLInputFactory2 {
public BadXMLInputFactory2() {
// ruleid:xmlinputfactory-possible-xxe
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
xmlInputFactory.setProperty(IS_SUPPORTING_EXTERNAL_ENTITIES, true);
}
}

class MaybeBadXMLInputFactory {
public Blah() {
public void foobar() {
// ruleid:xmlinputfactory-possible-xxe
final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
}
Expand Down
10 changes: 5 additions & 5 deletions java/lang/security/xmlinputfactory-possible-xxe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ rules:
to false.
patterns:
- pattern-not-inside: |
$RETURNTYPE $METHOD(...) {
$METHOD(...) {
...
$XMLFACTORY.setProperty("javax.xml.stream.isSupportingExternalEntities", false);
...
}
- pattern-not-inside: |
$RETURNTYPE $METHOD(...) {
$METHOD(...) {
...
$XMLFACTORY.setProperty(java.xml.stream.XMLFactoryInput.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
$XMLFACTORY.setProperty(javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
...
}
- pattern-either:
- pattern: $XMLFACTORY = $W.newFactory(...);
- pattern: $XMLFACTORY = new XMLInputFactory(...);
- pattern: javax.xml.stream.XMLInputFactory.newFactory(...)
- pattern: new XMLInputFactory(...)
languages:
- java

0 comments on commit 4b0ac74

Please sign in to comment.