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
crypto.timingSafeEqual throws if buffers have different lengths. Ensure both csrfCookie and csrfHeader buffers are the same length or guard against mismatches to prevent runtime errors.
Converting untrusted header values with Buffer.from could throw if the input is malformed. Consider wrapping timingSafeEqual and buffer creation in a try/catch to avoid crashing the middleware.
The setCsrfCookie helper always sets secure and sameSite flags. Verify whether secure should be conditional based on environment (e.g., disabled in local development) and if additional attributes (domain, path) are needed.
Avoid runtime errors when token lengths differ by checking buffer lengths before using timingSafeEqual. Treat a length mismatch as a failed validation.
Why: Pre-checking buffer lengths prevents runtime errors in crypto.timingSafeEqual and correctly treats mismatched lengths as validation failures.
Medium
Security
Make CSRF cookie readable by JS
Allow client-side scripts to read the CSRF token by disabling httpOnly. Double-submit cookie patterns require JavaScript access to the token for setting the header.
Why: Disabling httpOnly is required for the double-submit pattern so client-side code can read the token and set the header, though it changes the security posture.
Medium
General
Add header fallback support
Provide a fallback to the previous header name for backward compatibility so that clients still sending x-csrf-token aren’t rejected. This ensures a smoother upgrade path without breaking existing consumers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Type
Bug fix, Enhancement
Description
Rename CSRF header to
x-xsrf-tokenAdd timingSafeEqual for token comparison
Increase CSRF token length to 32 bytes
Introduce
setCsrfCookiehelper functionChanges walkthrough 📝
csrf_middleware.ts
Improve CSRF token validation and handlingfunctions/src/middlewares/csrf_middleware.ts
x-csrf-tokentox-xsrf-tokensetCsrfCookiefor secure cookie handling