Skip to content

Commit 1d12340

Browse files
committed
Java: Model Spring @responsebody methods.
1 parent fd2cd60 commit 1d12340

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import semmle.code.java.Maps
33
import SpringWeb
44

55
/**
6-
* An annotation type that identifies Spring components.
6+
* An annotation type that identifies Spring controllers.
77
*/
88
class SpringControllerAnnotation extends AnnotationType {
99
SpringControllerAnnotation() {
@@ -15,13 +15,31 @@ class SpringControllerAnnotation extends AnnotationType {
1515
}
1616
}
1717

18+
/**
19+
* An annotation type that identifies Spring rest controllers.
20+
*
21+
* Rest controllers are the same as controllers, but imply the @ResponseBody annotation.
22+
*/
23+
class SpringRestControllerAnnotation extends SpringControllerAnnotation {
24+
SpringRestControllerAnnotation() {
25+
hasName("RestController")
26+
}
27+
}
28+
1829
/**
1930
* A class annotated, directly or indirectly, as a Spring `Controller`.
2031
*/
2132
class SpringController extends Class {
2233
SpringController() { getAnAnnotation().getType() instanceof SpringControllerAnnotation }
2334
}
2435

36+
/**
37+
* A class annotated, directly or indirectly, as a Spring `RestController`.
38+
*/
39+
class SpringRestController extends SpringController {
40+
SpringRestController() { getAnAnnotation().getType() instanceof SpringRestControllerAnnotation }
41+
}
42+
2543
/**
2644
* A method on a Spring controller which is accessed by the Spring MVC framework.
2745
*/
@@ -73,6 +91,16 @@ class SpringRequestMappingAnnotationType extends AnnotationType {
7391
}
7492
}
7593

94+
/**
95+
* An `AnnotationType` which is used to indicate a `ResponseBody`.
96+
*/
97+
class SpringResponseBodyAnnotationType extends AnnotationType {
98+
SpringResponseBodyAnnotationType() {
99+
// `@ResponseBody` used directly as an annotation.
100+
hasQualifiedName("org.springframework.web.bind.annotation", "ResponseBody")
101+
}
102+
}
103+
76104
/**
77105
* A method on a Spring controller that is executed in response to a web request.
78106
*/
@@ -91,6 +119,15 @@ class SpringRequestMappingMethod extends SpringControllerMethod {
91119
SpringRequestMappingParameter getARequestParameter() {
92120
result = getAParameter()
93121
}
122+
123+
/** Holds if this is considered an @ResponseBody method. */
124+
predicate isResponseBody() {
125+
getAnAnnotation().getType() instanceof SpringResponseBodyAnnotationType
126+
or
127+
getDeclaringType().getAnAnnotation().getType() instanceof SpringResponseBodyAnnotationType
128+
or
129+
getDeclaringType() instanceof SpringRestController
130+
}
94131
}
95132

96133
/** A Spring framework annotation indicating remote user input from servlets. */

0 commit comments

Comments
 (0)