|
| 1 | +/** |
| 2 | + * Models the `execa` library in terms of `FileSystemAccess` and `SystemCommandExecution`. |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | +import semmle.javascript.security.dataflow.RequestForgeryCustomizations |
| 7 | +import semmle.javascript.security.dataflow.UrlConcatenation |
| 8 | + |
| 9 | +/** |
| 10 | + * Provide model for [Execa](https://github.com/sindresorhus/execa) package |
| 11 | + */ |
| 12 | +module Execa { |
| 13 | + /** |
| 14 | + * The Execa input file option |
| 15 | + */ |
| 16 | + class ExecaRead extends FileSystemReadAccess, DataFlow::Node { |
| 17 | + API::Node execaNode; |
| 18 | + |
| 19 | + ExecaRead() { |
| 20 | + ( |
| 21 | + execaNode = API::moduleImport("execa").getMember("$").getParameter(0) |
| 22 | + or |
| 23 | + execaNode = |
| 24 | + API::moduleImport("execa") |
| 25 | + .getMember(["execa", "execaCommand", "execaCommandSync", "execaSync"]) |
| 26 | + .getParameter([0, 1, 2]) |
| 27 | + ) and |
| 28 | + this = execaNode.asSink() |
| 29 | + } |
| 30 | + |
| 31 | + // data is the output of a command so IDK how it can be implemented |
| 32 | + override DataFlow::Node getADataNode() { none() } |
| 33 | + |
| 34 | + override DataFlow::Node getAPathArgument() { |
| 35 | + result = execaNode.getMember("inputFile").asSink() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * A call to `execa.execa` or `execa.execaSync` |
| 41 | + */ |
| 42 | + class ExecaCall extends API::CallNode { |
| 43 | + string name; |
| 44 | + |
| 45 | + ExecaCall() { |
| 46 | + this = API::moduleImport("execa").getMember("execa").getACall() and |
| 47 | + name = "execa" |
| 48 | + or |
| 49 | + this = API::moduleImport("execa").getMember("execaSync").getACall() and |
| 50 | + name = "execaSync" |
| 51 | + } |
| 52 | + |
| 53 | + /** Gets the name of the exported function, such as `rm` in `shelljs.rm()`. */ |
| 54 | + string getName() { result = name } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * The system command execution nodes for `execa.execa` or `execa.execaSync` functions |
| 59 | + */ |
| 60 | + class ExecaExec extends SystemCommandExecution, ExecaCall { |
| 61 | + ExecaExec() { name = ["execa", "execaSync"] } |
| 62 | + |
| 63 | + override DataFlow::Node getACommandArgument() { result = this.getArgument(0) } |
| 64 | + |
| 65 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 66 | + // if shell: true then first and second args are sinks |
| 67 | + // options can be third argument |
| 68 | + arg = [this.getArgument(0), this.getParameter(1).getUnknownMember().asSink()] and |
| 69 | + isExecaShellEnable(this.getParameter(2)) |
| 70 | + or |
| 71 | + // options can be second argument |
| 72 | + arg = this.getArgument(0) and |
| 73 | + isExecaShellEnable(this.getParameter(1)) |
| 74 | + } |
| 75 | + |
| 76 | + override predicate isSync() { name = "execaSync" } |
| 77 | + |
| 78 | + override DataFlow::Node getOptionsArg() { |
| 79 | + result = this.getLastArgument() and result.asExpr() instanceof ObjectExpr |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * A call to `execa.$` or `execa.$.sync` tag functions |
| 85 | + */ |
| 86 | + private class ExecaScriptExpr extends DataFlow::ExprNode { |
| 87 | + string name; |
| 88 | + |
| 89 | + ExecaScriptExpr() { |
| 90 | + this.asExpr() = |
| 91 | + [ |
| 92 | + API::moduleImport("execa").getMember("$"), |
| 93 | + API::moduleImport("execa").getMember("$").getReturn() |
| 94 | + ].getAValueReachableFromSource().asExpr() and |
| 95 | + name = "ASync" |
| 96 | + or |
| 97 | + this.asExpr() = |
| 98 | + [ |
| 99 | + API::moduleImport("execa").getMember("$").getMember("sync"), |
| 100 | + API::moduleImport("execa").getMember("$").getMember("sync").getReturn() |
| 101 | + ].getAValueReachableFromSource().asExpr() and |
| 102 | + name = "Sync" |
| 103 | + } |
| 104 | + |
| 105 | + /** Gets the name of the exported function, such as `rm` in `shelljs.rm()`. */ |
| 106 | + string getName() { result = name } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * The system command execution nodes for `execa.$` or `execa.$.sync` tag functions |
| 111 | + */ |
| 112 | + class ExecaScriptEec extends SystemCommandExecution, ExecaScriptExpr { |
| 113 | + ExecaScriptEec() { name = ["Sync", "ASync"] } |
| 114 | + |
| 115 | + override DataFlow::Node getACommandArgument() { |
| 116 | + result.asExpr() = templateLiteralChildAsSink(this.asExpr()).getChildExpr(0) |
| 117 | + } |
| 118 | + |
| 119 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 120 | + // $({shell: true})`${sink} ${sink} .. ${sink}` |
| 121 | + // ISSUE: $`cmd args` I can't reach the tag function argument easily |
| 122 | + exists(TemplateLiteral tmpL | templateLiteralChildAsSink(this.asExpr()) = tmpL | |
| 123 | + arg.asExpr() = tmpL.getAChildExpr+() and |
| 124 | + isExecaShellEnableWithExpr(this.asExpr().(CallExpr).getArgument(0)) |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + override DataFlow::Node getArgumentList() { |
| 129 | + // $`${Can Not Be sink} ${sink} .. ${sink}` |
| 130 | + exists(TemplateLiteral tmpL | templateLiteralChildAsSink(this.asExpr()) = tmpL | |
| 131 | + result.asExpr() = tmpL.getAChildExpr+() and |
| 132 | + not result.asExpr() = tmpL.getChildExpr(0) |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + override predicate isSync() { name = "Sync" } |
| 137 | + |
| 138 | + override DataFlow::Node getOptionsArg() { |
| 139 | + result = this.asExpr().getAChildExpr*().flow() and result.asExpr() instanceof ObjectExpr |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * A call to `execa.execaCommandSync` or `execa.execaCommand` |
| 145 | + */ |
| 146 | + private class ExecaCommandCall extends API::CallNode { |
| 147 | + string name; |
| 148 | + |
| 149 | + ExecaCommandCall() { |
| 150 | + this = API::moduleImport("execa").getMember("execaCommandSync").getACall() and |
| 151 | + name = "execaCommandSync" |
| 152 | + or |
| 153 | + this = API::moduleImport("execa").getMember("execaCommand").getACall() and |
| 154 | + name = "execaCommand" |
| 155 | + } |
| 156 | + |
| 157 | + /** Gets the name of the exported function, such as `rm` in `shelljs.rm()`. */ |
| 158 | + string getName() { result = name } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * The system command execution nodes for `execa.execaCommand` or `execa.execaCommandSync` functions |
| 163 | + */ |
| 164 | + class ExecaCommandExec2 extends SystemCommandExecution, DataFlow::CallNode { |
| 165 | + ExecaCommandExec2() { this = API::moduleImport("execa").getMember("execaCommand").getACall() } |
| 166 | + |
| 167 | + override DataFlow::Node getACommandArgument() { result = this.getArgument(0) } |
| 168 | + |
| 169 | + override DataFlow::Node getArgumentList() { result = this.getArgument(0) } |
| 170 | + |
| 171 | + override predicate isShellInterpreted(DataFlow::Node arg) { arg = this.getArgument(0) } |
| 172 | + |
| 173 | + override predicate isSync() { none() } |
| 174 | + |
| 175 | + override DataFlow::Node getOptionsArg() { result = this } |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * The system command execution nodes for `execa.execaCommand` or `execa.execaCommandSync` functions |
| 180 | + */ |
| 181 | + class ExecaCommandExec extends SystemCommandExecution, ExecaCommandCall { |
| 182 | + ExecaCommandExec() { name = ["execaCommand", "execaCommandSync"] } |
| 183 | + |
| 184 | + override DataFlow::Node getACommandArgument() { |
| 185 | + result = this.(DataFlow::CallNode).getArgument(0) |
| 186 | + } |
| 187 | + |
| 188 | + override DataFlow::Node getArgumentList() { |
| 189 | + // execaCommand("echo " + sink); |
| 190 | + // execaCommand(`echo ${sink}`); |
| 191 | + result.asExpr() = this.getParameter(0).asSink().asExpr().getAChildExpr+() and |
| 192 | + not result.asExpr() = this.getArgument(0).asExpr().getChildExpr(0) |
| 193 | + } |
| 194 | + |
| 195 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 196 | + // execaCommandSync(sink1 + sink2, {shell: true}) |
| 197 | + arg.asExpr() = this.getArgument(0).asExpr().getAChildExpr+() and |
| 198 | + isExecaShellEnable(this.getParameter(1)) |
| 199 | + or |
| 200 | + // there is only one argument that is constructed in previous nodes, |
| 201 | + // it makes sanitizing really hard to select whether it is vulnerable to argument injection or not |
| 202 | + arg = this.getParameter(0).asSink() and |
| 203 | + not exists(this.getArgument(0).asExpr().getChildExpr(1)) |
| 204 | + } |
| 205 | + |
| 206 | + override predicate isSync() { name = "execaCommandSync" } |
| 207 | + |
| 208 | + override DataFlow::Node getOptionsArg() { |
| 209 | + result = this.getLastArgument() and result.asExpr() instanceof ObjectExpr |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // Holds if left parameter is the left child of a template literal and returns the template literal |
| 214 | + private TemplateLiteral templateLiteralChildAsSink(Expr left) { |
| 215 | + exists(TaggedTemplateExpr parent | |
| 216 | + parent.getTemplate() = result and |
| 217 | + left = parent.getChildExpr(0) |
| 218 | + ) |
| 219 | + } |
| 220 | + |
| 221 | + // Holds whether Execa has shell enabled options or not, get Parameter responsible for options |
| 222 | + private predicate isExecaShellEnable(API::Node n) { |
| 223 | + n.getMember("shell").asSink().asExpr().(BooleanLiteral).getValue() = "true" |
| 224 | + } |
| 225 | + |
| 226 | + // Holds whether Execa has shell enabled options or not, get Parameter responsible for options |
| 227 | + private predicate isExecaShellEnableWithExpr(Expr n) { |
| 228 | + exists(ObjectExpr o, Property p | o = n.getAChildExpr*() | |
| 229 | + o.getAChild() = p and |
| 230 | + p.getAChild().(Label).getName() = "shell" and |
| 231 | + p.getAChild().(Literal).getValue() = "true" |
| 232 | + ) |
| 233 | + } |
| 234 | +} |
0 commit comments