Skip to content

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

@kenjis

Description

@kenjis

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions