Skip to content

Commit 58b6b3f

Browse files
authored
Merge pull request github#16789 from yoff/python/document-models-as-data
python: Document MaD format
2 parents 6a3bb4d + 6524b8e commit 58b6b3f

13 files changed

+534
-4
lines changed

docs/codeql/codeql-language-guides/codeql-for-python.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Experiment and learn how to write effective and efficient queries for CodeQL dat
1515
functions-in-python
1616
expressions-and-statements-in-python
1717
analyzing-control-flow-in-python
18+
customizing-library-models-for-python
1819

1920
- :doc:`Basic query for Python code <basic-query-for-python-code>`: Learn to write and run a simple CodeQL query.
2021

@@ -29,3 +30,5 @@ Experiment and learn how to write effective and efficient queries for CodeQL dat
2930
- :doc:`Expressions and statements in Python <expressions-and-statements-in-python>`: You can use syntactic classes from the CodeQL library to explore how Python expressions and statements are used in a codebase.
3031

3132
- :doc:`Analyzing control flow in Python <analyzing-control-flow-in-python>`: You can write CodeQL queries to explore the control-flow graph of a Python program, for example, to discover unreachable code or mutually exclusive blocks of code.
33+
34+
- :doc:`Customizing library models for Python <customizing-library-models-for-python>`: You can model frameworks and libraries that your codebase depends on using data extensions and publish them as CodeQL model packs.

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

Lines changed: 451 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* A number of Python queries now support sinks defined vi data extensions. The format of data extensions for Python has been documented.

python/ql/lib/semmle/python/frameworks/Fabric.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ private module FabricV1 {
2929
// -------------------------------------------------------------------------
3030
// fabric.api
3131
// -------------------------------------------------------------------------
32-
/** Gets a reference to the `fabric.api` module. */
33-
API::Node api() { result = fabric().getMember("api") }
32+
/** Gets a reference to the `fabric.api` module. Also known as `fabric.operations` */
33+
API::Node api() { result = fabric().getMember(["api", "operations"]) }
3434

3535
/** Provides models for the `fabric.api` module */
3636
module Api {

python/ql/lib/semmle/python/security/dataflow/CodeInjectionCustomizations.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import semmle.python.dataflow.new.DataFlow
99
private import semmle.python.Concepts
1010
private import semmle.python.dataflow.new.RemoteFlowSources
1111
private import semmle.python.dataflow.new.BarrierGuards
12+
private import semmle.python.frameworks.data.ModelsAsData
1213

1314
/**
1415
* Provides default sources, sinks and sanitizers for detecting
@@ -43,6 +44,10 @@ module CodeInjection {
4344
CodeExecutionAsSink() { this = any(CodeExecution e).getCode() }
4445
}
4546

47+
private class SinkFromModel extends Sink {
48+
SinkFromModel() { this = ModelOutput::getASinkNode("code-injection").asSink() }
49+
}
50+
4651
/**
4752
* A comparison with a constant string, considered as a sanitizer-guard.
4853
*/

python/ql/lib/semmle/python/security/dataflow/CommandInjectionCustomizations.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import semmle.python.dataflow.new.DataFlow
99
private import semmle.python.Concepts
1010
private import semmle.python.dataflow.new.RemoteFlowSources
1111
private import semmle.python.dataflow.new.BarrierGuards
12+
private import semmle.python.frameworks.data.ModelsAsData
1213

1314
/**
1415
* Provides default sources, sinks and sanitizers for detecting
@@ -78,6 +79,10 @@ module CommandInjection {
7879
}
7980
}
8081

82+
private class SinkFromModel extends Sink {
83+
SinkFromModel() { this = ModelOutput::getASinkNode("command-injection").asSink() }
84+
}
85+
8186
/**
8287
* A comparison with a constant string, considered as a sanitizer-guard.
8388
*/

python/ql/lib/semmle/python/security/dataflow/LogInjectionCustomizations.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import semmle.python.dataflow.new.DataFlow
99
private import semmle.python.Concepts
1010
private import semmle.python.dataflow.new.RemoteFlowSources
1111
private import semmle.python.dataflow.new.BarrierGuards
12+
private import semmle.python.frameworks.data.ModelsAsData
1213

1314
/**
1415
* Provides default sources, sinks and sanitizers for detecting
@@ -71,6 +72,10 @@ module LogInjection {
7172
}
7273
}
7374

75+
private class SinkFromModel extends Sink {
76+
SinkFromModel() { this = ModelOutput::getASinkNode("log-injection").asSink() }
77+
}
78+
7479
/**
7580
* A comparison with a constant string, considered as a sanitizer-guard.
7681
*/

python/ql/lib/semmle/python/security/dataflow/UnsafeDeserializationCustomizations.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import semmle.python.dataflow.new.DataFlow
99
private import semmle.python.Concepts
1010
private import semmle.python.dataflow.new.RemoteFlowSources
1111
private import semmle.python.dataflow.new.BarrierGuards
12+
private import semmle.python.frameworks.data.ModelsAsData
1213

1314
/**
1415
* Provides default sources, sinks and sanitizers for detecting
@@ -48,6 +49,10 @@ module UnsafeDeserialization {
4849
}
4950
}
5051

52+
private class SinkFromModel extends Sink {
53+
SinkFromModel() { this = ModelOutput::getASinkNode("unsafe-deserialization").asSink() }
54+
}
55+
5156
/**
5257
* A comparison with a constant string, considered as a sanitizer-guard.
5358
*/

python/ql/lib/semmle/python/security/dataflow/UrlRedirectCustomizations.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private import semmle.python.dataflow.new.DataFlow
99
private import semmle.python.Concepts
1010
private import semmle.python.dataflow.new.RemoteFlowSources
1111
private import semmle.python.dataflow.new.BarrierGuards
12+
private import semmle.python.frameworks.data.ModelsAsData
1213

1314
/**
1415
* Provides default sources, sinks and sanitizers for detecting
@@ -89,6 +90,10 @@ module UrlRedirect {
8990
}
9091
}
9192

93+
private class SinkFromModel extends Sink {
94+
SinkFromModel() { this = ModelOutput::getASinkNode("url-redirection").asSink() }
95+
}
96+
9297
/**
9398
* The right side of a string-concat, considered as a sanitizer.
9499
*/

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ extensions:
2525
- ["foo.MS_Class", "Member[instance_method]", "Argument[0]", "ReturnValue.TupleElement[1]", "value"]
2626
- ["foo.MS_Class", "Member[explicit_self]", "Argument[self:]", "ReturnValue", "value"]
2727
- ["json", "Member[MS_loads]", "Argument[0]", "ReturnValue", "taint"]
28+
29+
- addsTo:
30+
pack: codeql/python-all
31+
extensible: typeModel
32+
data:
33+
- ["foo.MS_Class", "foo", "Member[get_instance].ReturnValue"]
34+
- ["foo.MS_Class!", "foo", "Member[get_class].ReturnValue"]
35+
# Ideally this would be a consequence of the above line
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", ""]

0 commit comments

Comments
 (0)