Skip to content

Commit f94055f

Browse files
committed
Move tainted path ad-hoc guard back.
1 parent 33526f6 commit f94055f

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

java/ql/src/Security/CWE/CWE-022/TaintedPath.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java
1616
import semmle.code.java.dataflow.FlowSources
1717
import semmle.code.java.security.PathCreation
1818
import DataFlow::PathGraph
19+
import TaintedPathCommon
1920

2021
class ContainsDotDotSanitizer extends DataFlow::BarrierGuard {
2122
ContainsDotDotSanitizer() {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Models a very basic guard for the tainted path queries.
3+
*/
4+
5+
import java
6+
import semmle.code.java.controlflow.Guards
7+
import semmle.code.java.security.PathCreation
8+
9+
private predicate inWeakCheck(Expr e) {
10+
// None of these are sufficient to guarantee that a string is safe.
11+
exists(MethodAccess m, Method def | m.getQualifier() = e and m.getMethod() = def |
12+
def.getName() = "startsWith" or
13+
def.getName() = "endsWith" or
14+
def.getName() = "isEmpty" or
15+
def.getName() = "equals"
16+
)
17+
or
18+
// Checking against `null` has no bearing on path traversal.
19+
exists(EqualityTest b | b.getAnOperand() = e | b.getAnOperand() instanceof NullLiteral)
20+
}
21+
22+
// Ignore cases where the variable has been checked somehow,
23+
// but allow some particularly obviously bad cases.
24+
predicate guarded(VarAccess e) {
25+
exists(PathCreation p | e = p.getAnInput()) and
26+
exists(ConditionBlock cb, Expr c |
27+
cb.getCondition().getAChildExpr*() = c and
28+
c = e.getVariable().getAnAccess() and
29+
cb.controls(e.getBasicBlock(), true) and
30+
// Disallow a few obviously bad checks.
31+
not inWeakCheck(c)
32+
)
33+
}

java/ql/src/Security/CWE/CWE-022/TaintedPathLocal.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ import java
1616
import semmle.code.java.dataflow.FlowSources
1717
import semmle.code.java.security.PathCreation
1818
import DataFlow::PathGraph
19+
import TaintedPathCommon
1920

2021
class TaintedPathLocalConfig extends TaintTracking::Configuration {
2122
TaintedPathLocalConfig() { this = "TaintedPathLocalConfig" }
2223

2324
override predicate isSource(DataFlow::Node source) { source instanceof LocalUserInput }
2425

25-
override predicate isSink(DataFlow::Node sink) { sink.asExpr() = any(PathCreation p).getAnInput() }
26+
override predicate isSink(DataFlow::Node sink) {
27+
sink.asExpr() = any(PathCreation p).getAnInput()
28+
}
2629
}
2730

2831
from

java/ql/src/semmle/code/java/security/PathCreation.qll

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44

55
import java
6-
import semmle.code.java.controlflow.Guards
76

87
/** Models the creation of a path. */
98
abstract class PathCreation extends Expr {
@@ -140,29 +139,3 @@ private class FileOutputStreamCreation extends PathCreation, ClassInstanceExpr {
140139
result.getType() instanceof TypeString
141140
}
142141
}
143-
144-
private predicate inWeakCheck(Expr e) {
145-
// None of these are sufficient to guarantee that a string is safe.
146-
exists(MethodAccess m, Method def | m.getQualifier() = e and m.getMethod() = def |
147-
def.getName() = "startsWith" or
148-
def.getName() = "endsWith" or
149-
def.getName() = "isEmpty" or
150-
def.getName() = "equals"
151-
)
152-
or
153-
// Checking against `null` has no bearing on path traversal.
154-
exists(EqualityTest b | b.getAnOperand() = e | b.getAnOperand() instanceof NullLiteral)
155-
}
156-
157-
// Ignore cases where the variable has been checked somehow,
158-
// but allow some particularly obviously bad cases.
159-
predicate guarded(VarAccess e) {
160-
exists(PathCreation p | e = p.getAnInput()) and
161-
exists(ConditionBlock cb, Expr c |
162-
cb.getCondition().getAChildExpr*() = c and
163-
c = e.getVariable().getAnAccess() and
164-
cb.controls(e.getBasicBlock(), true) and
165-
// Disallow a few obviously bad checks.
166-
not inWeakCheck(c)
167-
)
168-
}

0 commit comments

Comments
 (0)