GeezerVison is showing github.com — Merge pull request #6218 from tausbn/python-add-typetrackingnode · max2max4u/codeql@d282f6a
File treeExpand file treeCollapse file tree Open diff view settings experimental/dataflow/typetracking library-tests/frameworks/modeling-example
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: python/.vscode/ql.code-snippets +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 106 | 106 | "prefix": "type tracking", | 107 | 107 | "body": [ | 108 | 108 | "/** Gets a reference to ${3:a thing}. */", | 109 | | - "private DataFlow::LocalSourceNode ${1:myType}(DataFlow::TypeTracker t) {", | | 109 | + "private DataFlow::TypeTrackingNode ${1:myType}(DataFlow::TypeTracker t) {", | 110 | 110 | " t.start() and", | 111 | 111 | " result = ${2:value}", | 112 | 112 | " or", | |
Collapse file Copy file name to clipboard +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +lgtm,codescanning | | 2 | +* The `track` and `backtrack` methods on `LocalSourceNode` have been deprecated. When writing | | 3 | + type trackers, the corresponding methods on `TypeTrackingNode` should be used instead. |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/Security/CVE-2018-1281/BindToAllInterfaces.ql +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,7 +27,7 @@ private string vulnerableHostname() { | 27 | 27 | } | 28 | 28 |
| 29 | 29 | /** Gets a reference to a hostname that can be used to bind to all interfaces. */ | 30 | | -private DataFlow::LocalSourceNode vulnerableHostnameRef(DataFlow::TypeTracker t, string hostname) { | | 30 | +private DataFlow::TypeTrackingNode vulnerableHostnameRef(DataFlow::TypeTracker t, string hostname) { | 31 | 31 | t.start() and | 32 | 32 | exists(StrConst allInterfacesStrConst | hostname = vulnerableHostname() | | 33 | 33 | allInterfacesStrConst.getText() = hostname and | @@ -43,7 +43,7 @@ DataFlow::Node vulnerableHostnameRef(string hostname) { | 43 | 43 | } | 44 | 44 |
| 45 | 45 | /** Gets a reference to a tuple for which the first element is a hostname that can be used to bind to all interfaces. */ | 46 | | -private DataFlow::LocalSourceNode vulnerableAddressTuple(DataFlow::TypeTracker t, string hostname) { | | 46 | +private DataFlow::TypeTrackingNode vulnerableAddressTuple(DataFlow::TypeTracker t, string hostname) { | 47 | 47 | t.start() and | 48 | 48 | result.asExpr() = any(Tuple tup | tup.getElt(0) = vulnerableHostnameRef(hostname).asExpr()) | 49 | 49 | or | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/Security/CWE-215/FlaskDebug.ql +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,7 +17,7 @@ import semmle.python.ApiGraphs | 17 | 17 | import semmle.python.frameworks.Flask | 18 | 18 |
| 19 | 19 | /** Gets a reference to a truthy literal. */ | 20 | | -private DataFlow::LocalSourceNode truthyLiteral(DataFlow::TypeTracker t) { | | 20 | +private DataFlow::TypeTrackingNode truthyLiteral(DataFlow::TypeTracker t) { | 21 | 21 | t.start() and | 22 | 22 | result.asExpr().(ImmutableLiteral).booleanValue() = true | 23 | 23 | or | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/ApiGraphs.qll +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -512,7 +512,7 @@ module API { | 512 | 512 | * | 513 | 513 | * The flow from `src` to that node may be inter-procedural. | 514 | 514 | */ | 515 | | - private DataFlow::LocalSourceNode trackUseNode( | | 515 | + private DataFlow::TypeTrackingNode trackUseNode( | 516 | 516 | DataFlow::LocalSourceNode src, DataFlow::TypeTracker t | 517 | 517 | ) { | 518 | 518 | t.start() and | @@ -530,7 +530,6 @@ module API { | 530 | 530 | cached | 531 | 531 | DataFlow::LocalSourceNode trackUseNode(DataFlow::LocalSourceNode src) { | 532 | 532 | result = trackUseNode(src, DataFlow::TypeTracker::end()) and | 533 | | - // We exclude module variable nodes, as these do not correspond to real uses. | 534 | 533 | not result instanceof DataFlow::ModuleVariableNode | 535 | 534 | } | 536 | 535 |
| |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/Concepts.qll +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -758,7 +758,7 @@ module Cryptography { | 758 | 758 | /** Provides classes for modeling new key-pair generation APIs. */ | 759 | 759 | module KeyGeneration { | 760 | 760 | /** Gets a back-reference to the keysize argument `arg` that was used to generate a new key-pair. */ | 761 | | - private DataFlow::LocalSourceNode keysizeBacktracker( | | 761 | + private DataFlow::TypeTrackingNode keysizeBacktracker( | 762 | 762 | DataFlow::TypeBackTracker t, DataFlow::Node arg | 763 | 763 | ) { | 764 | 764 | t.start() and | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/dataflow/new/SensitiveDataSources.qll +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,7 @@ private module SensitiveDataModeling { | 55 | 55 | * Gets a reference to a function that is considered to be a sensitive source of | 56 | 56 | * `classification`. | 57 | 57 | */ | 58 | | - private DataFlow::LocalSourceNode sensitiveFunction( | | 58 | + private DataFlow::TypeTrackingNode sensitiveFunction( | 59 | 59 | DataFlow::TypeTracker t, SensitiveDataClassification classification | 60 | 60 | ) { | 61 | 61 | t.start() and | @@ -109,7 +109,7 @@ private module SensitiveDataModeling { | 109 | 109 | * | 110 | 110 | * Also see `extraStepForCalls`. | 111 | 111 | */ | 112 | | - private DataFlow::LocalSourceNode possibleSensitiveCallable(DataFlow::TypeTracker t) { | | 112 | + private DataFlow::TypeTrackingNode possibleSensitiveCallable(DataFlow::TypeTracker t) { | 113 | 113 | t.start() and | 114 | 114 | result instanceof SensitiveDataSource | 115 | 115 | or | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/dataflow/new/TypeTracker.qll +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,7 @@ class OptionalAttributeName = Internal::OptionalContentName; | 23 | 23 | * It is recommended that all uses of this type are written in the following form, | 24 | 24 | * for tracking some type `myType`: | 25 | 25 | * ```ql | 26 | | - * DataFlow::LocalSourceNode myType(DataFlow::TypeTracker t) { | | 26 | + * DataFlow::TypeTrackingNode myType(DataFlow::TypeTracker t) { | 27 | 27 | * t.start() and | 28 | 28 | * result = < source of myType > | 29 | 29 | * or | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/dataflow/new/internal/LocalSources.qll +56-4Lines changed: 56 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,8 +36,13 @@ class LocalSourceNode extends Node { | 36 | 36 | this instanceof ExprNode and | 37 | 37 | not simpleLocalFlowStep(_, this) | 38 | 38 | or | 39 | | - // Module variable nodes must be local source nodes, otherwise type trackers cannot step through | 40 | | - // them. | | 39 | + // We include all module variable nodes, as these act as stepping stones between writes and | | 40 | + // reads of global variables. Without them, type tracking based on `LocalSourceNode`s would be | | 41 | + // unable to track across global variables. | | 42 | + // | | 43 | + // Once the `track` and `backtrack` methods have been fully deprecated, this disjunct can be | | 44 | + // removed, and the entire class can extend `ExprNode`. At that point, `TypeTrackingNode` should | | 45 | + // be used for type tracking instead of `LocalSourceNode`. | 41 | 46 | this instanceof ModuleVariableNode | 42 | 47 | or | 43 | 48 | // We explicitly include any read of a global variable, as some of these may have local flow going | @@ -98,21 +103,68 @@ class LocalSourceNode extends Node { | 98 | 103 | result = this.getAnAttributeRead(methodName).getACall() | 99 | 104 | } | 100 | 105 |
| | 106 | + /** | | 107 | + * DEPRECATED. Use `TypeTrackingNode::track` instead. | | 108 | + * | | 109 | + * Gets a node that this node may flow to using one heap and/or interprocedural step. | | 110 | + * | | 111 | + * See `TypeTracker` for more details about how to use this. | | 112 | + */ | | 113 | + pragma[inline] | | 114 | + deprecated LocalSourceNode track(TypeTracker t2, TypeTracker t) { t = t2.step(this, result) } | | 115 | + | | 116 | + /** | | 117 | + * DEPRECATED. Use `TypeTrackingNode::backtrack` instead. | | 118 | + * | | 119 | + * Gets a node that may flow into this one using one heap and/or interprocedural step. | | 120 | + * | | 121 | + * See `TypeBackTracker` for more details about how to use this. | | 122 | + */ | | 123 | + pragma[inline] | | 124 | + deprecated LocalSourceNode backtrack(TypeBackTracker t2, TypeBackTracker t) { | | 125 | + t2 = t.step(result, this) | | 126 | + } | | 127 | +} | | 128 | + | | 129 | +/** | | 130 | + * A node that can be used for type tracking or type back-tracking. | | 131 | + * | | 132 | + * All steps made during type tracking should be between instances of this class. | | 133 | + */ | | 134 | +class TypeTrackingNode extends Node { | | 135 | + TypeTrackingNode() { | | 136 | + this instanceof LocalSourceNode | | 137 | + or | | 138 | + this instanceof ModuleVariableNode | | 139 | + } | | 140 | + | | 141 | + /** | | 142 | + * Holds if this node can flow to `nodeTo` in one or more local flow steps. | | 143 | + * | | 144 | + * For `ModuleVariableNode`s, the only "local" step is to the node itself. | | 145 | + * For `LocalSourceNode`s, this is the usual notion of local flow. | | 146 | + */ | | 147 | + predicate flowsTo(Node node) { | | 148 | + this instanceof ModuleVariableNode and this = node | | 149 | + or | | 150 | + this.(LocalSourceNode).flowsTo(node) | | 151 | + } | | 152 | + | 101 | 153 | /** | 102 | 154 | * Gets a node that this node may flow to using one heap and/or interprocedural step. | 103 | 155 | * | 104 | 156 | * See `TypeTracker` for more details about how to use this. | 105 | 157 | */ | 106 | 158 | pragma[inline] | 107 | | - LocalSourceNode track(TypeTracker t2, TypeTracker t) { t = t2.step(this, result) } | | 159 | + TypeTrackingNode track(TypeTracker t2, TypeTracker t) { t = t2.step(this, result) } | 108 | 160 |
| 109 | 161 | /** | 110 | 162 | * Gets a node that may flow into this one using one heap and/or interprocedural step. | 111 | 163 | * | 112 | 164 | * See `TypeBackTracker` for more details about how to use this. | 113 | 165 | */ | 114 | 166 | pragma[inline] | 115 | | - LocalSourceNode backtrack(TypeBackTracker t2, TypeBackTracker t) { t2 = t.step(result, this) } | | 167 | + TypeTrackingNode backtrack(TypeBackTracker t2, TypeBackTracker t) { t2 = t.step(result, this) } | 116 | 168 | } | 117 | 169 |
| 118 | 170 | cached | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/dataflow/new/internal/TypeTracker.qll +13-13Lines changed: 13 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,7 +59,7 @@ private module Cached { | 59 | 59 | * Steps contained in this predicate should _not_ depend on the call graph. | 60 | 60 | */ | 61 | 61 | cached | 62 | | - predicate stepNoCall(LocalSourceNode nodeFrom, LocalSourceNode nodeTo, StepSummary summary) { | | 62 | + predicate stepNoCall(TypeTrackingNode nodeFrom, TypeTrackingNode nodeTo, StepSummary summary) { | 63 | 63 | exists(Node mid | nodeFrom.flowsTo(mid) and smallstepNoCall(mid, nodeTo, summary)) | 64 | 64 | } | 65 | 65 |
| @@ -68,7 +68,7 @@ private module Cached { | 68 | 68 | * inter-procedural step from `nodeFrom` to `nodeTo`. | 69 | 69 | */ | 70 | 70 | cached | 71 | | - predicate stepCall(LocalSourceNode nodeFrom, LocalSourceNode nodeTo, StepSummary summary) { | | 71 | + predicate stepCall(TypeTrackingNode nodeFrom, TypeTrackingNode nodeTo, StepSummary summary) { | 72 | 72 | exists(Node mid | nodeFrom.flowsTo(mid) and smallstepCall(mid, nodeTo, summary)) | 73 | 73 | } | 74 | 74 | } | @@ -96,7 +96,7 @@ class StepSummary extends TStepSummary { | 96 | 96 | } | 97 | 97 |
| 98 | 98 | pragma[noinline] | 99 | | -private predicate smallstepNoCall(Node nodeFrom, LocalSourceNode nodeTo, StepSummary summary) { | | 99 | +private predicate smallstepNoCall(Node nodeFrom, TypeTrackingNode nodeTo, StepSummary summary) { | 100 | 100 | jumpStep(nodeFrom, nodeTo) and | 101 | 101 | summary = LevelStep() | 102 | 102 | or | @@ -109,7 +109,7 @@ private predicate smallstepNoCall(Node nodeFrom, LocalSourceNode nodeTo, StepSum | 109 | 109 | } | 110 | 110 |
| 111 | 111 | pragma[noinline] | 112 | | -private predicate smallstepCall(Node nodeFrom, LocalSourceNode nodeTo, StepSummary summary) { | | 112 | +private predicate smallstepCall(Node nodeFrom, TypeTrackingNode nodeTo, StepSummary summary) { | 113 | 113 | callStep(nodeFrom, nodeTo) and summary = CallStep() | 114 | 114 | or | 115 | 115 | returnStep(nodeFrom, nodeTo) and | @@ -129,7 +129,7 @@ module StepSummary { | 129 | 129 | * call graph. | 130 | 130 | */ | 131 | 131 | pragma[inline] | 132 | | - predicate step(LocalSourceNode nodeFrom, LocalSourceNode nodeTo, StepSummary summary) { | | 132 | + predicate step(TypeTrackingNode nodeFrom, TypeTrackingNode nodeTo, StepSummary summary) { | 133 | 133 | stepNoCall(nodeFrom, nodeTo, summary) | 134 | 134 | or | 135 | 135 | stepCall(nodeFrom, nodeTo, summary) | @@ -143,7 +143,7 @@ module StepSummary { | 143 | 143 | * type-preserving steps. | 144 | 144 | */ | 145 | 145 | pragma[inline] | 146 | | - predicate smallstep(Node nodeFrom, LocalSourceNode nodeTo, StepSummary summary) { | | 146 | + predicate smallstep(Node nodeFrom, TypeTrackingNode nodeTo, StepSummary summary) { | 147 | 147 | smallstepNoCall(nodeFrom, nodeTo, summary) | 148 | 148 | or | 149 | 149 | smallstepCall(nodeFrom, nodeTo, summary) | @@ -174,7 +174,7 @@ module StepSummary { | 174 | 174 | * function. This means we will track the fact that `x.attr` can have the type of `y` into the | 175 | 175 | * assignment to `z` inside `bar`, even though this attribute write happens _after_ `bar` is called. | 176 | 176 | */ | 177 | | - predicate localSourceStoreStep(Node nodeFrom, LocalSourceNode nodeTo, string content) { | | 177 | + predicate localSourceStoreStep(Node nodeFrom, TypeTrackingNode nodeTo, string content) { | 178 | 178 | exists(Node obj | nodeTo.flowsTo(obj) and basicStoreStep(nodeFrom, obj, content)) | 179 | 179 | } | 180 | 180 | } | @@ -192,7 +192,7 @@ private newtype TTypeTracker = MkTypeTracker(Boolean hasCall, OptionalContentNam | 192 | 192 | * It is recommended that all uses of this type are written in the following form, | 193 | 193 | * for tracking some type `myType`: | 194 | 194 | * ```ql | 195 | | - * DataFlow::LocalSourceNode myType(DataFlow::TypeTracker t) { | | 195 | + * DataFlow::TypeTrackingNode myType(DataFlow::TypeTracker t) { | 196 | 196 | * t.start() and | 197 | 197 | * result = < source of myType > | 198 | 198 | * or | @@ -275,7 +275,7 @@ class TypeTracker extends TTypeTracker { | 275 | 275 | * heap and/or inter-procedural step from `nodeFrom` to `nodeTo`. | 276 | 276 | */ | 277 | 277 | pragma[inline] | 278 | | - TypeTracker step(LocalSourceNode nodeFrom, LocalSourceNode nodeTo) { | | 278 | + TypeTracker step(TypeTrackingNode nodeFrom, TypeTrackingNode nodeTo) { | 279 | 279 | exists(StepSummary summary | | 280 | 280 | StepSummary::step(nodeFrom, pragma[only_bind_out](nodeTo), pragma[only_bind_into](summary)) and | 281 | 281 | result = this.append(pragma[only_bind_into](summary)) | @@ -342,7 +342,7 @@ private newtype TTypeBackTracker = MkTypeBackTracker(Boolean hasReturn, Optional | 342 | 342 | * for back-tracking some callback type `myCallback`: | 343 | 343 | * | 344 | 344 | * ```ql | 345 | | - * DataFlow::LocalSourceNode myCallback(DataFlow::TypeBackTracker t) { | | 345 | + * DataFlow::TypeTrackingNode myCallback(DataFlow::TypeBackTracker t) { | 346 | 346 | * t.start() and | 347 | 347 | * result = (< some API call >).getArgument(< n >).getALocalSource() | 348 | 348 | * or | @@ -351,7 +351,7 @@ private newtype TTypeBackTracker = MkTypeBackTracker(Boolean hasReturn, Optional | 351 | 351 | * ) | 352 | 352 | * } | 353 | 353 | * | 354 | | - * DataFlow::LocalSourceNode myCallback() { result = myCallback(DataFlow::TypeBackTracker::end()) } | | 354 | + * DataFlow::TypeTrackingNode myCallback() { result = myCallback(DataFlow::TypeBackTracker::end()) } | 355 | 355 | * ``` | 356 | 356 | * | 357 | 357 | * Instead of `result = myCallback(t2).backtrack(t2, t)`, you can also use the equivalent | @@ -418,7 +418,7 @@ class TypeBackTracker extends TTypeBackTracker { | 418 | 418 | * heap and/or inter-procedural step from `nodeTo` to `nodeFrom`. | 419 | 419 | */ | 420 | 420 | pragma[inline] | 421 | | - TypeBackTracker step(LocalSourceNode nodeFrom, LocalSourceNode nodeTo) { | | 421 | + TypeBackTracker step(TypeTrackingNode nodeFrom, TypeTrackingNode nodeTo) { | 422 | 422 | exists(StepSummary summary | | 423 | 423 | StepSummary::step(pragma[only_bind_out](nodeFrom), nodeTo, pragma[only_bind_into](summary)) and | 424 | 424 | this = result.prepend(pragma[only_bind_into](summary)) | @@ -431,7 +431,7 @@ class TypeBackTracker extends TTypeBackTracker { | 431 | 431 | * | 432 | 432 | * Unlike `TypeBackTracker::step`, this predicate exposes all edges | 433 | 433 | * in the flowgraph, and not just the edges between | 434 | | - * `LocalSourceNode`s. It may therefore be less performant. | | 434 | + * `TypeTrackingNode`s. It may therefore be less performant. | 435 | 435 | * | 436 | 436 | * Type tracking predicates using small steps typically take the following form: | 437 | 437 | * ```ql | |
|