fix(cron): guard list sorting against malformed legacy jobs (#28896)

* fix(cron): guard list sorting against malformed legacy jobs

Prevent list operations from crashing when old or corrupted cron entries are missing name/id fields by hardening sort comparators.

Closes #28862

* cron: format list sort guard test imports

---------

Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
Sid
2026-03-01 21:01:36 +08:00
committed by GitHub
parent 645d963954
commit 29a55948d6
2 changed files with 59 additions and 2 deletions

View File

@@ -164,7 +164,9 @@ function sortJobs(jobs: CronJob[], sortBy: CronJobsSortBy, sortDir: CronSortDir)
return jobs.toSorted((a, b) => {
let cmp = 0;
if (sortBy === "name") {
cmp = a.name.localeCompare(b.name, undefined, { sensitivity: "base" });
const aName = typeof a.name === "string" ? a.name : "";
const bName = typeof b.name === "string" ? b.name : "";
cmp = aName.localeCompare(bName, undefined, { sensitivity: "base" });
} else if (sortBy === "updatedAtMs") {
cmp = a.updatedAtMs - b.updatedAtMs;
} else {
@@ -183,7 +185,9 @@ function sortJobs(jobs: CronJob[], sortBy: CronJobsSortBy, sortDir: CronSortDir)
if (cmp !== 0) {
return cmp * dir;
}
return a.id.localeCompare(b.id);
const aId = typeof a.id === "string" ? a.id : "";
const bId = typeof b.id === "string" ? b.id : "";
return aId.localeCompare(bId);
});
}