Codeigniter 3系统-启用CSRF保护导致致命错误CI_Controller not found

j13ufse2  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(167)

我正在开发一个基于CodeIgniter 3的系统,有人请求在登录和注册表单上启用CSRF。
在我的配置文件中,我做了:

$config['csrf_protection'] = FALSE;
if (isset($_SERVER["REQUEST_URI"])) {
    if (stripos($_SERVER["REQUEST_URI"], '/login') !== FALSE || stripos($_SERVER["REQUEST_URI"], '/register') !== FALSE ) {
        $config['csrf_protection'] = TRUE;
    }
}

字符串
但是当我试图将csrf_protection配置变量设置为true时,我的错误处理程序被触发,一个函数不存在。该函数确实存在于我的MY_url_helper中,错误是误导性的,因为它实际上抱怨这个函数没有首先找到,但实际的错误是CI_Controller应该没有找到,最初让我相信CodeIgniter示例没有被识别,即使回溯表明不是这样:

Array
(
    [0] => Array
        (
            [file] => path\to\system\core\Exceptions.php
            [line] => 268
            [function] => include
        )

    [1] => Array
        (
            [file] => path\to\system\core\Common.php
            [line] => 627
            [function] => show_php_error
            [class] => CI_Exceptions
            [object] => CI_Exceptions Object
                (
                    [ob_level] => 1
                    [levels] => Array
                        (
                            [1] => Error
                            [2] => Warning
                            [4] => Parsing Error
                            [8] => Notice
                            [16] => Core Error
                            [32] => Core Warning
                            [64] => Compile Error
                            [128] => Compile Warning
                            [256] => User Error
                            [512] => User Warning
                            [1024] => User Notice
                            [2048] => Runtime Notice
                        )

                )

            [type] => ->
            [args] => Array
                (
                    [0] => Error
                    [1] => Uncaught Error: Class "CI_Controller" not found in path\to\system\core\CodeIgniter.php:369
Stack trace:
#0 path\to\app\core\MY_Exceptions.php(54): get_instance()
#1 path\to\system\core\Common.php(195): MY_Exceptions->__construct()
#2 path\to\system\core\Common.php(656): load_class('Exceptions', 'core')
#3 [internal function]: _exception_handler(Object(Error))
#4 {main}
thrown
                    [2] => core/CodeIgniter.php
                    [3] => 369
                )

        )

    [2] => Array
        (
            [file] => path\to\system\core\Common.php
            [line] => 693
            [function] => _error_handler
            [args] => Array
                (
                    [0] => 1
                    [1] => Uncaught Error: Class "CI_Controller" not found in path\to\system\core\CodeIgniter.php:369
Stack trace:
#0 path\to\app\core\MY_Exceptions.php(54): get_instance()
#1 path\to\system\core\Common.php(195): MY_Exceptions->__construct()
#2 path\to\system\core\Common.php(656): load_class('Exceptions', 'core')
#3 [internal function]: _exception_handler(Object(Error))
#4 {main}
thrown
                    [2] => path\to\system\core\CodeIgniter.php
                    [3] => 369
                )

        )

    [3] => Array
        (
            [function] => _shutdown_handler
            [args] => Array
                (
                )

        )

)


我很困惑,如果从我所看到的所有迹象来看,CodeIgniter已经加载,它是如何不工作的。代码库扩展了CI_Igniter类,下面是我现在测试的构造函数:

class MY_Exceptions extends CI_Exceptions
{
    public function __construct() {
        parent::__construct();
        die('Error is showing');
        $this->CI =& get_instance();
        die('Error not showing');
    }
}


不知何故,当示例被分配时,一切都变成了梨形。
这个问题:Error on activating CSRF protection提供了一些看起来相同的信息,但接受的答案并不是我可以实现的答案。
将我的CI错误日志记录设置为级别4(所有消息),我会看到:

INFO - 2023-10-06 13:47:57 --> Config Class Initialized
INFO - 2023-10-06 13:47:57 --> Hooks Class Initialized
DEBUG - 2023-10-06 13:47:57 --> UTF-8 Support Enabled
INFO - 2023-10-06 13:47:57 --> Utf8 Class Initialized
INFO - 2023-10-06 13:47:57 --> URI Class Initialized
INFO - 2023-10-06 13:47:57 --> Router Class Initialized
INFO - 2023-10-06 13:47:57 --> Output Class Initialized
INFO - 2023-10-06 13:47:57 --> Security Class Initialized
DEBUG - 2023-10-06 13:47:57 --> Global POST, GET and COOKIE data sanitized


日志中没有任何错误。
有什么想法吗?

zwghvu4y

zwghvu4y1#

我终于找到了一个非常不优雅的解决方案(也违背了最佳实践,但由于缺乏更好的解决方案,这将不得不做)。
看起来CodeIgniter 3 CSRF/Security实现在一个阶段调用了get_instance()过程函数,然后它需要完成请求的类被初始化。它似乎发生在某种循环中,因为它只在第一次失败,然后所需的类似乎被初始化,第二次起作用了。这是我能想到的最好的解释--可能完全不靠谱,但我的修复是有效的。

注::我知道不要改变系统的核心文件,我也理解它的逻辑,但这不是一个类。它是过程代码,所以不能扩展它。然后我试图调整和扩展引用的类,但很快就变得混乱真实的-所以如果有人有更好的解决方案,我愿意接受建议。

system\core\CodeIgniter.php是这样的:

function &get_instance()
{
    return CI_Controller::get_instance();
}

字符串
为此:

function &get_instance()
{
    if (class_exists('CI_Controller')) {
        return CI_Controller::get_instance();
    }
}


解决了我的问题,没有任何进一步的问题,CSRF按预期工作。看起来好像第一次调用这个get_instance()方法时,类还没有初始化。回溯没有给给予我足够的信息来跟踪这实际发生的地方。

相关问题