Skip to content

Commit 8cb5e78

Browse files
committed
Refactor XXE files
1 parent 4c80ff0 commit 8cb5e78

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

java/ql/src/experimental/Security/CWE/CWE-611/XXE.ql

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414

1515
import java
1616
import XXELib
17+
import semmle.code.java.dataflow.TaintTracking
1718
import semmle.code.java.dataflow.FlowSources
18-
import DataFlow::PathGraph
19+
import XxeFlow::PathGraph
1920

20-
class XxeConfig extends TaintTracking::Configuration {
21-
XxeConfig() { this = "XxeConfig" }
21+
module XxeConfig implements DataFlow::ConfigSig {
22+
predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource }
2223

23-
override predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource }
24-
25-
override predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeXxeSink }
24+
predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeXxeSink }
2625
}
2726

28-
from DataFlow::PathNode source, DataFlow::PathNode sink, XxeConfig conf
29-
where conf.hasFlowPath(source, sink)
27+
module XxeFlow = TaintTracking::Global<XxeConfig>;
28+
29+
from XxeFlow::PathNode source, XxeFlow::PathNode sink
30+
where XxeFlow::flowPath(source, sink)
3031
select sink.getNode(), source, sink, "Unsafe parsing of XML file from $@.", source.getNode(),
3132
"user input"

java/ql/src/experimental/Security/CWE/CWE-611/XXELib.qll

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ class ValidatorValidate extends XmlParserCall {
5252

5353
override Expr getSink() { result = this.getArgument(0) }
5454

55-
override predicate isSafe() {
56-
exists(SafeValidatorFlowConfig svfc | svfc.hasFlowToExpr(this.getQualifier()))
57-
}
55+
override predicate isSafe() { SafeValidatorFlow::flowToExpr(this.getQualifier()) }
5856
}
5957

6058
/** A `ParserConfig` specific to `Validator`. */
@@ -82,21 +80,21 @@ class SafeValidator extends VarAccess {
8280
}
8381
}
8482

85-
private class SafeValidatorFlowConfig extends DataFlow3::Configuration {
86-
SafeValidatorFlowConfig() { this = "SafeValidatorFlowConfig" }
87-
88-
override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeValidator }
83+
private module SafeValidatorFlowConfig implements DataFlow::ConfigSig {
84+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeValidator }
8985

90-
override predicate isSink(DataFlow::Node sink) {
86+
predicate isSink(DataFlow::Node sink) {
9187
exists(MethodAccess ma |
9288
sink.asExpr() = ma.getQualifier() and
9389
ma.getMethod().getDeclaringType() instanceof Validator
9490
)
9591
}
9692

97-
override int fieldFlowBranchLimit() { result = 0 }
93+
int fieldFlowBranchLimit() { result = 0 }
9894
}
9995

96+
private module SafeValidatorFlow = DataFlow::Global<SafeValidatorFlowConfig>;
97+
10098
/**
10199
* The classes `org.apache.commons.digester3.Digester`, `org.apache.commons.digester.Digester` or `org.apache.tomcat.util.digester.Digester`.
102100
*/
@@ -121,9 +119,7 @@ class DigesterParse extends XmlParserCall {
121119

122120
override Expr getSink() { result = this.getArgument(0) }
123121

124-
override predicate isSafe() {
125-
exists(SafeDigesterFlowConfig sdfc | sdfc.hasFlowToExpr(this.getQualifier()))
126-
}
122+
override predicate isSafe() { SafeDigesterFlow::flowToExpr(this.getQualifier()) }
127123
}
128124

129125
/** A `ParserConfig` that is specific to `Digester`. */
@@ -170,20 +166,20 @@ class SafeDigester extends VarAccess {
170166
}
171167
}
172168

173-
private class SafeDigesterFlowConfig extends DataFlow4::Configuration {
174-
SafeDigesterFlowConfig() { this = "SafeDigesterFlowConfig" }
175-
176-
override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeDigester }
169+
private module SafeDigesterFlowConfig implements DataFlow::ConfigSig {
170+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeDigester }
177171

178-
override predicate isSink(DataFlow::Node sink) {
172+
predicate isSink(DataFlow::Node sink) {
179173
exists(MethodAccess ma |
180174
sink.asExpr() = ma.getQualifier() and ma.getMethod().getDeclaringType() instanceof Digester
181175
)
182176
}
183177

184-
override int fieldFlowBranchLimit() { result = 0 }
178+
int fieldFlowBranchLimit() { result = 0 }
185179
}
186180

181+
private module SafeDigesterFlow = DataFlow::Global<SafeDigesterFlowConfig>;
182+
187183
/** The class `java.beans.XMLDecoder`. */
188184
class XmlDecoder extends RefType {
189185
XmlDecoder() { this.hasQualifiedName("java.beans", "XMLDecoder") }

java/ql/src/experimental/Security/CWE/CWE-611/XXELocal.ql

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@
1616

1717
import java
1818
import XXELib
19+
import semmle.code.java.dataflow.TaintTracking
1920
import semmle.code.java.dataflow.FlowSources
20-
import DataFlow::PathGraph
21+
import XxeLocalFlow::PathGraph
2122

22-
class XxeLocalConfig extends TaintTracking::Configuration {
23-
XxeLocalConfig() { this = "XxeLocalConfig" }
23+
module XxeLocalConfig implements DataFlow::ConfigSig {
24+
predicate isSource(DataFlow::Node src) { src instanceof LocalUserInput }
2425

25-
override predicate isSource(DataFlow::Node src) { src instanceof LocalUserInput }
26-
27-
override predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeXxeSink }
26+
predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeXxeSink }
2827
}
2928

30-
from DataFlow::PathNode source, DataFlow::PathNode sink, XxeLocalConfig conf
31-
where conf.hasFlowPath(source, sink)
29+
module XxeLocalFlow = TaintTracking::Global<XxeLocalConfig>;
30+
31+
from XxeLocalFlow::PathNode source, XxeLocalFlow::PathNode sink
32+
where XxeLocalFlow::flowPath(source, sink)
3233
select sink.getNode(), source, sink, "Unsafe parsing of XML file from $@.", source.getNode(),
3334
"user input"

0 commit comments

Comments
 (0)