【问题标题】:Codeigniter CSRF protection VS tabsCodeigniter CSRF 保护 VS 选项卡
【发布时间】:2015-08-11 22:58:46
【问题描述】:

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

  1. 使用 Form1 打开一个选项卡
  2. 使用 Form2 打开标签页
  3. 使用表格 1 提交标签
  4. 使用表格 2 提交选项卡

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

【问题讨论】:

  • 查看link
  • CSRF tokens 假设对一个请求有效
  • @saty:该链接与此问题无关。
  • @Saqueib:确实——那么如何解决这个问题呢?
  • 请输入您的代码....

标签: php codeigniter csrf


【解决方案1】:

背景

无需在每次提交表单时重新生成 CSRF 令牌。几乎没有安全优势 - 如果攻击者可以从您的页面检索令牌,那么他们已经获胜。这将使您的网站能够无错误地运行交叉选项卡。

有关安全方面的一些背景信息,请参阅此页面:Why [you shouldn't] refresh CSRF token per form request?

CodeIgniter v3

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

CodeIgniter v2

这篇文章讨论了 CodeIgniter 使用的代码:CSRF Protection in CodeIgniter 2.0: A closer look。相关代码如下:

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();

【讨论】:

  • 这篇文章来自 2010 年,与 CodeIgniter 2.0 相关,它与 CodeIgniter 3 中的代码有很大不同。在查看 CI3 中的等效代码时,我看到似乎有一个配置选项“ csrf_regenerate”,它可以防止重新生成。感谢您提供背景信息链接
猜你喜欢
  • 1970-01-01
  • 2013-09-20
  • 2012-02-03
  • 2013-07-09
  • 2011-10-23
  • 2017-06-14
  • 2023-03-08
  • 2013-02-13
  • 2016-04-25
相关资源
最近更新 更多