|
| 1 | +/** Definitions related to the Server Side Template Injection (SSTI) query. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.TaintTracking |
| 5 | +import semmle.code.java.dataflow.FlowSources |
| 6 | +import experimental.semmle.code.java.frameworks.FreeMarker |
| 7 | +import experimental.semmle.code.java.frameworks.Velocity |
| 8 | +import experimental.semmle.code.java.frameworks.JinJava |
| 9 | +import experimental.semmle.code.java.frameworks.Pebble |
| 10 | +import experimental.semmle.code.java.frameworks.Thymeleaf |
| 11 | + |
| 12 | +/** A taint tracking configuration to reason about Server Side Template Injection (SSTI) vulnerabilities */ |
| 13 | +class TemplateInjectionFlowConfig extends TaintTracking::Configuration { |
| 14 | + TemplateInjectionFlowConfig() { this = "TemplateInjectionFlowConfig" } |
| 15 | + |
| 16 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 17 | + |
| 18 | + override predicate isSink(DataFlow::Node sink) { sink instanceof Sink } |
| 19 | + |
| 20 | + override predicate isSanitizer(DataFlow::Node node) { |
| 21 | + node.getType() instanceof PrimitiveType or node.getType() instanceof BoxedType |
| 22 | + } |
| 23 | + |
| 24 | + override predicate isAdditionalTaintStep(DataFlow::Node prev, DataFlow::Node succ) { |
| 25 | + exists(AdditionalFlowStep a | a.isAdditionalTaintStep(prev, succ)) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * A data flow sink for Server Side Template Injection (SSTI) vulnerabilities |
| 31 | + */ |
| 32 | +abstract private class Sink extends DataFlow::ExprNode { } |
| 33 | + |
| 34 | +/** |
| 35 | + * A data flow step for Server Side Template Injection (SSTI) vulnerabilities |
| 36 | + */ |
| 37 | +private class AdditionalFlowStep extends Unit { |
| 38 | + abstract predicate isAdditionalTaintStep(DataFlow::Node prev, DataFlow::Node succ); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * An argument to FreeMarker template engine's `process` method call. |
| 43 | + */ |
| 44 | +private class FreeMarkerProcessSink extends Sink { |
| 45 | + FreeMarkerProcessSink() { |
| 46 | + exists(MethodAccess m | |
| 47 | + m.getCallee() instanceof MethodFreeMarkerTemplateProcess and |
| 48 | + m.getArgument(0) = this.getExpr() |
| 49 | + ) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * An reader passed an argument to FreeMarker template engine's `Template` |
| 55 | + * construtor call. |
| 56 | + */ |
| 57 | +private class FreeMarkerConstructorSink extends Sink { |
| 58 | + FreeMarkerConstructorSink() { |
| 59 | + // Template(java.lang.String name, java.io.Reader reader) |
| 60 | + // Template(java.lang.String name, java.io.Reader reader, Configuration cfg) |
| 61 | + // Template(java.lang.String name, java.io.Reader reader, Configuration cfg, java.lang.String encoding) |
| 62 | + // Template(java.lang.String name, java.lang.String sourceName, java.io.Reader reader, Configuration cfg) |
| 63 | + // Template(java.lang.String name, java.lang.String sourceName, java.io.Reader reader, Configuration cfg, ParserConfiguration customParserConfiguration, java.lang.String encoding) |
| 64 | + // Template(java.lang.String name, java.lang.String sourceName, java.io.Reader reader, Configuration cfg, java.lang.String encoding) |
| 65 | + exists(ConstructorCall cc, Expr e | |
| 66 | + cc.getConstructor().getDeclaringType() instanceof TypeFreeMarkerTemplate and |
| 67 | + e = cc.getAnArgument() and |
| 68 | + ( |
| 69 | + e.getType().(RefType).hasQualifiedName("java.io", "Reader") and |
| 70 | + this.asExpr() = e |
| 71 | + ) |
| 72 | + ) |
| 73 | + or |
| 74 | + exists(ConstructorCall cc | |
| 75 | + cc.getConstructor().getDeclaringType() instanceof TypeFreeMarkerTemplate and |
| 76 | + // Template(java.lang.String name, java.lang.String sourceCode, Configuration cfg) |
| 77 | + cc.getNumArgument() = 3 and |
| 78 | + cc.getArgument(1).getType() instanceof TypeString and |
| 79 | + this.asExpr() = cc.getArgument(1) |
| 80 | + ) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * An argument to FreeMarker template engine's `putTemplate` method call. |
| 86 | + */ |
| 87 | +private class FreeMarkerStringTemplateLoaderPutTemplateSink extends Sink { |
| 88 | + FreeMarkerStringTemplateLoaderPutTemplateSink() { |
| 89 | + exists(MethodAccess ma | |
| 90 | + this.asExpr() = ma.getArgument(1) and |
| 91 | + ma.getMethod() instanceof MethodFreeMarkerStringTemplateLoaderPutTemplate |
| 92 | + ) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * An argument to Pebble template engine's `getLiteralTemplate` or `getTemplate` method call. |
| 98 | + */ |
| 99 | +private class PebbleGetTemplateSinkTemplateSink extends Sink { |
| 100 | + PebbleGetTemplateSinkTemplateSink() { |
| 101 | + exists(MethodAccess ma | |
| 102 | + this.asExpr() = ma.getArgument(0) and |
| 103 | + ma.getMethod() instanceof MethodPebbleGetTemplate |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * An argument to JinJava template engine's `render` or `renderForResult` method call. |
| 110 | + */ |
| 111 | +private class JinjavaRenderSink extends Sink { |
| 112 | + JinjavaRenderSink() { |
| 113 | + exists(MethodAccess ma | |
| 114 | + this.asExpr() = ma.getArgument(0) and |
| 115 | + ( |
| 116 | + ma.getMethod() instanceof MethodJinjavaRenderForResult |
| 117 | + or |
| 118 | + ma.getMethod() instanceof MethodJinjavaRender |
| 119 | + ) |
| 120 | + ) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * An argument to ThymeLeaf template engine's `process` method call. |
| 126 | + */ |
| 127 | +private class ThymeLeafRenderSink extends Sink { |
| 128 | + ThymeLeafRenderSink() { |
| 129 | + exists(MethodAccess ma | |
| 130 | + this.asExpr() = ma.getArgument(0) and |
| 131 | + ma.getMethod() instanceof MethodThymeleafProcess |
| 132 | + ) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Tainted data flowing into a Velocity Context through `put` method taints the context. |
| 138 | + */ |
| 139 | +private class VelocityContextFlow extends AdditionalFlowStep { |
| 140 | + override predicate isAdditionalTaintStep(DataFlow::Node prev, DataFlow::Node succ) { |
| 141 | + exists(MethodAccess m | m.getMethod() instanceof MethodVelocityContextPut | |
| 142 | + m.getArgument(1) = prev.asExpr() and |
| 143 | + succ.asExpr() = m.getQualifier() |
| 144 | + ) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * An argument to Velocity template engine's `mergeTemplate` method call. |
| 150 | + */ |
| 151 | +private class VelocityMergeTempSink extends Sink { |
| 152 | + VelocityMergeTempSink() { |
| 153 | + exists(MethodAccess m | |
| 154 | + // static boolean mergeTemplate(String templateName, String encoding, Context context, Writer writer) |
| 155 | + m.getCallee() instanceof MethodVelocityMergeTemplate and |
| 156 | + m.getArgument(2) = this.getExpr() |
| 157 | + ) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * An argument to Velocity template engine's `mergeTemplate` method call. |
| 163 | + */ |
| 164 | +private class VelocityMergeSink extends Sink { |
| 165 | + VelocityMergeSink() { |
| 166 | + exists(MethodAccess m | |
| 167 | + m.getCallee() instanceof MethodVelocityMerge and |
| 168 | + // public void merge(Context context, Writer writer) |
| 169 | + // public void merge(Context context, Writer writer, List<String> macroLibraries) |
| 170 | + m.getArgument(0) = this.getExpr() |
| 171 | + ) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * An argument to Velocity template engine's `evaluate` method call. |
| 177 | + */ |
| 178 | +private class VelocityEvaluateSink extends Sink { |
| 179 | + VelocityEvaluateSink() { |
| 180 | + exists(MethodAccess m | |
| 181 | + m.getCallee() instanceof MethodVelocityEvaluate and |
| 182 | + m.getArgument([0, 3]) = this.getExpr() |
| 183 | + ) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * An argument to Velocity template engine's `parse` method call. |
| 189 | + */ |
| 190 | +private class VelocityParseSink extends Sink { |
| 191 | + VelocityParseSink() { |
| 192 | + exists(MethodAccess ma | |
| 193 | + this.asExpr() = ma.getArgument(0) and |
| 194 | + ma.getMethod() instanceof MethodVelocityParse |
| 195 | + ) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +/** |
| 200 | + * An argument to Velocity template engine's `putStringResource` method call. |
| 201 | + */ |
| 202 | +private class VelocityPutStringResSink extends Sink { |
| 203 | + VelocityPutStringResSink() { |
| 204 | + exists(MethodAccess ma | |
| 205 | + this.asExpr() = ma.getArgument(1) and |
| 206 | + ma.getMethod() instanceof MethodVelocityPutStringResource |
| 207 | + ) |
| 208 | + } |
| 209 | +} |
0 commit comments