TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Acceptable CSRF Token Lifecycle?

1 点作者 fpereiro超过 5 年前
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!

暂无评论

暂无评论