相关疑难解决方法(0)

javascript可以访问Django CSRF cookie吗?

在 django 网站上,https : //docs.djangoproject.com/en/dev/ref/contrib/csrf/它指出:

The CSRF protection is based on the following things:

1. A CSRF cookie that is set to a random value (a session independent nonce, as it is called), which other sites will not have access to.
2. ...
Run Code Online (Sandbox Code Playgroud)

然后,它还说明可以通过 javascript 从 cookie 中获取 csrf 令牌:

var csrftoken = $.cookie('csrftoken');
Run Code Online (Sandbox Code Playgroud)

这两个说法不矛盾吗?假设有一个跨域攻击,那么攻击者可以从cookie中获取CSRF令牌,然后在标头中使用CSRF令牌进行POST请求?有人可以解释一下吗?

更新

我现在意识到,只有来自同一来源的 javascript 才被允许访问 cookie。一个后续问题是:

如果 POST 请求自动将 cookie 作为请求的一部分添加,并且 django 的 csrf cookie 值与 csrf 令牌相同,那么恶意跨源请求仍然具有正确的 CSRF 令牌?(在饼干中)

django django-csrf

5
推荐指数
1
解决办法
2201
查看次数

标签 统计

django ×1

django-csrf ×1