|
| 1 | +/** |
| 2 | + * Models the `execa` library in terms of `FileSystemAccess` and `SystemCommandExecution`. |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | + |
| 7 | +/** |
| 8 | + * Provide model for [Execa](https://github.com/sindresorhus/execa) package |
| 9 | + */ |
| 10 | +module Execa { |
| 11 | + /** |
| 12 | + * The Execa input file read and output file write |
| 13 | + */ |
| 14 | + class ExecaFileSystemAccess extends FileSystemReadAccess, DataFlow::Node { |
| 15 | + API::Node execaArg; |
| 16 | + boolean isPipedToFile; |
| 17 | + |
| 18 | + ExecaFileSystemAccess() { |
| 19 | + ( |
| 20 | + execaArg = API::moduleImport("execa").getMember("$").getParameter(0) and |
| 21 | + isPipedToFile = false |
| 22 | + or |
| 23 | + execaArg = |
| 24 | + API::moduleImport("execa") |
| 25 | + .getMember(["execa", "execaCommand", "execaCommandSync", "execaSync"]) |
| 26 | + .getParameter([0, 1, 2]) and |
| 27 | + isPipedToFile = false |
| 28 | + or |
| 29 | + execaArg = |
| 30 | + API::moduleImport("execa") |
| 31 | + .getMember(["execa", "execaCommand", "execaCommandSync", "execaSync"]) |
| 32 | + .getReturn() |
| 33 | + .getMember(["pipeStdout", "pipeAll", "pipeStderr"]) |
| 34 | + .getParameter(0) and |
| 35 | + isPipedToFile = true |
| 36 | + ) and |
| 37 | + this = execaArg.asSink() |
| 38 | + } |
| 39 | + |
| 40 | + override DataFlow::Node getADataNode() { none() } |
| 41 | + |
| 42 | + override DataFlow::Node getAPathArgument() { |
| 43 | + result = execaArg.getMember("inputFile").asSink() and isPipedToFile = false |
| 44 | + or |
| 45 | + result = execaArg.asSink() and isPipedToFile = true |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * A call to `execa.execa` or `execa.execaSync` |
| 51 | + */ |
| 52 | + class ExecaCall extends API::CallNode { |
| 53 | + boolean isSync; |
| 54 | + |
| 55 | + ExecaCall() { |
| 56 | + this = API::moduleImport("execa").getMember("execa").getACall() and |
| 57 | + isSync = false |
| 58 | + or |
| 59 | + this = API::moduleImport("execa").getMember("execaSync").getACall() and |
| 60 | + isSync = true |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * The system command execution nodes for `execa.execa` or `execa.execaSync` functions |
| 66 | + */ |
| 67 | + class ExecaExec extends SystemCommandExecution, ExecaCall { |
| 68 | + ExecaExec() { isSync = [false, true] } |
| 69 | + |
| 70 | + override DataFlow::Node getACommandArgument() { result = this.getArgument(0) } |
| 71 | + |
| 72 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 73 | + // if shell: true then first and second args are sinks |
| 74 | + // options can be third argument |
| 75 | + arg = [this.getArgument(0), this.getParameter(1).getUnknownMember().asSink()] and |
| 76 | + isExecaShellEnable(this.getParameter(2)) |
| 77 | + or |
| 78 | + // options can be second argument |
| 79 | + arg = this.getArgument(0) and |
| 80 | + isExecaShellEnable(this.getParameter(1)) |
| 81 | + } |
| 82 | + |
| 83 | + override DataFlow::Node getArgumentList() { |
| 84 | + // execa(cmd, [arg]); |
| 85 | + exists(DataFlow::Node arg | arg = this.getArgument(1) | |
| 86 | + // if it is a object then it is a option argument not command argument |
| 87 | + result = arg and not arg.asExpr() instanceof ObjectExpr |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + override predicate isSync() { isSync = true } |
| 92 | + |
| 93 | + override DataFlow::Node getOptionsArg() { |
| 94 | + result = this.getLastArgument() and result.asExpr() instanceof ObjectExpr |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * A call to `execa.$` or `execa.$.sync` or `execa.$({})` or `execa.$.sync({})` tag functions |
| 100 | + */ |
| 101 | + private class ExecaScriptCall extends API::CallNode { |
| 102 | + boolean isSync; |
| 103 | + |
| 104 | + ExecaScriptCall() { |
| 105 | + exists(API::Node script | |
| 106 | + script = |
| 107 | + [ |
| 108 | + API::moduleImport("execa").getMember("$"), |
| 109 | + API::moduleImport("execa").getMember("$").getReturn() |
| 110 | + ] |
| 111 | + | |
| 112 | + this = script.getACall() and |
| 113 | + isSync = false |
| 114 | + or |
| 115 | + this = script.getMember("sync").getACall() and |
| 116 | + isSync = true |
| 117 | + ) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * The system command execution nodes for `execa.$` or `execa.$.sync` tag functions |
| 123 | + */ |
| 124 | + class ExecaScript extends SystemCommandExecution, ExecaScriptCall { |
| 125 | + ExecaScript() { isSync = [false, true] } |
| 126 | + |
| 127 | + override DataFlow::Node getACommandArgument() { |
| 128 | + result = this.getParameter(1).asSink() and |
| 129 | + not isTaggedTemplateFirstChildAnElement(this.getParameter(1).asSink().asExpr().getParent()) |
| 130 | + } |
| 131 | + |
| 132 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 133 | + isExecaShellEnable(this.getParameter(0)) and |
| 134 | + arg = this.getAParameter().asSink() |
| 135 | + } |
| 136 | + |
| 137 | + override DataFlow::Node getArgumentList() { |
| 138 | + result = this.getParameter(any(int i | i >= 1)).asSink() and |
| 139 | + isTaggedTemplateFirstChildAnElement(this.getParameter(1).asSink().asExpr().getParent()) |
| 140 | + or |
| 141 | + result = this.getParameter(any(int i | i >= 2)).asSink() and |
| 142 | + not isTaggedTemplateFirstChildAnElement(this.getParameter(1).asSink().asExpr().getParent()) |
| 143 | + } |
| 144 | + |
| 145 | + override DataFlow::Node getOptionsArg() { result = this.getParameter(0).asSink() } |
| 146 | + |
| 147 | + override predicate isSync() { isSync = true } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * A call to `execa.execaCommandSync` or `execa.execaCommand` |
| 152 | + */ |
| 153 | + private class ExecaCommandCall extends API::CallNode { |
| 154 | + boolean isSync; |
| 155 | + |
| 156 | + ExecaCommandCall() { |
| 157 | + this = API::moduleImport("execa").getMember("execaCommandSync").getACall() and |
| 158 | + isSync = true |
| 159 | + or |
| 160 | + this = API::moduleImport("execa").getMember("execaCommand").getACall() and |
| 161 | + isSync = false |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * The system command execution nodes for `execa.execaCommand` or `execa.execaCommandSync` functions |
| 167 | + */ |
| 168 | + class ExecaCommandExec extends SystemCommandExecution, ExecaCommandCall { |
| 169 | + ExecaCommandExec() { isSync = [false, true] } |
| 170 | + |
| 171 | + override DataFlow::Node getACommandArgument() { |
| 172 | + result = this.(DataFlow::CallNode).getArgument(0) |
| 173 | + } |
| 174 | + |
| 175 | + override DataFlow::Node getArgumentList() { |
| 176 | + // execaCommand(`${cmd} ${arg}`); |
| 177 | + result.asExpr() = this.getParameter(0).asSink().asExpr().getAChildExpr() and |
| 178 | + not result.asExpr() = this.getArgument(0).asExpr().getChildExpr(0) |
| 179 | + } |
| 180 | + |
| 181 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 182 | + // execaCommandSync(`${cmd} ${arg}`, {shell: true}) |
| 183 | + arg.asExpr() = this.getArgument(0).asExpr().getAChildExpr+() and |
| 184 | + isExecaShellEnable(this.getParameter(1)) |
| 185 | + or |
| 186 | + // there is only one argument that is constructed in previous nodes, |
| 187 | + // it makes sanitizing really hard to select whether it is vulnerable to argument injection or not |
| 188 | + arg = this.getParameter(0).asSink() and |
| 189 | + not exists(this.getArgument(0).asExpr().getChildExpr(1)) |
| 190 | + } |
| 191 | + |
| 192 | + override predicate isSync() { isSync = true } |
| 193 | + |
| 194 | + override DataFlow::Node getOptionsArg() { |
| 195 | + result = this.getLastArgument() and result.asExpr() instanceof ObjectExpr |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + /** Gets a TemplateLiteral and check if first child is a template element */ |
| 200 | + private predicate isTaggedTemplateFirstChildAnElement(TemplateLiteral templateLit) { |
| 201 | + exists(templateLit.getChildExpr(0).(TemplateElement)) |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Holds whether Execa has shell enabled options or not, get Parameter responsible for options |
| 206 | + */ |
| 207 | + pragma[inline] |
| 208 | + private predicate isExecaShellEnable(API::Node n) { |
| 209 | + n.getMember("shell").asSink().asExpr().(BooleanLiteral).getValue() = "true" |
| 210 | + } |
| 211 | +} |
0 commit comments