【问题标题】:Set config item (csrf) doesnt work in Codeigniter设置配置项(csrf)在 Codeigniter 中不起作用
【发布时间】:2013-03-28 08:24:01
【问题描述】:

我只想在我的几个控制器中打开 csrf 保护,所以我有

function __construct() {

    parent::__construct();
    $this->load->library('form_validation');        
    $this->load->library('tank_auth');
    $this->load->helper(array('form', 'url'));
    $this->load->model('user_model', '', true);

    $this->config->set_item('csrf_protection', TRUE);

}

但它似乎不起作用,尽管当我在页面上执行 var_dump($this->config) 时它显示 csrf_protection 为 TRUE,但未设置 cookie 并且表单有一个没有值的隐藏字段

<input type="hidden" name="ci_csrf_token" value="" />

Csrf token name 和 cookie name 都设置好了,forms 用form_open() 调用。

任何帮助将不胜感激。

更新:所以这在 2.1.1 版本中是不可能的,因为安全类构造中的行 if (config_item('csrf_protection') === TRUE) {

Security类是在controller之前初始化的,所以controller里面的config项改变自然不会影响到它。

【问题讨论】:

  • 尝试在加载表单助手之前设置配置项?
  • 嗨!感谢您的快速回答,不幸的是,这并没有解决问题,我将 set_item 放在 parent::construct 之后
  • CSRF 保护是核心的一部分,并且在处理任何控制器之前就已初始化。如果您想执行此操作并且您正在运行 CI 2.x,您可能需要使用 application/core 中的 MY_Security.php 文件修改核心,并在其中提供您的逻辑以将 CSRF 应用于特定控制器。您想阅读this 并检查文件system/core/Security.php 如果您运行的是CI 3.x,它有一个新的配置项$config['csrf_exclude_uris'] = array();,它将满足您的需求。但它只存在于 CI 3.x 中
  • 嗨!感谢您的回答。问题是,我知道它应该起作用。在我的其他几个项目中,它运行良好,我将 csrf_protection 设置为 false,并在特定页面上使用 set_item 将其打开。我会暂时修改我的核心文件,直到找到可行的解决方案。好吧,没关系。我发现在我的另一个项目中,安全类构造没有if (config_item('csrf_protection') === TRUE)这行,它一定是在以后的版本中添加的。

标签: php codeigniter config csrf


【解决方案1】:

我有一个解决方案。创建一个自定义 application/core/MY_Security.php 并将其放入其中:

<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class MY_Security extends CI_Security
{
    public function csrf_verify( )
    {
        foreach ( config_item('csrf_excludes') as $exclude )
        {
            $uri = load_class('URI', 'core');
            if ( preg_match( $exclude, $uri->uri_string() ) > 0 )
            {
                // still do input filtering to prevent parameter piggybacking in the form
                if (isset($_COOKIE[$this->_csrf_cookie_name]) && preg_match( '#^[0-9a-f]{32}$#iS', $_COOKIE[$this->_csrf_cookie_name] ) == 0)
                {
                    unset( $_COOKIE[$this->_csrf_cookie_name] );
                }
                return;
            }
        }
        parent::csrf_verify( );
    }
}

这将检查您需要在 CSRF 部分中的 application/config.php 中添加的以下排除项:

$config['csrf_excludes'] = array
    ( '@^/?excluded_url_1/?@i'
    , '@^/?excluded_url_2/?@i' );

每个匹配的 URL 模式都将从 CSRF 检查中排除。您可以在http://rubular.com

处构建正则表达式

干杯

【讨论】:

  • 嗨!谢谢你的回答,我就是这么做的。我将 CI 3.0 的安全类复制到 MY_security 类,因为它已经可以使用 $config['csrf_exclude_uris'] 关闭特定 url 的 CSRF 保护,这是未来迁移到 CI 3.0 的更简单的解决方案。不过你的解决方案也不错,谢谢!
猜你喜欢
  • 2016-04-21
  • 1970-01-01
  • 2014-06-21
  • 2020-09-14
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多