Skip to content

Commit ab49397

Browse files
committed
Add check for disabled CSRF protection in Spring
1 parent 564013d commit ab49397

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import org.springframework.context.annotation.Configuration;
2+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
4+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
5+
6+
@EnableWebSecurity
7+
@Configuration
8+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
9+
@Override
10+
protected void configure(HttpSecurity http) throws Exception {
11+
http
12+
.csrf(csrf ->
13+
// BAD - CSRF protection shouldn't be disabled
14+
csrf.disable()
15+
);
16+
}
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
2+
<qhelp>
3+
4+
<overview>
5+
<p>When a web server is designed to receive a request from a client without any mechanism
6+
for verifying that it was intentionally sent, then it might be possible for an attacker
7+
to trick a client into making an unintentional request to the web server which will be treated
8+
as an authentic request. This can be done via a URL, image load, XMLHttpRequest, etc. and can
9+
result in exposure of data or unintended code execution.</p>
10+
</overview>
11+
12+
<recommendation>
13+
<p>Cross-Site Request Forgery (CSRF) protection is enabled by default in Spring with Java
14+
configuration. It's recommended to not disable this.</p>
15+
</recommendation>
16+
17+
<example>
18+
<p>The following example shows the Spring Java configuration with CSRF protection disabled.</p>
19+
20+
<sample src="SpringCSRFProtection.java" />
21+
</example>
22+
23+
<references>
24+
<li>
25+
CWE:
26+
<a href="https://cwe.mitre.org/data/definitions/352.html">CWE-352: Cross-Site Request Forgery (CSRF)</a>.
27+
</li>
28+
<li>
29+
OWASP:
30+
<a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)">Cross-Site Request Forgery (CSRF)</a>.
31+
</li>
32+
<li>
33+
Spring Security Reference:
34+
<a href="https://docs.spring.io/spring-security/site/docs/current/reference/html5/#servlet-csrf">
35+
Cross Site Request Forgery (CSRF) for Servlet Environments
36+
</a>.
37+
</li>
38+
</references>
39+
</qhelp>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @name Disabled Spring CSRF protection
3+
* @description Disabling CSRF protection makes the application vulnerable to
4+
* Cross-Site Request Forgery (CSRF) attack.
5+
* @kind problem
6+
* @problem.severity error
7+
* @precision high
8+
* @id java/spring-disabled-csrf-protection
9+
* @tags security
10+
* external/cwe/cwe-352
11+
*/
12+
13+
import java
14+
15+
from MethodAccess call, Method method
16+
where
17+
call.getMethod() = method and
18+
method.hasName("disable") and
19+
method.getDeclaringType().getQualifiedName().regexpMatch(
20+
"org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer<CsrfConfigurer<.*>,.*>"
21+
)
22+
select call, "CSRF vulnerability due to protection being disabled."

0 commit comments

Comments
 (0)