Skip to content

Commit 4be03ed

Browse files
author
François Kooman
committed
implement constant time password compare for Basic auth
1 parent 1650aab commit 4be03ed

File tree

3 files changed

+126
-4
lines changed

3 files changed

+126
-4
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"ext-pcre": "*",
2222
"ext-pdo": "*",
2323
"ext-spl": "*",
24-
"php": ">=5.4"
24+
"php": ">=5.4",
25+
"symfony/polyfill-php56": "^1.3"
2526
}
2627
}

composer.lock

Lines changed: 111 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Rest/Plugin/BasicAuthentication.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function execute(Request $request)
4848
$requestBasicAuthUser = $request->getBasicAuthUser();
4949
$requestBasicAuthPass = $request->getBasicAuthPass();
5050

51-
if ($this->basicAuthUser !== $requestBasicAuthUser || $this->basicAuthPass !== $requestBasicAuthPass) {
51+
if ($this->basicAuthUser !== $requestBasicAuthUser || !$this->checkPassword($this->basicAuthPass, $requestBasicAuthPass)) {
5252
$response = new JsonResponse(401);
5353
$response->setHeader(
5454
'WWW-Authenticate',
@@ -66,4 +66,16 @@ public function execute(Request $request)
6666

6767
return true;
6868
}
69+
70+
/**
71+
* @param string $knownString the expected password
72+
* @param string $userString the (user) provided password
73+
*
74+
* @return bool
75+
*/
76+
private function checkPassword($knownString, $userString)
77+
{
78+
// constant time string compare
79+
return hash_equals($knownString, $userString);
80+
}
6981
}

0 commit comments

Comments
 (0)