Skip to content

Commit 6ad9858

Browse files
authored
fix(Router): fix back-reference regex in replaceBackReferences() for 9+ placeholders (#10762)
1 parent 07b812e commit 6ad9858

4 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Release Date: Unreleased
3030
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
3131
- **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.
3232
- **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.
33+
- **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.
3334
- **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.
3435
- **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.
3536
- **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.

‎docs/incoming/routing.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,9 @@ $routes->get('product/(:any)', 'Catalog::productLookup/$1');
10211021

10221022
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.
10231023

1024+
!!! note "Note"
1025+
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 (`''`).
1026+
10241027
## Auto Routing (Improved)
10251028

10261029
!!! success "Available from version 4.2.0"

‎system/Router/Router.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ protected function checkRoutes(string $uri): bool
569569
*/
570570
private function replaceBackReferences(string $input, array $matches): string
571571
{
572-
$pattern = '/\$([1-' . count($matches) . '])/u';
572+
$pattern = '/\$(0|[1-9][0-9]*)/u';
573573

574574
return preg_replace_callback(
575575
$pattern,

‎tests/system/Router/RouterTest.php‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,75 @@ public function testRoutePlaceholderAnyWithMultipleSegmentsParamTrue(): void
10351035
$this->assertSame(['123/456'], $router->params());
10361036
}
10371037

1038+
/**
1039+
* @param list<string> $expectedParams
1040+
*/
1041+
#[DataProvider('provideMultipleSegmentsParamTrueWithManyPlaceholders')]
1042+
public function testMultipleSegmentsParamTrueWithManyPlaceholders(
1043+
string $route,
1044+
string $handler,
1045+
string $uri,
1046+
array $expectedParams,
1047+
): void {
1048+
$routingConfig = new Routing();
1049+
$routingConfig->multipleSegmentsOneParam = true;
1050+
Factories::injectMock('config', Routing::class, $routingConfig);
1051+
1052+
$this->createRouteCollection($routingConfig);
1053+
$this->collection->get($route, $handler);
1054+
$router = new Router($this->collection, $this->request);
1055+
1056+
$router->handle($uri);
1057+
1058+
$this->assertSame('\Catalog', $router->controllerName());
1059+
$this->assertSame('lookup', $router->methodName());
1060+
$this->assertSame($expectedParams, $router->params());
1061+
}
1062+
1063+
/**
1064+
* @return iterable<string, array{string, string, string, list<string>}>
1065+
*/
1066+
public static function provideMultipleSegmentsParamTrueWithManyPlaceholders(): iterable
1067+
{
1068+
$nineSegments = implode('/', array_fill(0, 9, '(:segment)'));
1069+
$tenSegments = implode('/', array_fill(0, 10, '(:segment)'));
1070+
1071+
yield 'nine placeholders (boundary)' => [
1072+
$nineSegments,
1073+
'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9',
1074+
'a/b/c/d/e/f/g/h/i',
1075+
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'],
1076+
];
1077+
1078+
yield 'ten placeholders' => [
1079+
$tenSegments,
1080+
'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9/$10',
1081+
'a/b/c/d/e/f/g/h/i/j',
1082+
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
1083+
];
1084+
1085+
yield 'out-of-range reference resolves to empty string' => [
1086+
$tenSegments,
1087+
'Catalog::lookup/$1/$2/$3/$4/$5/$6/$7/$8/$9/$10/$11',
1088+
'a/b/c/d/e/f/g/h/i/j',
1089+
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ''],
1090+
];
1091+
1092+
yield 'single capture with out-of-range multi-digit reference' => [
1093+
'product/(:segment)',
1094+
'Catalog::lookup/$10',
1095+
'product/foo',
1096+
[''],
1097+
];
1098+
1099+
yield 'reference $0 resolves to entire matched path' => [
1100+
$nineSegments,
1101+
'Catalog::lookup/$0/$1',
1102+
'a/b/c/d/e/f/g/h/i',
1103+
['a/b/c/d/e/f/g/h/i', 'a'],
1104+
];
1105+
}
1106+
10381107
public function testLogsRouteAttributeInstantiationFailureWithContext(): void
10391108
{
10401109
$collection = clone $this->collection;

0 commit comments

Comments
 (0)