Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Routing] Router ignores inline requirements of important param #40749

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

Closed
Foxprodev opened this issue Apr 9, 2021 · 0 comments · Fixed by #40755
Closed

[Routing] Router ignores inline requirements of important param #40749

Foxprodev opened this issue Apr 9, 2021 · 0 comments · Fixed by #40755

Comments

@Foxprodev
Copy link
Contributor

Foxprodev commented Apr 9, 2021

Symfony version(s) affected: 4.4 5.2?

Description
As I mentioned here #40701 (comment). The problem not only in UrlGenerator. Router does not check important (I mean started with !) params inline requirements at all. I decided to open separate issue, because it could be more dangerous and it can be fixed separately.

How to reproduce
Create route with important param, and set some inline requirements like:
#[Route('/foo/{!id<\d+>}')]
Open /foo/bar url and yes route matches the requirement

Reason

preg_match_all('#\{(!)?(\w+)\}#', $pattern, $matches, \PREG_OFFSET_CAPTURE | \PREG_SET_ORDER);
foreach ($matches as $match) {
$important = $match[1][1] >= 0;
$varName = $match[2][0];

$varName does not start with !
$regexp = $route->getRequirement($varName);

However Route requirement key starts with !, and param requirement set do default regex like [^/]++

Possible Solution

private function extractInlineDefaultsAndRequirements(string $pattern): string
{
if (false === strpbrk($pattern, '?<')) {
return $pattern;
}
return preg_replace_callback('#\{(!?\w++)(<.*?>)?(\?[^\}]*+)?\}#', function ($m) {
if (isset($m[3][0])) {
$this->setDefault($m[1], '?' !== $m[3] ? substr($m[3], 1) : null);
}
if (isset($m[2][0])) {
$this->setRequirement($m[1], substr($m[2], 1, -1));
}
return '{'.$m[1].'}';
}, $pattern);
}

Do not include ! in requirement key.
However it does not fix defaults for UrlGenerator described here #40701

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants