设置配置项(csrf)不工作在Codeigniter [英] Set config item (csrf) doesnt work in Codeigniter

查看:375
本文介绍了设置配置项(csrf)不工作在Codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开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,表单有一个没有值的隐藏字段。



code>< input type =hiddenname =ci_csrf_tokenvalue =/>



<和cookie名称都已设置,表单将使用form_open()调用。



任何帮助将非常感激。



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



安全类在控制器之前初始化,因此控制器中的配置项更改不会影响它。

解决方案

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

 <?php if(!defined BASEPATH'))exit('不允许直接脚本访问); 

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)
{
//仍然做输入过滤以防止参数捎带形式
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();
}
}

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

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

每个匹配的URL模式将从CSRF检查中排除。您可以在 http://rubular.com 构建正则表达式



欢呼


I want to turn ON csrf protection only in a few of my controllers, so I have

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

}

But it doesn't seem to work, although when I do var_dump($this->config) on a page it shows that csrf_protection is TRUE, but the cookies are not set and the form has a hidden field without value

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

Csrf token name and cookie name are all set, the forms are called with form_open().

Any help would be much appreciated.

Update: So this is not possible from version 2.1.1 because of the line in security class construct if (config_item('csrf_protection') === TRUE) {

Security class is initialized before the controller, so its natural that the config item change in the controller will not affect it.

解决方案

I have a solution for you. Create a custom application/core/MY_Security.php and put this in it:

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

This will check the following excludes which you need to put in your application/config.php in the CSRF section:

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

Every matching URL pattern will be excluded from CSRF checks. You can build regex here at http://rubular.com

cheers

这篇关于设置配置项(csrf)不工作在Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆