Skip to content

Commit 85a119d

Browse files
committed
fix: harden cache policy deserialization and await clearAuth in tests
BugBot #9: Wrap CachePolicy.fromObject() and related calls in try-catch inside getCachedResponse(). A corrupted or version-incompatible policy object now triggers a cache miss (and best-effort cleanup of the broken entry) instead of crashing the API request. BugBot #10: Add missing `await` to clearAuth() calls in project/list tests at lines 1080 and 1362 to prevent floating promises.
1 parent 8dcc111 commit 85a119d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/lib/response-cache.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,25 @@ export async function getCachedResponse(
336336
return;
337337
}
338338

339-
const policy = CachePolicy.fromObject(entry.policy);
340-
if (!isEntryFresh(policy, entry, requestHeaders, url)) {
339+
try {
340+
const policy = CachePolicy.fromObject(entry.policy);
341+
if (!isEntryFresh(policy, entry, requestHeaders, url)) {
342+
return;
343+
}
344+
345+
const responseHeaders = buildResponseHeaders(policy, entry);
346+
return new Response(JSON.stringify(entry.body), {
347+
status: entry.status,
348+
headers: responseHeaders,
349+
});
350+
} catch {
351+
// Corrupted or version-incompatible policy object — treat as cache miss.
352+
// Delete the broken entry so it doesn't keep failing on every request.
353+
await unlink(cacheFilePath(key)).catch(() => {
354+
// Best-effort cleanup
355+
});
341356
return;
342357
}
343-
344-
const responseHeaders = buildResponseHeaders(policy, entry);
345-
return new Response(JSON.stringify(entry.body), {
346-
status: entry.status,
347-
headers: responseHeaders,
348-
});
349358
}
350359

351360
/**

test/commands/project/list.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ describe("fetchOrgProjectsSafe", () => {
10771077

10781078
test("propagates AuthError when not authenticated", async () => {
10791079
// Clear auth token so the API client throws AuthError before making any request
1080-
clearAuth();
1080+
await clearAuth();
10811081

10821082
await expect(fetchOrgProjectsSafe("myorg")).rejects.toThrow(AuthError);
10831083
});
@@ -1359,7 +1359,7 @@ describe("handleAutoDetect", () => {
13591359
test("fast path: AuthError still propagates", async () => {
13601360
await setDefaults("test-org");
13611361
// Clear auth so getAuthToken() throws AuthError before any fetch
1362-
clearAuth();
1362+
await clearAuth();
13631363
const { writer } = createCapture();
13641364

13651365
await expect(

0 commit comments

Comments
 (0)