8000 [Security] Fixed digest authentication · symfony/symfony@694697d · GitHub
[go: up one dir, main page]

Skip to content

Commit 694697d

Browse files
Vincent Simoninsstok
Vincent Simonin
authored andcommitted
[Security] Fixed digest authentication
Digest authentication fail if digest parameters contains `=` character or `, ` string. * Support escaped characters
1 parent c067586 commit 694697d

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/Symfony/Component/Security/Http/Firewall/DigestAuthenticationListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class DigestData
141141
public function __construct($header)
142142
{
143143
$this->header = $header;
144-
preg_match_all('/(\w+)=("([^"]+)"|([^\s,$]+))/', $header, $matches, PREG_SET_ORDER);
144+
preg_match_all('/(\w+)=("((?:[^"\\\\]|\\\\.)+)"|([^\s,$]+))/', $header, $matches, PREG_SET_ORDER);
145145
$this->elements = array();
146146
foreach ($matches as $match) {
147147
if (isset($match[1]) && isset($match[3])) {

tests/Symfony/Tests/Component/Security/Http/Firewall/DigestDataTest.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public function testGetUsername()
4444
$this->assertEquals('user', $digestAuth->getUsername());
4545
}
4646

47+
public function testGetUsernameWithQuote()
48+
{
49+
$digestAuth = new DigestData(
50+
'username="\"user\"", realm="Welcome, robot!", ' .
51+
'nonce="MTM0NzMyMTgyMy42NzkzOmRlZjM4NmIzOGNjMjE0OWJiNDU0MDAxNzJmYmM1MmZl", ' .
52+
'uri="/path/info?p1=5&p2=5", cnonce="MDIwODkz", nc=00000001, qop="auth", ' .
53+
'response="b52938fc9e6d7c01be7702ece9031b42"'
54+
);
55+
56+
$this->assertEquals('\"user\"', $digestAuth->getUsername());
57+
}
58+
4759
public function testValidateAndDecode()
4860
{
4961
$time = microtime(true);
@@ -65,24 +77,24 @@ public function testValidateAndDecode()
6577

6678
public function testCalculateServerDigest()
6779
{
68-
$username = 'user';
69-
$realm = 'Welcome, robot!';
70-
$password = 'pass,word=password';
80+
$this->calculateServerDigest('user', 'Welcome, robot!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5');
81+
}
82+
83+
public function testCalculateServerDigestWithQuote()
84+
{
85+
$this->calculateServerDigest('\"user\"', 'Welcome, \"robot\"!', 'pass,word=password', 'ThisIsAKey', '00000001', 'MDIwODkz', 'auth', 'GET', '/path/info?p1=5&p2=5');
86+
}
87+
88+
private function calculateServerDigest($username, $realm, $password, $key, $nc, $cnonce, $qop, $method, $uri)
89+
{
7190
$time = microtime(true);
72-
$key = 'ThisIsAKey';
7391
$nonce = base64_encode($time . ':' . md5($time . ':' . $key));
74-
$nc = '00000001';
75-
$cnonce = 'MDIwODkz';
76-
$qop = 'auth';
77-
$method = 'GET';
78-
$uri = '/path/info?p1=5&p2=5';
7992

8093
$response = md5(
81-
md5($username . ':' . $realm . ':' . $password) .
82-
':' . $nonce . ':' . $nc . ':' . $cnonce . ':' . $qop . ':' . md5($method . ':' . $uri)
94+
md5($username . ':' . $realm . ':' . $password) . ':' . $nonce . ':' . $nc . ':' . $cnonce . ':' . $qop . ':' . md5($method . ':' . $uri)
8395
);
8496

85-
$digest = sprintf('username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc="%s", qop="%s", response="%s"',
97+
$digest = sprintf('username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc=%s, qop="%s", response="%s"',
8698
$username, $realm, $nonce, $uri, $cnonce, $nc, $qop, $response
8799
);
88100

0 commit comments

Comments
 (0)
0