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

Skip to content

[Routing] Inline defaults and requirements are not working as documented #40701

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
liarco opened this issue Apr 3, 2021 · 1 comment
Closed

Comments

@liarco
Copy link
Contributor

liarco commented Apr 3, 2021

Symfony version(s) affected: 5.2.3 (previous versions are probably affected too)

Description/How to reproduce
Using the syntax @Route("sitemap/pages.{!_format<xml>?xml}", name="sitemap__pages") as documented here doesn't register the default value properly.
Generating the URL like this $this->generateUrl('sitemap__pages') throws a MissingMandatoryParametersException for _format.

Changing the route definition to @Route("sitemap/pages.{!_format<xml>?xml}", name="sitemap__pages", defaults={"_format"="xml"}) works as expected, but this way I'm specifying the default value twice...

Possible Solution
The exception is thrown by https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/Routing/Generator/UrlGenerator.php#L178. I dumped $defaults passed to doGenerate and I found that it contains:

array:2 [▼
  "!_format" => "xml"
  "_controller" => "App\Controller\FrontEnd\SitemapController::pages"
]

The default value for _format is not found because of the leading ! in the key.

Inline defaults and requirements are extracted: https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/Routing/Route.php#L536

I'm afraid that 826db22 (by @nicolas-grekas) is not compatible with this check: https://github.com/symfony/symfony/blob/5.x/src/Symfony/Component/Routing/Generator/UrlGenerator.php#L177

Unfortunately I can't open a PR because I'm not sure if I should update the way defaults/requirements/params are stored inside the Route object or if I should update the check inside UrlGenerator.

Thank you for your time.

@liarco liarco added the Bug label Apr 3, 2021
@liarco liarco changed the title [symfony/routing] Inline defaults and requirements are not working as documented [Routing] Inline defaults and requirements are not working as documented Apr 3, 2021
@Foxprodev
Copy link
Contributor

Foxprodev commented Apr 8, 2021

+1/ Found that inline requirements doesn't work with important params at all.

$regexp = $route->getRequirement($varName);

$varName matched by \{(!)?(\w+)\} pattern and doesn't include !, however Route requirement starts with !
Thats why all important params are matching default regex and vulnerable.

nicolas-grekas added a commit that referenced this issue Apr 11, 2021
…(Foxprodev)

This PR was merged into the 4.4 branch.

Discussion
----------

[Routing] Better inline requirements and defaults parsing

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #40749 #40701
| License       | MIT
| Doc PR        |

Remove `!` symbol from requirements and defaults array keys in Route class. Leave `!` symbol in Route compiled path for correct token creation.
**The only restriction I found:**
Important variable can't get default value, only in UrlGenerator.
As mentioned in
https://github.com/symfony/symfony/blob/0f96ac74847d114c9d9679655bcf3e94b6ba69d1/src/Symfony/Component/Routing/RouteCompiler.php#L217
they are not optional

Feel free to help me with some advice. Thank you in advance

Commits
-------

2a8c94a [Route] Better inline requirements and defaults parsing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants