Skip to content

Commit 816a8d1

Browse files
authored
Merge pull request #2586 from ggolawski/spring_disable_csrf
Add check for disabled CSRF protection in Spring
2 parents 1ce77ff + 5596944 commit 816a8d1

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
2+
<qhelp>
3+
4+
<overview>
5+
<p>When you set up a web server to receive a request from a client without any mechanism
6+
for verifying that it was intentionally sent, then it is vulnerable to attack. An attacker can
7+
trick a client into making an unintended request to the web server that will be treated as
8+
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>When you use Spring, Cross-Site Request Forgery (CSRF) protection is enabled by default. Spring's recommendation
14+
is to use CSRF protection for any request that could be processed by a browser client by normal
15+
users.</p>
16+
</recommendation>
17+
18+
<example>
19+
<p>The following example shows the Spring Java configuration with CSRF protection disabled.
20+
This type of configuration should only be used if you are creating a service that is used only
21+
by non-browser clients.</p>
22+
23+
<sample src="SpringCSRFProtection.java" />
24+
</example>
25+
26+
<references>
27+
<li>
28+
OWASP:
29+
<a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)">Cross-Site Request Forgery (CSRF)</a>.
30+
</li>
31+
<li>
32+
Spring Security Reference:
33+
<a href="https://docs.spring.io/spring-security/site/docs/current/reference/html5/#servlet-csrf">
34+
Cross Site Request Forgery (CSRF) for Servlet Environments
35+
</a>.
36+
</li>
37+
</references>
38+
</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+
* a 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
16+
where
17+
call.getMethod().hasName("disable") and
18+
call
19+
.getReceiverType()
20+
.hasQualifiedName("org.springframework.security.config.annotation.web.configurers",
21+
"CsrfConfigurer<HttpSecurity>")
22+
select call, "CSRF vulnerability due to protection being disabled."

0 commit comments

Comments
 (0)