Skip to content

Commit 4408482

Browse files
committed
Java: Refactor NumericCastTainted, NumericCastTaintedLocal
1 parent d4e6e77 commit 4408482

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

java/ql/src/Security/CWE/CWE-681/NumericCastTainted.ql

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@
1515
import java
1616
import semmle.code.java.dataflow.FlowSources
1717
import NumericCastCommon
18-
import DataFlow::PathGraph
1918

20-
private class NumericCastFlowConfig extends TaintTracking::Configuration {
21-
NumericCastFlowConfig() { this = "NumericCastTainted::RemoteUserInputToNumericNarrowingCastExpr" }
19+
module NumericCastFlowConfig implements DataFlow::ConfigSig {
20+
predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource }
2221

23-
override predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource }
24-
25-
override predicate isSink(DataFlow::Node sink) {
22+
predicate isSink(DataFlow::Node sink) {
2623
sink.asExpr() = any(NumericNarrowingCastExpr cast).getExpr() and
2724
sink.asExpr() instanceof VarAccess
2825
}
2926

30-
override predicate isSanitizer(DataFlow::Node node) {
27+
predicate isBarrier(DataFlow::Node node) {
3128
boundedRead(node.asExpr()) or
3229
castCheck(node.asExpr()) or
3330
node.getType() instanceof SmallType or
@@ -37,12 +34,14 @@ private class NumericCastFlowConfig extends TaintTracking::Configuration {
3734
}
3835
}
3936

40-
from
41-
DataFlow::PathNode source, DataFlow::PathNode sink, NumericNarrowingCastExpr exp,
42-
NumericCastFlowConfig conf
37+
module NumericCastFlow = TaintTracking::Make<NumericCastFlowConfig>;
38+
39+
import NumericCastFlow::PathGraph
40+
41+
from NumericCastFlow::PathNode source, NumericCastFlow::PathNode sink, NumericNarrowingCastExpr exp
4342
where
4443
sink.getNode().asExpr() = exp.getExpr() and
45-
conf.hasFlowPath(source, sink)
44+
NumericCastFlow::hasFlowPath(source, sink)
4645
select exp, source, sink,
4746
"This cast to a narrower type depends on a $@, potentially causing truncation.", source.getNode(),
4847
"user-provided value"

java/ql/src/Security/CWE/CWE-681/NumericCastTaintedLocal.ql

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,15 @@
1515
import java
1616
import semmle.code.java.dataflow.FlowSources
1717
import NumericCastCommon
18-
import DataFlow::PathGraph
1918

20-
private class NumericCastFlowConfig extends TaintTracking::Configuration {
21-
NumericCastFlowConfig() {
22-
this = "NumericCastTaintedLocal::LocalUserInputToNumericNarrowingCastExpr"
23-
}
24-
25-
override predicate isSource(DataFlow::Node src) { src instanceof LocalUserInput }
19+
module NumericCastFlowConfig implements DataFlow::ConfigSig {
20+
predicate isSource(DataFlow::Node src) { src instanceof LocalUserInput }
2621

27-
override predicate isSink(DataFlow::Node sink) {
22+
predicate isSink(DataFlow::Node sink) {
2823
sink.asExpr() = any(NumericNarrowingCastExpr cast).getExpr()
2924
}
3025

31-
override predicate isSanitizer(DataFlow::Node node) {
26+
predicate isBarrier(DataFlow::Node node) {
3227
boundedRead(node.asExpr()) or
3328
castCheck(node.asExpr()) or
3429
node.getType() instanceof SmallType or
@@ -37,13 +32,17 @@ private class NumericCastFlowConfig extends TaintTracking::Configuration {
3732
}
3833
}
3934

35+
module NumericCastFlow = TaintTracking::Make<NumericCastFlowConfig>;
36+
37+
import NumericCastFlow::PathGraph
38+
4039
from
41-
DataFlow::PathNode source, DataFlow::PathNode sink, NumericNarrowingCastExpr exp,
42-
VarAccess tainted, NumericCastFlowConfig conf
40+
NumericCastFlow::PathNode source, NumericCastFlow::PathNode sink, NumericNarrowingCastExpr exp,
41+
VarAccess tainted
4342
where
4443
exp.getExpr() = tainted and
4544
sink.getNode().asExpr() = tainted and
46-
conf.hasFlowPath(source, sink) and
45+
NumericCastFlow::hasFlowPath(source, sink) and
4746
not exists(RightShiftOp e | e.getShiftedVariable() = tainted.getVariable())
4847
select exp, source, sink,
4948
"This cast to a narrower type depends on a $@, potentially causing truncation.", source.getNode(),

0 commit comments

Comments
 (0)