|
| 1 | +private import python |
| 2 | +private import semmle.python.dataflow.new.FlowSummary |
| 3 | +private import semmle.python.ApiGraphs |
| 4 | + |
| 5 | +/** This module ensures that the `callStep` predicate in |
| 6 | + * our type tracker implelemtation does not refer to the |
| 7 | + * `getACall` predicate on `SummarizedCallable`. |
| 8 | + */ |
| 9 | +module RecursionGuard { |
| 10 | + private import semmle.python.dataflow.new.internal.TypeTrackerSpecific as TT |
| 11 | + |
| 12 | + private class RecursionGuard extends SummarizedCallable { |
| 13 | + RecursionGuard() { this = "RecursionGuard" } |
| 14 | + |
| 15 | + override CallNode getACall() { |
| 16 | + result.getFunction().(NameNode).getId() = this and |
| 17 | + (TT::callStep(_, _) implies any()) |
| 18 | + } |
| 19 | + |
| 20 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +private class SummarizedCallableIdentity extends SummarizedCallable { |
| 25 | + SummarizedCallableIdentity() { this = "identity" } |
| 26 | + |
| 27 | + override CallNode getACall() { result.getFunction().(NameNode).getId() = this } |
| 28 | + |
| 29 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 30 | + |
| 31 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 32 | + input = "Argument[0]" and |
| 33 | + output = "ReturnValue" and |
| 34 | + preservesValue = true |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// For lambda flow to work, implement lambdaCall and lambdaCreation |
| 39 | +private class SummarizedCallableApplyLambda extends SummarizedCallable { |
| 40 | + SummarizedCallableApplyLambda() { this = "apply_lambda" } |
| 41 | + |
| 42 | + override CallNode getACall() { result.getFunction().(NameNode).getId() = this } |
| 43 | + |
| 44 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 45 | + |
| 46 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 47 | + input = "Argument[1]" and |
| 48 | + output = "Argument[0].Parameter[0]" and |
| 49 | + preservesValue = true |
| 50 | + or |
| 51 | + input = "Argument[0].ReturnValue" and |
| 52 | + output = "ReturnValue" and |
| 53 | + preservesValue = true |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +private class SummarizedCallableReversed extends SummarizedCallable { |
| 58 | + SummarizedCallableReversed() { this = "reversed" } |
| 59 | + |
| 60 | + override CallNode getACall() { result.getFunction().(NameNode).getId() = this } |
| 61 | + |
| 62 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 63 | + |
| 64 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 65 | + input = "Argument[0].ListElement" and |
| 66 | + output = "ReturnValue.ListElement" and |
| 67 | + preservesValue = true |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +private class SummarizedCallableMap extends SummarizedCallable { |
| 72 | + SummarizedCallableMap() { this = "map" } |
| 73 | + |
| 74 | + override CallNode getACall() { result.getFunction().(NameNode).getId() = this } |
| 75 | + |
| 76 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 77 | + |
| 78 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 79 | + input = "Argument[1].ListElement" and |
| 80 | + output = "Argument[0].Parameter[0]" and |
| 81 | + preservesValue = true |
| 82 | + or |
| 83 | + input = "Argument[0].ReturnValue" and |
| 84 | + output = "ReturnValue.ListElement" and |
| 85 | + preservesValue = true |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +private class SummarizedCallableAppend extends SummarizedCallable { |
| 90 | + SummarizedCallableAppend() { this = "append_to_list" } |
| 91 | + |
| 92 | + override CallNode getACall() { result.getFunction().(NameNode).getId() = this } |
| 93 | + |
| 94 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 95 | + |
| 96 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 97 | + input = "Argument[0]" and |
| 98 | + output = "ReturnValue" and |
| 99 | + preservesValue = false |
| 100 | + or |
| 101 | + input = "Argument[1]" and |
| 102 | + output = "ReturnValue.ListElement" and |
| 103 | + preservesValue = true |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +private class SummarizedCallableJsonLoads extends SummarizedCallable { |
| 108 | + SummarizedCallableJsonLoads() { this = "json.loads" } |
| 109 | + |
| 110 | + override CallNode getACall() { |
| 111 | + result = API::moduleImport("json").getMember("loads").getACall().getNode() |
| 112 | + } |
| 113 | + |
| 114 | + override DataFlow::ArgumentNode getACallback() { |
| 115 | + result = API::moduleImport("json").getMember("loads").getAValueReachableFromSource() |
| 116 | + } |
| 117 | + |
| 118 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 119 | + input = "Argument[0]" and |
| 120 | + output = "ReturnValue.ListElement" and |
| 121 | + preservesValue = true |
| 122 | + } |
| 123 | +} |
0 commit comments