-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Routing] Validate "namespace" (when using Psr4DirectoryLoader
)
#59189
[Routing] Validate "namespace" (when using Psr4DirectoryLoader
)
#59189
Conversation
fe08401
to
b7d77a7
Compare
Failing checks are unrelated, they should be fixed by #59182: |
@@ -43,6 +43,8 @@ public function load(mixed $resource, ?string $type = null): ?RouteCollection | |||
return new RouteCollection(); | |||
} | |||
|
|||
$this->validateNamespace($resource['namespace']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
borrowed from YamlFileLoader in DI:
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $resource['namespace'])) {
throw new InvalidArgumentException(\sprintf('Namespace is not a valid PSR-4 prefix: "%s".', resource['namespace']));
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works with preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\)++$/', trim($resource['namespace'], '\\'). '\\')
, note the original \\\\
became \\\
src/Symfony/Component/Routing/Tests/Loader/Psr4DirectoryLoaderTest.php
Outdated
Show resolved
Hide resolved
204071e
to
c96f1c5
Compare
c96f1c5
to
3d807c1
Compare
Status: Needs review |
Thank you @Kocal. |
I spend a small amount of time to understand why my controller was inaccessible:
With the following configuration:
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: