|
| 1 | +/** |
| 2 | + * @name Server Side Template Injection |
| 3 | + * @description Rendering templates with unsanitized user input allows a malicious user arbitrary |
| 4 | + * code execution. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @precision high |
| 8 | + * @id js/code-injection |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-094 |
| 11 | + */ |
| 12 | + |
| 13 | +import javascript |
| 14 | +import DataFlow |
| 15 | + |
| 16 | +class ServerSideTemplateInjectionConfiguration extends TaintTracking::Configuration { |
| 17 | + ServerSideTemplateInjectionConfiguration() { this = "ServerSideTemplateInjectionConfiguration" } |
| 18 | + |
| 19 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 20 | + |
| 21 | + override predicate isSink(DataFlow::Node sink) { sink instanceof ServerSideTemplateInjectionSink } |
| 22 | +} |
| 23 | + |
| 24 | +abstract class ServerSideTemplateInjectionSink extends DataFlow::Node { } |
| 25 | + |
| 26 | +class SSTIPugSink extends ServerSideTemplateInjectionSink { |
| 27 | + SSTIPugSink() { |
| 28 | + exists(CallNode compile, ModuleImportNode renderImport, Node sink | |
| 29 | + (renderImport = moduleImport("pug") or renderImport = moduleImport("jade")) and |
| 30 | + ( |
| 31 | + compile = renderImport.getAMemberCall("compile") and |
| 32 | + compile.flowsTo(sink) and |
| 33 | + sink.getStartLine() != sink.getASuccessor().getStartLine() |
| 34 | + or |
| 35 | + compile = renderImport.getAMemberCall("render") and compile.flowsTo(sink) |
| 36 | + ) and |
| 37 | + this = compile.getArgument(0) |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +class SSTIDotSink extends ServerSideTemplateInjectionSink { |
| 43 | + SSTIDotSink() { |
| 44 | + exists(CallNode compile, Node sink | |
| 45 | + compile = moduleImport("dot").getAMemberCall("template") and |
| 46 | + compile.flowsTo(sink) and |
| 47 | + sink.getStartLine() != sink.getASuccessor().getStartLine() and |
| 48 | + this = compile.getArgument(0) |
| 49 | + ) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +class SSTIEjsSink extends ServerSideTemplateInjectionSink { |
| 54 | + SSTIEjsSink() { |
| 55 | + exists(CallNode compile, Node sink | |
| 56 | + compile = moduleImport("ejs").getAMemberCall("render") and |
| 57 | + compile.flowsTo(sink) and |
| 58 | + this = compile.getArgument(0) |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +class SSTINunjucksSink extends ServerSideTemplateInjectionSink { |
| 64 | + SSTINunjucksSink() { |
| 65 | + exists(CallNode compile, Node sink | |
| 66 | + compile = moduleImport("nunjucks").getAMemberCall("renderString") and |
| 67 | + compile.flowsTo(sink) and |
| 68 | + this = compile.getArgument(0) |
| 69 | + ) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +from DataFlow::PathNode source, DataFlow::PathNode sink, ServerSideTemplateInjectionConfiguration c |
| 74 | +where c.hasFlowPath(source, sink) |
| 75 | +select sink.getNode(), source, sink, "$@ flows to here and is used in an XPath expression.", |
| 76 | + source.getNode(), "User-provided value" |
0 commit comments