Skip to content

[Security] Fix error when calling HttpUtils::generateUri() #20946

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions src/Symfony/Component/Security/Http/HttpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ public function generateUri($request, $path)
return $path;
}

if (!$request instanceof Request) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Imo. we should make this the first check.

Copy link
Member Author

@chalasr chalasr Dec 16, 2016

Choose a reason for hiding this comment

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

This targets 2.7, preventing the fatal error. Making this the first check would break for people which rely on the current first check, because it doesn't need $request. I would suggest to throw an exception here as it is and deprecate passing something else than a Request in 3.3, for adding the typehint in 4.0.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point 👍

Copy link
Member

Choose a reason for hiding this comment

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

But we never add any similar checks if the doctype restricts the argument to a particular type.

Copy link
Contributor

@ro0NL ro0NL Dec 17, 2016

Choose a reason for hiding this comment

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

Problem is it also lacks a object typehint (Request $request). We should prefer that over docblocks of course.. however the current policy doesnt seem to allow it.. (?)

#19960 (comment) closed for the same reason. Allthough i still disagree ;-)

And i also prefer a cosmetic error over PHP just crashing.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @xabbuh. If we go down this path, we can potentially add such checks in a lot of places, which is a no go.

Copy link
Contributor

@ro0NL ro0NL Dec 17, 2016

Choose a reason for hiding this comment

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

Fair enough :)) I guess the thing is we want to improve method signatures / type hints in a upcoming major release, but the policy makes it real hard. And personally i feel it's holding us back from improving (read: simplifying) the codebase.

Ie. this is not about adding safety guards, scalar type checks, etc. but about forcing integrity where possible.

Anyway, as any error will be probably clear enough i understand it's not worth the effort.

throw new \InvalidArgumentException(sprintf('The first argument of %s() must be an instance of %s.', __METHOD__, Request::class));
}

if ('/' === $path[0]) {
return $request->getUriForPath($path);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ public function testGenerateUriRemovesQueryString()
$this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The first argument of Symfony\Component\Security\Http\HttpUtils::generateUri() must be an instance of Symfony\Component\HttpFoundation\Request.
*/
public function testGenerateUriThrowsExceptionIfNotAnInstanceOfRequest()
{
$utils = new HttpUtils();
$utils->generateUri(null, '/foo/bar');
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage You must provide a UrlGeneratorInterface instance to be able to use routes.
Expand Down