Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 6ef417e

Browse files
committed
#8 make authenticate method more robust
* use strict parameter of base64_decode instead of maintaining list of valid characters in regexp * use limit in explode() to allow passwords with colons * check if base64_decode failed * check that array index is set
1 parent f8e478d commit 6ef417e

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/BasicAccess.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,31 @@ public function __construct(
5353
public function authenticate(ServerRequestInterface $request) : ?UserInterface
5454
{
5555
$authHeader = $request->getHeader('Authorization');
56-
if (empty($authHeader)) {
56+
if (! isset($authHeader[0])) {
5757
return null;
5858
}
5959

60-
if (! preg_match('/Basic (?P<credentials>[a-zA-Z0-9\+\/\=]+)/', $authHeader[0], $match)) {
60+
if (! preg_match('/Basic (?P<credentials>.+)/', $authHeader[0], $match)) {
6161
return null;
6262
}
6363

64-
[$username, $password] = explode(':', base64_decode($match['credentials']));
64+
$decodedCredentials = base64_decode($match['credentials'], true);
65+
66+
if (false === $decodedCredentials) {
67+
return null;
68+
}
69+
70+
$credentialParts = explode(':', $decodedCredentials, 2);
71+
72+
if (false === $credentialParts) {
73+
return null;
74+
}
75+
76+
if (2 !== count($credentialParts)) {
77+
return null;
78+
}
79+
80+
[$username, $password] = $credentialParts;
6581

6682
return $this->repository->authenticate($username, $password);
6783
}

0 commit comments

Comments
 (0)