You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added guidance that all Fetch Metadata implementations must include a mandatory fallback to Origin/Referer verification for compatibility.
Reworked browser compatibility notes and Limitations and gotchas section.
Changed language to avoid undermining Fetch Metadata headers
Copy file name to clipboardExpand all lines: cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.md
+7-8Lines changed: 7 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ In short, the following principles should be followed to defend against CSRF:
15
15
-**See the OWASP [XSS Prevention Cheat Sheet](Cross_Site_Scripting_Prevention_Cheat_Sheet.md) for detailed guidance on how to prevent XSS flaws.**
16
16
-**First, check if your framework has [built-in CSRF protection](#use-built-in-or-existing-csrf-implementations-for-csrf-protection) and use it**
17
17
-**If the framework does not have built-in CSRF protection, add [CSRF tokens](#token-based-mitigation) to all state changing requests (requests that cause actions on the site) and validate them on the backend, or validate [Fetch Metadata headers](#fetch-metadata-headers) on the backend for all state-changing requests.**
18
-
-**If your software is intended to be used only on modern browsers, you may rely primarily on [Fetch Metadata headers](#fetch-metadata-headers) to block cross-site state-changing requests**
18
+
-**If your software is intended to be used only on modern browsers, you may rely on [Fetch Metadata headers](#fetch-metadata-headers) to block cross-site state-changing requests**
19
19
-**Stateful software should use the [synchronizer token pattern](#synchronizer-token-pattern)**
20
20
-**Stateless software should use [double submit cookies](#alternative-using-a-double-submit-cookie-pattern)**
21
21
-**If an API-driven site can't use `<form>` tags, consider [using custom request headers](#employing-custom-request-headers-for-ajaxapi)**
@@ -156,7 +156,7 @@ Though the Naive Double-Submit Cookie method is simple and scalable, it remains
156
156
157
157
Fetch Metadata request headers provide extra context about how an HTTP request was made, and how the resource will be used, enabling servers to reject suspicious cross-site requests. Servers can use these headers — most importantly `Sec-Fetch-Site` — as a lightweight and reliable method to block obvious cross-site requests. See the [Fetch Metadata specification](https://www.w3.org/TR/fetch-metadata/) for details.
158
158
159
-
Although Fetch Metadata headers are relatively new compared to [token-based defenses](#token-based-mitigation), they provide a simple way to block cross-origin state-changing requests and on modern browsers—can serve as a primary CSRF mitigation. The main caveat is compatibility: some legacy browsers, non-browser clients and certain webviews may not send `Sec-Fetch-*` headers, so deployments should include tested fallback behavior and a careful rollout plan.
159
+
Because some legacy browsers may not send `Sec-Fetch-*` headers, a fallback to [standard origin verification](#using-standard-headers-to-verify-origin) using the `Origin`and `Referer` headers**is a mandatory requirement** for any Fetch Metadata implementation.
160
160
161
161
The Fetch Metadata request headers are:
162
162
@@ -173,9 +173,9 @@ Unlike [synchronizer tokens](#synchronizer-token-pattern) or [double-submit patt
173
173
174
174
### Browser compatibility
175
175
176
-
Fetch Metadata request headers are supported in most modern browsers on both desktop and mobile (Chrome, Edge, Firefox, Safari 16.4+, and even in webviews on both iOS and Android). For compatibility detail, see the [browser support table](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site#browser_compatibility).
176
+
Fetch Metadata request headers are supported in most modern browsers on both desktop and mobile (Chrome, Edge, Firefox, Safari 16.4+, and even in webviews on both iOS and Android), with [over 97% global coverage](https://caniuse.com/mdn-http_headers_sec-fetch-site). For compatibility detail, see the [browser support table](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Sec-Fetch-Site#browser_compatibility).
177
177
178
-
If your project requires absolute, 100% client coverage, [CSRF tokens](#token-based-mitigation) remain the safest universal option.
178
+
For the rare cases of outdated or embedded browsers that lack `Sec-Fetch-*` support, a fallback to [standard origin verification](#using-standard-headers-to-verify-origin) should provide the required coverage. If this is acceptable for your project, consider prompting users to update their browsers, as they are running on outdated and potentially insecure versions.
179
179
180
180
### How to treat Fetch Metadata headers on the server-side
181
181
@@ -214,7 +214,7 @@ If your project requires absolute, 100% client coverage, [CSRF tokens](#token-ba
214
214
215
215
2. If `Sec-Fetch-*` headers are absent: choose a fallback based on risk and compatibility requirements:
216
216
2.1. Fail-safe (recommended for sensitive endpoints): treat absence as unknown and block the request.
217
-
2.2. Fail-open (compatibility-first): fallback to other security measure (CSRF tokens, validate Origin/Referer, and/or require additional validation).
217
+
2.2. Fail-open (compatibility-first): fallback to other security measure ([standard origin verification](#using-standard-headers-to-verify-origin), CSRF tokens, and/or require additional validation).
218
218
219
219
3. Additionall options
220
220
3.1 To ensure that your site can still be linked from other sites, you have to allow simple (HTTPGET) top-level navigation.
@@ -230,11 +230,10 @@ If your project requires absolute, 100% client coverage, [CSRF tokens](#token-ba
230
230
231
231
3.2 Whitelist explicit cross-origin flows. If certain endpoints intentionally accept cross-origin requests (CORSJSON APIs, third-party integrations, webhooks), explicitly exempt those endpoints from the global Sec-Fetch deny policy and secure them with proper CORS configuration, authentication, and logging.
232
232
233
-
### Limitations and gotchas
233
+
### Things to consider
234
234
235
-
- Not universal. Some older browsers, webviews, bots, and non-browser HTTP clients do not send Sec-Fetch-*. Do not assume presence on every request — implement fallbacks.
236
-
- May break legitimate cross-origin integrations. Aglobal Sec-Fetch policy can unintentionally block legitimate CORS or third-party flows; plan explicit whitelisting.
237
235
- One limitation is that Fetch Metadata request headers are only sent to [potentially trustworthy URLs](https://www.w3.org/TR/secure-contexts/#is-url-trustworthy). This means the headers will generally be present for requests to origins whose scheme is `https`, `wss`, or `file`, and for `localhost` (hosts in the `127.0.0.0/8` or `::1/128` ranges). For the full rules and additional edge cases (the algorithm the user agent uses to decide trustworthiness), see the [W3C Secure Contexts spec](https://www.w3.org/TR/secure-contexts/#is-origin-trustworthy).
236
+
- To ensure consistent inclusion of Fetch Metadata headers, enforce HTTPS across your site. Enabling [HTTP Strict Transport Security (HSTS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Strict-Transport-Security) helps achieve this by automatically upgrading all HTTP requests to HTTPS, ensuring requests are always sent from a secure, trustworthy context.
0 commit comments