Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 284c0a5

Browse filesBrowse files
[Routing] don't decode nor double-encode already encoded chars when generating URLs
1 parent a760037 commit 284c0a5
Copy full SHA for 284c0a5

File tree

3 files changed

+23
-3
lines changed
Filter options

3 files changed

+23
-3
lines changed

‎src/Symfony/Component/Routing/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* already encoded chars are not decoded nor double-encoded anymore when generating URLs
8+
49
5.2.0
510
-----
611

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Generator/UrlGenerator.php
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,19 @@ protected function doGenerate(array $variables, array $defaults, array $requirem
212212

213213
if ('' === $url) {
214214
$url = '/';
215-
}
215+
} else {
216+
// the contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request)
217+
$urlParts = preg_split('/((?:%[0-9a-fA-F]{2})++)/', $url, -1, \PREG_SPLIT_DELIM_CAPTURE);
218+
$url = '';
219+
220+
foreach ($urlParts as $i => $part) {
221+
if (0 === $i % 2) {
222+
$part = strtr(rawurlencode($part), $this->decodedChars);
223+
}
216224

217-
// the contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request)
218-
$url = strtr(rawurlencode($url), $this->decodedChars);
225+
$url .= $part;
226+
}
227+
}
219228

220229
// the path segments "." and ".." are interpreted as relative reference when resolving a URI; see http://tools.ietf.org/html/rfc3986#section-3.3
221230
// so we need to encode them as they are not used for this purpose here

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,12 @@ public function testEncodingOfRelativePathSegments()
467467
$this->assertSame('/app.php/a./.a/a../..a/...', $this->getGenerator($routes)->generate('test'));
468468
}
469469

470+
public function testEncodingOfSlashInPath()
471+
{
472+
$routes = $this->getRoutes('test', new Route('/dir/{path}/dir2', [], ['path' => '.+']));
473+
$this->assertSame('/app.php/dir/foo/bar%2Fbaz/dir2', $this->getGenerator($routes)->generate('test', ['path' => 'foo/bar%2Fbaz']));
474+
}
475+
470476
public function testAdjacentVariables()
471477
{
472478
$routes = $this->getRoutes('test', new Route('/{x}{y}{z}.{_format}', ['z' => 'default-z', '_format' => 'html'], ['y' => '\d+']));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.