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`,