codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: CLI routing using session does not work after ver4.2.11 #7013

Closed lf-uraku-yuki closed 1 year ago

lf-uraku-yuki commented 1 year ago

PHP Version

8.0

CodeIgniter4 Version

4.2.11

CodeIgniter4 Installation Method

Composer (as dependency to an existing project)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

cli-server (PHP built-in webserver)

Database

MySQL 8.0

What happened?

After updating CodeIgniter v4.2.11, it is not possible to execute routing using sessions from the CLI.

Sessions are not normally used for CLI routing, but it was working fine until now, so I'll report it just in case.

CRITICAL - 2022-12-23 17:01:10 --> The arguments array must contain 4 items, 1 given
in SYSTEMPATH/HTTP/RequestTrait.php on line 123.
 1 SYSTEMPATH/HTTP/RequestTrait.php(123): vsprintf()
 2 SYSTEMPATH/Config/Services.php(658): CodeIgniter\HTTP\Request->getIPAddress()
 3 SYSTEMPATH/Config/BaseService.php(253): CodeIgniter\Config\Services::session()

Steps to Reproduce

Load the session in your controller.

$this->session = session();

Run from CLI

php public/index.php xxx

Expected Output

Up to v4.2.10 the process is executed normally.

Anything else?

No response

kenjis commented 1 year ago

Cannot reproduce.

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {
        session();
    }
}
$ php public/index.php 
$ 
lf-uraku-yuki commented 1 year ago

Even though I'm running from the CLI the logs are [ Session: Initialization under CLI aborted. ] It turned out that it does not become a message.

However, I can't find the additional steps to reproduce it, so if I find it and it's not a problem specific to my project, I'll share it again.

lf-uraku-yuki commented 1 year ago

I found the additional steps needed to reproduce. I also confirmed the reproduction in a newly created project by setting a reverse proxy IP with subnet for Config/App.php.

     public $proxyIPs = [
         '172.31.34.0/23' => 'X-Forwarded-For',
     ];

Also, for routing, enable automatic routing in advance.

Config/Routes.php $routes->setAutoRoute(true);

Load the session in your controller.

class Home extends BaseController
{
     public function index()
     {
         session();
         return "index test !!!!\r\n";
     }
}

Execute from CLI (In this example, it is executed in Windows environment, but it will occur in Linux environment as well)

> php public\index.php home index

[ErrorException]

explode(): Passing null to parameter #2 ($string) of type string is deprecated

at SYSTEMPATH\HTTP\RequestTrait.php:119

Backtrace:
   1 [internal functions]
        CodeIgniter\Debug\Exceptions()->errorHandler(8192, 'explode(): Passing null to parameter #2 ($string) of type string is deprecated', 'C:\\eclipse\\ci4test\\vendor\\codeigniter4 \\framework\\system\\HTTP\\RequestTrait.php', 119)

   2 SYSTEMPATH\HTTP\RequestTrait.php:119
        explode('.', null)
...
kenjis commented 1 year ago

Thank you. I've confirmed the error on develop.

diff --git a/app/Config/App.php b/app/Config/App.php
index c6eec07a6..94bf23595 100644
--- a/app/Config/App.php
+++ b/app/Config/App.php
@@ -346,7 +346,9 @@ class App extends BaseConfig
      *
      * @var array<string, string>
      */
-    public $proxyIPs = [];
+    public $proxyIPs = [
+        '172.31.34.0/23' => 'X-Forwarded-For',
+    ];

     /**
      * --------------------------------------------------------------------------
diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
index 7f867e95f..6eb577588 100644
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -6,6 +6,7 @@ class Home extends BaseController
 {
     public function index()
     {
-        return view('welcome_message');
+        session();
+        return "index test !!!!\r\n";
     }
 }
$ php public/index.php 

[ValueError]

The arguments array must contain 4 items, 1 given

at SYSTEMPATH/HTTP/RequestTrait.php:121