简体   繁体   English

是否需要为每个请求/响应在JWT令牌中续订CSRF令牌?

[英]Is it neccessary to renew CSRF token in JWT token for every request/response?

I'm developing a web application using symfony and JWT token for authentication. 我正在开发一个使用symfony和JWT令牌进行身份验证的Web应用程序。 For preventing XSS, JWT token is stored in cookies with HttpOnly attribute. 为了防止XSS,JWT令牌存储在具有HttpOnly属性的cookie中。 And for preventing CSRF, I used random csrf token. 为了防止CSRF,我使用了随机的CSRF令牌。 This token are stored in cookie and JWT token (encrypted). 该令牌存储在cookie和JWT令牌中(已加密)。 What I want to know is, is it necessary to renew csrf token in every response? 我想知道的是,是否需要在每个响应中都更新csrf令牌? Whats the best implementation? 什么是最佳实施?

Here's my settings in details: 这是我的详细设置:

  • We've got a single page app. 我们有一个单页应用程序。 Most requests will be sent using ajax. 大多数请求将使用ajax发送。
  • The user authenticates using POST. 用户使用POST进行身份验证。
  • On successful authentication, the server will generate random csrf token then store it in the cookies (HttpOnly) and inside JWT payload. 成功进行身份验证后,服务器将生成随机的csrf令牌,然后将其存储在cookie(HttpOnly)和JWT有效负载内部。 Before it is stored in JWT payload, the csrf token will be encrypted. 在将其存储在JWT有效负载中之前,csrf令牌将被加密。
  • After JWT token is encoded, it will be stored in cookies (HttpOnly) 对JWT令牌进行编码后,它将存储在cookie中(HttpOnly)
  • Evertime user request to access another page, the server will validate the csrf token in cookies dan JWT token when JWT token decoded. 每当用户请求访问另一个页面时,服务器将在解码JWT令牌时验证cookie和JWT令牌中的csrf令牌。
  • LocalStorage is not used because it is accessible through javascript 未使用LocalStorage,因为可以通过javascript访问

Generally there is no need to renew CSRF token at every request. 通常,不需要在每次请求时都更新CSRF令牌。

BUT let's see what happens in your setting: 但是,让我们看看您的设置会发生什么:

  • you store your JWT as well as CSRF token in cookie, 您将JWT和CSRF令牌存储在cookie中,
  • you visit malicious website that provoques a malicious request with malicious data to your site, 您访问的恶意网站会向您的网站发出带有恶意数据的恶意请求,
  • your browser attaches a cookie to this request with JWT+CSRF, 您的浏览器使用JWT + CSRF将Cookie附加到此请求,
  • your security is broken. 您的安全性受到了破坏。

So you must not put CSRF token in cookie because it is useless whether you renew it or not. 因此,您切勿将CSRF令牌放入cookie中,因为无论您是否续签它都没有用。

If you use "single page application" it would be better to pass JWT in Authorization header. 如果使用“单页应用程序”,最好在Authorization标头中传递JWT。 That makes CSRF-attack impossible (watch out anohter threats). 这使得CSRF攻击成为不可能(注意其他威胁)。

If you use "classical web application" it would be better to use "classical" CSRF tokens and "classical" session identifiers. 如果使用“经典Web应用程序”,则最好使用“经典” CSRF令牌和“经典”会话标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM