|
| 1 | +private import python |
| 2 | +private import semmle.python.dataflow.new.FlowSummary |
| 3 | +private import semmle.python.ApiGraphs |
| 4 | + |
| 5 | +/** |
| 6 | + * This module ensures that the `callStep` predicate in |
| 7 | + * our type tracker implelemtation does not refer to the |
| 8 | + * `getACall` predicate on `SummarizedCallable`. |
| 9 | + */ |
| 10 | +module RecursionGuard { |
| 11 | + private import semmle.python.dataflow.new.internal.TypeTrackerSpecific as TT |
| 12 | + |
| 13 | + private class RecursionGuard extends SummarizedCallable { |
| 14 | + RecursionGuard() { this = "RecursionGuard" } |
| 15 | + |
| 16 | + override DataFlow::CallCfgNode getACall() { |
| 17 | + result.getFunction().asCfgNode().(NameNode).getId() = this and |
| 18 | + (TT::callStep(_, _) implies any()) |
| 19 | + } |
| 20 | + |
| 21 | + override DataFlow::CallCfgNode getACallSimple() { none() } |
| 22 | + |
| 23 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 24 | + } |
| 25 | + |
| 26 | + predicate test(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 27 | + TT::levelStepNoCall(nodeFrom, nodeTo) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +private class SummarizedCallableIdentity extends SummarizedCallable { |
| 32 | + SummarizedCallableIdentity() { this = "identity" } |
| 33 | + |
| 34 | + override DataFlow::CallCfgNode getACall() { none() } |
| 35 | + |
| 36 | + override DataFlow::CallCfgNode getACallSimple() { |
| 37 | + result.getFunction().asCfgNode().(NameNode).getId() = this |
| 38 | + } |
| 39 | + |
| 40 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 41 | + |
| 42 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 43 | + input = "Argument[0]" and |
| 44 | + output = "ReturnValue" and |
| 45 | + preservesValue = true |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// For lambda flow to work, implement lambdaCall and lambdaCreation |
| 50 | +private class SummarizedCallableApplyLambda extends SummarizedCallable { |
| 51 | + SummarizedCallableApplyLambda() { this = "apply_lambda" } |
| 52 | + |
| 53 | + override DataFlow::CallCfgNode getACall() { none() } |
| 54 | + |
| 55 | + override DataFlow::CallCfgNode getACallSimple() { |
| 56 | + result.getFunction().asCfgNode().(NameNode).getId() = this |
| 57 | + } |
| 58 | + |
| 59 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 60 | + |
| 61 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 62 | + input = "Argument[1]" and |
| 63 | + output = "Argument[0].Parameter[0]" and |
| 64 | + preservesValue = true |
| 65 | + or |
| 66 | + input = "Argument[0].ReturnValue" and |
| 67 | + output = "ReturnValue" and |
| 68 | + preservesValue = true |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +private class SummarizedCallableReversed extends SummarizedCallable { |
| 73 | + SummarizedCallableReversed() { this = "reversed" } |
| 74 | + |
| 75 | + override DataFlow::CallCfgNode getACall() { none() } |
| 76 | + |
| 77 | + override DataFlow::CallCfgNode getACallSimple() { |
| 78 | + result.getFunction().asCfgNode().(NameNode).getId() = this |
| 79 | + } |
| 80 | + |
| 81 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 82 | + |
| 83 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 84 | + input = "Argument[0].ListElement" and |
| 85 | + output = "ReturnValue.ListElement" and |
| 86 | + preservesValue = true |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +private class SummarizedCallableMap extends SummarizedCallable { |
| 91 | + SummarizedCallableMap() { this = "list_map" } |
| 92 | + |
| 93 | + override DataFlow::CallCfgNode getACall() { none() } |
| 94 | + |
| 95 | + override DataFlow::CallCfgNode getACallSimple() { |
| 96 | + result.getFunction().asCfgNode().(NameNode).getId() = this |
| 97 | + } |
| 98 | + |
| 99 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 100 | + |
| 101 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 102 | + input = "Argument[1].ListElement" and |
| 103 | + output = "Argument[0].Parameter[0]" and |
| 104 | + preservesValue = true |
| 105 | + or |
| 106 | + input = "Argument[0].ReturnValue" and |
| 107 | + output = "ReturnValue.ListElement" and |
| 108 | + preservesValue = true |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +private class SummarizedCallableAppend extends SummarizedCallable { |
| 113 | + SummarizedCallableAppend() { this = "append_to_list" } |
| 114 | + |
| 115 | + override DataFlow::CallCfgNode getACall() { none() } |
| 116 | + |
| 117 | + override DataFlow::CallCfgNode getACallSimple() { |
| 118 | + result.getFunction().asCfgNode().(NameNode).getId() = this |
| 119 | + } |
| 120 | + |
| 121 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 122 | + |
| 123 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 124 | + input = "Argument[0]" and |
| 125 | + output = "ReturnValue" and |
| 126 | + preservesValue = false |
| 127 | + or |
| 128 | + input = "Argument[1]" and |
| 129 | + output = "ReturnValue.ListElement" and |
| 130 | + preservesValue = true |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +private class SummarizedCallableJsonLoads extends SummarizedCallable { |
| 135 | + SummarizedCallableJsonLoads() { this = "json.loads" } |
| 136 | + |
| 137 | + override DataFlow::CallCfgNode getACall() { |
| 138 | + result = API::moduleImport("json").getMember("loads").getACall() |
| 139 | + } |
| 140 | + |
| 141 | + override DataFlow::CallCfgNode getACallSimple() { none() } |
| 142 | + |
| 143 | + override DataFlow::ArgumentNode getACallback() { |
| 144 | + result = API::moduleImport("json").getMember("loads").getAValueReachableFromSource() |
| 145 | + } |
| 146 | + |
| 147 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 148 | + input = "Argument[0]" and |
| 149 | + output = "ReturnValue.ListElement" and |
| 150 | + preservesValue = true |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// read and store |
| 155 | +private class SummarizedCallableReadSecret extends SummarizedCallable { |
| 156 | + SummarizedCallableReadSecret() { this = "read_secret" } |
| 157 | + |
| 158 | + override DataFlow::CallCfgNode getACall() { none() } |
| 159 | + |
| 160 | + override DataFlow::CallCfgNode getACallSimple() { |
| 161 | + result.getFunction().asCfgNode().(NameNode).getId() = this |
| 162 | + } |
| 163 | + |
| 164 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 165 | + |
| 166 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 167 | + input = "Argument[0].Attribute[secret]" and |
| 168 | + output = "ReturnValue" and |
| 169 | + preservesValue = true |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +private class SummarizedCallableSetSecret extends SummarizedCallable { |
| 174 | + SummarizedCallableSetSecret() { this = "set_secret" } |
| 175 | + |
| 176 | + override DataFlow::CallCfgNode getACall() { none() } |
| 177 | + |
| 178 | + override DataFlow::CallCfgNode getACallSimple() { |
| 179 | + result.getFunction().asCfgNode().(NameNode).getId() = this |
| 180 | + } |
| 181 | + |
| 182 | + override DataFlow::ArgumentNode getACallback() { result.asExpr().(Name).getId() = this } |
| 183 | + |
| 184 | + override predicate propagatesFlowExt(string input, string output, boolean preservesValue) { |
| 185 | + input = "Argument[1]" and |
| 186 | + output = "Argument[0].Attribute[secret]" and |
| 187 | + preservesValue = true |
| 188 | + } |
| 189 | +} |
0 commit comments