diff --git a/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.java b/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.java new file mode 100644 index 000000000000..018c16ecadde --- /dev/null +++ b/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.java @@ -0,0 +1,17 @@ +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@EnableWebSecurity +@Configuration +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .csrf(csrf -> + // BAD - CSRF protection shouldn't be disabled + csrf.disable() + ); + } +} diff --git a/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.qhelp b/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.qhelp new file mode 100644 index 000000000000..af1119bcd1c5 --- /dev/null +++ b/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.qhelp @@ -0,0 +1,38 @@ + + + + +

When you set up a web server to receive a request from a client without any mechanism +for verifying that it was intentionally sent, then it is vulnerable to attack. An attacker can +trick a client into making an unintended request to the web server that will be treated as +an authentic request. This can be done via a URL, image load, XMLHttpRequest, etc. and can +result in exposure of data or unintended code execution.

+
+ + +

When you use Spring, Cross-Site Request Forgery (CSRF) protection is enabled by default. Spring's recommendation +is to use CSRF protection for any request that could be processed by a browser client by normal +users.

+
+ + +

The following example shows the Spring Java configuration with CSRF protection disabled. +This type of configuration should only be used if you are creating a service that is used only +by non-browser clients.

+ + +
+ + +
  • +OWASP: +Cross-Site Request Forgery (CSRF). +
  • +
  • +Spring Security Reference: + + Cross Site Request Forgery (CSRF) for Servlet Environments +. +
  • +
    +
    \ No newline at end of file diff --git a/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.ql b/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.ql new file mode 100644 index 000000000000..01438894b33d --- /dev/null +++ b/java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.ql @@ -0,0 +1,22 @@ +/** + * @name Disabled Spring CSRF protection + * @description Disabling CSRF protection makes the application vulnerable to + * a Cross-Site Request Forgery (CSRF) attack. + * @kind problem + * @problem.severity error + * @precision high + * @id java/spring-disabled-csrf-protection + * @tags security + * external/cwe/cwe-352 + */ + +import java + +from MethodAccess call +where + call.getMethod().hasName("disable") and + call + .getReceiverType() + .hasQualifiedName("org.springframework.security.config.annotation.web.configurers", + "CsrfConfigurer") +select call, "CSRF vulnerability due to protection being disabled."