Prompt
How do I protect against CSRF with a WAF?
Latest observation
A WAF can help reduce CSRF risk, but it should not be your only defense. CSRF is best prevented in the application with anti-CSRF tokens, SameSite cookies, and proper Origin/Referer checks. A WAF can add a useful layer, mainly by filtering out suspicious cross-site requests and enforcing request patterns.
What a WAF can do for CSRF
1) Block requests missing expected CSRF tokens
If your app uses a CSRF token in a header or form field, the WAF can be configured to:
- require the token on state-changing requests (
POST,PUT,PATCH,DELETE) - block if the token is missing
- block if the token format is invalid
This works best when:
- the token is sent in a custom header or form field
- the WAF can inspect request bodies and headers
- the token is not easily guessable
2) Validate Origin / Referer headers
For state-changing requests, a WAF can enforce:
Originmust match your domain- if
Originis absent,Referermust match your domain
This is a good supplemental defense, especially for browsers, though not all requests include these headers.
3) Require custom headers on AJAX/API requests
For browser-based APIs, you can require a custom header such as:
X-CSRF-TokenX-Requested-With
Cross-site forms can’t set arbitrary custom headers without CORS preflight, so this helps distinguish legitimate app requests from CSRF attempts.
4) Restrict unsafe methods
At the WAF layer, you can:
- allow only safe methods on certain endpoints
- block
POST/PUT/DELETEto endpoints that shouldn’t accept them - enforce content-type expectations, such as only JSON for APIs
5) Enforce cookie attributes indirectly
A WAF can’t set cookie flags itself, but it can help detect or reject requests that suggest weak cookie handling. Still, the real fix is to set:
SameSite=LaxorSameSite=StrictSecureHttpOnlywhere appropriate
Recommended WAF strategy
Use the WAF as a layered control:
-
State-changing endpoints only
/transfer/update-profile/change-password/api/*mutating routes
-
Require one or more of:
- valid CSRF token
- matching
Origin/Referer - custom header
- authenticated session plus same-site cookie policy
-
Log and alert
- missing token
- mismatched origin
- unexpected method/content-type
- repeated suspicious cross-site attempts
Limitations of using a WAF for CSRF
- Not all browsers send
Origin/Refererreliably - Tokens can be bypassed if the app logic is flawed
- Attackers may still exploit endpoints the WAF doesn’t understand
- WAF rules can break legitimate traffic
- If tokens are reflected or predictable, the WAF won’t save you
Best-practice combination
For strongest protection:
- Application-level CSRF tokens
SameSite=LaxorStrictcookies- Origin/Referer validation
- WAF rules for additional enforcement and anomaly detection
Example WAF rules to consider
- Block
POST,PUT,PATCH,DELETEunless:Originequals your site, orX-CSRF-Tokenexists and matches expected format
- Block requests with missing
Content-Typefor JSON APIs - Block state-changing requests from empty/missing
Originunless a valid token is present - Alert on requests with cross-site
Origin
If you want, I can also give you:
- example rules for a specific WAF (Cloudflare, ModSecurity, AWS WAF, F5, etc.), or
- a recommended CSRF policy for browser-based apps vs APIs.