Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions docs/incoming/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
69 changes: 69 additions & 0 deletions tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,75 @@ public function testRoutePlaceholderAnyWithMultipleSegmentsParamTrue(): void
$this->assertSame(['123/456'], $router->params());
}

/**
* @param list<string> $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<string, array{string, string, string, list<string>}>
*/
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;
Expand Down
Loading