-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FakeCredentialGenerator for preventing username enumeration (#603)
* Add FakeCredentialGenerator for fake credentials generation The update introduces a new FakeCredentialGenerator and its simple implementation, SimpleFakeCredentialGenerator, for generating fake credentials. This addition helps prevent username enumeration by providing fake credentials for nonexistent users. Changes have been made across multiple files, including service configuration updates and logic changes in the ProfileBasedRequestOptionsBuilder.
- Loading branch information
Showing
10 changed files
with
138 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webauthn; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
interface FakeCredentialGenerator | ||
{ | ||
/** | ||
* @return PublicKeyCredentialDescriptor[] | ||
*/ | ||
public function generate(Request $request, string $username): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webauthn; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use function count; | ||
use function is_int; | ||
|
||
final class SimpleFakeCredentialGenerator implements FakeCredentialGenerator | ||
{ | ||
public function __construct( | ||
private readonly null|CacheItemPoolInterface $cache = null | ||
) { | ||
} | ||
|
||
/** | ||
* @return PublicKeyCredentialDescriptor[] | ||
*/ | ||
public function generate(Request $request, string $username): array | ||
{ | ||
if ($this->cache === null) { | ||
return $this->generateCredentials($username); | ||
} | ||
|
||
$cacheKey = 'fake_credentials_' . hash('xxh128', $username); | ||
$cacheItem = $this->cache->getItem($cacheKey); | ||
if ($cacheItem->isHit()) { | ||
return $cacheItem->get(); | ||
} | ||
|
||
$credentials = $this->generateCredentials($username); | ||
$cacheItem->set($credentials); | ||
$this->cache->save($cacheItem); | ||
|
||
return $credentials; | ||
} | ||
|
||
/** | ||
* @return PublicKeyCredentialDescriptor[] | ||
*/ | ||
private function generateCredentials(string $username): array | ||
{ | ||
$transports = [ | ||
PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_USB, | ||
PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_NFC, | ||
PublicKeyCredentialDescriptor::AUTHENTICATOR_TRANSPORT_BLE, | ||
]; | ||
$credentials = []; | ||
for ($i = 0; $i < random_int(1, 3); $i++) { | ||
$randomTransportKeys = array_rand($transports, random_int(1, count($transports))); | ||
if (is_int($randomTransportKeys)) { | ||
$randomTransportKeys = [$randomTransportKeys]; | ||
} | ||
$randomTransports = array_values(array_intersect_key($transports, array_flip($randomTransportKeys))); | ||
$credentials[] = PublicKeyCredentialDescriptor::create( | ||
PublicKeyCredentialDescriptor::CREDENTIAL_TYPE_PUBLIC_KEY, | ||
hash('sha256', random_bytes(16) . $username), | ||
$randomTransports | ||
); | ||
} | ||
|
||
return $credentials; | ||
} | ||
} |