-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSpringController.qll
More file actions
407 lines (368 loc) · 14.9 KB
/
SpringController.qll
File metadata and controls
407 lines (368 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
overlay[local?]
module;
import java
import semmle.code.java.Maps
import SpringWeb
import SpringWebClient
/**
* An annotation type that identifies Spring controllers.
*/
class SpringControllerAnnotation extends AnnotationType {
SpringControllerAnnotation() {
// `@Controller` used directly as an annotation.
this.hasQualifiedName("org.springframework.stereotype", "Controller")
or
// `@Controller` can be used as a meta-annotation on other annotation types.
this.getAnAnnotation().getType() instanceof SpringControllerAnnotation
}
}
/**
* An annotation type that identifies Spring rest controllers.
*
* Rest controllers are the same as controllers, but imply the `@ResponseBody` annotation.
*/
class SpringRestControllerAnnotation extends SpringControllerAnnotation {
SpringRestControllerAnnotation() { this.hasName("RestController") }
}
/**
* A class annotated, directly or indirectly, as a Spring `Controller`.
*/
class SpringController extends Class {
SpringController() { this.getAnAnnotation().getType() instanceof SpringControllerAnnotation }
}
/**
* A class annotated, directly or indirectly, as a Spring `RestController`.
*/
class SpringRestController extends SpringController {
SpringRestController() {
this.getAnAnnotation().getType() instanceof SpringRestControllerAnnotation
}
}
/**
* A method on a Spring controller which is accessed by the Spring MVC framework.
*/
abstract class SpringControllerMethod extends Method {
SpringControllerMethod() { this.getDeclaringType() instanceof SpringController }
}
/**
* A method on a Spring controller that builds a "model attribute" that will be returned with the
* response as part of the model.
*/
class SpringModelAttributeMethod extends SpringControllerMethod {
SpringModelAttributeMethod() {
// Any method that declares the @ModelAttribute annotation, or overrides a method that declares
// the annotation. We have to do this explicit check because the @ModelAttribute annotation is
// not declared with @Inherited.
exists(Method superMethod |
this.overrides*(superMethod) and
superMethod.getAnAnnotation() instanceof SpringModelAttributeAnnotation
)
}
}
/**
* A method on a Spring controller that configures a binder for this controller.
*/
class SpringInitBinderMethod extends SpringControllerMethod {
SpringInitBinderMethod() {
// Any method that declares the @InitBinder annotation, or overrides a method that declares
// the annotation. We have to do this explicit check because the @InitBinder annotation is
// not declared with @Inherited.
exists(Method superMethod |
this.overrides*(superMethod) and
superMethod.hasAnnotation("org.springframework.web.bind.annotation", "InitBinder")
)
}
}
/**
* An `AnnotationType` that is used to indicate a `RequestMapping`.
*/
class SpringRequestMappingAnnotationType extends AnnotationType {
SpringRequestMappingAnnotationType() {
// `@RequestMapping` used directly as an annotation.
this.hasQualifiedName("org.springframework.web.bind.annotation", "RequestMapping")
or
// `@RequestMapping` can be used as a meta-annotation on other annotation types, e.g. GetMapping, PostMapping etc.
this.getAnAnnotation().getType() instanceof SpringRequestMappingAnnotationType
}
}
/**
* An `AnnotationType` that is used to indicate a `ResponseBody`.
*/
class SpringResponseBodyAnnotationType extends AnnotationType {
SpringResponseBodyAnnotationType() {
// `@ResponseBody` used directly as an annotation.
this.hasQualifiedName("org.springframework.web.bind.annotation", "ResponseBody")
}
}
private class SpringRequestMappingAnnotation extends Annotation {
SpringRequestMappingAnnotation() { this.getType() instanceof SpringRequestMappingAnnotationType }
}
private Expr getProducesExpr(RefType rt) {
result = rt.getAnAnnotation().(SpringRequestMappingAnnotation).getValue("produces")
or
rt.getAnAnnotation().(SpringRequestMappingAnnotation).getValue("produces").(ArrayInit).getSize() =
0 and
result = getProducesExpr(rt.getASupertype())
}
/**
* A method on a Spring controller that is executed in response to a web request.
*/
class SpringRequestMappingMethod extends SpringControllerMethod {
SpringRequestMappingAnnotation requestMappingAnnotation;
SpringRequestMappingMethod() {
// Any method that declares the @RequestMapping annotation, or overrides a method that declares
// the annotation. We have to do this explicit check because the @RequestMapping annotation is
// not declared with @Inherited.
exists(Method superMethod |
this.overrides*(superMethod) and
requestMappingAnnotation = superMethod.getAnAnnotation()
)
}
/** Gets a request mapping parameter. */
SpringRequestMappingParameter getARequestParameter() { result = this.getAParameter() }
/** Gets the "produces" @RequestMapping annotation value, if present. If an array is specified, gets the array. */
Expr getProducesExpr() {
result = requestMappingAnnotation.getValue("produces")
or
requestMappingAnnotation.getValue("produces").(ArrayInit).getSize() = 0 and
result = getProducesExpr(this.getDeclaringType())
}
/** Gets a "produces" @RequestMapping annotation value. If an array is specified, gets a member of the array. */
Expr getAProducesExpr() {
result = this.getProducesExpr() and not result instanceof ArrayInit
or
result = this.getProducesExpr().(ArrayInit).getAnInit()
}
/** Gets the "produces" @RequestMapping annotation value, if present and a string constant. */
string getProduces() {
result = this.getProducesExpr().(CompileTimeConstantExpr).getStringValue()
}
/**
* Gets a "value" @RequestMapping annotation string value, if present.
*
* If the annotation element is defined with an array initializer, then the result will be one of the
* elements of that array. Otherwise, the result will be the single expression used as value.
*/
string getAValue() { result = requestMappingAnnotation.getAStringArrayValue("value") }
/** Gets the "method" @RequestMapping annotation value, if present. */
string getMethodValue() {
result = requestMappingAnnotation.getAnEnumConstantArrayValue("method").getName()
}
/** Holds if this is considered an `@ResponseBody` method. */
predicate isResponseBody() {
this.getAnAnnotation().getType() instanceof SpringResponseBodyAnnotationType or
this.getDeclaringType().getAnAnnotation().getType() instanceof SpringResponseBodyAnnotationType or
this.getDeclaringType() instanceof SpringRestController
}
}
/** A Spring framework annotation indicating remote user input from servlets. */
class SpringServletInputAnnotation extends Annotation {
SpringServletInputAnnotation() {
exists(AnnotationType a |
a = this.getType() and
a.getPackage().getName() = "org.springframework.web.bind.annotation"
|
a.hasName([
"MatrixVariable", "RequestParam", "RequestHeader", "CookieValue", "RequestPart",
"PathVariable", "RequestBody"
])
)
}
}
/** An annotation of the type `org.springframework.web.bind.annotation.ModelAttribute`. */
class SpringModelAttributeAnnotation extends Annotation {
SpringModelAttributeAnnotation() {
this.getType().hasQualifiedName("org.springframework.web.bind.annotation", "ModelAttribute")
}
}
/** A parameter of a `SpringRequestMappingMethod`. */
class SpringRequestMappingParameter extends Parameter {
SpringRequestMappingParameter() { this.getCallable() instanceof SpringRequestMappingMethod }
/** Holds if the parameter should not be consider a direct source of taint. */
predicate isNotDirectlyTaintedInput() {
this.getType().(RefType).getAnAncestor() instanceof SpringWebRequest or
this.getType().(RefType).getAnAncestor() instanceof SpringNativeWebRequest or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName(javaxOrJakarta() + ".servlet", "ServletRequest") or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName(javaxOrJakarta() + ".servlet", "ServletResponse") or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName(javaxOrJakarta() + ".servlet.http", "HttpSession") or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName(javaxOrJakarta() + ".servlet.http", "PushBuilder") or
this.getType().(RefType).getAnAncestor().hasQualifiedName("java.security", "Principal") or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName("org.springframework.http", "HttpMethod") or
this.getType().(RefType).getAnAncestor().hasQualifiedName("java.util", "Locale") or
this.getType().(RefType).getAnAncestor().hasQualifiedName("java.util", "TimeZone") or
this.getType().(RefType).getAnAncestor().hasQualifiedName("java.time", "ZoneId") or
this.getType().(RefType).getAnAncestor().hasQualifiedName("java.io", "OutputStream") or
this.getType().(RefType).getAnAncestor().hasQualifiedName("java.io", "Writer") or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName("org.springframework.web.servlet.mvc.support", "RedirectAttributes") or
// Also covers BindingResult. Note, you can access the field value through this interface, which should be considered tainted
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName("org.springframework.validation", "Errors") or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName("org.springframework.web.bind.support", "SessionStatus") or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName("org.springframework.web.util", "UriComponentsBuilder") or
this.getType()
.(RefType)
.getAnAncestor()
.hasQualifiedName("org.springframework.data.domain", "Pageable") or
this instanceof SpringModel
}
private predicate isExplicitlyTaintedInput() {
// InputStream or Reader parameters allow access to the body of a request
this.getType().(RefType).getAnAncestor() instanceof TypeInputStream or
this.getType().(RefType).getAnAncestor().hasQualifiedName("java.io", "Reader") or
// The SpringServletInputAnnotations allow access to the URI, request parameters, cookie values and the body of the request
this.getAnAnnotation() instanceof SpringServletInputAnnotation or
// HttpEntity is like @RequestBody, but with a wrapper including the headers
// TODO model unwrapping aspects
this.getType().(RefType).getASourceSupertype*() instanceof SpringHttpEntity or
this.getAnAnnotation()
.getType()
.hasQualifiedName("org.springframework.web.bind.annotation", "RequestAttribute") or
this.getAnAnnotation()
.getType()
.hasQualifiedName("org.springframework.web.bind.annotation", "SessionAttribute")
}
private predicate isImplicitRequestParam() {
// Any parameter which is not explicitly handled, is consider to be an `@RequestParam`, if
// it is a simple bean property
not this.isNotDirectlyTaintedInput() and
not this.isExplicitlyTaintedInput() and
(
this.getType() instanceof PrimitiveType or
this.getType() instanceof TypeString
)
}
private predicate isImplicitModelAttribute() {
// Any parameter which is not explicitly handled, is consider to be an `@ModelAttribute`, if
// it is not an implicit request param
not this.isNotDirectlyTaintedInput() and
not this.isExplicitlyTaintedInput() and
not this.isImplicitRequestParam()
}
/** Holds if this is an explicit or implicit `@ModelAttribute` parameter. */
predicate isModelAttribute() {
this.isImplicitModelAttribute() or
this.getAnAnnotation() instanceof SpringModelAttributeAnnotation
}
/** Holds if the input is tainted. */
predicate isTaintedInput() {
this.isExplicitlyTaintedInput()
or
// Any parameter which is not explicitly identified, is consider to be an `@RequestParam`, if
// it is a simple bean property) or a @ModelAttribute if not
not this.isNotDirectlyTaintedInput()
}
}
/**
* A parameter to a `SpringRequestMappingMethod` which represents a model that can be populated by
* the method, which will be used to render the response e.g. as a JSP file.
*/
abstract class SpringModel extends Parameter {
SpringModel() { this.getCallable() instanceof SpringRequestMappingMethod }
/**
* Types for which instances are placed inside the model.
*/
abstract RefType getATypeInModel();
}
/**
* A `java.util.Map` can be accepted as the model parameter for a Spring `RequestMapping` method.
*/
class SpringModelPlainMap extends SpringModel {
SpringModelPlainMap() { this.getType() instanceof MapType }
override RefType getATypeInModel() {
exists(MethodCall methodCall |
methodCall.getQualifier() = this.getAnAccess() and
methodCall.getCallee().hasName("put")
|
result = methodCall.getArgument(1).getType()
)
}
}
/**
* A Spring `Model` or `ModelMap` can be accepted as the model parameter for a Spring `RequestMapping`
* method.
*/
class SpringModelModel extends SpringModel {
SpringModelModel() {
this.getType().(RefType).hasQualifiedName("org.springframework.ui", "Model") or
this.getType().(RefType).hasQualifiedName("org.springframework.ui", "ModelMap")
}
override RefType getATypeInModel() {
exists(MethodCall methodCall |
methodCall.getQualifier() = this.getAnAccess() and
methodCall.getCallee().hasName("addAttribute")
|
result = methodCall.getArgument(methodCall.getNumArgument() - 1).getType()
)
}
}
/**
* A `RefType` that is included in a model that is used in a response by the Spring MVC.
*/
class SpringModelResponseType extends RefType {
SpringModelResponseType() {
exists(SpringModelAttributeMethod modelAttributeMethod |
this = modelAttributeMethod.getReturnType()
) or
exists(SpringModel model | usesType(model.getATypeInModel(), this))
}
}
/** Strips wrapper types. */
private RefType stripType(Type t) {
result = t or
result = stripType(t.(Array).getComponentType()) or
result = stripType(t.(ParameterizedType).getATypeArgument())
}
/**
* A user data type that may be populated from an HTTP request.
*
* This includes types directly referred to as either `@ModelAttribute` or `@RequestBody` parameters,
* or types that are referred to by those types.
*/
class SpringUntrustedDataType extends RefType {
SpringUntrustedDataType() {
exists(SpringRequestMappingParameter p |
p.isModelAttribute()
or
p.getAnAnnotation().(SpringServletInputAnnotation).getType().hasName("RequestBody")
|
this.fromSource() and
this = stripType(p.getType())
)
or
exists(SpringRestTemplateResponseEntityMethod rm |
this = stripType(rm.getAReference().getType().(ParameterizedType).getTypeArgument(0)) and
this.fromSource()
)
or
exists(SpringUntrustedDataType mt |
this = stripType(mt.getAField().getType()) and
this.fromSource()
)
}
}