-
Notifications
You must be signed in to change notification settings - Fork 84
Do not sanity check hash of anonymous requests #289
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
Changes from all commits
cad6b5d
6492160
fe38fb5
d19e9fa
0990981
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the FOSHttpCacheBundle package. | ||
| * | ||
| * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace FOS\HttpCacheBundle\Tests\Unit\UserContext; | ||
|
|
||
| use FOS\HttpCacheBundle\UserContext\AnonymousRequestMatcher; | ||
| use PHPUnit_Framework_TestCase; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| class AnonymousRequestMatcherTest extends PHPUnit_Framework_TestCase | ||
| { | ||
| public function testMatchAnonymousRequest() | ||
| { | ||
| $request = new Request(); | ||
|
|
||
| $requestMatcher = new AnonymousRequestMatcher(['Cookie', 'Authorization']); | ||
|
|
||
| $this->assertTrue($requestMatcher->matches($request)); | ||
| } | ||
|
|
||
| public function testNoMatchIfCookie() | ||
| { | ||
| $request = new Request(); | ||
| $request->headers->set('Cookie', 'PHPSESSID7e476fc9f29f69d2ad6f11dbcd663b42=25f6d9c5a843e3c948cd26902385a527'); | ||
| $request->cookies->set('PHPSESSID7e476fc9f29f69d2ad6f11dbcd663b42', '25f6d9c5a843e3c948cd26902385a527'); | ||
|
|
||
| $requestMatcher = new AnonymousRequestMatcher(['Cookie', 'Authorization']); | ||
|
|
||
| $this->assertFalse($requestMatcher->matches($request)); | ||
| } | ||
|
|
||
| public function testNoMatchIfEmptyCookieHeader() | ||
| { | ||
| $request = new Request(); | ||
| $request->headers->set('Cookie', ''); | ||
|
|
||
| $requestMatcher = new AnonymousRequestMatcher(['Cookie', 'Authorization']); | ||
|
|
||
| $this->assertTrue($requestMatcher->matches($request)); | ||
| } | ||
|
|
||
| public function testNoMatchIfAuthenticationHeader() | ||
| { | ||
| $request = new Request(); | ||
| $request->headers->set('Authorization', 'foo: bar'); | ||
|
|
||
| $requestMatcher = new AnonymousRequestMatcher(['Cookie', 'Authorization']); | ||
|
|
||
| $this->assertFalse($requestMatcher->matches($request)); | ||
| } | ||
|
|
||
| public function testMatchEmptyCookieHeaderHeader() | ||
| { | ||
| $request = new Request(); | ||
| $request->headers->set('Cookie', ''); | ||
|
|
||
| $requestMatcher = new AnonymousRequestMatcher(['Cookie', 'Authorization']); | ||
|
|
||
| $this->assertTrue($requestMatcher->matches($request)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the FOSHttpCacheBundle package. | ||
| * | ||
| * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace FOS\HttpCacheBundle\UserContext; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\RequestMatcherInterface; | ||
|
|
||
| /** | ||
| * Matches anonymous requests using a list of identification headers. | ||
| */ | ||
| class AnonymousRequestMatcher implements RequestMatcherInterface | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a class level comment to explain that this matcher matches all requests that are anonymous.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please add tests for this matcher?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I will. |
||
| { | ||
| private $userIdentifierHeaders; | ||
|
|
||
| /** | ||
| * @param array $userIdentifierHeaders List of request headers that authenticate a non-anonymous request | ||
| */ | ||
| public function __construct(array $userIdentifierHeaders) | ||
| { | ||
| $this->userIdentifierHeaders = $userIdentifierHeaders; | ||
| } | ||
|
|
||
| public function matches(Request $request) | ||
| { | ||
| foreach ($this->userIdentifierHeaders as $header) { | ||
| if ($request->headers->has($header)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, one more question: what does symfony request do with the cookie header? is that removed from the header bag? what happens if we have a request with only an empty cookie header? won't we then get in here and see the cookie header, making the previous if on the cookies count kind of pointless? should we do
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, with test, @dbu.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the first if with cookie header and nonzero number of cookies is now redundant and should be removed.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the if, adjusted the existing test, and added a new case. |
||
| if (strtolower($header) === 'cookie' && 0 === $request->cookies->count()) { | ||
| // ignore empty cookie header | ||
| continue; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and please explain here that not booting the symfony kernel is the reason why we use this matcher rather than just looking at symfony and see if there is a non-anonymous user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, its not only performance. if we would boot the kernel, we would treat the expired session as anonymous which is exactly what #274 wanted to prevent. maybe explain the thing about anonymous fake hashes in the phpdoc here so we still know why this is done like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a comment.