Skip to content

Commit e111a19

Browse files
committed
python: split tests into taint and value
and add summaries
1 parent eb3c33d commit e111a19

File tree

9 files changed

+123
-18
lines changed

9 files changed

+123
-18
lines changed

python/ql/test/experimental/dataflow/model-summaries/TestSummaries.qll

Lines changed: 0 additions & 8 deletions
This file was deleted.
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.NormalDataflowTest
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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) {
8+
row =
9+
[
10+
"Foo;Member[MS_identity];Argument[0];ReturnValue;value",
11+
"Foo;Member[MS_apply_lambda];Argument[1];Argument[0].Parameter[0];value",
12+
"Foo;Member[MS_apply_lambda];Argument[0].ReturnValue;ReturnValue;value",
13+
"Foo;Member[MS_reversed];Argument[0].ListElement;ReturnValue.ListElement;value",
14+
"Foo;Member[MS_list_map];Argument[1].ListElement;Argument[0].Parameter[0];value",
15+
"Foo;Member[MS_list_map];Argument[0].ReturnValue;ReturnValue.ListElement;value",
16+
"Foo;Member[MS_append_to_list];Argument[0].ListElement;ReturnValue.ListElement;value",
17+
"Foo;Member[MS_append_to_list];Argument[1];ReturnValue.ListElement;value"
18+
]
19+
}
20+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 MS_identity, MS_apply_lambda, MS_reversed, MS_list_map, MS_append_to_list
32+
33+
# Simple summary
34+
tainted = MS_identity(SOURCE)
35+
SINK(tainted) # $ flow="SOURCE, l:-1 -> tainted"
36+
37+
# Lambda summary
38+
tainted_lambda = MS_apply_lambda(lambda x: [x], SOURCE)
39+
SINK(tainted_lambda[0]) # $ flow="SOURCE, l:-1 -> tainted_lambda[0]"
40+
41+
# A lambda that breaks the flow
42+
untainted_lambda = MS_apply_lambda(lambda x: 1, SOURCE)
43+
SINK_F(untainted_lambda)
44+
45+
# Collection summaries
46+
tainted_list = MS_reversed([SOURCE])
47+
SINK(tainted_list[0]) # $ flow="SOURCE, l:-1 -> tainted_list[0]"
48+
49+
# Complex summaries
50+
def box(x):
51+
return [x]
52+
53+
tainted_mapped = MS_list_map(box, [SOURCE])
54+
SINK(tainted_mapped[0][0]) # $ flow="SOURCE, l:-1 -> tainted_mapped[0][0]"
55+
56+
def explicit_identity(x):
57+
return x
58+
59+
tainted_mapped_explicit = MS_list_map(explicit_identity, [SOURCE])
60+
SINK(tainted_mapped_explicit[0]) # $ flow="SOURCE, l:-1 -> tainted_mapped_explicit[0]"
61+
62+
tainted_mapped_summary = MS_list_map(MS_identity, [SOURCE])
63+
SINK(tainted_mapped_summary[0]) # $ flow="SOURCE, l:-1 -> tainted_mapped_summary[0]"
64+
65+
tainted_list = MS_append_to_list([], SOURCE)
66+
SINK(tainted_list[0]) # $ flow="SOURCE, l:-1 -> tainted_list[0]"
67+
68+
tainted_list = MS_append_to_list([SOURCE], NONSOURCE)
69+
SINK(tainted_list[0]) # $ flow="SOURCE, l:-1 -> tainted_list[0]"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
missingAnnotationOnSink
2+
failures
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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) {
8+
row =
9+
[
10+
"Foo;Member[MS_identity];Argument[0];ReturnValue;value",
11+
"Foo;Member[MS_apply_lambda];Argument[1];Argument[0].Parameter[0];value",
12+
"Foo;Member[MS_apply_lambda];Argument[0].ReturnValue;ReturnValue;value",
13+
"Foo;Member[MS_reversed];Argument[0];ReturnValue;taint",
14+
"Foo;Member[MS_list_map];Argument[1];ReturnValue;taint",
15+
"Foo;Member[MS_append_to_list];Argument[0];ReturnValue;taint",
16+
"json;Member[MS_loads];Argument[0];ReturnValue;taint"
17+
]
18+
}
19+
}

python/ql/test/experimental/dataflow/model-summaries/model_summaries.py renamed to python/ql/test/experimental/dataflow/model-summaries/taint/model_summaries_taint.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,43 +28,43 @@ def SINK_F(x):
2828
print("OK")
2929

3030

31-
from Foo import MS_identity
31+
from Foo import MS_identity, MS_apply_lambda, MS_reversed, MS_list_map, MS_append_to_list
3232

3333
# Simple summary
3434
tainted = MS_identity(SOURCE)
35-
SINK(tainted) # $ MISSING: flow="SOURCE, l:-1 -> tainted"
35+
SINK(tainted) # $ flow="SOURCE, l:-1 -> tainted"
3636

3737
# Lambda summary
3838
tainted_lambda = MS_apply_lambda(lambda x: x + 1, SOURCE)
39-
SINK(tainted_lambda) # $ MISSING: flow="SOURCE, l:-1 -> tainted_lambda"
39+
SINK(tainted_lambda) # $ flow="SOURCE, l:-1 -> tainted_lambda"
4040

4141
# A lambda that breaks the flow
4242
untainted_lambda = MS_apply_lambda(lambda x: 1, SOURCE)
4343
SINK_F(untainted_lambda)
4444

4545
# Collection summaries
4646
tainted_list = MS_reversed([SOURCE])
47-
SINK(tainted_list[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_list[0]"
47+
SINK(tainted_list[0]) # $ flow="SOURCE, l:-1 -> tainted_list[0]"
4848

4949
# Complex summaries
5050
def add_colon(x):
5151
return x + ":"
5252

5353
tainted_mapped = MS_list_map(add_colon, [SOURCE])
54-
SINK(tainted_mapped[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_mapped[0]"
54+
SINK(tainted_mapped[0]) # $ flow="SOURCE, l:-1 -> tainted_mapped[0]"
5555

5656
def explicit_identity(x):
5757
return x
5858

5959
tainted_mapped_explicit = MS_list_map(explicit_identity, [SOURCE])
60-
SINK(tainted_mapped_explicit[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_mapped_explicit[0]"
60+
SINK(tainted_mapped_explicit[0]) # $ flow="SOURCE, l:-1 -> tainted_mapped_explicit[0]"
6161

6262
tainted_mapped_summary = MS_list_map(MS_identity, [SOURCE])
63-
SINK(tainted_mapped_summary[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_mapped_summary[0]"
63+
SINK(tainted_mapped_summary[0]) # $ flow="SOURCE, l:-1 -> tainted_mapped_summary[0]"
6464

65-
tainted_list = MS_append_to_list([], SOURCE)
66-
SINK(tainted_list[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_list[0]"
65+
tainted_list = MS_append_to_list([SOURCE], NONSOURCE)
66+
SINK(tainted_list[0]) # $ flow="SOURCE, l:-1 -> tainted_list[0]"
6767

6868
from json import MS_loads as json_loads
6969
tainted_resultlist = json_loads(SOURCE)
70-
SINK(tainted_resultlist[0]) # $ MISSING: flow="SOURCE, l:-1 -> tainted_resultlist[0]"
70+
SINK(tainted_resultlist[0]) # $ flow="SOURCE, l:-1 -> tainted_resultlist[0]"

0 commit comments

Comments
 (0)