fix(ci): reject malformed cache retention timestamps (#12215)

This commit is contained in:
ShuaixinHuang authored and GitHub committed 2026-11-19 06:34:31 -05:00
1 parent a997c3ee4b
commit 845e72ea5a
2 files changed
+60 -3

No files matched your search

+21 -2
View File
@@ -33,6 +33,23 @@ function assertRetentionPrefix(prefix) {
);
}
const CACHE_TIMESTAMP =
/^(?<date>\d{4}-\d{2}-\d{2})T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/u;
/** Accept timestamp strings only and rule out native Date rollover. */
function parseCacheTimestamp(value) {
if (typeof value !== "string") return NaN;
const date = CACHE_TIMESTAMP.exec(value)?.groups?.date;
if (date === undefined) return NaN;
const midnight = new Date(`${date}T00:00:00Z`);
if (
!Number.isFinite(midnight.getTime()) ||
midnight.toISOString().slice(0, 10) !== date
)
return NaN;
return Date.parse(value);
}
function normalizeCache(cache, index) {
if (typeof cache !== "object" || cache === null || Array.isArray(cache))
throw new Error(`Cache entry ${String(index + 1)} is malformed`);
@@ -41,8 +58,10 @@ function normalizeCache(cache, index) {
throw new Error(`Cache entry ${String(index + 1)} has an invalid id`);
if (typeof key !== "string" || key.length === 0 || /[\r\n]/u.test(key))
throw new Error(`Cache entry ${String(index + 1)} has an invalid key`);
const createdAt = Date.parse(cache.created_at);
const accessedAt = Date.parse(cache.last_accessed_at ?? cache.created_at);
const createdAt = parseCacheTimestamp(cache.created_at);
const accessedAt = parseCacheTimestamp(
cache.last_accessed_at ?? cache.created_at,
);
if (!Number.isFinite(createdAt) || !Number.isFinite(accessedAt))
throw new Error(
`Cache entry ${String(index + 1)} has an invalid timestamp`,
@@ -39,7 +39,13 @@ describe("Turbo snapshot retention", () => {
[
cache(1, `${prefix}old`, "2026-10-20T01:00:00Z", undefined, 10),
cache(2, `${prefix}older`, "2026-10-20T02:00:00Z", undefined, 20),
cache(3, `${prefix}recent`, "2026-10-20T03:00:00Z", undefined, 30),
cache(
3,
`${prefix}recent`,
"2026-10-20T04:00:00.125+01:00",
undefined,
30,
),
cache(4, keepKey, "2026-10-20T00:00:00Z", undefined, 40),
],
{ prefix, keepKey },
@@ -509,3 +515,35 @@ exit 1
).toThrow(/one Turbo family/u);
await expect(readFile(log, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
});
it.each([
{ created_at: "2026-02-30T00:00:00Z" },
{ created_at: 0 },
{ last_accessed_at: ["2026-04-01T00:00:00Z"] },
])(
"does not delete any cache when its timestamps are malformed: %j",
async (override) => {
const prefix = "rea-static-v1-Linux-X64-";
const deleted: number[] = [];
await expect(
retainCaches({
repository: "example/fixture",
families: [{ prefix }],
request: async () => ({
actions_caches: [
{
...cache(1, prefix + "old", "2026-01-01T00:00:00Z"),
...override,
},
cache(2, prefix + "middle", "2026-04-01T00:00:00Z"),
cache(3, prefix + "new", "2026-04-02T00:00:00Z"),
],
}),
deleteCache: async (id: number) => {
deleted.push(id);
},
}),
).rejects.toThrow("invalid timestamp");
expect(deleted).toEqual([]);
},
);