Skip to content

Commit

Permalink
fix(session): Avoid race condition for cache::get() vs. cache::hasKey()
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot[bot] committed May 6, 2024
1 parent 48f3b91 commit 487dfb6
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/private/Authentication/Token/PublicKeyTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ public function getToken(string $tokenId): OCPIToken {
*/
private function getTokenFromCache(string $tokenHash): ?PublicKeyToken {
$serializedToken = $this->cache->get($tokenHash);
if ($serializedToken === null) {
if ($this->cache->hasKey($tokenHash)) {
throw new InvalidTokenException('Token does not exist: ' . $tokenHash);
}
if ($serializedToken === false) {
throw new InvalidTokenException('Token does not exist: ' . $tokenHash);
}

if ($serializedToken === null) {
return null;
}

Expand All @@ -211,9 +211,9 @@ private function cacheToken(PublicKeyToken $token): void {
$this->cache->set($token->getToken(), serialize($token), self::TOKEN_CACHE_TTL);
}

private function cacheInvalidHash(string $tokenHash) {
private function cacheInvalidHash(string $tokenHash): void {
// Invalid entries can be kept longer in cache since it’s unlikely to reuse them
$this->cache->set($tokenHash, null, self::TOKEN_CACHE_TTL * 2);
$this->cache->set($tokenHash, false, self::TOKEN_CACHE_TTL * 2);
}

public function getTokenById(int $tokenId): OCPIToken {
Expand Down

0 comments on commit 487dfb6

Please sign in to comment.