Skip to content

Commit aa4fd19

Browse files
committed
Python: compact types in type models
1 parent b902dd5 commit aa4fd19

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

docs/codeql/codeql-language-guides/customizing-library-models-for-python.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ We could add a data extension similar to the previous one, but with the type **i
128128
pack: codeql/python-all
129129
extensible: typeModel
130130
data:
131-
- ["invoke.Context", "invoke", "Member[context].Member[Context].Instance"]
131+
- ["invoke.Context", "invoke.context.Context", ""]
132132
133133
- The first column, **"invoke.Context"**, is the name of the type to reach.
134-
- The second column, **"invoke"**, is the name of the type from which to evaluate the path.
135-
- The third column, **"Member[context].Member[Context].Instance"**, is the access path leading from **invoke** to **invoke.Context**.
134+
- The second column, **"invoke.context.Context"**, is the name of the type from which to evaluate the path.
135+
- The third column is just an empty string, indicating that any instance of **invoke.context.Context** is also an instance of **invoke.Context**.
136136

137137
Combining this with the sink model we added earlier, the sink in the example is detected by the model.
138138

python/ql/test/library-tests/dataflow/model-summaries/InlineTaintTest.ext.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ extensions:
3333
- ["foo.MS_Class", "foo", "Member[get_instance].ReturnValue"]
3434
- ["foo.MS_Class!", "foo", "Member[get_class].ReturnValue"]
3535
# Ideally this would be a consequence of the above line
36-
- ["foo.MS_Class", "foo", "Member[get_class].ReturnValue.Instance"]
36+
- ["foo.MS_Class", "foo", "Member[get_class].ReturnValue.Instance"]
37+
- ["foo.MS_Class", "foo.MS_Factory!", "Member[get_instance].ReturnValue"]
38+
- ["foo.MS_Class", "foo.MS_Factory", "Member[make].ReturnValue"]
39+
- ["foo.MS_Class", "foo.Impl.MS_Class_Impl", ""]

python/ql/test/library-tests/dataflow/model-summaries/NormalDataflowTest.ext.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ extensions:
3333
- ["foo.MS_Class", "foo", "Member[get_instance].ReturnValue"]
3434
- ["foo.MS_Class!", "foo", "Member[get_class].ReturnValue"]
3535
# Ideally this would be a consequence of the above line
36-
- ["foo.MS_Class", "foo", "Member[get_class].ReturnValue.Instance"]
36+
- ["foo.MS_Class", "foo", "Member[get_class].ReturnValue.Instance"]
37+
- ["foo.MS_Class", "foo.MS_Factory!", "Member[get_instance].ReturnValue"]
38+
- ["foo.MS_Class", "foo.MS_Factory", "Member[make].ReturnValue"]
39+
- ["foo.MS_Class", "foo.Impl.MS_Class_Impl", ""]

python/ql/test/library-tests/dataflow/model-summaries/model_summaries.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def SINK_F(x):
3030
ensure_tainted = ensure_not_tainted = print
3131
TAINTED_STRING = "TAINTED_STRING"
3232

33-
from foo import MS_identity, MS_apply_lambda, MS_reversed, MS_list_map, MS_append_to_list, MS_spread, MS_spread_all
33+
from foo import MS_identity, MS_apply_lambda, MS_reversed, MS_list_map, MS_append_to_list, MS_spread, MS_spread_all, Impl
3434

3535
# Simple summary
3636
via_identity = MS_identity(SOURCE)
@@ -122,7 +122,7 @@ def explicit_identity(x):
122122
SINK(a) # $ flow="SOURCE, l:-1 -> a"
123123
SINK(b) # $ flow="SOURCE, l:-2 -> b"
124124

125-
from foo import MS_Class, MS_Class_transitive, get_instance, get_class
125+
from foo import MS_Class, MS_Class_transitive, get_instance, get_class, MS_Factory
126126

127127
# Class summaries
128128
class_via_positional = MS_Class(SOURCE)
@@ -184,6 +184,14 @@ def __init__(y, x):
184184

185185
SINK(returned_class().instance_method(SOURCE)[1]) # $flow="SOURCE -> returned_class().instance_method(..)[1]"
186186

187+
fatory_instance = MS_Factory.get_instance()
188+
SINK(fatory_instance.instance_method(SOURCE)[1]) # $ flow="SOURCE -> fatory_instance.instance_method(..)[1]"
189+
190+
factory = MS_Factory()
191+
SINK(factory.make().instance_method(SOURCE)[1]) # $ flow="SOURCE -> factory.make().instance_method(..)[1]"
192+
193+
also_instance = Impl.MS_Class_Impl()
194+
SINK(also_instance.instance_method(SOURCE)[1]) # $ flow="SOURCE -> also_instance.instance_method(..)[1]"
187195

188196
# Modeled flow-summary is not value preserving
189197
from json import MS_loads as json_loads

0 commit comments

Comments
 (0)