Skip to content

Commit 6c637ed

Browse files
committed
refactor(Router): remove inappropriate input transformation from RouteCollection identifiers
1 parent 680873b commit 6c637ed

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

‎system/Router/RouteCollection.php‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,22 +404,21 @@ public function getPlaceholders(): array
404404

405405
public function setDefaultNamespace(string $value): RouteCollectionInterface
406406
{
407-
$this->defaultNamespace = esc(strip_tags($value));
408-
$this->defaultNamespace = rtrim($this->defaultNamespace, '\\') . '\\';
407+
$this->defaultNamespace = rtrim($value, '\\') . '\\';
409408

410409
return $this;
411410
}
412411

413412
public function setDefaultController(string $value): RouteCollectionInterface
414413
{
415-
$this->defaultController = esc(strip_tags($value));
414+
$this->defaultController = $value;
416415

417416
return $this;
418417
}
419418

420419
public function setDefaultMethod(string $value): RouteCollectionInterface
421420
{
422-
$this->defaultMethod = esc(strip_tags($value));
421+
$this->defaultMethod = $value;
423422

424423
return $this;
425424
}
@@ -754,7 +753,7 @@ public function resource(string $name, ?array $options = null): RouteCollectionI
754753
// If a new controller is specified, then we replace the
755754
// $name value with the name of the new controller.
756755
if (isset($options['controller'])) {
757-
$newName = ucfirst(esc(strip_tags($options['controller'])));
756+
$newName = ucfirst($options['controller']);
758757
}
759758

760759
// In order to allow customization of allowed id values
@@ -848,7 +847,7 @@ public function presenter(string $name, ?array $options = null): RouteCollection
848847
// If a new controller is specified, then we replace the
849848
// $name value with the name of the new controller.
850849
if (isset($options['controller'])) {
851-
$newName = ucfirst(esc(strip_tags($options['controller'])));
850+
$newName = ucfirst($options['controller']);
852851
}
853852

854853
// In order to allow customization of allowed id values

‎tests/system/Router/RouteCollectionTest.php‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,14 @@ public function testSetDefaultControllerStoresIt(): void
272272
$this->assertSame('godzilla', $routes->getDefaultController());
273273
}
274274

275+
public function testSetDefaultControllerPreservesSpecialCharacters(): void
276+
{
277+
$routes = $this->getCollector();
278+
$routes->setDefaultController('Foo&Bar');
279+
280+
$this->assertSame('Foo&Bar', $routes->getDefaultController());
281+
}
282+
275283
public function testSetDefaultMethodStoresIt(): void
276284
{
277285
$routes = $this->getCollector();
@@ -280,6 +288,22 @@ public function testSetDefaultMethodStoresIt(): void
280288
$this->assertSame('biggerBox', $routes->getDefaultMethod());
281289
}
282290

291+
public function testSetDefaultMethodPreservesSpecialCharacters(): void
292+
{
293+
$routes = $this->getCollector();
294+
$routes->setDefaultMethod('get&set');
295+
296+
$this->assertSame('get&set', $routes->getDefaultMethod());
297+
}
298+
299+
public function testSetDefaultNamespacePreservesSpecialCharacters(): void
300+
{
301+
$routes = $this->getCollector();
302+
$routes->setDefaultNamespace('App\Controllers&Services');
303+
304+
$this->assertSame('App\Controllers&Services\\', $routes->getDefaultNamespace());
305+
}
306+
283307
public function testTranslateURIDashesWorks(): void
284308
{
285309
$routes = $this->getCollector();
@@ -732,7 +756,7 @@ public function testResourcesWithCustomController(): void
732756
service('request')->setMethod(Method::GET);
733757
$routes = $this->getCollector();
734758

735-
$routes->resource('photos', ['controller' => '<script>gallery']);
759+
$routes->resource('photos', ['controller' => 'gallery']);
736760

737761
$expected = [
738762
'photos' => '\Gallery::index',
@@ -744,6 +768,25 @@ public function testResourcesWithCustomController(): void
744768
$this->assertSame($expected, $routes->getRoutes());
745769
}
746770

771+
public function testPresenterWithCustomController(): void
772+
{
773+
service('request')->setMethod(Method::GET);
774+
$routes = $this->getCollector();
775+
776+
$routes->presenter('photos', ['controller' => 'gallery']);
777+
778+
$expected = [
779+
'photos' => '\Gallery::index',
780+
'photos/show/(.*)' => '\Gallery::show/$1',
781+
'photos/new' => '\Gallery::new',
782+
'photos/edit/(.*)' => '\Gallery::edit/$1',
783+
'photos/remove/(.*)' => '\Gallery::remove/$1',
784+
'photos/(.*)' => '\Gallery::show/$1',
785+
];
786+
787+
$this->assertSame($expected, $routes->getRoutes());
788+
}
789+
747790
public function testResourcesWithCustomPlaceholder(): void
748791
{
749792
service('request')->setMethod(Method::GET);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ Changes
385385
*******
386386

387387
- **Config:** Added the ``md`` key for ``Config\Mimes::$mimes`` for Markdown files.
388+
- **Router:** Removed inappropriate ``esc(strip_tags())`` input transformations on controller and method identifiers in ``RouteCollection``.
388389

389390
************
390391
Deprecations

0 commit comments

Comments
 (0)