繁体   English   中英

Codeigniter CSRF保护VS选项卡

[英]Codeigniter CSRF protection VS tabs

在新的CodeIgniter v3中,CSRF令牌仅有效一次。 因此,我在处理多个标签时遇到了一些问题:

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

步骤4将导致CSRF错误。 显然这不太理想......怎么解决这个问题呢?

背景

每次表单提交时都无需重新生成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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM