8000 Simplify UriSigner when working with HttpFoundation's Request · symfony/symfony@4887b4b · GitHub
[go: up one dir, main page]

Skip to content

Commit 4887b4b

Browse files
Toflarfabpot
authored andcommitted
Simplify UriSigner when working with HttpFoundation's Request
1 parent 5c37ab0 commit 4887b4b

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ protected function validateRequest(Request $request)
8383
}
8484

8585
// is the Request signed?
86-
// we cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
87-
if ($this->signer->check($request->getSchemeAndHttpHost().$request->getBaseUrl().$request->getPathInfo().(null !== ($qs = $request->server->get('QUERY_STRING')) ? '?'.$qs : ''))) {
86+
if ($this->signer->checkRequest($request)) {
8887
return;
8988
}
9089

src/Symfony/Component/HttpKernel/Tests/UriSignerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpKernel\UriSigner;
1617

1718
class UriSignerTest extends TestCase
@@ -52,6 +53,15 @@ public function testCheckWithDifferentArgSeparator()
5253
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar&baz=bay' 10000 ;)));
5354
}
5455

56+
public function testCheckWithRequest()
57+
{
58+
$signer = new UriSigner('foobar');
59+
60+
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo'))));
61+
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar'))));
62+
$this->assertTrue($signer->checkRequest(Request::create($signer->sign('http://example.com/foo?foo=bar&0=integer'))));
63+
}
64+
5565
public function testCheckWithDifferentParameter()
5666
{
5767
$signer = new UriSigner('foobar', 'qux');

src/Symfony/Component/HttpKernel/UriSigner.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpKernel;
1313

14+
use Symfony\Component\HttpFoundation\Request;
15+
1416
/**
1517
* Signs URIs.
1618
*
@@ -78,6 +80,14 @@ public function check(string $uri)
7880
return hash_equals($this->computeHash($this->buildUrl($url, $params)), $hash);
7981
}
8082

83+
public function checkRequest(Request $request): bool
84+
{
85+
$qs = ($qs = $request->server->get('QUERY_STRING')) ? '?'.$qs : '';
86+
87+
// we cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
88+
return $this->check($request->getSchemeAndHttpHost().$request->getBaseUrl().$request->getPathInfo().$qs);
89+
}
90+
8191
private function computeHash(string $uri): string
8292
{
8393
return base64_encode(hash_hmac('sha256', $uri, $this->secret, true));

0 commit comments

Comments
 (0)
0