在新的CodeIgniter v3中,CSRF令牌只有效一次。因此,我在处理多个选项卡时遇到了一些困难:
步骤4将导致CSRF错误。很明显这并不理想..。如何才能解决这一问题?
发布于 2015-05-30 04:11:17
背景
没有必要在每次表单提交时重新生成CSRF令牌。没有什么安全好处--如果攻击者可以从您的页面中检索令牌,那么他们已经赢了。这将使您的站点能够运行跨选项卡,而不会出错。
有关安全性方面的一些背景信息,请参阅此页面:为什么[您不应该]刷新每个表单请求的CSRF令牌?。
CodeIgniter v3
v3使用名为csrf_regenerate
的配置项。将其设置为FALSE
,以防止在每个请求之后重新生成。
CodeIgniter v2
CodeIgniter使用的代码将在本文中讨论:CodeIgniter 2.0中的CSRF保护:更仔细的观察。有关守则如下:
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]);
// Re-generate CSRF Token and Cookie
unset($_COOKIE[$this->csrf_cookie_name]);
$this->_csrf_set_hash();
$this->csrf_set_cookie();
log_message('debug', "CSRF token verified ");
}
只需从函数中删除以下代码:
// Re-generate CSRF Token and Cookie
unset($_COOKIE[$this->csrf_cookie_name]);
$this->_csrf_set_hash();
$this->csrf_set_cookie();
https://stackoverflow.com/questions/30523435
复制