CSRFValidationException
security
auth_error
ai_generated
true
CSRF 令牌未绑定到用户会话
CSRF token is not bound to the user session
ID: security/csrf-token-not-bound-to-session
90%修复率
82%置信度
1证据数
2023-09-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Django 4.2 | active | — | — | — |
| Spring Security 5.7 | active | — | — | — |
| Flask-WTF 1.1 | active | — | — | — |
| Express.js | active | — | — | — |
根因分析
CSRF 令牌是全局生成或按请求生成的,但未与会话绑定,允许攻击者跨会话预测或重用令牌。
English
The CSRF token is generated globally or per-request but not tied to the session, allowing an attacker to predict or reuse a token across sessions.
官方文档
https://owasp.org/www-community/attacks/csrf解决方案
-
将会话中的 CSRF 令牌存储为每个会话的随机值。示例(Python Flask):`session['csrf_token'] = secrets.token_hex(32)` 并在视图中进行比较。
-
使用框架内置的 CSRF 保护,例如 Django 的 `{% csrf_token %}` 或 Spring Security 的 `CsrfTokenRepository` 配合 `HttpSessionCsrfTokenRepository`。 -
实现双重提交 Cookie:生成随机令牌,将其设置为 Cookie,并包含在隐藏的表单字段中;在服务器上验证两者是否匹配。
无效尝试
常见但无效的做法:
-
80% 失败
Using a static CSRF token for all users doesn't fix the issue; it makes the token easily guessable.
-
70% 失败
Only checking the token's presence, not its value, leaves the application vulnerable to token fixation.
-
60% 失败
Regenerating the token on every request without storing it in the session causes validation failures.