GeezerVison is showing github.com — Merge pull request #6064 from tausbn/python-add-get-method-call · max2max4u/codeql@e05d6e7
File treeExpand file treeCollapse file tree Open diff view settings experimental/semmle/python/frameworks
test/experimental/dataflow/method-calls
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +5Lines changed: 5 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 | +* A new class `DataFlow::MethodCallNode` extends `DataFlow::CallCfgNode` with convenient methods for | | 3 | + accessing the receiver and method name of a method call. | | 4 | +* The `LocalSourceNode` class now has a `getAMethodCall` method, with which one can easily access | | 5 | + method calls with the given node as a receiver. |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/Security/CWE-327/Ssl.qll +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,12 +39,10 @@ API::Node sslContextInstance() { | 39 | 39 | result = API::moduleImport("ssl").getMember(["SSLContext", "create_default_context"]).getReturn() | 40 | 40 | } | 41 | 41 |
| 42 | | -class WrapSocketCall extends ConnectionCreation, DataFlow::CallCfgNode { | | 42 | +class WrapSocketCall extends ConnectionCreation, DataFlow::MethodCallNode { | 43 | 43 | WrapSocketCall() { this = sslContextInstance().getMember("wrap_socket").getACall() } | 44 | 44 |
| 45 | | - override DataFlow::Node getContext() { | 46 | | - result = this.getFunction().(DataFlow::AttrRead).getObject() | 47 | | - } | | 45 | + override DataFlow::Node getContext() { result = this.getObject() } | 48 | 46 | } | 49 | 47 |
| 50 | 48 | class OptionsAugOr extends ProtocolRestriction, DataFlow::CfgNode { | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/experimental/semmle/python/frameworks/Stdlib.qll +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,15 +64,14 @@ private module Re { | 64 | 64 | * | 65 | 65 | * See https://docs.python.org/3/library/re.html#regular-expression-objects | 66 | 66 | */ | 67 | | - private class CompiledRegex extends DataFlow::CallCfgNode, RegexExecution::Range { | | 67 | + private class CompiledRegex extends DataFlow::MethodCallNode, RegexExecution::Range { | 68 | 68 | DataFlow::Node regexNode; | 69 | 69 |
| 70 | 70 | CompiledRegex() { | 71 | | - exists(DataFlow::CallCfgNode patternCall, DataFlow::AttrRead reMethod | | 72 | | - this.getFunction() = reMethod and | | 71 | + exists(DataFlow::MethodCallNode patternCall | | 73 | 72 | patternCall = API::moduleImport("re").getMember("compile").getACall() and | 74 | | - patternCall.flowsTo(reMethod.getObject()) and | 75 | | - reMethod.getAttributeName() instanceof RegexExecutionMethods and | | 73 | + patternCall.flowsTo(this.getObject()) and | | 74 | + this.getMethodName() instanceof RegexExecutionMethods and | 76 | 75 | regexNode = patternCall.getArg(0) | 77 | 76 | ) | 78 | 77 | } | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/dataflow/new/internal/Attributes.qll +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -191,7 +191,7 @@ private class ClassDefinitionAsAttrWrite extends AttrWrite, CfgNode { | 191 | 191 | * - Dynamic attribute reads using `getattr`: `getattr(object, attr)` | 192 | 192 | * - Qualified imports: `from module import attr as name` | 193 | 193 | */ | 194 | | -abstract class AttrRead extends AttrRef, Node { } | | 194 | +abstract class AttrRead extends AttrRef, Node, LocalSourceNode { } | 195 | 195 |
| 196 | 196 | /** A simple attribute read, e.g. `object.attr` */ | 197 | 197 | private class AttributeReadAsAttrRead extends AttrRead, CfgNode { | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/dataflow/new/internal/DataFlowPublic.qll +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -180,6 +180,45 @@ class CallCfgNode extends CfgNode, LocalSourceNode { | 180 | 180 | Node getArgByName(string name) { result.asCfgNode() = node.getArgByName(name) } | 181 | 181 | } | 182 | 182 |
| | 183 | +/** | | 184 | + * A data-flow node corresponding to a method call, that is `foo.bar(...)`. | | 185 | + * | | 186 | + * Also covers the case where the method lookup is done separately from the call itself, as in | | 187 | + * `temp = foo.bar; temp(...)`. Note that this is only tracked through local scope. | | 188 | + */ | | 189 | +class MethodCallNode extends CallCfgNode { | | 190 | + AttrRead method_lookup; | | 191 | + | | 192 | + MethodCallNode() { method_lookup = this.getFunction().getALocalSource() } | | 193 | + | | 194 | + /** | | 195 | + * Gets the name of the method being invoked (the `bar` in `foo.bar(...)`) if it can be determined. | | 196 | + * | | 197 | + * Note that this method may have multiple results if a single call node represents calls to | | 198 | + * multiple different objects and methods. If you want to link up objects and method names | | 199 | + * accurately, use the `calls` method instead. | | 200 | + */ | | 201 | + string getMethodName() { result = method_lookup.getAttributeName() } | | 202 | + | | 203 | + /** | | 204 | + * Gets the data-flow node corresponding to the object receiving this call. That is, the `foo` in | | 205 | + * `foo.bar(...)`. | | 206 | + * | | 207 | + * Note that this method may have multiple results if a single call node represents calls to | | 208 | + * multiple different objects and methods. If you want to link up objects and method names | | 209 | + * accurately, use the `calls` method instead. | | 210 | + */ | | 211 | + Node getObject() { result = method_lookup.getObject() } | | 212 | + | | 213 | + /** Holds if this data-flow node calls method `methodName` on the object node `object`. */ | | 214 | + predicate calls(Node object, string methodName) { | | 215 | + // As `getObject` and `getMethodName` may both have multiple results, we must look up the object | | 216 | + // and method name directly on `method_lookup`. | | 217 | + object = method_lookup.getObject() and | | 218 | + methodName = method_lookup.getAttributeName() | | 219 | + } | | 220 | +} | | 221 | + | 183 | 222 | /** | 184 | 223 | * An expression, viewed as a node in a data flow graph. | 185 | 224 | * | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/dataflow/new/internal/LocalSources.qll +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -78,6 +78,16 @@ class LocalSourceNode extends Node { | 78 | 78 | */ | 79 | 79 | CallCfgNode getACall() { Cached::call(this, result) } | 80 | 80 |
| | 81 | + /** | | 82 | + * Gets a call to the method `methodName` on this node. | | 83 | + * | | 84 | + * Includes both calls that have the syntactic shape of a method call (as in `obj.m(...)`), and | | 85 | + * calls where the callee undergoes some additional local data flow (as in `tmp = obj.m; m(...)`). | | 86 | + */ | | 87 | + MethodCallNode getAMethodCall(string methodName) { | | 88 | + result = this.getAnAttributeRead(methodName).getACall() | | 89 | + } | | 90 | + | 81 | 91 | /** | 82 | 92 | * Gets a node that this node may flow to using one heap and/or interprocedural step. | 83 | 93 | * | |
Collapse file Copy file name to clipboardExpand all lines: python/ql/src/semmle/python/frameworks/Cryptography.qll +9-27Lines changed: 9 additions & 27 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -228,11 +228,7 @@ private module CryptographyModel { | 228 | 228 | /** Gets a reference to the encryptor of a Cipher instance using algorithm with `algorithmName`. */ | 229 | 229 | DataFlow::LocalSourceNode cipherEncryptor(DataFlow::TypeTracker t, string algorithmName) { | 230 | 230 | t.start() and | 231 | | - exists(DataFlow::AttrRead attr | | 232 | | - result.(DataFlow::CallCfgNode).getFunction() = attr and | 233 | | - attr.getAttributeName() = "encryptor" and | 234 | | - attr.getObject() = cipherInstance(algorithmName) | 235 | | - ) | | 231 | + result.(DataFlow::MethodCallNode).calls(cipherInstance(algorithmName), "encryptor") | 236 | 232 | or | 237 | 233 | exists(DataFlow::TypeTracker t2 | result = cipherEncryptor(t2, algorithmName).track(t2, t)) | 238 | 234 | } | @@ -249,11 +245,7 @@ private module CryptographyModel { | 249 | 245 | /** Gets a reference to the dncryptor of a Cipher instance using algorithm with `algorithmName`. */ | 250 | 246 | DataFlow::LocalSourceNode cipherDecryptor(DataFlow::TypeTracker t, string algorithmName) { | 251 | 247 | t.start() and | 252 | | - exists(DataFlow::AttrRead attr | | 253 | | - result.(DataFlow::CallCfgNode).getFunction() = attr and | 254 | | - attr.getAttributeName() = "decryptor" and | 255 | | - attr.getObject() = cipherInstance(algorithmName) | 256 | | - ) | | 248 | + result.(DataFlow::MethodCallNode).calls(cipherInstance(algorithmName), "decryptor") | 257 | 249 | or | 258 | 250 | exists(DataFlow::TypeTracker t2 | result = cipherDecryptor(t2, algorithmName).track(t2, t)) | 259 | 251 | } | @@ -271,18 +263,14 @@ private module CryptographyModel { | 271 | 263 | * An encrypt or decrypt operation from `cryptography.hazmat.primitives.ciphers`. | 272 | 264 | */ | 273 | 265 | class CryptographyGenericCipherOperation extends Cryptography::CryptographicOperation::Range, | 274 | | - DataFlow::CallCfgNode { | | 266 | + DataFlow::MethodCallNode { | 275 | 267 | string algorithmName; | 276 | 268 |
| 277 | 269 | CryptographyGenericCipherOperation() { | 278 | | - exists(DataFlow::AttrRead attr | | 279 | | - this.getFunction() = attr and | 280 | | - attr.getAttributeName() = ["update", "update_into"] and | 281 | | - ( | 282 | | - attr.getObject() = cipherEncryptor(algorithmName) | 283 | | - or | 284 | | - attr.getObject() = cipherDecryptor(algorithmName) | 285 | | - ) | | 270 | + exists(DataFlow::Node object, string method | | | 271 | + object in [cipherEncryptor(algorithmName), cipherDecryptor(algorithmName)] and | | 272 | + method in ["update", "update_into"] and | | 273 | + this.calls(object, method) | 286 | 274 | ) | 287 | 275 | } | 288 | 276 |
| @@ -337,16 +325,10 @@ private module CryptographyModel { | 337 | 325 | * An hashing operation from `cryptography.hazmat.primitives.hashes`. | 338 | 326 | */ | 339 | 327 | class CryptographyGenericHashOperation extends Cryptography::CryptographicOperation::Range, | 340 | | - DataFlow::CallCfgNode { | | 328 | + DataFlow::MethodCallNode { | 341 | 329 | string algorithmName; | 342 | 330 |
| 343 | | - CryptographyGenericHashOperation() { | 344 | | - exists(DataFlow::AttrRead attr | | 345 | | - this.getFunction() = attr and | 346 | | - attr.getAttributeName() = "update" and | 347 | | - attr.getObject() = hashInstance(algorithmName) | 348 | | - ) | 349 | | - } | | 331 | + CryptographyGenericHashOperation() { this.calls(hashInstance(algorithmName), "update") } | 350 | 332 |
| 351 | 333 | override Cryptography::CryptographicAlgorithm getAlgorithm() { | 352 | 334 | result.matchesName(algorithmName) | |
Collapse file Copy file name to clipboard +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +conjunctive_lookup | | 2 | +| test.py:6:1:6:6 | ControlFlowNode for meth() | meth() | obj1 | bar | | | 3 | +| test.py:6:1:6:6 | ControlFlowNode for meth() | meth() | obj1 | foo | | | 4 | +| test.py:6:1:6:6 | ControlFlowNode for meth() | meth() | obj2 | bar | | | 5 | +| test.py:6:1:6:6 | ControlFlowNode for meth() | meth() | obj2 | foo | | | 6 | +calls_lookup | | 7 | +| test.py:6:1:6:6 | ControlFlowNode for meth() | meth() | obj1 | foo | | | 8 | +| test.py:6:1:6:6 | ControlFlowNode for meth() | meth() | obj2 | bar | |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +if cond: | | 2 | + meth = obj1.foo | | 3 | +else: | | 4 | + meth = obj2.bar | | 5 | + | | 6 | +meth() |
Collapse file Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import python | | 2 | +import semmle.python.dataflow.new.DataFlow | | 3 | +import experimental.dataflow.TestUtil.PrintNode | | 4 | + | | 5 | +query predicate conjunctive_lookup( | | 6 | + DataFlow::MethodCallNode methCall, string call, string object, string methodName | | 7 | +) { | | 8 | + call = prettyNode(methCall) and | | 9 | + object = prettyNode(methCall.getObject()) and | | 10 | + methodName = methCall.getMethodName() | | 11 | +} | | 12 | + | | 13 | +query predicate calls_lookup( | | 14 | + DataFlow::MethodCallNode methCall, string call, string object, string methodName | | 15 | +) { | | 16 | + call = prettyNode(methCall) and | | 17 | + exists(DataFlow::Node o | methCall.calls(o, methodName) and object = prettyNode(o)) | | 18 | +} |
|