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

j13ufse2  于 2024-01-04  发布在  其他
关注(0)|答案(1)|浏览(279)

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

  1. $config['csrf_protection'] = FALSE;
  2. if (isset($_SERVER["REQUEST_URI"])) {
  3. if (stripos($_SERVER["REQUEST_URI"], '/login') !== FALSE || stripos($_SERVER["REQUEST_URI"], '/register') !== FALSE ) {
  4. $config['csrf_protection'] = TRUE;
  5. }
  6. }

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

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [file] => path\to\system\core\Exceptions.php
  6. [line] => 268
  7. [function] => include
  8. )
  9. [1] => Array
  10. (
  11. [file] => path\to\system\core\Common.php
  12. [line] => 627
  13. [function] => show_php_error
  14. [class] => CI_Exceptions
  15. [object] => CI_Exceptions Object
  16. (
  17. [ob_level] => 1
  18. [levels] => Array
  19. (
  20. [1] => Error
  21. [2] => Warning
  22. [4] => Parsing Error
  23. [8] => Notice
  24. [16] => Core Error
  25. [32] => Core Warning
  26. [64] => Compile Error
  27. [128] => Compile Warning
  28. [256] => User Error
  29. [512] => User Warning
  30. [1024] => User Notice
  31. [2048] => Runtime Notice
  32. )
  33. )
  34. [type] => ->
  35. [args] => Array
  36. (
  37. [0] => Error
  38. [1] => Uncaught Error: Class "CI_Controller" not found in path\to\system\core\CodeIgniter.php:369
  39. Stack trace:
  40. #0 path\to\app\core\MY_Exceptions.php(54): get_instance()
  41. #1 path\to\system\core\Common.php(195): MY_Exceptions->__construct()
  42. #2 path\to\system\core\Common.php(656): load_class('Exceptions', 'core')
  43. #3 [internal function]: _exception_handler(Object(Error))
  44. #4 {main}
  45. thrown
  46. [2] => core/CodeIgniter.php
  47. [3] => 369
  48. )
  49. )
  50. [2] => Array
  51. (
  52. [file] => path\to\system\core\Common.php
  53. [line] => 693
  54. [function] => _error_handler
  55. [args] => Array
  56. (
  57. [0] => 1
  58. [1] => Uncaught Error: Class "CI_Controller" not found in path\to\system\core\CodeIgniter.php:369
  59. Stack trace:
  60. #0 path\to\app\core\MY_Exceptions.php(54): get_instance()
  61. #1 path\to\system\core\Common.php(195): MY_Exceptions->__construct()
  62. #2 path\to\system\core\Common.php(656): load_class('Exceptions', 'core')
  63. #3 [internal function]: _exception_handler(Object(Error))
  64. #4 {main}
  65. thrown
  66. [2] => path\to\system\core\CodeIgniter.php
  67. [3] => 369
  68. )
  69. )
  70. [3] => Array
  71. (
  72. [function] => _shutdown_handler
  73. [args] => Array
  74. (
  75. )
  76. )
  77. )


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

  1. class MY_Exceptions extends CI_Exceptions
  2. {
  3. public function __construct() {
  4. parent::__construct();
  5. die('Error is showing');
  6. $this->CI =& get_instance();
  7. die('Error not showing');
  8. }
  9. }


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

  1. INFO - 2023-10-06 13:47:57 --> Config Class Initialized
  2. INFO - 2023-10-06 13:47:57 --> Hooks Class Initialized
  3. DEBUG - 2023-10-06 13:47:57 --> UTF-8 Support Enabled
  4. INFO - 2023-10-06 13:47:57 --> Utf8 Class Initialized
  5. INFO - 2023-10-06 13:47:57 --> URI Class Initialized
  6. INFO - 2023-10-06 13:47:57 --> Router Class Initialized
  7. INFO - 2023-10-06 13:47:57 --> Output Class Initialized
  8. INFO - 2023-10-06 13:47:57 --> Security Class Initialized
  9. 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是这样的:

  1. function &get_instance()
  2. {
  3. return CI_Controller::get_instance();
  4. }

字符串
为此:

  1. function &get_instance()
  2. {
  3. if (class_exists('CI_Controller')) {
  4. return CI_Controller::get_instance();
  5. }
  6. }


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

展开查看全部

相关问题