|
| 1 | +/** |
| 2 | + * @name User controlled environment injection |
| 3 | + * @description full control on creating environment variables from user controlled data is not secure |
| 4 | + * @kind path-problem |
| 5 | + * @id js/envinjection |
| 6 | + * @problem.severity error |
| 7 | + * @security-severity 7.5 |
| 8 | + * @precision medium |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-089 |
| 11 | + */ |
| 12 | + |
| 13 | +import javascript |
| 14 | +import DataFlow::PathGraph |
| 15 | + |
| 16 | +/** A taint tracking configuration for unsafe environment injection. */ |
| 17 | +class Configuration extends TaintTracking::Configuration { |
| 18 | + Configuration() { this = "envInjection" } |
| 19 | + |
| 20 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 21 | + |
| 22 | + override predicate isSink(DataFlow::Node sink) { |
| 23 | + NodeJSLib::process() |
| 24 | + .getAPropertyRead("env") |
| 25 | + .asExpr() |
| 26 | + .getParent() |
| 27 | + .(IndexExpr) |
| 28 | + .getAChildExpr() |
| 29 | + .(VarRef) = sink.asExpr() |
| 30 | + or |
| 31 | + sink = API::moduleImport("process").getMember("env").getAMember().asSink() |
| 32 | + } |
| 33 | + |
| 34 | + override predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) { |
| 35 | + exists(DataFlow::InvokeNode ikn | |
| 36 | + ikn = DataFlow::globalVarRef("Object").getAMemberInvocation("keys") |
| 37 | + | |
| 38 | + pred = ikn.getArgument(0) and |
| 39 | + ( |
| 40 | + succ = ikn.getAChainedMethodCall(["filter", "map"]) or |
| 41 | + succ = ikn or |
| 42 | + succ = ikn.getAChainedMethodCall("forEach").getABoundCallbackParameter(0, 0) |
| 43 | + ) |
| 44 | + ) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +from |
| 49 | + Configuration cfg, Configuration cfg2, DataFlow::PathNode source, DataFlow::PathNode sink, |
| 50 | + DataFlow::PathNode sink2 |
| 51 | +where |
| 52 | + cfg.hasFlowPath(source, sink) and |
| 53 | + sink.getNode() = API::moduleImport("process").getMember("env").getAMember().asSink() and |
| 54 | + cfg2.hasFlowPath(source, sink2) and |
| 55 | + sink.getNode().asExpr() = |
| 56 | + NodeJSLib::process() |
| 57 | + .getAPropertyRead("env") |
| 58 | + .asExpr() |
| 59 | + .getParent() |
| 60 | + .(IndexExpr) |
| 61 | + .getAChildExpr() |
| 62 | + .(VarRef) |
| 63 | +select sink.getNode(), source, sink, "this environment variable assignment is $@.", |
| 64 | + source.getNode(), "user controllable" |
0 commit comments