Skip to content

Commit cc9a7fe

Browse files
committed
Python: Move BarrierGuards to own file
1 parent f35ffa5 commit cc9a7fe

File tree

9 files changed

+62
-54
lines changed

9 files changed

+62
-54
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/** Provides commonly used BarrierGuards. */
2+
3+
private import python
4+
private import semmle.python.dataflow.new.DataFlow
5+
6+
/** A validation of unknown node by comparing with a constant string value. */
7+
class StringConstCompare extends DataFlow::BarrierGuard, CompareNode {
8+
ControlFlowNode checked_node;
9+
boolean safe_branch;
10+
11+
StringConstCompare() {
12+
exists(StrConst str_const, Cmpop op |
13+
op = any(Eq eq) and safe_branch = true
14+
or
15+
op = any(NotEq ne) and safe_branch = false
16+
|
17+
this.operands(str_const.getAFlowNode(), op, checked_node)
18+
or
19+
this.operands(checked_node, op, str_const.getAFlowNode())
20+
)
21+
or
22+
exists(ControlFlowNode str_const_iterable, Cmpop op |
23+
op = any(In in_) and safe_branch = true
24+
or
25+
op = any(NotIn ni) and safe_branch = false
26+
|
27+
this.operands(checked_node, op, str_const_iterable) and
28+
(
29+
str_const_iterable instanceof SequenceNode
30+
or
31+
str_const_iterable instanceof SetNode
32+
) and
33+
forall(ControlFlowNode elem |
34+
elem = str_const_iterable.(SequenceNode).getAnElement()
35+
or
36+
elem = str_const_iterable.(SetNode).getAnElement()
37+
|
38+
elem.getNode() instanceof StrConst
39+
)
40+
)
41+
}
42+
43+
override predicate checks(ControlFlowNode node, boolean branch) {
44+
node = checked_node and branch = safe_branch
45+
}
46+
}

python/ql/src/semmle/python/dataflow/new/internal/DataFlowPublic.qll

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -355,51 +355,6 @@ class BarrierGuard extends GuardNode {
355355
}
356356
}
357357

358-
/** Provides commonly used BarrierGuards */
359-
module BarrierGuard {
360-
/** A validation of unknown node by comparing with a constant string value. */
361-
class StringConstCompare extends BarrierGuard, CompareNode {
362-
ControlFlowNode checked_node;
363-
boolean safe_branch;
364-
365-
StringConstCompare() {
366-
exists(StrConst str_const, Cmpop op |
367-
op = any(Eq eq) and safe_branch = true
368-
or
369-
op = any(NotEq ne) and safe_branch = false
370-
|
371-
this.operands(str_const.getAFlowNode(), op, checked_node)
372-
or
373-
this.operands(checked_node, op, str_const.getAFlowNode())
374-
)
375-
or
376-
exists(ControlFlowNode str_const_iterable, Cmpop op |
377-
op = any(In in_) and safe_branch = true
378-
or
379-
op = any(NotIn ni) and safe_branch = false
380-
|
381-
this.operands(checked_node, op, str_const_iterable) and
382-
(
383-
str_const_iterable instanceof SequenceNode
384-
or
385-
str_const_iterable instanceof SetNode
386-
) and
387-
forall(ControlFlowNode elem |
388-
elem = str_const_iterable.(SequenceNode).getAnElement()
389-
or
390-
elem = str_const_iterable.(SetNode).getAnElement()
391-
|
392-
elem.getNode() instanceof StrConst
393-
)
394-
)
395-
}
396-
397-
override predicate checks(ControlFlowNode node, boolean branch) {
398-
node = checked_node and branch = safe_branch
399-
}
400-
}
401-
}
402-
403358
/**
404359
* Algebraic datatype for tracking data content associated with values.
405360
* Content can be collection elements or object attributes.

python/ql/src/semmle/python/security/dataflow/CodeInjection.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import semmle.python.dataflow.new.DataFlow
88
import semmle.python.dataflow.new.TaintTracking
99
import semmle.python.Concepts
1010
import semmle.python.dataflow.new.RemoteFlowSources
11+
import semmle.python.dataflow.new.BarrierGuards
1112

1213
/**
1314
* A taint-tracking configuration for detecting code injection vulnerabilities.
@@ -20,6 +21,6 @@ class CodeInjectionConfiguration extends TaintTracking::Configuration {
2021
override predicate isSink(DataFlow::Node sink) { sink = any(CodeExecution e).getCode() }
2122

2223
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
23-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
24+
guard instanceof StringConstCompare
2425
}
2526
}

python/ql/src/semmle/python/security/dataflow/CommandInjection.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import semmle.python.dataflow.new.DataFlow
88
import semmle.python.dataflow.new.TaintTracking
99
import semmle.python.Concepts
1010
import semmle.python.dataflow.new.RemoteFlowSources
11+
import semmle.python.dataflow.new.BarrierGuards
1112

1213
/**
1314
* A taint-tracking configuration for detecting command injection vulnerabilities.
@@ -50,6 +51,6 @@ class CommandInjectionConfiguration extends TaintTracking::Configuration {
5051
}
5152

5253
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
53-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
54+
guard instanceof StringConstCompare
5455
}
5556
}

python/ql/src/semmle/python/security/dataflow/PathInjection.qll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import semmle.python.dataflow.new.TaintTracking2
3131
import semmle.python.Concepts
3232
import semmle.python.dataflow.new.RemoteFlowSources
3333
import ChainedConfigs12
34+
import semmle.python.dataflow.new.BarrierGuards
3435

3536
// ---------------------------------------------------------------------------
3637
// Case 1. The path is never normalized.
@@ -48,7 +49,7 @@ class PathNotNormalizedConfiguration extends TaintTracking::Configuration {
4849
override predicate isSanitizer(DataFlow::Node node) { node instanceof Path::PathNormalization }
4950

5051
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
51-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
52+
guard instanceof StringConstCompare
5253
}
5354
}
5455

@@ -74,7 +75,7 @@ class FirstNormalizationConfiguration extends TaintTracking::Configuration {
7475
override predicate isSanitizerOut(DataFlow::Node node) { node instanceof Path::PathNormalization }
7576

7677
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
77-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
78+
guard instanceof StringConstCompare
7879
}
7980
}
8081

@@ -91,7 +92,7 @@ class NormalizedPathNotCheckedConfiguration extends TaintTracking2::Configuratio
9192
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
9293
guard instanceof Path::SafeAccessCheck
9394
or
94-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
95+
guard instanceof StringConstCompare
9596
}
9697
}
9798

python/ql/src/semmle/python/security/dataflow/ReflectedXSS.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import semmle.python.dataflow.new.DataFlow
88
import semmle.python.dataflow.new.TaintTracking
99
import semmle.python.Concepts
1010
import semmle.python.dataflow.new.RemoteFlowSources
11+
import semmle.python.dataflow.new.BarrierGuards
1112

1213
/**
1314
* A taint-tracking configuration for detecting reflected server-side cross-site
@@ -26,6 +27,6 @@ class ReflectedXssConfiguration extends TaintTracking::Configuration {
2627
}
2728

2829
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
29-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
30+
guard instanceof StringConstCompare
3031
}
3132
}

python/ql/src/semmle/python/security/dataflow/SqlInjection.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import semmle.python.dataflow.new.DataFlow
88
import semmle.python.dataflow.new.TaintTracking
99
import semmle.python.Concepts
1010
import semmle.python.dataflow.new.RemoteFlowSources
11+
import semmle.python.dataflow.new.BarrierGuards
1112

1213
/**
1314
* A taint-tracking configuration for detecting SQL injection vulnerabilities.
@@ -20,6 +21,6 @@ class SQLInjectionConfiguration extends TaintTracking::Configuration {
2021
override predicate isSink(DataFlow::Node sink) { sink = any(SqlExecution e).getSql() }
2122

2223
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
23-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
24+
guard instanceof StringConstCompare
2425
}
2526
}

python/ql/src/semmle/python/security/dataflow/UnsafeDeserialization.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import semmle.python.dataflow.new.DataFlow
88
import semmle.python.dataflow.new.TaintTracking
99
import semmle.python.Concepts
1010
import semmle.python.dataflow.new.RemoteFlowSources
11+
import semmle.python.dataflow.new.BarrierGuards
1112

1213
/**
1314
* A taint-tracking configuration for detecting arbitrary code execution
@@ -26,6 +27,6 @@ class UnsafeDeserializationConfiguration extends TaintTracking::Configuration {
2627
}
2728

2829
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
29-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
30+
guard instanceof StringConstCompare
3031
}
3132
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import experimental.dataflow.tainttracking.TestTaintLib
2+
import semmle.python.dataflow.new.BarrierGuards
23

34
class CustomSanitizerOverrides extends TestTaintTrackingConfiguration {
45
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
5-
guard instanceof DataFlow::BarrierGuard::StringConstCompare
6+
guard instanceof StringConstCompare
67
}
78
}

0 commit comments

Comments
 (0)