Skip to content

Commit d698726

Browse files
committed
Merge branch '2.3' into 2.5
* 2.3: adapted previous commit for 2.3 [Security] Don't send remember cookie for sub request [HttpKernel] Fix UriSigner::check when _hash is not at the end of the uri Conflicts: src/Symfony/Component/Security/Http/Tests/RememberMe/ResponseListenerTest.php
2 parents e30d714 + f56fc97 commit d698726

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Tests/UriSignerTest.php

Lines 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
}

UriSigner.php

Lines 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('sha256', $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)