8000 [Routing] allow comma and other reserved chars without special meaing… · symfony/symfony@76f6c97 · GitHub
[go: up one dir, main page]

Skip to content

Commit 76f6c97

Browse files
committed
[Routing] allow comma and other reserved chars without special meaing to not be encoded in the query and fragment
1 parent 278a7ec commit 76f6c97

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
*/
2828
class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInterface
2929
{
30+
private const QUERY_FRAGMENT_DECODED = [
31+
// RFC 3986 explicitly allows those in the query/fragment to reference other URIs unencoded
32+
'%2F' => '/',
33+
'%3F' => '?',
34+
// reserved chars that have no special meaning for HTTP URIs in a query or fragment
35+
// this excludes esp. "&", "=" and also "+" because PHP would treat it as a space (form-encoded)
36+
'%40' => '@',
37+
'%3A' => ':',
38+
'%21' => '!',
39+
'%3B' => ';',
40+
'%2C' => ',',
41+
'%2A' => '*',
42+
];
43+
3044
protected $routes;
3145
protected $context;
3246

@@ -275,13 +289,11 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
275289
}
276290

277291
if ($extra && $query = http_build_query($extra, '', '&', PHP_QUERY_RFC3986)) {
278-
// "/" and "?" can be left decoded for better user experience, see
279-
// http://tools.ietf.org/html/rfc3986#section-3.4
280-
$url .= '?'.strtr($query, ['%2F' => '/']);
292+
$url .= '?'.strtr($query, self::QUERY_FRAGMENT_DECODED);
281293
}
282294

283295
if ('' !== $fragment) {
284-
$url .= '#'.strtr(rawurlencode($fragment), ['%2F' => '/', '%3F' => '?']);
296+
$url .= '#'.strtr(rawurlencode($fragment), self::QUERY_FRAGMENT_DECODED);
285297
}
286298

287299
return $url;

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function testUrlEncoding()
337337
{
338338
$expectedPath = '/app.php/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
339339
.'/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
340-
.'?query=%40%3A%5B%5D/%28%29%2A%27%22%20%2B%2C%3B-._~%26%24%3C%3E%7C%7B%7D%25%5C%5E%60%21%3Ffoo%3Dbar%23id';
340+
.'?query=@:%5B%5D/%28%29*%27%22%20%2B,;-._~%26%24%3C%3E%7C%7B%7D%25%5C%5E%60!?foo%3Dbar%23id';
341341

342342
// This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)
343343
// and other special ASCII chars. These chars are tested as static text path, variable path and query param.

0 commit comments

Comments
0 (0)
0