Codeigniter CSRF 保护 VS 选项卡

Mal*_*ala 5 php codeigniter csrf

在新的 CodeIgniter v3 中,CSRF 令牌仅有效一次。结果,我在处理多个选项卡时遇到了一些麻烦:

  1. 使用 Form1 打开选项卡
  2. 使用 Form2 打开选项卡
  3. 使用表格 1 提交选项卡
  4. 使用表格 2 提交选项卡

第 4 步将导致 CSRF 错误。显然这并不理想...... onemeant 如何解决这个问题?

Sil*_*Fox 3

背景

无需在每次提交表单时重新生成 CSRF 令牌。安全方面的好处很少——如果攻击者可以从您的页面检索令牌,那么他们就已经获胜了。这将使您的网站能够无错误地运行交叉表。

有关安全方面的一些背景信息,请参阅此页面:为什么[您不应该]在每个表单请求中刷新 CSRF 令牌?

代码点火器 v3

v3 使用名为 的配置项csrf_regenerate。将其设置为FALSE可防止每次请求后重新生成。

代码点火器 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 ");
}
Run Code Online (Sandbox Code Playgroud)

只需从函数中删除以下代码:

// Re-generate CSRF Token and Cookie
unset($_COOKIE[$this->csrf_cookie_name]);
$this->_csrf_set_hash();
$this->csrf_set_cookie();
Run Code Online (Sandbox Code Playgroud)