Skip to content

Commit bb1a73a

Browse files
sweetmantechclaude
andcommitted
fix: treat exact-expiry and unparsable timestamps as invalid snapshots
Use <= instead of < so snapshots expiring exactly now are treated as expired. Add NaN check so unparsable expires_at values don't slip through as valid. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 8a0f32a commit bb1a73a

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/sandbox/__tests__/getValidSnapshotId.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ describe("getValidSnapshotId", () => {
5353
expect(result).toBeUndefined();
5454
});
5555

56+
it("returns undefined when expires_at equals now", async () => {
57+
const now = new Date().toISOString();
58+
mockSelectAccountSnapshots.mockResolvedValue([
59+
{ snapshot_id: "snap_edge", account_id: "acc_1", expires_at: now },
60+
]);
61+
62+
const result = await getValidSnapshotId("acc_1");
63+
64+
expect(result).toBeUndefined();
65+
});
66+
67+
it("returns undefined when expires_at is unparsable", async () => {
68+
mockSelectAccountSnapshots.mockResolvedValue([
69+
{ snapshot_id: "snap_bad", account_id: "acc_1", expires_at: "not-a-date" },
70+
]);
71+
72+
const result = await getValidSnapshotId("acc_1");
73+
74+
expect(result).toBeUndefined();
75+
});
76+
5677
it("returns undefined when snapshot has no snapshot_id", async () => {
5778
mockSelectAccountSnapshots.mockResolvedValue([
5879
{ snapshot_id: null, account_id: "acc_1", expires_at: null },

lib/sandbox/getValidSnapshotId.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ export async function getValidSnapshotId(accountId: string): Promise<string | un
1111
const snapshot = accountSnapshots[0];
1212
if (!snapshot?.snapshot_id) return undefined;
1313

14-
if (snapshot.expires_at && new Date(snapshot.expires_at) < new Date()) {
15-
return undefined;
14+
if (snapshot.expires_at) {
15+
const expiresAt = new Date(snapshot.expires_at).getTime();
16+
if (Number.isNaN(expiresAt) || expiresAt <= Date.now()) {
17+
return undefined;
18+
}
1619
}
1720

1821
return snapshot.snapshot_id;

0 commit comments

Comments
 (0)