Skip to content

Commit a41c2d8

Browse files
committed
Java: Make a few predicates private and autoformat SpringController.
1 parent 6de612a commit a41c2d8

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

java/ql/src/semmle/code/java/frameworks/spring/SpringController.qll

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ class SpringControllerAnnotation extends AnnotationType {
2222
* Rest controllers are the same as controllers, but imply the @ResponseBody annotation.
2323
*/
2424
class SpringRestControllerAnnotation extends SpringControllerAnnotation {
25-
SpringRestControllerAnnotation() {
26-
hasName("RestController")
27-
}
25+
SpringRestControllerAnnotation() { hasName("RestController") }
2826
}
2927

3028
/**
@@ -107,6 +105,7 @@ class SpringResponseBodyAnnotationType extends AnnotationType {
107105
*/
108106
class SpringRequestMappingMethod extends SpringControllerMethod {
109107
Annotation requestMappingAnnotation;
108+
110109
SpringRequestMappingMethod() {
111110
// Any method that declares the @RequestMapping annotation, or overrides a method that declares
112111
// the annotation. We have to do this explicit check because the @RequestMapping annotation is
@@ -119,13 +118,12 @@ class SpringRequestMappingMethod extends SpringControllerMethod {
119118
}
120119

121120
/** Gets a request mapping parameter. */
122-
SpringRequestMappingParameter getARequestParameter() {
123-
result = getAParameter()
124-
}
121+
SpringRequestMappingParameter getARequestParameter() { result = getAParameter() }
125122

126123
/** Gets the "produces" @RequestMapping annotation value, if present. */
127124
string getProduces() {
128-
result = requestMappingAnnotation.getValue("produces").(CompileTimeConstantExpr).getStringValue()
125+
result =
126+
requestMappingAnnotation.getValue("produces").(CompileTimeConstantExpr).getStringValue()
129127
}
130128

131129
/** Holds if this is considered an @ResponseBody method. */
@@ -180,29 +178,50 @@ class SpringRequestMappingParameter extends Parameter {
180178
getType().(RefType).getAnAncestor().hasQualifiedName("java.time", "ZoneId") or
181179
getType().(RefType).getAnAncestor().hasQualifiedName("java.io", "OutputStream") or
182180
getType().(RefType).getAnAncestor().hasQualifiedName("java.io", "Writer") or
183-
getType().(RefType).getAnAncestor().hasQualifiedName("org.springframework.web.servlet.mvc.support", "RedirectAttributes") or
181+
getType()
182+
.(RefType)
183+
.getAnAncestor()
184+
.hasQualifiedName("org.springframework.web.servlet.mvc.support", "RedirectAttributes") or
184185
// Also covers BindingResult. Note, you can access the field value through this interface, which should be considered tainted
185186
getType().(RefType).getAnAncestor().hasQualifiedName("org.springframework.validation", "Errors") or
186-
getType().(RefType).getAnAncestor().hasQualifiedName("org.springframework.web.bind.support", "SessionStatus") or
187-
getType().(RefType).getAnAncestor().hasQualifiedName("org.springframework.web.util", "UriComponentsBuilder") or
188-
getType().(RefType).getAnAncestor().hasQualifiedName("org.springframework.data.domain", "Pageable") or
187+
getType()
188+
.(RefType)
189+
.getAnAncestor()
190+
.hasQualifiedName("org.springframework.web.bind.support", "SessionStatus") or
191+
getType()
192+
.(RefType)
193+
.getAnAncestor()
194+
.hasQualifiedName("org.springframework.web.util", "UriComponentsBuilder") or
195+
getType()
196+
.(RefType)
197+
.getAnAncestor()
198+
.hasQualifiedName("org.springframework.data.domain", "Pageable") or
189199
this instanceof SpringModel
190200
}
191201

192-
predicate isExplicitlyTaintedInput() {
202+
private predicate isExplicitlyTaintedInput() {
193203
// InputStream or Reader parameters allow access to the body of a request
194204
getType().(RefType).getAnAncestor().hasQualifiedName("java.io", "InputStream") or
195205
getType().(RefType).getAnAncestor().hasQualifiedName("java.io", "Reader") or
196206
// The SpringServletInputAnnotations allow access to the URI, request parameters, cookie values and the body of the request
197207
this.getAnAnnotation() instanceof SpringServletInputAnnotation or
198208
// HttpEntity is like @RequestBody, but with a wrapper including the headers
199209
// TODO model unwrapping aspects
200-
getType().(RefType).getAnAncestor().hasQualifiedName("org.springframework.http", "HttpEntity<T>") or
201-
this.getAnAnnotation().getType().hasQualifiedName("org.springframework.web.bind.annotation", "RequestAttribute") or
202-
this.getAnAnnotation().getType().hasQualifiedName("org.springframework.web.bind.annotation", "SessionAttribute")
210+
getType()
211+
.(RefType)
212+
.getAnAncestor()
213+
.hasQualifiedName("org.springframework.http", "HttpEntity<T>") or
214+
this
215+
.getAnAnnotation()
216+
.getType()
217+
.hasQualifiedName("org.springframework.web.bind.annotation", "RequestAttribute") or
218+
this
219+
.getAnAnnotation()
220+
.getType()
221+
.hasQualifiedName("org.springframework.web.bind.annotation", "SessionAttribute")
203222
}
204223

205-
predicate isImplicitRequestParam() {
224+
private predicate isImplicitRequestParam() {
206225
// Any parameter which is not explicitly handled, is consider to be an `@RequestParam`, if
207226
// it is a simple bean property
208227
not isNotDirectlyTaintedInput() and
@@ -213,7 +232,7 @@ class SpringRequestMappingParameter extends Parameter {
213232
)
214233
}
215234

216-
predicate isImplicitModelAttribute() {
235+
private predicate isImplicitModelAttribute() {
217236
// Any parameter which is not explicitly handled, is consider to be an `@ModelAttribute`, if
218237
// it is not an implicit request param
219238
not isNotDirectlyTaintedInput() and
@@ -229,7 +248,8 @@ class SpringRequestMappingParameter extends Parameter {
229248

230249
/** Holds if the input is tainted */
231250
predicate isTaintedInput() {
232-
isExplicitlyTaintedInput() or
251+
isExplicitlyTaintedInput()
252+
or
233253
// Any parameter which is not explicitly identified, is consider to be an `@RequestParam`, if
234254
// it is a simple bean property) or a @ModelAttribute if not
235255
not isNotDirectlyTaintedInput()
@@ -316,7 +336,7 @@ class SpringUntrustedDataType extends RefType {
316336
p.isModelAttribute()
317337
or
318338
p.getAnAnnotation().(SpringServletInputAnnotation).getType().hasName("RequestBody")
319-
|
339+
|
320340
this.fromSource() and
321341
this = stripType(p.getType())
322342
)

0 commit comments

Comments
 (0)