Skip to content

Commit f049dd0

Browse files
author
Robin Chalas
committed
bug symfony#24203 [Security] Preserve URI fragment in HttpUtils::generateUri() (chalasr)
This PR was merged into the 3.3 branch. Discussion ---------- [Security] Preserve URI fragment in HttpUtils::generateUri() | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#23675 | License | MIT | Doc PR | n/a Commits ------- 4dd2e3e Preserve URI fragment in HttpUtils::generateUri()
2 parents d94b9ac + 4dd2e3e commit f049dd0

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Symfony/Component/Security/Http/HttpUtils.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,12 @@ public function generateUri($request, $path)
150150
// fortunately, they all are, so we have to remove entire query string
151151
$position = strpos($url, '?');
152152
if (false !== $position) {
153+
$fragment = parse_url($url, PHP_URL_FRAGMENT);
153154
$url = substr($url, 0, $position);
155+
// fragment must be preserved
156+
if ($fragment) {
157+
$url .= "#$fragment";
158+
}
154159
}
155160

156161
return $url;

src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ public function testGenerateUriRemovesQueryString()
252252
$this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
253253
}
254254

255+
public function testGenerateUriPreservesFragment()
256+
{
257+
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value#fragment'));
258+
$this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name'));
259+
260+
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar#fragment'));
261+
$this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name'));
262+
}
263+
255264
/**
256265
* @expectedException \LogicException
257266
* @expectedExceptionMessage You must provide a UrlGeneratorInterface instance to be able to use routes.

0 commit comments

Comments
 (0)