为什么生成的 CSRF 保护 token 没有像建议的那样通过 SESSION 保存和使用 here ?目前在CI2中,CSRF保护机制(Security类)是这样的:
1.在_csrf_set_hash()函数中为CSRF token生成一个唯一值:
$this->csrf_hash = md5(uniqid(rand(), TRUE));
2.将该标记插入表单隐藏字段(使用 form_open 帮助器)
3.用户提交表单,服务器通过 POST 获取 token 。 CI在Input类的“_sanitize_globals()”函数中进行token校验:
$this->security->csrf_verify();
4.Security类的函数“csrf_verify”只是检查是否设置了POST['token']并且POST['token']等于COOKIE['token'];
public function csrf_verify(){
// If no POST data exists we will set the CSRF cookie
if (count($_POST) == 0)
{
return $this->csrf_set_cookie();
}
// Do the tokens exist in both the _POST and _COOKIE arrays?
if ( ! isset($_POST[$this->_csrf_token_name]) OR
! isset($_COOKIE[$this->_csrf_cookie_name]))
{
$this->csrf_show_error();
}
// Do the tokens match?
if ($_POST[$this->_csrf_token_name] != $_COOKIE[$this->_csrf_cookie_name])
{
$this->csrf_show_error();
}
// We kill this since we're done and we don't want to
// polute the _POST array
unset($_POST[$this->_csrf_token_name]);
// Nothing should last forever
unset($_COOKIE[$this->_csrf_cookie_name]);
$this->_csrf_set_hash();
$this->csrf_set_cookie();
log_message('debug', "CSRF token verified ");
return $this;
}
为什么不在 session 中存储 token ?恕我直言,仅检查 POST['token'] 非空且等于 COOKIE['token'] 是不够的,因为两者都可能由恶意站点发送。
最佳答案
有几个原因。
首先是将 token 存储在 cookie 中并非不安全。反 CSRF 不是为了防止自动发布内容而设计的,它是为了防止伪造作为经过身份验证的用户的请求(通过 iframe 或简单的链接)。只要 token 本身不可猜测,就足够了。
第二个是如果它存储在 session 中,那么您需要启用 session ,这也会导致可用性问题,因为如果您的 session 超时并且您打开了一个包含表单的页面,您将无法再提交该表单(即使表单本身不需要登录状态)。
关于php - 为什么 codeigniter2 不以更安全的方式(例如 session )存储 csrf_hash?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8781059/