From 0d23de43401f0ca86697ec8ff4210ba823fd00a0 Mon Sep 17 00:00:00 2001 From: MGatner Date: Thu, 16 May 2019 15:25:18 -0400 Subject: [PATCH 1/5] Use request->method for HTTP verb --- system/Router/RouteCollection.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 34ea10d4da60..c1a933e3037f 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -39,6 +39,7 @@ namespace CodeIgniter\Router; +use Config\Services; use CodeIgniter\Autoloader\FileLocator; use CodeIgniter\Router\Exceptions\RouterException; @@ -234,7 +235,7 @@ class RouteCollection implements RouteCollectionInterface public function __construct(FileLocator $locator, $moduleConfig) { // Get HTTP verb - $this->HTTPVerb = strtolower($_SERVER['REQUEST_METHOD'] ?? 'cli'); + $this->HTTPVerb = is_cli() ? 'cli' : Services::request()->getMethod(); $this->fileLocator = $locator; @@ -1115,12 +1116,6 @@ public function reverseRoute(string $search, ...$params) { $from = key($route['route']); $to = $route['route'][$from]; - - // ignore closures - if (! is_string($to)) - { - continue; - } // Lose any namespace slash at beginning of strings // to ensure more consistent match. From 12c32bab8611105040b5a109b68fc0aeae4e6704 Mon Sep 17 00:00:00 2001 From: MGatner Date: Fri, 17 May 2019 11:43:28 -0400 Subject: [PATCH 2/5] Remove CLI redundancy; restore Closure check --- system/Router/RouteCollection.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index c1a933e3037f..6654077f3858 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -234,8 +234,8 @@ class RouteCollection implements RouteCollectionInterface */ public function __construct(FileLocator $locator, $moduleConfig) { - // Get HTTP verb - $this->HTTPVerb = is_cli() ? 'cli' : Services::request()->getMethod(); + // Get HTTP verb from current request (accounts for spoofing) + $this->HTTPVerb = Services::request()->getMethod(); $this->fileLocator = $locator; @@ -1116,6 +1116,12 @@ public function reverseRoute(string $search, ...$params) { $from = key($route['route']); $to = $route['route'][$from]; + + // ignore closures + if (! is_string($to)) + { + continue; + } // Lose any namespace slash at beginning of strings // to ensure more consistent match. From d5c90a1204344efd5cfbcb89f66cbfee12e93920 Mon Sep 17 00:00:00 2001 From: MGatner Date: Fri, 17 May 2019 11:47:33 -0400 Subject: [PATCH 3/5] Set request method on CLI --- system/CodeIgniter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index e518db4409e1..ca41cb228e3f 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -986,12 +986,13 @@ public function storePreviousURL($uri) * Modifies the Request Object to use a different method if a POST * variable called _method is found. * - * Does not work on CLI commands. */ public function spoofRequestMethod() { + // CLI commands always use 'cli' method if (is_cli()) { + $this->request->setMethod('cli'); return; } From 62acd0729a01faed7d1a2b6c791a8aeebe5cf9b0 Mon Sep 17 00:00:00 2001 From: MGatner Date: Fri, 17 May 2019 11:53:55 -0400 Subject: [PATCH 4/5] Use `$request->setMethod` instead of `$_SERVER` --- tests/system/Router/RouteCollectionTest.php | 92 ++++++++++----------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index f8ea52988ef2..ae3403b9abf3 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -90,7 +90,7 @@ public function testAddIgnoresDefaultNamespaceWhenExists() public function testAddWorksWithCurrentHTTPMethods() { - $_SERVER['REQUEST_METHOD'] = 'GET'; + Services::request()->setMethod('get'); $routes = $this->getCollector(); @@ -126,7 +126,7 @@ public function testAddWithLeadingSlash() public function testMatchIgnoresInvalidHTTPMethods() { - $_SERVER['REQUEST_METHOD'] = 'GET'; + Services::request()->setMethod('get'); $routes = $this->getCollector(); @@ -141,7 +141,7 @@ public function testMatchIgnoresInvalidHTTPMethods() public function testAddWorksWithArrayOFHTTPMethods() { - $_SERVER['REQUEST_METHOD'] = 'POST'; + Services::request()->setMethod('post'); $routes = $this->getCollector(); @@ -328,7 +328,7 @@ public function testHostnameOption() public function testResourcesScaffoldsCorrectly() { - $_SERVER['REQUEST_METHOD'] = 'GET'; + Services::request()->setMethod('get'); $routes = $this->getCollector(); $routes->resource('photos'); @@ -342,7 +342,7 @@ public function testResourcesScaffoldsCorrectly() $this->assertEquals($expected, $routes->getRoutes()); - $_SERVER['REQUEST_METHOD'] = 'POST'; + Services::request()->setMethod('post'); $routes = $this->getCollector(); $routes->resource('photos'); @@ -352,7 +352,7 @@ public function testResourcesScaffoldsCorrectly() $this->assertEquals($expected, $routes->getRoutes()); - $_SERVER['REQUEST_METHOD'] = 'PUT'; + Services::request()->setMethod('put'); $routes = $this->getCollector(); $routes->resource('photos'); @@ -362,7 +362,7 @@ public function testResourcesScaffoldsCorrectly() $this->assertEquals($expected, $routes->getRoutes()); - $_SERVER['REQUEST_METHOD'] = 'PATCH'; + Services::request()->setMethod('patch'); $routes = $this->getCollector(); $routes->resource('photos'); @@ -372,7 +372,7 @@ public function testResourcesScaffoldsCorrectly() $this->assertEquals($expected, $routes->getRoutes()); - $_SERVER['REQUEST_METHOD'] = 'DELETE'; + Services::request()->setMethod('delete'); $routes = $this->getCollector(); $routes->resource('photos'); @@ -387,7 +387,7 @@ public function testResourcesScaffoldsCorrectly() public function testResourcesWithCustomController() { - $_SERVER['REQUEST_METHOD'] = 'GET'; + Services::request()->setMethod('get'); $routes = $this->getCollector(); $routes->resource('photos', ['controller' => '