Skip to content

Commit 6554e80

Browse files
committed
python: add test for model summaries
(but no summaries yet)
1 parent 3cf9e3e commit 6554e80

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
missingAnnotationOnSink
2+
failures
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import python
2+
private import TestSummaries
3+
import experimental.dataflow.TestUtil.NormalTaintTrackingTest
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
private import python
2+
private import semmle.python.dataflow.new.FlowSummary
3+
private import semmle.python.frameworks.data.ModelsAsData
4+
private import semmle.python.ApiGraphs
5+
6+
private class StepsFromModel extends ModelInput::SummaryModelCsv {
7+
override predicate row(string row) { none() }
8+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
import sys
3+
import os
4+
5+
sys.path.append(os.path.dirname(os.path.dirname((__file__))))
6+
from testlib import expects
7+
8+
# These are defined so that we can evaluate the test code.
9+
NONSOURCE = "not a source"
10+
SOURCE = "source"
11+
12+
13+
def is_source(x):
14+
return x == "source" or x == b"source" or x == 42 or x == 42.0 or x == 42j
15+
16+
17+
def SINK(x):
18+
if is_source(x):
19+
print("OK")
20+
else:
21+
print("Unexpected flow", x)
22+
23+
24+
def SINK_F(x):
25+
if is_source(x):
26+
print("Unexpected flow", x)
27+
else:
28+
print("OK")
29+
30+
31+
from Foo import identity
32+
33+
# Simple summary
34+
tainted = identity(SOURCE)
35+
SINK(tainted) # $ MISSING: flow="SOURCE, l:-1 -> tainted"
36+
37+
# Lambda summary
38+
tainted_lambda = apply_lambda(lambda x: x + 1, SOURCE)
39+
SINK(tainted_lambda) # $ MISSING: flow="SOURCE, l:-1 -> tainted_lambda"
40+
41+
# A lambda that breaks the flow
42+
untainted_lambda = apply_lambda(lambda x: 1, SOURCE)
43+
SINK_F(untainted_lambda)
44+
45+
# Collection summaries
46+
tainted_list = my_reversed([SOURCE])
47+
SINK(tainted_list[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_list[0]"
48+
49+
# Complex summaries
50+
def add_colon(x):
51+
return x + ":"
52+
53+
tainted_mapped = list_map(add_colon, [SOURCE])
54+
SINK(tainted_mapped[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_mapped[0]"
55+
56+
def explicit_identity(x):
57+
return x
58+
59+
tainted_mapped_explicit = list_map(explicit_identity, [SOURCE])
60+
SINK(tainted_mapped_explicit[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_mapped_explicit[0]"
61+
62+
tainted_mapped_summary = list_map(identity, [SOURCE])
63+
SINK(tainted_mapped_summary[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_mapped_summary[0]"
64+
65+
tainted_list = append_to_list([], SOURCE)
66+
SINK(tainted_list[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_list[0]"
67+
68+
from json import my_loads as json_loads
69+
tainted_resultlist = json_loads(SOURCE)
70+
SINK(tainted_resultlist[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_resultlist[0]"

0 commit comments

Comments
 (0)