CodeQL 文档

已禁用 Spring CSRF 保护

ID: java/spring-disabled-csrf-protection
Kind: problem
Security severity: 8.8
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-352
Query suites:
   - java-code-scanning.qls
   - java-security-extended.qls
   - java-security-and-quality.qls

点击查看 CodeQL 存储库中的查询

当您设置 Web 服务器以接收来自客户端的请求,而没有任何机制来验证请求是否已有意发送时,它就容易受到攻击。攻击者可以诱骗客户端向 Web 服务器发出意外的请求,该请求将被视为真实请求。这可以通过 URL、图像加载、XMLHttpRequest 等方式完成,并可能导致数据泄露或意外的代码执行。

建议

使用 Spring 时,跨站点请求伪造 (CSRF) 保护默认启用。Spring 建议对普通用户可以通过浏览器客户端处理的任何请求使用 CSRF 保护。

示例

以下示例显示了禁用 CSRF 保护的 Spring Java 配置。此类配置仅在创建仅供非浏览器客户端使用的服务时使用。

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() 
      );
  }
}

参考

  • ©GitHub, Inc.
  • 条款
  • 隐私