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

Commit 29b217c

Browse filesBrowse files
nyroDevfabpot
authored andcommitted
[HttpKernel] Fix UriSigner::check when _hash is not at the end of the uri
1 parent a7d5624 commit 29b217c
Copy full SHA for 29b217c

File tree

2 files changed

+41
-2
lines changed
Filter options

2 files changed

+41
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/UriSignerTest.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ public function testCheck()
3333

3434
$this->assertTrue($signer->check($signer->sign('http://example.com/foo')));
3535
$this->assertTrue($signer->check($signer->sign('http://example.com/foo?foo=bar')));
36+
37+
$this->assertTrue($signer->sign('http://example.com/foo?foo=bar&bar=foo') === $signer->sign('http://example.com/foo?bar=foo&foo=bar'));
3638
}
3739
}

‎src/Symfony/Component/HttpKernel/UriSigner.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/UriSigner.php
+39-2Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public function __construct($secret)
4242
*/
4343
public function sign($uri)
4444
{
45+
$url = parse_url($uri);
46+
if (isset($url['query'])) {
47+
parse_str($url['query'], $params);
48+
} else {
49+
$params = array();
50+
}
51+
52+
$uri = $this->buildUrl($url, $params);
53+
4554
return $uri.(false === (strpos($uri, '?')) ? '?' : '&').'_hash='.$this->computeHash($uri);
4655
}
4756

@@ -58,15 +67,43 @@ public function sign($uri)
5867
*/
5968
public function check($uri)
6069
{
61-
if (!preg_match('/^(.*)(?:\?|&)_hash=(.+?)$/', $uri, $matches)) {
70+
$url = parse_url($uri);
71+
if (isset($url['query'])) {
72+
parse_str($url['query'], $params);
73+
} else {
74+
$params = array();
75+
}
76+
77+
if (empty($params['_hash'])) {
6278
return false;
6379
}
6480

65-
return $this->computeHash($matches[1]) === $matches[2];
81+
$hash = urlencode($params['_hash']);
82+
unset($params['_hash']);
83+
84+
return $this->computeHash($this->buildUrl($url, $params)) === $hash;
6685
}
6786

6887
private function computeHash($uri)
6988
{
7089
return urlencode(base64_encode(hash_hmac('sha1', $uri, $this->secret, true)));
7190
}
91+
92+
private function buildUrl(array $url, array $params = array())
93+
{
94+
ksort($params);
95+
$url['query'] = http_build_query($params);
96+
97+
$scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
98+
$host = isset($url['host']) ? $url['host'] : '';
99+
$port = isset($url['port']) ? ':'.$url['port'] : '';
100+
$user = isset($url['user']) ? $url['user'] : '';
101+
$pass = isset($url['pass']) ? ':'.$url['pass'] : '';
102+
$pass = ($user || $pass) ? "$pass@" : '';
103+
$path = isset($url['path']) ? $url['path'] : '';
104+
$query = isset($url['query']) && $url['query'] ? '?'.$url['query'] : '';
105+
$fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';
106+
107+
return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
108+
}
72109
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.