-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not sanity check hash of anonymous requests (#289)
- Loading branch information
Showing
7 changed files
with
186 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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)) { | ||
if (strtolower($header) === 'cookie' && 0 === $request->cookies->count()) { | ||
// ignore empty cookie header | ||
continue; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters