spring-projects / spring-projects/spring-security

Fetch Metadata (`Sec-Fetch-*`) based CSRF protection

Open
#18,361 1 comment 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Expected Behavior

Spring Security could provide first-class, opt-in support for CSRF protection based on Fetch Metadata headers (Sec-Fetch-Site, Sec-Fetch-Mode, Sec-Fetch-Dest, Sec-Fetch-User) as a documented and supported alternative or complement to the existing token-based CSRF protection.

It should be possible to configure Spring Security to:

  • Reject state-changing requests (e.g. POST, PUT, PATCH, DELETE) when browsers send Sec-Fetch-Site: cross-site.
  • Allow same-origin (and optionally same-site) requests by default.
  • Provide simple configuration via the existing HttpSecurity.csrf() DSL.
  • Support whitelisting for endpoints that intentionally accept cross-site requests (e.g. webhooks).
  • Optionally support a “report-only” / logging mode for safe rollout.

This would make it easier for Spring applications to adopt a simpler CSRF model aligned with modern browser capabilities and OWASP guidance.

OWASP CSRF Cheat Sheet discussion on Fetch Metadata headers

CSRF protection without tokens (Miguel Grinberg)

MDN – Fetch Metadata request headers

Current Behavior

Spring Security currently supports classic CSRF protection mechanisms as stated in docs: https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html

Context

For modern SPA applications, token-based CSRF protection can be conceptually complex and operationally heavy (token propagation, caching concerns, custom clients, backend support etc.). Fetch Metadata headers provide a simpler and more intuitive model that aligns closely with how browsers already distinguish same-origin vs cross-site requests.

I can always continue to use classic CSRF tokens everywhere or rely on Origin/Referer checks by implementing a custom OncePerRequestFilter for Fetch Metadata validation. All of these work, but lack the clarity, consistency, and safety guarantees that first-class framework support would provide.

How does the spring-security team feel about this idea? :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the existing Spring Security CSRF documentation and the HttpSecurity.csrf() DSL described in the issue, then compare the custom OncePerRequestFilter approach mentioned there. Done should provide opt-in Fetch Metadata protection with same-origin handling, endpoint whitelisting, and an optional report-only mode, with the behavior documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
security
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.