codeigniter4 / CodeIgniter4

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

Bug: route with "/" respond in any method [GET, POST, PUT, etc] #9217

Open nicojmb opened 2 weeks ago

nicojmb commented 2 weeks ago

PHP Version

8.3

CodeIgniter4 Version

4.5.5

CodeIgniter4 Installation Method

Manual (zip or tar.gz)

Which operating systems have you tested for this bug?

Linux

Which server did you use?

fpm-fcgi

Database

MariaDB

What happened?

Hi, I've these routes: image

In postman if i execute:

GET --> /api/devices --> Respond OK POST --> /api/devices --> Respond OK DELETE --> /api/devices --> Respond ERROR (it's normal not exists route)

image

but if i execute with and slash "/" at the end, all routes respond as if they were GET.

GET --> /api/devices/ --> Respond OK POST --> /api/devices/ --> Respond OK DELETE --> /api/devices/ --> Respond OK

image

and I can't find where the problem is

Steps to Reproduce

Make a postman call with adding a slash to the end

Expected Output

Same answer whether it has a slash or not

Anything else?

No response

ddevsr commented 4 hours ago

I cannot reproduce bug

$ git diff tests/system/Router/RouterTest.php
diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php
index bb5302e721..212294c3d2 100644
--- a/tests/system/Router/RouterTest.php
+++ b/tests/system/Router/RouterTest.php
@@ -716,6 +716,34 @@ final class RouterTest extends CIUnitTestCase
         $this->assertSame('auth_post', $router->methodName());
     }

+    public function testRouteEndSlash(): void
+    {
+        $this->collection->post('api/devices', 'Main::auth_post');
+        $this->collection->get('api/devices', 'Main::index');
+        $router = new Router($this->collection, $this->request);
+
+        $this->collection->setHTTPVerb(Method::POST);
+
+        $router->handle('api/devices');
+        $this->assertSame('\Main', $router->controllerName());
+        $this->assertSame('auth_post', $router->methodName());
+
+        $this->collection->setHTTPVerb(Method::GET);
+
+        $router->handle('api/devices');
+        $this->assertSame('\Main', $router->controllerName());
+        $this->assertSame('index', $router->methodName());
+
+        $this->collection->setHTTPVerb(Method::DELETE);
+
+        $router->handle('api/devices/');
+        $this->assertSame('\Main', $router->controllerName());
+        $this->assertSame('index', $router->methodName());
+    }
+
$ vendor/bin/phpunit tests/system/Router/RouterTest.php
PHPUnit 11.4.2 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.12 with Xdebug 3.3.2
Configuration: D:\Project\laragon\www\ci4\phpunit.xml.dist

........................................E...................                                                                                  60 / 60 (100%)

Time: 00:01.434, Memory: 20.00 MB

There was 1 error:

1) CodeIgniter\Router\RouterTest::testMultipleThree
CodeIgniter\Exceptions\PageNotFoundException: Can't find a route for 'DELETE: api/devices/'.

D:\Project\laragon\www\ci4\system\Router\Router.php:222
D:\Project\laragon\www\ci4\tests\system\Router\RouterTest.php:742

ERRORS!
Tests: 60, Assertions: 161, Errors: 1, PHPUnit Deprecations: 1.

Generating code coverage report in Clover XML format ... done [00:00.923]

Generating code coverage report in HTML format ... done [00:07.057]
ddevsr commented 4 hours ago

@nicojmb can you share configuration in app/Config/Routes?