Skip to content

Commit eb6ac9f

Browse files
authored
fix: FeatureTestTrait::withRoutes() may throw all sorts of errors on invalid HTTP methods (#10004)
* add failing test * add fix and changelog
1 parent cfa7042 commit eb6ac9f

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

system/HTTP/Method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Method
102102
/**
103103
* Returns all HTTP methods.
104104
*
105-
* @return list<string>
105+
* @return list<uppercase-string>
106106
*/
107107
public static function all(): array
108108
{

system/Test/FeatureTestTrait.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Closure;
1717
use CodeIgniter\Events\Events;
18+
use CodeIgniter\Exceptions\RuntimeException;
1819
use CodeIgniter\HTTP\Exceptions\RedirectException;
1920
use CodeIgniter\HTTP\IncomingRequest;
2021
use CodeIgniter\HTTP\Method;
@@ -76,11 +77,16 @@ protected function withRoutes(?array $routes = null)
7677
);
7778
}
7879

79-
/**
80-
* @TODO For backward compatibility. Remove strtolower() in the future.
81-
* @deprecated 4.5.0
82-
*/
83-
$method = strtolower($route[0]);
80+
// @todo v4.7.1 Remove the strtoupper() and use 'add' in v4.8.0
81+
if (! in_array(strtoupper($route[0]), ['ADD', 'CLI', ...Method::all()], true)) {
82+
throw new RuntimeException(sprintf(
83+
'Invalid HTTP method "%s" provided for route "%s".',
84+
$route[0],
85+
$route[1],
86+
));
87+
}
88+
89+
$method = strtolower($route[0]); // convert to method of RouteCollection
8490

8591
if (isset($route[3])) {
8692
$collection->{$method}($route[1], $route[2], $route[3]);

tests/system/Test/FeatureTestTraitTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\Config\Factories;
1717
use CodeIgniter\Events\Events;
1818
use CodeIgniter\Exceptions\PageNotFoundException;
19+
use CodeIgniter\Exceptions\RuntimeException;
1920
use CodeIgniter\HTTP\Method;
2021
use CodeIgniter\HTTP\Response;
2122
use CodeIgniter\Test\Mock\MockCodeIgniter;
@@ -689,4 +690,33 @@ public function testForceGlobalSecureRequests(): void
689690
// Do not redirect.
690691
$response->assertStatus(200);
691692
}
693+
694+
#[DataProvider('provideWithRoutesWithInvalidMethod')]
695+
public function testWithRoutesWithInvalidMethod(string $method): void
696+
{
697+
$this->expectException(RuntimeException::class);
698+
$this->expectExceptionMessage(sprintf('Invalid HTTP method "%s" provided for route "home".', $method));
699+
700+
$this->withRoutes([
701+
[
702+
$method,
703+
'home',
704+
static fn (): string => 'Hello World',
705+
],
706+
]);
707+
}
708+
709+
/**
710+
* @return iterable<string, array{0: string}>
711+
*/
712+
public static function provideWithRoutesWithInvalidMethod(): iterable
713+
{
714+
foreach (['ADD', 'CLI', ...Method::all()] as $method) {
715+
yield "wrong {$method}" => [$method . 'S'];
716+
}
717+
718+
yield 'route collection addRedirect' => ['addRedirect'];
719+
720+
yield 'route collection setHTTPVerb' => ['setHTTPVerb'];
721+
}
692722
}

user_guide_src/source/changelogs/v4.7.1.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Bugs Fixed
5757
- **Session:** Fixed a bug in ``MemcachedHandler`` where the constructor incorrectly threw an exception when ``savePath`` was not empty.
5858
- **Toolbar:** Fixed a bug where the standalone toolbar page loaded from ``?debugbar_time=...`` was not interactive.
5959
- **Toolbar:** Fixed a bug in the Routes panel where only the first route parameter was converted to an input field on hover.
60+
- **Testing:** Fixed a bug in ``FeatureTestTrait::withRoutes()`` where invalid HTTP methods were not properly validated, thus passing them all to ``RouteCollection``.
6061
- **View:** Fixed a bug where ``View`` would throw an error if the ``appOverridesFolder`` config property was not defined.
6162

6263
See the repo's

0 commit comments

Comments
 (0)