TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Acceptable CSRF Token Lifecycle?

1 pointsby fpereiroover 5 years ago
Hi HN! Question for those with experience writing and&#x2F;or auditing the auth flows of a web application. I recently decided to move my session cookies to HttpOnly, to mitigate the damage of a XSS attack.<p>To prevent CSRF, I was sending said session cookie as an extra field with every POST request (double submit cookie pattern: https:&#x2F;&#x2F;medium.com&#x2F;cross-site-request-forgery-csrf&#x2F;double-submit-cookie-pattern-65bb71d80d9f). Now that the session is not readable from javascript, I need to create a separate CSRF token.<p>Based on great feedback from the community (see https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22209588 and https:&#x2F;&#x2F;github.com&#x2F;fpereiro&#x2F;backendlore&#x2F;issues&#x2F;12), I&#x27;m considering the following approach:<p>- On every successful login, create a new secret&#x2F;token (using the same crypto mechanism I use to create the session secret, but a different secret altogether) and store it on the database, tied to the session itself. Set both the session and the CSRF token to expire at the same time. - Every time I get a request with a valid session, renew the life of both the session AND the associated CSRF token. - On every successful login, return the CSRF token in the body so that it can be read by client-side javascript. - Set up an endpoint to retrieve the associated CSRF token for its session. If no session is present (or the session has expired), return a 403 code. This also solves the problem of letting the client-side app know whether the user is logged in or not (I would hit this GET &#x2F;csrf endpoint when the javascript loads to determine whether there&#x27;s a valid session available).<p>My understanding is that, as long as the browser supports Same-Origin Policy (https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Same-origin_policy), a CSRF attacker could not submit a GET request to my server and obtain the result. If you see any security issues in the above scheme - or if you use a similar scheme and know it to be secure - please let me know. Thank you very much for your feedback!

no comments

no comments