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
Copy file name to clipboardExpand all lines: public/blogs/csrf-mitigation.mdx
+14-14Lines changed: 14 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,15 +13,15 @@ tags:
13
13
---
14
14
15
15
<C>
16
-
If you don't rely on a framework to do the heavy lifting for you, or a third party library. As I always say that you have to [understand]() the subject before you abstract it. Here's how to do it manually, but first note that none of the techniques below will work if you're already [XSS]()vulnerable
16
+
If you don't rely on a framework to do the heavy lifting for you, or a third party library. As I always say that you have to <Lhref='/blog/fundamentals'>understand</L> the subject before you abstract it. Here's how to do it manually, but first note that none of the techniques below will work if you're already vulnerable to <Lhref='https://owasp.org/www-community/attacks/xss/'>XSS.</L>
17
17
</C>
18
18
<H2>Where Did The Request Come From?</H2>
19
19
<C>
20
20
If you do not trust the request's origin, block it.
21
21
</C>
22
22
<H3>"Origin" Header</H3>
23
23
<C>
24
-
Since the `Origin` header is being added automatically by browsers as part of the [`CORS`]() mechanism, you can check and validate the header, to check if the request that is coming in, actually comes from you site, block it if it doesn't
24
+
Since the <Lhref='https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin'>`Origin`</L> header is being added automatically by browsers as part of the <Lhref='https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS'>`CORS`</L> mechanism, you can check and validate the header, to check if the request that is coming in, actually comes from you site, block it if it doesn't
@@ -34,7 +34,7 @@ Since the `Origin` header is being added automatically by browsers as part of th
34
34
<H3>"Sec-Fetc-" Headers</H3>
35
35
36
36
<C>
37
-
Fetch Metadata headers, [introduced]() last year to provide additional context about the resource request. But all you have to know for now is you can use it to to check the origin
37
+
Fetch Metadata headers, introduced last year to provide additional context about the resource request. But all you have to know for now is you can use it to to check the origin
@@ -47,7 +47,7 @@ Fetch Metadata headers, [introduced]() last year to provide additional context a
47
47
showLineNumbers={false}
48
48
/>
49
49
<C>
50
-
Learn more about `Sec-Fetch` headers [here](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Mode) and [here](https://w3c.github.io/webappsec-fetch-metadata/#sec-fetch-site-header)
50
+
Learn more about `Sec-Fetch` headers <Lhref='https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Mode'>here</L> and <Lhref='https://w3c.github.io/webappsec-fetch-metadata/#sec-fetch-site-header)'>here</L>
51
51
</C>
52
52
<C>
53
53
You can use a combo of these two, just to make sure, if one doesn't exist is altered the other might be used. But solely relying on headers is not sufficient, headers could potentially be tampered with or omitted due to certain browser vulnerabilities, network configurations or proxies misconfigurations. So other techniques should be employed too, like
@@ -65,9 +65,9 @@ There are two main ways to implement anti-CSRF tokens:
65
65
</C>
66
66
<H3>Stateless Services</H3>
67
67
<C>
68
-
As in, user sessions can be managed without a database by storing session information as a [JWT]() or [JWE]() within a [cookie](). To defend against CSRF attacks in this setup, use the "Signed Double Submit Cookie Method.":
68
+
As in, user sessions can be managed without a database by storing session information as a <Lhref='https://datatracker.ietf.org/doc/html/rfc7519'>JWT</L> or <Lhref='https://datatracker.ietf.org/doc/html/rfc7516'>JWE</L> within a <Lhref='#cookies'>cookie</L>. To defend against CSRF attacks in this setup, use the "Signed Double Submit Cookie Method.":
69
69
</C><C>
70
-
This method involves generating a unique and cryptographically secure random value, typically a UUID, when a user first authenticates. This UUID is then embedded within both the payload of the JWT/JWE and the CSRF token. To ensure the integrity and authenticity of the CSRF token, use an [HMAC](https://datatracker.ietf.org/doc/html/rfc2104.html), which you combine a secret key (used for signing/encrypting the JWT/JWE) with a hash function like [Blake3](https://github.com/BLAKE3-team/BLAKE3) or SHA256 (make sure it's not computationally intensive but secure enough) , to produce a fixed-size hash value representing the message's integrity. The HMAC message includes two parts: a new random value for collision avoidance and the shared UUID between the JWT/JWE and the CSRF token.
70
+
This method involves generating a unique and cryptographically secure random value, typically a UUID, when a user first authenticates. This UUID is then embedded within both the payload of the JWT/JWE and the CSRF token. To ensure the integrity and authenticity of the CSRF token, use an <Lhref='https://datatracker.ietf.org/doc/html/rfc2104.html'>HMAC</L>, which you combine a secret key (used for signing/encrypting the JWT/JWE) with a hash function like <Lhref='https://github.com/BLAKE3-team/BLAKE3'>Blake3</L> or SHA256 (make sure it's not computationally intensive but secure enough) , to produce a fixed-size hash value representing the message's integrity. The HMAC message includes two parts: a new random value for collision avoidance and the shared UUID between the JWT/JWE and the CSRF token.
71
71
</C><C>
72
72
The idea might **Go** like this
73
73
</C>
@@ -243,22 +243,22 @@ Here's an AJAX example
243
243
showLineNumbers={false}
244
244
/>
245
245
<C>
246
-
If you want to see a framework implementation of the synchronized pattern , check out [`Django`]()'s implementation with the CSRF mitigation middleware, you can read the source code [here]().
246
+
If you want to see a framework implementation of the synchronized pattern , check out <Lhref='https://docs.djangoproject.com'>`Django`</L>'s implementation with the CSRF mitigation middleware, you can read the source code <Lhref='https://github.com/django/django/blob/main/django/middleware/csrf.py'>here.</L>
247
247
</C>
248
-
<H2>Cookies</H2>
248
+
<H2id='cookies'>Cookies</H2>
249
249
<C>
250
250
Avoid setting cookies with a specific domain to minimize security risks. When a cookie is domain-specific, all subdomains share that cookie, which can pose risks if you get hit with a <Lhref="https://developer.mozilla.org/en-US/docs/Web/Security/Subdomain_takeovers">subdomain takeover.</L>
251
251
</C><C>
252
252
For session cookies, ensure they are protected by:
253
253
</C><C>
254
254
-\- Using the `HttpOnly` attribute to prevent client-side scripts from accessing cookies, enhancing security against XSS attacks.
255
-
-\- Setting the `SameSite` attribute to `Lax` or `Strict` to control cookie behavior in cross-site requests, more [below]().
255
+
-\- Setting the `SameSite` attribute to `Lax` or `Strict` to control cookie behavior in cross-site requests, more <Lhref='#samesite'>below</L>.
256
256
-\- Avoid specifying a domain (`Domain=None`) to prevent cookies from being sent in cross-origin requests.
257
257
-\- Using the `Secure` attribute to ensure that cookies are only sent over HTTPS connections.
258
258
</C><C>
259
-
Also, for enhanced security measures, consider cookie prefixes like `__Host-` and `__Secure-`, read more [here]()
259
+
Also, for enhanced security measures, consider cookie prefixes like `__Host-` and `__Secure-`, read more <Lhref='https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-cookie-prefixes-00'>here.</L>
260
260
</C>
261
-
<H3>About "SameSite" Cookies</H3>
261
+
<H3id="#samesite">About "SameSite" Cookies</H3>
262
262
<C>
263
263
Use the `SameSite` cookie attribute to set your users sessions (older browsers do not support this)
264
264
</C>
@@ -276,7 +276,7 @@ Or
276
276
showLineNumbers={false}
277
277
/>
278
278
<C>
279
-
Read about [`Lax`]() and [`Strict`]() from the official [RFC](). Here's is basically what they do:
279
+
Read about `Lax` and `Strict` from the official <Lhref='https://datatracker.ietf.org/doc/html/draft-west-first-party-cookies-07'>RFC</L>. Here's is basically what they do:
280
280
</C>
281
281
<C>
282
282
**``Strict``:** This value prevents cookies from being sent in all cross-site browsing contexts, including regular links. For example: You're logged in your banking account, which means you have a session cookie. If this banking website employs the Strict ``SameSite`` value for its session cookies, and you click on a link to perform a banking transaction from an external website (like a forum or email) say https://scam-me-please.com , the banking website won't receive the session cookie due to the ``Strict`` setting. Consequently, the user won't be able to complete the transaction because the session information is not sent with the request.
@@ -288,9 +288,9 @@ Read about [`Lax`]() and [`Strict`]() from the official [RFC](). Here's is basic
288
288
<H2>User Interaction-Based CSRF Defense</H2>
289
289
290
290
<C>
291
-
Requiring users to authenticate using their password, biometric data, security questions, or [OTP]() is a highly effective security measure. However, it can significantly impact user experience, so it's typically used only for critical operations like changing passwords or conducting financial transactions. Avoid using CAPTCHA for this purpose, as it's primarily aimed at preventing automated bot attacks and doesn't provide the necessary level of security for these sensitive activities.
291
+
Requiring users to authenticate using their password, biometric data, security questions, or <Lhref="https://en.wikipedia.org/wiki/One-time_password">OTP</L> is a highly effective security measure. However, it can significantly impact user experience, so it's typically used only for critical operations like changing passwords or conducting financial transactions. Avoid using CAPTCHA for this purpose, as it's primarily aimed at preventing automated bot attacks and doesn't provide the necessary level of security for these sensitive activities.
292
292
</C>
293
293
<H2>HTTP Methods</H2>
294
294
<C>
295
-
And oh yeah, did I mention that for any state changing request, DON'T USE [safe methods]().
295
+
And oh yeah, did I mention that for any state changing request, DON'T USE <Lhref='https://datatracker.ietf.org/doc/html/rfc7231#section-4.2.1'>safe methods.</L>
0 commit comments