Skip to content

Commit b50fcd0

Browse files
Fix cache get() on absent encrypted value
1 parent dd183bd commit b50fcd0

3 files changed

Lines changed: 19 additions & 12 deletions

File tree

backend/src/db/repo/cache.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ export class CacheRepo {
2424
await this.client.cache.delete({ where: { key } });
2525
}
2626

27-
async get(key: string): Promise<string> {
27+
async get(key: string): Promise<string | null> {
2828
return (
29-
await this.client.cache.findUnique({
30-
where: { key, expiresAt: { gt: new Date() } },
31-
select: { value: true },
32-
})
33-
)?.value;
29+
(
30+
await this.client.cache.findUnique({
31+
where: { key, expiresAt: { gt: new Date() } },
32+
select: { value: true },
33+
})
34+
)?.value ?? null
35+
);
3436
}
3537

3638
async set(key: string, value: string, expiresAt: Date) {
@@ -41,10 +43,11 @@ export class CacheRepo {
4143
});
4244
}
4345

44-
async getEncrypted(key: string) {
46+
async getEncrypted(key: string): Promise<string | null> {
4547
const row = await this.client.cache.findUnique({
4648
where: { key, expiresAt: { gt: new Date() } },
4749
});
50+
if (row === null) return null;
4851
if (!row.encryptionKey) {
4952
throw new Error("getEncrypted called on a row without an encryption key");
5053
}

backend/src/service/common/cache.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ export class KVCacheService {
4949
await this.cacheRepo.remove(key);
5050
}
5151

52-
async get(
53-
key: string,
54-
encrypt: boolean = false,
55-
): Promise<string | undefined> {
52+
async get(key: string, encrypt: boolean = false): Promise<string | null> {
5653
const localResult = this.localCache.get(key);
5754
if (localResult) {
5855
return Promise.resolve(localResult);
@@ -72,7 +69,7 @@ export class KVCacheService {
7269
: await this.cacheRepo.get(key);
7370
} catch (e) {
7471
logger.warn({ cacheKey: key, error: e }, "Failed to look up cache key");
75-
return undefined;
72+
return null;
7673
}
7774
}
7875

backend/test/db/repo/cache.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ test("encrypted cached values", async () => {
1414

1515
expect(value).toEqual("value");
1616
});
17+
18+
test("get blank encrypted value", async () => {
19+
const db = await createDB();
20+
21+
const value = await db.cache.getEncrypted("unknown_key");
22+
expect(value).toBeNull();
23+
});

0 commit comments

Comments
 (0)