CSRF 保护:我们是否必须为每个表单生成一个令牌?

发布于 2024-12-23 02:25:25 字数 62 浏览 1 评论 0原文

我们是否必须为网站中的每个表单生成一个令牌?我的意思是,每次为每个请求的表单生成不同的令牌?如果没有,为什么?

Do we have to generate a token, for every form in a website? I mean, every-time to generate different token for every requested form? If not, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2024-12-30 02:25:25

一般来说,每个会话一个令牌就足够了,所谓的每个会话令牌

一般来说,开发者只需为当前会话生成一次此令牌。初始生成此令牌后,该值将存储在会话中,并用于每个后续请求,直到会话过期。

如果您想进一步增强安全性,您可以为每个表单/URL 使用一个令牌(每个表单令牌),以减轻一个令牌泄漏时的影响(例如XSS),因为攻击者只能成功攻击该特定表格/URL。

但是使用每个请求令牌,即。随着每个请求而变化的令牌,反而会降低网站的可用性,因为它限制了并行浏览:

为了进一步增强此提议设计的安全性,请考虑为每个请求随机化 CSRF 令牌 [...]。实施此方法会生成每个请求令牌,而不是每个会话令牌。但请注意,这可能会导致可用性问题。例如,“后退”按钮浏览器功能通常会受到阻碍,因为前一页可能包含不再有效的令牌。与上一页的交互将导致服务器上发生 CSRF 误报安全事件。

因此,我建议您使用每会话令牌或每表单令牌。

In general, it suffices to have just one token per session, a so called per-session token:

In general, developers need only generate this token once for the current session. After initial generation of this token, the value is stored in the session and is utilized for each subsequent request until the session expires.

If you want to further enhance the security, you can use one token per each form/URL (per-form token) to mitigate the impact when one token leaks (e. g. XSS) as an attacker would only be able to successfully attack that specific form/URL.

But using per-request tokens, i. e. tokens that change with each request, rather cuts the usability of the website as it restricts parallel browsing:

To further enhance the security of this proposed design, consider randomizing the CSRF token […] for each request. Implementing this approach results in the generation of per-request tokens as opposed to per-session tokens. Note, however, that this may result in usability concerns. For example, the "Back" button browser capability is often hindered as the previous page may contain a token that is no longer valid. Interaction with this previous page will result in a CSRF false positive security event at the server.

So I recommend you to use either per-session tokens or per-form tokens.

千纸鹤带着心事 2024-12-30 02:25:25

不,您只需要在每个-上生成一个令牌会话基础

令牌不太可能被用户意外泄露,并且如果用户同时在两个不同的选项卡/窗口中浏览网站,则每个表单生成令牌会使事情变得非常复杂。

No, you just need to generate a token on a per-session basis.

Tokens are very unlikely to be leaked accidentally by users and generating a token per form makes things very complicated if a user is browsing the site in two different tabs/windows at once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文