Skip to content

Commit 3452438

Browse files
committed
fix(Router): fully decode URI before checking disallowed characters
A double-encoded URI could pass checkDisallowedChars() because the Router decoded it only once. Any component decoding it again could then introduce disallowed characters. Decode the URI until it stops changing before validation. The single-decoded path is still used for routing so that URI filter matching stays consistent.
1 parent df8b96b commit 3452438

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎system/Router/Router.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,14 @@ protected function setMatchedRoute(string $route, $handler): void
759759
*/
760760
private function checkDisallowedChars(string $uri): void
761761
{
762+
// Decode the URI until it no longer changes. A URI can be encoded
763+
// multiple times, and decoding it only once would allow a
764+
// double-encoded URI to bypass this check when it is decoded again
765+
// by another component.
766+
while (($decoded = urldecode($uri)) !== $uri) {
767+
$uri = $decoded;
768+
}
769+
762770
foreach (explode('/', $uri) as $segment) {
763771
if ($segment !== '' && $this->permittedURIChars !== ''
764772
&& preg_match('/\A[' . $this->permittedURIChars . ']+\z/iu', $segment) !== 1

‎tests/system/Router/RouterTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ public function testNotPermittedChars(): void
120120
$router->handle('test/%3Ca%3E');
121121
}
122122

123+
public function testDoubleEncodedNotPermittedChars(): void
124+
{
125+
$router = new Router($this->collection, $this->request);
126+
127+
$this->expectException(BadRequestException::class);
128+
$this->expectExceptionMessage('The URI you submitted has disallowed characters: "<a>"');
129+
130+
$router->handle('test/%253Ca%253E');
131+
}
132+
133+
public function testPermittedCharsWithPercentSign(): void
134+
{
135+
$this->collection->get('encoded/(:any)', 'News::view/$1');
136+
137+
$router = new Router($this->collection, $this->request);
138+
139+
$router->handle('encoded/100%25');
140+
141+
$this->assertSame('\News', $router->controllerName());
142+
$this->assertSame('view', $router->methodName());
143+
$this->assertSame(['100%'], $router->params());
144+
}
145+
123146
public function testURIMapsToController(): void
124147
{
125148
$router = new Router($this->collection, $this->request);

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Bugs Fixed
3838
is used instead. Values that cannot be converted to a string, such as objects
3939
not implementing ``Stringable``, now throw a ``FormatException`` instead of a
4040
fatal ``Error``.
41+
- **Router:** Fixed a bug where a double-encoded URI could bypass the
42+
disallowed character check when the URI was decoded again by another
43+
component.
4144
- **Security:** Fixed a bug where a valid JSON request body that is not an object
4245
(for example a JSON array) was corrupted after CSRF verification when the
4346
token was sent via the ``X-CSRF-TOKEN`` header.

‎user_guide_src/source/general/urls.rst‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ data can be passed to your application. URIs may only contain the following:
8686

8787
.. note::
8888
This check is performed by the ``Router``. The Router takes the URL-encoded
89-
value held by the ``SiteURI`` class, decodes it, and then checks that it
90-
does not contain not permitted strings.
89+
value held by the ``SiteURI`` class, decodes it repeatedly until it no
90+
longer changes, and then checks that it does not contain not permitted
91+
strings.
9192

9293
Adding Permitted Characters
9394
---------------------------

0 commit comments

Comments
 (0)