Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Filters are executed even when controller does not exist with Auto Routing (Legacy) #7205

Closed
kenjis opened this issue Feb 1, 2023 · 4 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@kenjis
Copy link
Member

kenjis commented Feb 1, 2023

PHP Version

8.1

CodeIgniter4 Version

4.3.1 and develop ea1b628

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli-server (PHP built-in webserver)

Database

n/a

What happened?

When enabling Auto Routing (Legacy), if we navigate to the URI that does not exist,
global filters are executed.

Steps to Reproduce

--- a/app/Config/Filters.php
+++ b/app/Config/Filters.php
@@ -31,7 +31,7 @@ class Filters extends BaseConfig
         'before' => [
             // 'honeypot',
             // 'csrf',
-            // 'invalidchars',
+            'invalidchars',
         ],
         'after' => [
             'toolbar',
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -19,7 +19,7 @@ $routes->set404Override();
 // where controller filters or CSRF protection are bypassed.
 // If you don't want to define all routes, please use the Auto Routing (Improved).
 // Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
-// $routes->setAutoRoute(false);
+$routes->setAutoRoute(true);
 
 /*
  * --------------------------------------------------------------------

Navigate to http://localhost:8080/xxx.

The stack trace:

InvalidChars.php:52, CodeIgniter\Filters\InvalidChars->before()
Filters.php:173, CodeIgniter\Filters\Filters->run()
CodeIgniter.php:473, CodeIgniter\CodeIgniter->handleRequest()
CodeIgniter.php:368, CodeIgniter\CodeIgniter->run()
index.php:67, require_once()
rewrite.php:47, {main}()

Expected Output

No filter is executed.

Anything else?

But if we navigate to http://localhost:8080/404, no filter is executed.
Because 404 is invalid as a classname, and throw an exception here:

if (! $this->isValidSegment($controllerName)) {
throw new PageNotFoundException($this->controller . ' is not a valid controller name');
}

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 1, 2023
@ping-yee
Copy link
Contributor

ping-yee commented Sep 9, 2023

May I ask a question that how can I get the stack trace log?

@kenjis
Copy link
Member Author

kenjis commented Sep 9, 2023

I got the stack trace with PhpStrom step debugging by Xdebug.
https://www.jetbrains.com/help/phpstorm/stepping-through-the-program.html

@neznaika0
Copy link
Contributor

Example of filter behavior for 404
https://forum.codeigniter.com/showthread.php?tid=86691
When a response is created via an exception, no filters are applied. It is ok. But it is not suitable for changing behavior for the API

@kenjis
Copy link
Member Author

kenjis commented Oct 29, 2023

Closed by #7925

@kenjis kenjis closed this as completed Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants