Skip to content

Commit 882000a

Browse files
authored
python: not is confusing our logic
- added `is_unsafe` - added "negated version" of two tests. These versions do not use `not` and the analysis gets the taint right.
1 parent 3543864 commit 882000a

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

python/ql/test/experimental/dataflow/tainttracking/customSanitizer/InlineTaintTest.expected

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ isSanitizer
66
| TestTaintTrackingConfiguration | test.py:34:39:34:39 | ControlFlowNode for s |
77
| TestTaintTrackingConfiguration | test.py:52:28:52:28 | ControlFlowNode for s |
88
| TestTaintTrackingConfiguration | test.py:66:10:66:29 | ControlFlowNode for emulated_escaping() |
9-
| TestTaintTrackingConfiguration | test_logical.py:30:28:30:28 | ControlFlowNode for s |
10-
| TestTaintTrackingConfiguration | test_logical.py:45:28:45:28 | ControlFlowNode for s |
11-
| TestTaintTrackingConfiguration | test_logical.py:50:28:50:28 | ControlFlowNode for s |
12-
| TestTaintTrackingConfiguration | test_logical.py:89:28:89:28 | ControlFlowNode for s |
13-
| TestTaintTrackingConfiguration | test_logical.py:100:28:100:28 | ControlFlowNode for s |
14-
| TestTaintTrackingConfiguration | test_logical.py:145:28:145:28 | ControlFlowNode for s |
9+
| TestTaintTrackingConfiguration | test_logical.py:33:28:33:28 | ControlFlowNode for s |
10+
| TestTaintTrackingConfiguration | test_logical.py:48:28:48:28 | ControlFlowNode for s |
11+
| TestTaintTrackingConfiguration | test_logical.py:53:28:53:28 | ControlFlowNode for s |
12+
| TestTaintTrackingConfiguration | test_logical.py:92:28:92:28 | ControlFlowNode for s |
13+
| TestTaintTrackingConfiguration | test_logical.py:103:28:103:28 | ControlFlowNode for s |
1514
| TestTaintTrackingConfiguration | test_logical.py:148:28:148:28 | ControlFlowNode for s |
16-
| TestTaintTrackingConfiguration | test_logical.py:155:28:155:28 | ControlFlowNode for s |
15+
| TestTaintTrackingConfiguration | test_logical.py:151:28:151:28 | ControlFlowNode for s |
16+
| TestTaintTrackingConfiguration | test_logical.py:158:28:158:28 | ControlFlowNode for s |
17+
| TestTaintTrackingConfiguration | test_logical.py:176:24:176:24 | ControlFlowNode for s |
18+
| TestTaintTrackingConfiguration | test_logical.py:193:24:193:24 | ControlFlowNode for s |
1719
| TestTaintTrackingConfiguration | test_reference.py:31:28:31:28 | ControlFlowNode for s |

python/ql/test/experimental/dataflow/tainttracking/customSanitizer/InlineTaintTest.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ predicate isSafeCheck(DataFlow::GuardNode g, ControlFlowNode node, boolean branc
66
branch = true
77
}
88

9+
predicate isUnsafeCheck(DataFlow::GuardNode g, ControlFlowNode node, boolean branch) {
10+
g.(CallNode).getNode().getFunc().(Name).getId() in ["is_unsafe", "emulated_is_unsafe"] and
11+
node = g.(CallNode).getAnArg() and
12+
branch = false
13+
}
14+
915
class CustomSanitizerOverrides extends TestTaintTrackingConfiguration {
1016
override predicate isSanitizer(DataFlow::Node node) {
1117
exists(Call call |
@@ -16,6 +22,8 @@ class CustomSanitizerOverrides extends TestTaintTrackingConfiguration {
1622
node.asExpr().(Call).getFunc().(Name).getId() = "emulated_escaping"
1723
or
1824
node = DataFlow::BarrierGuard<isSafeCheck/3>::getABarrierNode()
25+
or
26+
node = DataFlow::BarrierGuard<isUnsafeCheck/3>::getABarrierNode()
1927
}
2028
}
2129

python/ql/test/experimental/dataflow/tainttracking/customSanitizer/test_logical.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def random_choice():
2222
def is_safe(arg):
2323
return arg == "safe"
2424

25+
def is_unsafe(arg):
26+
return arg == TAINTED_STRING
27+
2528

2629
def test_basic():
2730
s = TAINTED_STRING
@@ -164,6 +167,15 @@ def test_with_return():
164167
ensure_not_tainted(s) # $ SPURIOUS: tainted
165168

166169

170+
def test_with_return_neg():
171+
s = TAINTED_STRING
172+
173+
if is_unsafe(s):
174+
return
175+
176+
ensure_not_tainted(s)
177+
178+
167179
def test_with_exception():
168180
s = TAINTED_STRING
169181

@@ -172,6 +184,14 @@ def test_with_exception():
172184

173185
ensure_not_tainted(s) # $ SPURIOUS: tainted
174186

187+
def test_with_exception_neg():
188+
s = TAINTED_STRING
189+
190+
if is_unsafe(s):
191+
raise Exception("unsafe")
192+
193+
ensure_not_tainted(s)
194+
175195
# Make tests runable
176196

177197
test_basic()
@@ -182,7 +202,12 @@ def test_with_exception():
182202
test_nesting_not()
183203
test_nesting_not_with_and_true()
184204
test_with_return()
205+
test_with_return_neg()
185206
try:
186207
test_with_exception()
187208
except:
188209
pass
210+
try:
211+
test_with_exception_neg()
212+
except:
213+
pass

0 commit comments

Comments
 (0)