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
@@ -216,15 +216,57 @@ As a crucial second layer of defense, the middleware **always** performs `Origin
216
216
217
217
### Built-in Extractors
218
218
219
-
**Secure (Recommended):**
219
+
**Most Secure (Recommended):**
220
220
221
-
-`csrf.FromHeader("X-Csrf-Token")` - Most secure, preferred for APIs
222
-
-`csrf.FromForm("_csrf")` - Secure for form submissions
221
+
-`csrf.FromHeader("X-Csrf-Token")` - Headers are not logged and cannot be manipulated via URL
222
+
-`csrf.FromForm("_csrf")` - Form data is secure and not typically logged
223
223
224
-
**Acceptable:**
224
+
**Less Secure (Use with caution):**
225
225
226
-
-`csrf.FromQuery("csrf_token")` - URL parameters
227
-
-`csrf.FromParam("csrf")` - Route parameters
226
+
-`csrf.FromQuery("csrf_token")` - URLs may be logged by servers, proxies, browsers
227
+
-`csrf.FromParam("csrf")` - URLs may be logged by servers, proxies, browsers
228
+
229
+
**Advanced:**
230
+
231
+
-`csrf.Chain(...)` - Try multiple extractors in sequence
232
+
233
+
:::note What about cookies?
234
+
**Cookies are generally not a secure source for CSRF tokens.** The middleware does not provide a built-in cookie extractor because reading the CSRF token from a cookie with the same name as the CSRF cookie defeats CSRF protection.
235
+
236
+
**Advanced usage:**
237
+
In rare cases, you may securely extract a CSRF token from a cookie if:
238
+
- You read from a different cookie (not the CSRF cookie itself)
239
+
- You use multiple cookies for custom validation
240
+
- You implement custom logic across different cookie sources
241
+
242
+
If you do this, set the extractor’s `Source` to `SourceCookie` and allow the middleware to check that the cookie name is different from your CSRF cookie. It will panic if this is the case.
243
+
244
+
**Warning:**
245
+
We strongly discourage cookie-based extraction, as it is easy to misconfigure and creates security risks. Prefer extracting tokens from headers or form fields for robust CSRF protection.
246
+
:::
247
+
248
+
### Extractor Metadata
249
+
250
+
Each extractor returns an `Extractor` struct with metadata about its behavior:
You can create a custom extractor to handle specific cases:
291
+
You can create a custom extractor to handle specific cases by creating an `Extractor` struct:
250
292
251
293
:::danger Never Extract from Cookies
252
294
**NEVER create custom extractors that read from cookies using the same `CookieName` as your CSRF configuration.** This completely defeats CSRF protection by making the extracted token always match the cookie value, allowing any CSRF attack to succeed.
253
295
254
296
```go
255
297
// ❌ NEVER DO THIS - Completely defeats CSRF protection
0 commit comments