Skip to content

Navigation Menu

Sign in
Appearance settings

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

[Security] Preserve URI fragment in HttpUtils::generateUri() #24203

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

Merged
merged 1 commit into from
Sep 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion 7 src/Symfony/Component/Security/Http/HttpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HttpUtils
public function __construct(UrlGeneratorInterface $urlGenerator = null, $urlMatcher = null)
{
$this->urlGenerator = $urlGenerator;
if ($urlMatcher !== null && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) {
if (null !== $urlMatcher && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) {
throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
}
$this->urlMatcher = $urlMatcher;
Expand Down Expand Up @@ -150,7 +150,12 @@ public function generateUri($request, $path)
// fortunately, they all are, so we have to remove entire query string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update this comment too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dedicated comment added just before fragment handling

$position = strpos($url, '?');
if (false !== $position) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that necessary? What about a path like /foo/bar#fragment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fragment gets stripped only if there is a query string, otherwise the urlGenerator result is returned directly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification, but maybe we should add a test for that too to prevent regressions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In most cases there is always a query string, because it uses the attributes from the request to build it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh Test added

$fragment = parse_url($url, PHP_URL_FRAGMENT);
$url = substr($url, 0, $position);
// fragment must be preserved
if ($fragment) {
$url .= "#$fragment";
}
}

return $url;
Expand Down
9 changes: 9 additions & 0 deletions 9 src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ public function testGenerateUriRemovesQueryString()
$this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
}

public function testGenerateUriPreservesFragment()
{
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value#fragment'));
$this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name'));

$utils = new HttpUtils($this->getUrlGenerator('/foo/bar#fragment'));
$this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name'));
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage You must provide a UrlGeneratorInterface instance to be able to use routes.
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.