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

Skip to content

Commit 4b63c3a

Browse files
[Routing] Fix dot-segment encoding for chained "../" and "./" in generated URLs
1 parent 44cf082 commit 4b63c3a

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,16 @@ protected function doGenerate(array $variables, array $defaults, array $requirem
221221
// the path segments "." and ".." are interpreted as relative reference when resolving a URI; see http://tools.ietf.org/html/rfc3986#section-3.3
222222
// so we need to encode them as they are not used for this purpose here
223223
// otherwise we would generate a URI that, when followed by a user agent (e.g. browser), does not match this route
224-
$url = strtr($url, ['/../' => '/%2E%2E/', '/./' => '/%2E/']);
225-
if (str_ends_with($url, '/..')) {
226-
$url = substr($url, 0, -2).'%2E%2E';
227-
} elseif (str_ends_with($url, '/.')) {
228-
$url = substr($url, 0, -1).'%2E';
224+
if (str_contains($url, '/.')) {
225+
$segments = explode('/', $url);
226+
foreach ($segments as $i => $segment) {
227+
if ('.' === $segment) {
228+
$segments[$i] = '%2E';
229+
} elseif ('..' === $segment) {
230+
$segments[$i] = '%2E%2E';
231+
}
232+
}
233+
$url = implode('/', $segments);
229234
}
230235

231236
$schemeAuthority = '';

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,18 @@ public function testEncodingOfRelativePathSegments()
505505
$this->assertSame('/app.php/a./.a/a../..a/...', $this->getGenerator($routes)->generate('test'));
506506
}
507507

508+
public function testEncodingOfChainedRelativePathSegments()
509+
{
510+
$routes = $this->getRoutes('test', new Route('/foo/{path}/bar', [], ['path' => '.+']));
511+
$this->assertSame('/app.php/foo/%2E%2E/%2E%2E/%2E%2E/bar', $this->getGenerator($routes)->generate('test', ['path' => '../../..']));
512+
$this->assertSame('/app.php/foo/%2E/%2E/%2E/bar', $this->getGenerator($routes)->generate('test', ['path' => '././.']));
513+
$this->assertSame('/app.php/foo/%2E%2E/%2E/%2E/%2E%2E/bar', $this->getGenerator($routes)->generate('test', ['path' => '../././..']));
514+
515+
$routes = $this->getRoutes('test', new Route('/foo/{path}', [], ['path' => '.+']));
516+
$this->assertSame('/app.php/foo/%2E%2E/%2E%2E/%2E%2E', $this->getGenerator($routes)->generate('test', ['path' => '../../..']));
517+
$this->assertSame('/app.php/foo/%2E/%2E/%2E', $this->getGenerator($routes)->generate('test', ['path' => '././.']));
518+
}
519+
508520
public function testEncodingOfSlashInPath()
509521
{
510522
$routes = $this->getRoutes('test', new Route('/dir/{path}/dir2', [], ['path' => '.+']));

0 commit comments

Comments
 (0)