Skip to content

Commit

Permalink
Merge pull request #45192 from nextcloud/backport/45093/stable29
Browse files Browse the repository at this point in the history
[stable29] fix(session): Avoid race condition for cache::get() vs. cache::hasKey()
  • Loading branch information
nickvergessen authored May 6, 2024
2 parents 137d461 + 487dfb6 commit 88e871f
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 88e871f

Please sign in to comment.