8000 minor #59189 [Routing] Validate "namespace" (when using `Psr4Director… · symfony/symfony@30b8038 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30b8038

Browse files
committed
minor #59189 [Routing] Validate "namespace" (when using Psr4DirectoryLoader) (Kocal)
This PR was merged into the 7.3 branch. Discussion ---------- [Routing] Validate "namespace" (when using `Psr4DirectoryLoader`) | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> I spend a small amount of time to understand why my controller was inaccessible: <img width="496" alt="image" src="https://github.com/user-attachments/assets/959188fc-75d3-43db-a4e8-78fc4d140a74" /> With the following configuration: ```yaml controllers: resource: path: ../src/Application/Controller/ namespace: App\Application/Controller type: attribute ``` You may have seen that the namespace `App\Application/Controller` is invalid because the presence of `/` instead of `\`. If I correct it, then my controller is accessible. It means that invalid namespaces are simply ignored without any hints for the user, and I think it can be improved and be more user-friendly :) This PR add namespace validation, it checks if `/` is found, and if namespace is composed of valid PHP identifiers: <img width="759" alt="image" src="https://github.com/user-attachments/assets/ae8d1dc8-f250-4faf-bc07-22aaefc363fd" /> <img width="1011" alt="image" src="https://github.com/user-attachments/assets/7fe97b1e-77be-45ab-b875-b2a67b7501dd" /> Commits ------- 3d807c1 [Routing] Validate "namespace" (when using `Psr4DirectoryLoader`)
2 parents 95c43e3 + 3d807c1 commit 30b8038

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Symfony/Component/Routing/Loader/Psr4DirectoryLoader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\Loader\DirectoryAwareLoaderInterface;
1616
use Symfony\Component\Config\Loader\Loader;
1717
use Symfony\Component\Config\Resource\DirectoryResource;
18+
use Symfony\Component\Routing\Exception\InvalidArgumentException;
1819
use Symfony\Component\Routing\RouteCollection;
1920

2021
/**
@@ -43,6 +44,10 @@ public function load(mixed $resource, ?string $type = null): ?RouteCollection
4344
return new RouteCollection();
4445
}
4546

47+
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\)++$/', trim($resource['namespace'], '\\').'\\')) {
48+
throw new InvalidArgumentException(\sprintf('Namespace "%s" is not a valid PSR-4 prefix.', $resource['namespace']));
49+
}
50+
4651
return $this->loadFromDirectory($path, trim($resource['namespace'], '\\'));
4752
}
4853

src/Symfony/Component/Routing/Tests/Loader/Psr4DirectoryLoaderTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Config\FileLocator;
1616
use Symfony\Component\Config\Loader\DelegatingLoader;
1717
use Symfony\Component\Config\Loader\LoaderResolver;
18+
use Symfony\Component\Routing\Exception\InvalidArgumentException;
1819
use Symfony\Component\Routing\Loader\AttributeClassLoader;
1920
use Symfony\Component\Routing\Loader\Psr4DirectoryLoader;
2021
use Symfony\Component\Routing\Route;
@@ -90,6 +91,34 @@ public static function provideNamespacesThatNeedTrimming(): array
9091
];
9192
}
9293

94+
/**
95+
* @dataProvider provideInvalidPsr4Namespaces
96+
*/
97+
public function testInvalidPsr4Namespace(string $namespace, string $expectedExceptionMessage)
98+
{
99+
$this->expectException(InvalidArgumentException::class);
100+
$this->expectExceptionMessage($expectedExceptionMessage);
101+
102+
$this->getLoader()->load(
103+
['path' => 'Psr4Controllers', 'namespace' => $namespace],
104+
'attribute'
105+
);
106+
}
107+
108+
public static function provideInvalidPsr4Namespaces(): array
109+
{
110+
return [
111+
'slash instead of back-slash' => [
112+
'namespace' => 'App\Application/Controllers',
113+
'exceptionMessage' => 'Namespace "App\Application/Controllers" is not a valid PSR-4 prefix.',
114+
],
115+
'invalid namespace' => [
116+
'namespace' => 'App\Contro llers',
117+
'exceptionMessage' => 'Namespace "App\Contro llers" is not a valid PSR-4 prefix.',
118+
],
119+
];
120+
}
121+
93122
private function loadPsr4Controllers(): RouteCollection
94123
{
95124
return $this->getLoader()->load(

0 commit comments

Comments
 (0)
0