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 0df5bac7ef1f..970260a7564c 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`. 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) !!! success "Available from version 4.2.0" diff --git a/system/Router/Router.php b/system/Router/Router.php index 175935c34efd..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-' . count($matches) . '])/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 591ac780cb7b..868e405d631e 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -1035,6 +1035,75 @@ public function testRoutePlaceholderAnyWithMultipleSegmentsParamTrue(): void $this->assertSame(['123/456'], $router->params()); } + /** + * @param list $expectedParams + */ + #[DataProvider('provideMultipleSegmentsParamTrueWithManyPlaceholders')] + 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($route, $handler); + $router = new Router($this->collection, $this->request); + + $router->handle($uri); + + $this->assertSame('\Catalog', $router->controllerName()); + $this->assertSame('lookup', $router->methodName()); + $this->assertSame($expectedParams, $router->params()); + } + + /** + * @return iterable}> + */ + public static function provideMultipleSegmentsParamTrueWithManyPlaceholders(): 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', ''], + ]; + + yield 'single capture with out-of-range multi-digit reference' => [ + 'product/(:segment)', + 'Catalog::lookup/$10', + '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 { $collection = clone $this->collection;