Skip to content

Commit 506d668

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: add class for Spring request mapping methods that are not default-protected from CSRF
1 parent 0c69253 commit 506d668

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/** Provides classes and predicates to reason about CSRF vulnerabilities due to use of unprotected HTTP request types. */
2+
3+
import java
4+
private import semmle.code.java.frameworks.spring.SpringController
5+
6+
/** A method that is not protected from CSRF by default. */
7+
abstract class CsrfUnprotectedMethod extends Method { }
8+
9+
/**
10+
* A Spring request mapping method that is not protected from CSRF by default.
11+
*
12+
* https://docs.spring.io/spring-security/reference/features/exploits/csrf.html#csrf-protection-read-only
13+
*/
14+
private class SpringCsrfUnprotectedMethod extends CsrfUnprotectedMethod instanceof SpringRequestMappingMethod
15+
{
16+
SpringCsrfUnprotectedMethod() {
17+
this.hasAnnotation("org.springframework.web.bind.annotation", "GetMapping")
18+
or
19+
this.hasAnnotation("org.springframework.web.bind.annotation", "RequestMapping") and
20+
(
21+
this.getAnAnnotation().getAnEnumConstantArrayValue("method").getName() =
22+
["GET", "HEAD", "OPTIONS", "TRACE"]
23+
or
24+
// If no request type is specified with `@RequestMapping`, then all request types
25+
// are possible, so we treat this as unsafe; example: @RequestMapping(value = "test").
26+
not exists(this.getAnAnnotation().getAnArrayValue("method"))
27+
)
28+
}
29+
}

0 commit comments

Comments
 (0)