|
| 1 | +/** Provides modeling for parsed command line arguments. */ |
| 2 | + |
| 3 | +import javascript |
| 4 | + |
| 5 | +/** |
| 6 | + * An object containing command-line arguments, potentially parsed by a library. |
| 7 | + * |
| 8 | + * Extend this class to refine existing API models. If you want to model new APIs, |
| 9 | + * extend `CommandLineArguments::Range` instead. |
| 10 | + */ |
| 11 | +class CommandLineArguments extends ThreatModelSource instanceof CommandLineArguments::Range { } |
| 12 | + |
| 13 | +/** Provides a class for modeling new sources of remote user input. */ |
| 14 | +module CommandLineArguments { |
| 15 | + /** |
| 16 | + * An object containing command-line arguments, potentially parsed by a library. |
| 17 | + * |
| 18 | + * Extend this class to model new APIs. If you want to refine existing API models, |
| 19 | + * extend `CommandLineArguments` instead. |
| 20 | + */ |
| 21 | + abstract class Range extends ThreatModelSource::Range { |
| 22 | + override string getThreatModel() { result = "commandargs" } |
| 23 | + |
| 24 | + override string getSourceType() { result = "CommandLineArguments" } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** A read of `process.argv`, considered as a threat-model source. */ |
| 29 | +private class ProcessArgv extends CommandLineArguments::Range { |
| 30 | + ProcessArgv() { |
| 31 | + // `process.argv[0]` and `process.argv[1]` are paths to `node` and `main`, and |
| 32 | + // therefore should not be considered a threat-source... However, we don't have an |
| 33 | + // easy way to exclude them, so we need to allow them. |
| 34 | + this = NodeJSLib::process().getAPropertyRead("argv") |
| 35 | + } |
| 36 | + |
| 37 | + override string getSourceType() { result = "process.argv" } |
| 38 | +} |
| 39 | + |
| 40 | +private class DefaultModels extends CommandLineArguments::Range { |
| 41 | + DefaultModels() { |
| 42 | + // `require('get-them-args')(...)` => `{ unknown: [], a: ... b: ... }` |
| 43 | + this = DataFlow::moduleImport("get-them-args").getACall() |
| 44 | + or |
| 45 | + // `require('optimist').argv` => `{ _: [], a: ... b: ... }` |
| 46 | + this = DataFlow::moduleMember("optimist", "argv") |
| 47 | + or |
| 48 | + // `require("arg")({...spec})` => `{_: [], a: ..., b: ...}` |
| 49 | + this = DataFlow::moduleImport("arg").getACall() |
| 50 | + or |
| 51 | + // `(new (require(argparse)).ArgumentParser({...spec})).parse_args()` => `{a: ..., b: ...}` |
| 52 | + this = |
| 53 | + API::moduleImport("argparse") |
| 54 | + .getMember("ArgumentParser") |
| 55 | + .getInstance() |
| 56 | + .getMember("parse_args") |
| 57 | + .getACall() |
| 58 | + or |
| 59 | + // `require('command-line-args')({...spec})` => `{a: ..., b: ...}` |
| 60 | + this = DataFlow::moduleImport("command-line-args").getACall() |
| 61 | + or |
| 62 | + // `require('meow')(help, {...spec})` => `{a: ..., b: ....}` |
| 63 | + this = DataFlow::moduleImport("meow").getACall() |
| 64 | + or |
| 65 | + // `require("dashdash").createParser(...spec)` => `{a: ..., b: ...}` |
| 66 | + this = |
| 67 | + [ |
| 68 | + API::moduleImport("dashdash"), |
| 69 | + API::moduleImport("dashdash").getMember("createParser").getReturn() |
| 70 | + ].getMember("parse").getACall() |
| 71 | + or |
| 72 | + // `require('commander').myCmdArgumentName` |
| 73 | + this = commander().getAMember().asSource() |
| 74 | + or |
| 75 | + // `require('commander').opt()` => `{a: ..., b: ...}` |
| 76 | + this = commander().getMember("opts").getACall() |
| 77 | + or |
| 78 | + this = API::moduleImport("yargs/yargs").getReturn().getMember("argv").asSource() |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * A step for propagating taint through command line parsing, |
| 84 | + * such as `var succ = require("minimist")(pred)`. |
| 85 | + */ |
| 86 | +private class ArgsParseStep extends TaintTracking::SharedTaintStep { |
| 87 | + override predicate step(DataFlow::Node pred, DataFlow::Node succ) { |
| 88 | + exists(DataFlow::CallNode call | |
| 89 | + call = DataFlow::moduleMember("args", "parse").getACall() or |
| 90 | + call = DataFlow::moduleImport(["yargs-parser", "minimist", "subarg"]).getACall() |
| 91 | + | |
| 92 | + succ = call and |
| 93 | + pred = call.getArgument(0) |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Gets a Command instance from the `commander` library. |
| 100 | + */ |
| 101 | +private API::Node commander() { |
| 102 | + result = API::moduleImport("commander") |
| 103 | + or |
| 104 | + // `require("commander").program === require("commander")` |
| 105 | + result = commander().getMember("program") |
| 106 | + or |
| 107 | + result = commander().getMember("Command").getInstance() |
| 108 | + or |
| 109 | + // lots of chainable methods |
| 110 | + result = commander().getAMember().getReturn() |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Gets an instance of `yargs`. |
| 115 | + * Either directly imported as a module, or through some chained method call. |
| 116 | + */ |
| 117 | +private DataFlow::SourceNode yargs() { |
| 118 | + result = DataFlow::moduleImport("yargs") |
| 119 | + or |
| 120 | + // script used to generate list of chained methods: https://gist.github.com/erik-krogh/f8afe952c0577f4b563a993e613269ba |
| 121 | + exists(string method | |
| 122 | + not method = |
| 123 | + // the methods that does not return a chained `yargs` object. |
| 124 | + [ |
| 125 | + "getContext", "getDemandedOptions", "getDemandedCommands", "getDeprecatedOptions", |
| 126 | + "_getParseContext", "getOptions", "getGroups", "getStrict", "getStrictCommands", |
| 127 | + "getExitProcess", "locale", "getUsageInstance", "getCommandInstance" |
| 128 | + ] |
| 129 | + | |
| 130 | + result = yargs().getAMethodCall(method) |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * An array of command line arguments (`argv`) parsed by the `yargs` library. |
| 136 | + */ |
| 137 | +private class YargsArgv extends CommandLineArguments::Range { |
| 138 | + YargsArgv() { |
| 139 | + this = yargs().getAPropertyRead("argv") |
| 140 | + or |
| 141 | + this = yargs().getAMethodCall("parse") and |
| 142 | + this.(DataFlow::MethodCallNode).getNumArgument() = 0 |
| 143 | + } |
| 144 | +} |
0 commit comments