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
fix: when validateOnReuse was false, behaviour was incorrect
Prior to this fix, if you set validateOnReuse to false, this only
prevented the error from being thrown, it did not actually
prevent validation from happening. This means if you were to
make a GET request without including the token in the request
and you were hoping to have the existing CSRF token included
in the response, it wasn't really possible, a new one would be
generated every time. It should not be necessary to included
the CSRF token in GET requests.
Now when validateOnReuse is false, the existing CSRF token
will be reused from the CSRF token cookie, without prior
validation. This is a semi breaking change, but it is a correction
of expected behaviour.
Copy file name to clipboardExpand all lines: README.md
+8-10Lines changed: 8 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -403,7 +403,7 @@ Used to customise the error response <code>statusCode</code>, the contained erro
403
403
{
404
404
cookieOptions?: CookieOptions, // allows overriding of cookieOptions
405
405
overwrite?: boolean, // Set to true to force a new token to be generated
406
-
validateOnReuse?: boolean, //Set to true to throw an error when overwrite is false and the current CSRF token is invalid
406
+
validateOnReuse?: boolean, //Deprecated, leave as default
407
407
} // optional
408
408
) =>string;
409
409
```
@@ -414,33 +414,31 @@ Used to customise the error response <code>statusCode</code>, the contained erro
414
414
generateCsrfToken(req, res, { overwrite: true }); // This will force a new token to be generated, and a new cookie to be set, even if one already exists
415
415
```
416
416
417
-
<p>If the <code>overwrite</code> parameter is set to <em>false</em> (default), the existing token will be re-used and returned. If the current / existing CSRF token is not valid, then a new token will be generated without any error being thrown. If you want the <code>generateCsrfToken</code> to throw an error instead, provide the <code>validateOnReuse: true</code> option.</p>
417
+
<p>If the <code>overwrite</code> parameter is set to <em>false</em> (default), the existing token will be re-used and returned.</p>
418
418
419
419
<p>If <code>overwrite</code> is <em>true</em> a new token will always be generated, even if the current one is invalid.</p>
420
420
421
421
```ts
422
-
generateCsrfToken(req, res, { overwrite: true }); // As overwrite is true, an error will never be thrown.
423
-
generateCsrfToken(req, res, { overwrite: false }); // As validateOnReuse is false (default), if the current CSRF token from the cookie is invalid, a new token will be generated without any error being thrown.
422
+
generateCsrfToken(req, res, { overwrite: true }); // As overwrite is true a new CSRF token will be generated.
423
+
generateCsrfToken(req, res, { overwrite: false }); // As overwrite is false, the existing CSRF token will be reused from the CSRF token cookie
424
424
generateCsrfToken(req, res); // same as previous
425
-
generateCsrfToken(req, res, { overwrite: false, validateOnReuse: true }); // As validateOnReuse is true, if the CSRF token from the cookie is invalid, a new token will be generated without an error being thrown.
425
+
generateCsrfToken(req, res, { overwrite: false, validateOnReuse: true }); //DEPRECATED - As validateOnReuse is true, if the CSRF token from the cookie is invalid, an error will be thrown
426
426
```
427
427
428
428
<p>Instead of importing and using <code>generateCsrfToken</code>, you can also use <code>req.csrfToken</code> any time after the <code>doubleCsrfProtection</code> middleware has executed on your incoming request.</p>
429
429
430
430
```ts
431
431
req.csrfToken(); // same as generateCsrfToken(req, res);
432
-
req.csrfToken({ overwrite: true }); // same as generateCsrfToken(req, res, { overwrite: true, validateOnReuse });
433
-
req.csrfToken({ overwrite: false, validateOnReuse: false }); // same as generateCsrfToken(req, res, { overwrite: false, validateOnReuse: false });
432
+
req.csrfToken({ overwrite: true }); // same as generateCsrfToken(req, res, { overwrite: true });
<p>The <code>generateCsrfToken</code> function serves the purpose of establishing a CSRF protection mechanism by generating a token and an associated cookie. This function also provides the option to utilise a third parameter, which is an object that may contain: <code>overwrite</code>, <code>validateOnReuse</code>, or <code>cookieOptions</code>. By default, <code>overwrite</code> and <code>validateOnReuse</code> are both set to <em>false</em>. <code>cookieOptions</code> if not provided will just default to the options originally provided to the initialisation configuration, any options that are provided will override those initially provided.</p>
436
+
<p>The <code>generateCsrfToken</code> function serves the purpose of establishing a CSRF protection mechanism by generating a token and an associated cookie. This function also provides the option to utilise a third parameter, which is an object that may contain: <code>overwrite</code>, and <code>cookieOptions</code>. By default, <code>overwrite</code> is set to false. <code>cookieOptions</code> if not provided will just default to the options originally provided to the initialisation configuration, any options that are provided will override those initially provided.</p>
439
437
<p>It returns a CSRF token and attaches a cookie to the response object. The cookie content is <code>`${hmac}${csrfTokenDelimiter}${randomValue}`</code>.</p>
440
438
<p>In some cases you should only transmit your token to the frontend as part of a response payload. Consult the <ahref="./FAQ.md#do-i-need-csrf-csrf">"Do I need csrf-csrf?"</a> and <ahref="./FAQ.md#does-httponly-have-to-be-true">"Does httpOnly have to be true?"</a> sections of the FAQ.</p>
441
439
<p>When <code>overwrite</code> is set to <em>false</em>, the function behaves in a way that preserves the existing CSRF token. In other words, if a valid CSRF token is already present in the incoming request cookie, the function will reuse the existing CSRF token.</p>
442
440
<p>If <code>overwrite</code> is set to <em>true</em>, the function will generate a new token and cookie each time it is invoked. This behavior can potentially lead to certain complications, particularly when multiple tabs are being used to interact with your web application. In such scenarios, the creation of new cookies with every call to the function can disrupt the proper functioning of your web app across different tabs, as the changes might not be synchronised effectively (you would need to write your own synchronisation logic).</p>
443
-
<p>If overwrite is set to <em>false</em>, the function will also validate the existing cookie information. If the information is found to be invalid, a new token will be generated and returned. If you want an error to be thrown when validation fails during generation you can set the <code>validateOnReuse</code> (by default, <em>false</em>) to <em>true</em>. If it is <em>true</em> then an error will be thrown instead of a new token being generated.</p>
441
+
<p>If overwrite is set to <em>false</em>, the function will return the existing CSRF token from the existing CSRF token cookie.</p>
0 commit comments