403
security
auth_error
ai_generated
true
AJAX请求头中缺少CSRF令牌
CSRF token missing in AJAX request header
ID: security/csrf-token-missing-in-ajax-request
90%修复率
85%置信度
1证据数
2023-11-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Django 4.2 | active | — | — | — |
| Spring Security 6.1 | active | — | — | — |
| Rails 7.0 | active | — | — | — |
根因分析
前端JavaScript代码未在AJAX请求的X-CSRF-Token头中包含CSRF令牌,导致服务端中间件以403状态拒绝请求。
English
The frontend JavaScript code does not include the CSRF token in the X-CSRF-Token header for AJAX requests, so the server-side middleware rejects the request with a 403 status.
官方文档
https://owasp.org/www-community/attacks/csrf解决方案
-
在前端,添加全局AJAX设置以包含CSRF令牌。对于jQuery:$.ajaxSetup({ headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') } }) -
确保包含CSRF令牌的meta标签在HTML模板中渲染,并且JavaScript代码可以访问它。
-
使用自动从cookie或meta标签添加CSRF头到所有请求的fetch包装器。
无效尝试
常见但无效的做法:
-
70% 失败
Disabling CSRF protection globally in the framework removes a critical security control, making the application vulnerable to CSRF attacks.
-
50% 失败
Only adding the token to form submissions but not to AJAX requests leaves the AJAX endpoints unprotected.
-
60% 失败
Placing the token in a query parameter instead of a header can leak it in server logs and browser history.