Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 71cb111

Browse files
authored
Merge branch 'main' into securingdev-remove-class-files
2 parents c9e1075 + c01f192 commit 71cb111

4 files changed

Lines changed: 81 additions & 15 deletions

File tree

java/CWE-611/XXELocal.ql

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,43 @@ import java
1717
import semmle.code.java.security.XmlParsers
1818
import semmle.code.java.dataflow.FlowSources
1919
import semmle.code.java.dataflow.TaintTracking2
20-
import DataFlow::PathGraph
20+
//import DataFlow::PathGraph
2121
import github.LocalSources
2222

23-
class SafeSAXSourceFlowConfig extends TaintTracking2::Configuration {
24-
SafeSAXSourceFlowConfig() { this = "XmlParsers::SafeSAXSourceFlowConfig" }
23+
module SafeSAXSourceFlowConfig implements DataFlow::ConfigSig {
24+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeSaxSource }
2525

26-
override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeSaxSource }
27-
28-
override predicate isSink(DataFlow::Node sink) {
26+
predicate isSink(DataFlow::Node sink) {
2927
sink.asExpr() = any(XmlParserCall parse).getSink()
3028
}
3129

32-
override int fieldFlowBranchLimit() { result = 0 }
30+
int fieldFlowBranchLimit() { result = 0 }
3331
}
3432

33+
module SafeSAXSourceFlow = TaintTracking::Global<SafeSAXSourceFlowConfig>;
34+
3535
class UnsafeXxeSink extends DataFlow::ExprNode {
3636
UnsafeXxeSink() {
37-
not exists(SafeSAXSourceFlowConfig safeSource | safeSource.hasFlowTo(this)) and
37+
not SafeSAXSourceFlow::flowTo(this) and
3838
exists(XmlParserCall parse |
3939
parse.getSink() = this.getExpr() and
4040
not parse.isSafe()
4141
)
4242
}
4343
}
4444

45-
class XxeConfig extends TaintTracking::Configuration {
46-
XxeConfig() { this = "XXE.ql::XxeConfig" }
45+
module XXELocalConfig implements DataFlow::ConfigSig {
46+
predicate isSource(DataFlow::Node source) {
47+
source instanceof LocalUserInput and
48+
not exists(DataFlow::Node src | src.asExpr() instanceof SafeSaxSource)}
4749

48-
override predicate isSource(DataFlow::Node src) { src instanceof LocalUserInput }
49-
50-
override predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeXxeSink }
50+
predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeXxeSink }
5151
}
5252

53-
from DataFlow::PathNode source, DataFlow::PathNode sink, XxeConfig conf
54-
where conf.hasFlowPath(source, sink)
53+
module XXELocalFlow = TaintTracking::Global<XXELocalConfig>;
54+
import XXELocalFlow::PathGraph
55+
56+
from XXELocalFlow::PathNode source, XXELocalFlow::PathNode sink
57+
where XXELocalFlow::flowPath(source, sink)
5558
select sink.getNode(), source, sink, "Unsafe parsing of XML file from $@.", source.getNode(),
5659
"user input"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
edges
2+
| XXELocal.java:15:39:15:63 | new FileInputStream(...) : FileInputStream | XXELocal.java:16:51:16:61 | inputStream : FileInputStream |
3+
| XXELocal.java:16:35:16:62 | new InputSource(...) : InputSource | XXELocal.java:24:25:24:35 | inputSource |
4+
| XXELocal.java:16:51:16:61 | inputStream : FileInputStream | XXELocal.java:16:35:16:62 | new InputSource(...) : InputSource |
5+
nodes
6+
| XXELocal.java:15:39:15:63 | new FileInputStream(...) : FileInputStream | semmle.label | new FileInputStream(...) : FileInputStream |
7+
| XXELocal.java:16:35:16:62 | new InputSource(...) : InputSource | semmle.label | new InputSource(...) : InputSource |
8+
| XXELocal.java:16:51:16:61 | inputStream : FileInputStream | semmle.label | inputStream : FileInputStream |
9+
| XXELocal.java:24:25:24:35 | inputSource | semmle.label | inputSource |
10+
subpaths
11+
#select
12+
| XXELocal.java:24:25:24:35 | inputSource | XXELocal.java:15:39:15:63 | new FileInputStream(...) : FileInputStream | XXELocal.java:24:25:24:35 | inputSource | Unsafe parsing of XML file from $@. | XXELocal.java:15:39:15:63 | new FileInputStream(...) | user input |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.io.File;
2+
import java.io.FileInputStream;
3+
import javax.xml.parsers.SAXParser;
4+
import javax.xml.parsers.SAXParserFactory;
5+
import org.xml.sax.Attributes;
6+
import org.xml.sax.InputSource;
7+
import org.xml.sax.SAXException;
8+
import org.xml.sax.helpers.DefaultHandler;
9+
import org.xml.sax.XMLReader;
10+
11+
public class XXELocal {
12+
public static void main(String[] args) throws Exception {
13+
// Get user input from file
14+
File file = new File("input.xml");
15+
FileInputStream inputStream = new FileInputStream(file);
16+
InputSource inputSource = new InputSource(inputStream);
17+
18+
// Get XML reader
19+
XMLReader xmlReader = getXMLReader();
20+
21+
// Parse XML
22+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
23+
SAXParser saxParser = saxParserFactory.newSAXParser();
24+
saxParser.parse(inputSource, new MyHandler());
25+
}
26+
27+
private static XMLReader getXMLReader() throws Exception {
28+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
29+
SAXParser saxParser = saxParserFactory.newSAXParser();
30+
XMLReader xmlReader = saxParser.getXMLReader();
31+
return xmlReader;
32+
}
33+
34+
private static class MyHandler extends DefaultHandler {
35+
@Override
36+
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
37+
// Handle start element
38+
}
39+
40+
@Override
41+
public void endElement(String uri, String localName, String qName) throws SAXException {
42+
// Handle end element
43+
}
44+
45+
@Override
46+
public void characters(char[] ch, int start, int length) throws SAXException {
47+
// Handle character data
48+
}
49+
}
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CWE-611/XXELocal.ql

0 commit comments

Comments
 (0)