From a25082b8a6a1ffafa6b63a700029a9d2ac3c0a48 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Wed, 11 Nov 2026 17:21:23 +0700 Subject: [PATCH 1/5] fix(Router): fix back-reference regex in replaceBackReferences() for 9+ placeholders --- system/Router/Router.php | 2 +- tests/system/Router/RouterTest.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/system/Router/Router.php b/system/Router/Router.php index 175935c34efd..b355b2052444 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -569,7 +569,7 @@ protected function checkRoutes(string $uri): bool */ private function replaceBackReferences(string $input, array $matches): string { - $pattern = '/\$([1-' . count($matches) . '])/u'; + $pattern = '/\$([1-9][0-9]*)/u'; return preg_replace_callback( $pattern, diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 591ac780cb7b..5cc8f27dff7a 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -1035,6 +1035,26 @@ public function testRoutePlaceholderAnyWithMultipleSegmentsParamTrue(): void $this->assertSame(['123/456'], $router->params()); } + public function testMultipleSegmentsParamTrueWithTenPlaceholders(): void + { + $routingConfig = new Routing(); + $routingConfig->multipleSegmentsOneParam = true; + Factories::injectMock('config', Routing::class, $routingConfig); + + $this->createRouteCollection($routingConfig); + $this->collection->get( + '(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)', + 'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9/$10', + ); + $router = new Router($this->collection, $this->request); + + $router->handle('a/b/c/d/e/f/g/h/i/j'); + + $this->assertSame('\Catalog', $router->controllerName()); + $this->assertSame('lookup', $router->methodName()); + $this->assertSame(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], $router->params()); + } + public function testLogsRouteAttributeInstantiationFailureWithContext(): void { $collection = clone $this->collection; From 82f112fe6f1f5c46606d713f806126e451331c59 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 12 Nov 2026 02:48:14 +0700 Subject: [PATCH 2/5] test(Router): cover 9-placeholder boundary and out-of-range back-references --- tests/system/Router/RouterTest.php | 51 +++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 5cc8f27dff7a..478f290cc03d 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -1035,24 +1035,59 @@ public function testRoutePlaceholderAnyWithMultipleSegmentsParamTrue(): void $this->assertSame(['123/456'], $router->params()); } - public function testMultipleSegmentsParamTrueWithTenPlaceholders(): void - { + /** + * @param list $expectedParams + */ + #[DataProvider('provideMultipleSegmentsParamTruePlaceholders')] + public function testMultipleSegmentsParamTrueWithManyPlaceholders( + string $route, + string $handler, + string $uri, + array $expectedParams, + ): void { $routingConfig = new Routing(); $routingConfig->multipleSegmentsOneParam = true; Factories::injectMock('config', Routing::class, $routingConfig); $this->createRouteCollection($routingConfig); - $this->collection->get( - '(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)/(:segment)', - 'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9/$10', - ); + $this->collection->get($route, $handler); $router = new Router($this->collection, $this->request); - $router->handle('a/b/c/d/e/f/g/h/i/j'); + $router->handle($uri); $this->assertSame('\Catalog', $router->controllerName()); $this->assertSame('lookup', $router->methodName()); - $this->assertSame(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], $router->params()); + $this->assertSame($expectedParams, $router->params()); + } + + /** + * @return iterable}> + */ + public static function provideMultipleSegmentsParamTruePlaceholders(): iterable + { + $nineSegments = implode('/', array_fill(0, 9, '(:segment)')); + $tenSegments = implode('/', array_fill(0, 10, '(:segment)')); + + yield 'nine placeholders (boundary)' => [ + $nineSegments, + 'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9', + 'a/b/c/d/e/f/g/h/i', + ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], + ]; + + yield 'ten placeholders' => [ + $tenSegments, + 'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9/$10', + 'a/b/c/d/e/f/g/h/i/j', + ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], + ]; + + yield 'out-of-range reference resolves to empty string' => [ + $tenSegments, + 'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9/$10/$11', + 'a/b/c/d/e/f/g/h/i/j', + ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ''], + ]; } public function testLogsRouteAttributeInstantiationFailureWithContext(): void From a088df41972f58d960bc7b332fe754d215ffa62c Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Thu, 12 Nov 2026 05:59:16 +0700 Subject: [PATCH 3/5] style(RouterTest): rename data provider to match PHP CS Fixer convention --- tests/system/Router/RouterTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 478f290cc03d..0284e752f4a8 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -1038,7 +1038,7 @@ public function testRoutePlaceholderAnyWithMultipleSegmentsParamTrue(): void /** * @param list $expectedParams */ - #[DataProvider('provideMultipleSegmentsParamTruePlaceholders')] + #[DataProvider('provideMultipleSegmentsParamTrueWithManyPlaceholders')] public function testMultipleSegmentsParamTrueWithManyPlaceholders( string $route, string $handler, @@ -1063,7 +1063,7 @@ public function testMultipleSegmentsParamTrueWithManyPlaceholders( /** * @return iterable}> */ - public static function provideMultipleSegmentsParamTruePlaceholders(): iterable + public static function provideMultipleSegmentsParamTrueWithManyPlaceholders(): iterable { $nineSegments = implode('/', array_fill(0, 9, '(:segment)')); $tenSegments = implode('/', array_fill(0, 10, '(:segment)')); From 86814acdb299aecf2f523f190024e8c64bcae822 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:16:30 +0700 Subject: [PATCH 4/5] docs(Router): document multi-digit back-references and add single-capture $10 test --- docs/incoming/routing.md | 3 +++ tests/system/Router/RouterTest.php | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/docs/incoming/routing.md b/docs/incoming/routing.md index 0df5bac7ef1f..6264122f5c0c 100644 --- a/docs/incoming/routing.md +++ b/docs/incoming/routing.md @@ -1021,6 +1021,9 @@ $routes->get('product/(:any)', 'Catalog::productLookup/$1'); will match **product/123**, **product/123/456**, **product/123/456/789** and so on. And if the URI is **product/123/456**, `123/456` will be passed to the first parameter of the `Catalog::productLookup()` method. +!!! note "Note" + When this option is enabled, consecutive digits in back-references (such as `$10`) are treated as a single reference rather than `$1` followed by `0`. If a back-reference does not have a matching captured group, it resolves to an empty string (`''`). + ## Auto Routing (Improved) !!! success "Available from version 4.2.0" diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 0284e752f4a8..60521dcd7ac0 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -1088,6 +1088,13 @@ public static function provideMultipleSegmentsParamTrueWithManyPlaceholders(): i 'a/b/c/d/e/f/g/h/i/j', ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ''], ]; + + yield 'single capture with out-of-range multi-digit reference' => [ + 'product/(:segment)', + 'Catalog::lookup/$10', + 'product/foo', + [''], + ]; } public function testLogsRouteAttributeInstantiationFailureWithContext(): void From c203b597237e1ee50d35c118e2cdfb4190bd7d3f Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Sun, 15 Nov 2026 12:46:03 +0700 Subject: [PATCH 5/5] fix(Router): preserve $0 reference, document behavior, and add changelog entry --- docs/changelogs/v4.7.7.md | 1 + docs/incoming/routing.md | 2 +- system/Router/Router.php | 2 +- tests/system/Router/RouterTest.php | 7 +++++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 22e3643629f3..47701eb06157 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -29,6 +29,7 @@ Release Date: Unreleased - **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. - **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names. - **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format. +- **Router:** Fixed a bug where `Router::replaceBackReferences()` failed to match and replace multi-digit back-references (such as `$10` or higher) when `Routing::$multipleSegmentsOneParam` was enabled. - **Routing:** Fixed a bug where redirect routes containing the `{locale}` placeholder were not recognized as redirects and did not replace `{locale}` in the redirect target. - **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests. - **Security:** Fixed a bug in `strip_image_tags()` where an invalid PCRE character class caused attribute injection and improper quote handling, and ensured tags without `src` are stripped. diff --git a/docs/incoming/routing.md b/docs/incoming/routing.md index 6264122f5c0c..970260a7564c 100644 --- a/docs/incoming/routing.md +++ b/docs/incoming/routing.md @@ -1022,7 +1022,7 @@ $routes->get('product/(:any)', 'Catalog::productLookup/$1'); will match **product/123**, **product/123/456**, **product/123/456/789** and so on. And if the URI is **product/123/456**, `123/456` will be passed to the first parameter of the `Catalog::productLookup()` method. !!! note "Note" - When this option is enabled, consecutive digits in back-references (such as `$10`) are treated as a single reference rather than `$1` followed by `0`. If a back-reference does not have a matching captured group, it resolves to an empty string (`''`). + When this option is enabled, consecutive digits in back-references (such as `$10`) are treated as a single reference rather than `$1` followed by `0`. The `$0` back-reference resolves to the entire matched path. If a back-reference does not have a matching captured group, it resolves to an empty string (`''`). ## Auto Routing (Improved) diff --git a/system/Router/Router.php b/system/Router/Router.php index b355b2052444..5eece587a75b 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -569,7 +569,7 @@ protected function checkRoutes(string $uri): bool */ private function replaceBackReferences(string $input, array $matches): string { - $pattern = '/\$([1-9][0-9]*)/u'; + $pattern = '/\$(0|[1-9][0-9]*)/u'; return preg_replace_callback( $pattern, diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 60521dcd7ac0..868e405d631e 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -1095,6 +1095,13 @@ public static function provideMultipleSegmentsParamTrueWithManyPlaceholders(): i 'product/foo', [''], ]; + + yield 'reference $0 resolves to entire matched path' => [ + $nineSegments, + 'Catalog::lookup/$0/$1', + 'a/b/c/d/e/f/g/h/i', + ['a/b/c/d/e/f/g/h/i', 'a'], + ]; } public function testLogsRouteAttributeInstantiationFailureWithContext(): void