Protection against cross-site request forgery is turned off

tx0/kotlin:csrf-protection-disabledstrictwarningsecurity/authn-authzdepth fastsecurity, cwe-352, owasp-a01

What it finds

Cross-site request forgery protection makes the browser prove that a state-changing request comes from the application's own pages. Without it, any site the user visits while signed in can submit forms to the application in the user's name, as the browser adds the session cookie by itself. Turning it off is only safe for endpoints that no browser session can reach, as APIs authenticated by a header token.

Reported for csrf { disable() }, csrf { it.disable() }, .csrf().disable() and csrf(AbstractHttpConfigurer::disable) in files that import Spring Security.

From the rule's source, csrf_disabled.rs.

Example from the rule's tests
import org.springframework.security.config.annotation.web.builders.HttpSecurity class Security {    fun chain(http: HttpSecurity) {        http {            csrf { disable() }tx0 reports: CSRF protection is turned off        }        http.csrf { it.disable() }tx0 reports: CSRF protection is turned off        http.csrf().disable()tx0 reports: CSRF protection is turned off        http.csrf(AbstractHttpConfigurer::disable)tx0 reports: CSRF protection is turned off        http.csrf { it.ignoringRequestMatchers("/webhook") }        http.cors { it.disable() }    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:csrf-protection-disabled" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:csrf-protection-disabled] reason="..."

tx0 explain tx0/kotlin:csrf-protection-disabled shows the same in the terminal. Found a false positive? Report it.