Skip to content

Commit ba5d109

Browse files
fix(ui): validate cron intervals exactly
Co-authored-by: xingzhou <[email protected]>
1 parent 6693331 commit ba5d109

5 files changed

Lines changed: 84 additions & 16 deletions

File tree

‎ui/src/lib/cron/decimal.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
const CRON_POSITIVE_DECIMAL_RE = /^(?:\d+(?:\.\d*)?|\.\d+)$/u;
22

3-
export function parseCronPositiveDecimal(value: string): number | undefined {
3+
const CRON_EVERY_UNIT_MS = {
4+
minutes: 60_000,
5+
hours: 3_600_000,
6+
days: 86_400_000,
7+
} as const;
8+
9+
export type CronEveryUnit = keyof typeof CRON_EVERY_UNIT_MS;
10+
11+
export function parseCronEveryMs(value: string, unit: CronEveryUnit): number | undefined {
412
const trimmed = value.trim();
513
if (!CRON_POSITIVE_DECIMAL_RE.test(trimmed)) {
614
return undefined;
715
}
8-
const parsed = Number(trimmed);
9-
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
16+
const [wholePart, fractionalPart = ""] = trimmed.split(".");
17+
const wholeDigits = (wholePart || "0").replace(/^0+/u, "") || "0";
18+
const fractionalDigits = fractionalPart.replace(/0+$/u, "");
19+
// Days contain 2^10 milliseconds, the largest base-10 scale any supported
20+
// unit can cancel after trailing decimal zeroes are removed.
21+
if (wholeDigits.length > String(Number.MAX_SAFE_INTEGER).length || fractionalDigits.length > 10) {
22+
return undefined;
23+
}
24+
25+
const scale = 10n ** BigInt(fractionalDigits.length);
26+
const decimal = BigInt(wholeDigits) * scale + BigInt(fractionalDigits || "0");
27+
const scaledMilliseconds = decimal * BigInt(CRON_EVERY_UNIT_MS[unit]);
28+
if (scaledMilliseconds % scale !== 0n) {
29+
return undefined;
30+
}
31+
const milliseconds = scaledMilliseconds / scale;
32+
return milliseconds > 0n && milliseconds <= BigInt(Number.MAX_SAFE_INTEGER)
33+
? Number(milliseconds)
34+
: undefined;
1035
}

‎ui/src/lib/cron/index.test.ts‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,8 +1486,8 @@ describe("cron controller", () => {
14861486
expect(errors.deliveryTo).toBe("cron.errors.webhookUrlInvalid");
14871487
});
14881488

1489-
it.each(["0x10", "1e3", "+1"])(
1490-
"rejects nondecimal recurring amounts before submit: %s",
1489+
it.each(["0x10", "1e3", "+1", String(Number.MAX_SAFE_INTEGER), "0.000001"])(
1490+
"rejects invalid recurring amounts before submit: %s",
14911491
async (everyAmount) => {
14921492
const request = vi.fn(async (method: string) => {
14931493
if (method === "cron.add") {
@@ -1520,6 +1520,51 @@ describe("cron controller", () => {
15201520
},
15211521
);
15221522

1523+
it.each([
1524+
["1.5", "minutes", 90_000],
1525+
["4.1", "minutes", 246_000],
1526+
["0.1", "hours", 360_000],
1527+
["0.000125", "hours", 450],
1528+
["0.1", "days", 8_640_000],
1529+
["0.0009765625", "days", 84_375],
1530+
] as const)(
1531+
"converts %s %s to safe integer milliseconds",
1532+
async (everyAmount, everyUnit, expectedEveryMs) => {
1533+
const request = vi.fn(async (method: string) => {
1534+
if (method === "cron.add") {
1535+
return { id: "job-decimal" };
1536+
}
1537+
if (method === "cron.list") {
1538+
return { jobs: [] };
1539+
}
1540+
if (method === "cron.status") {
1541+
return { enabled: true, jobs: 0, nextWakeAtMs: null };
1542+
}
1543+
return {};
1544+
});
1545+
const state = createState({
1546+
client: { request } as unknown as CronState["client"],
1547+
cronForm: {
1548+
...DEFAULT_CRON_FORM,
1549+
name: "decimal interval",
1550+
everyAmount,
1551+
everyUnit,
1552+
payloadText: "run",
1553+
deliveryMode: "none",
1554+
},
1555+
});
1556+
1557+
const saved = await addCronJob(state);
1558+
1559+
expect(saved.saved).toBe(true);
1560+
const addCall = findRequestCall(request.mock.calls, "cron.add");
1561+
expect(requestPayload(addCall).schedule).toEqual({
1562+
kind: "every",
1563+
everyMs: expectedEveryMs,
1564+
});
1565+
},
1566+
);
1567+
15231568
it("does not require cron expression fields for on-exit schedules", () => {
15241569
const errors = validateCronForm({
15251570
...DEFAULT_CRON_FORM,

‎ui/src/lib/cron/index.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
isMissingOperatorReadScopeError,
2525
} from "../gateway-errors.ts";
2626
import { normalizeLowercaseStringOrEmpty, sortUniqueStrings } from "../string-coerce.ts";
27-
import { parseCronPositiveDecimal } from "./decimal.ts";
27+
import { parseCronEveryMs } from "./decimal.ts";
2828
import { loadCronFailingCount } from "./scope.ts";
2929

3030
export { loadCronFailingCount, loadCronScopeStats } from "./scope.ts";
@@ -294,7 +294,7 @@ export function validateCronForm(form: CronFormState): CronFieldErrors {
294294
errors.scheduleAt = "cron.errors.scheduleAtInvalid";
295295
}
296296
} else if (form.scheduleKind === "every") {
297-
if (parseCronPositiveDecimal(form.everyAmount) === undefined) {
297+
if (parseCronEveryMs(form.everyAmount, form.everyUnit) === undefined) {
298298
errors.everyAmount = "cron.errors.everyAmountInvalid";
299299
}
300300
} else if (form.scheduleKind === "cron") {
@@ -834,13 +834,11 @@ function buildCronSchedule(form: CronFormState) {
834834
return { kind: "at" as const, at: new Date(ms).toISOString() };
835835
}
836836
if (form.scheduleKind === "every") {
837-
const amount = parseCronPositiveDecimal(form.everyAmount);
838-
if (amount === undefined) {
837+
const everyMs = parseCronEveryMs(form.everyAmount, form.everyUnit);
838+
if (everyMs === undefined) {
839839
throw new Error(t("cron.errors.invalidIntervalAmount"));
840840
}
841-
const unit = form.everyUnit;
842-
const mult = unit === "minutes" ? 60_000 : unit === "hours" ? 3_600_000 : 86_400_000;
843-
return { kind: "every" as const, everyMs: amount * mult };
841+
return { kind: "every" as const, everyMs };
844842
}
845843
const expr = form.cronExpr.trim();
846844
if (!expr) {

‎ui/src/pages/cron/view.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,8 @@ describe("cron view editor", () => {
618618
expect(onceText).toContain("2026");
619619
});
620620

621-
it("hides the schedule summary for nondecimal recurring amounts", () => {
622-
for (const everyAmount of ["0x10", "1e3", "+1"]) {
621+
it("hides the schedule summary for recurring amounts that cannot produce safe milliseconds", () => {
622+
for (const everyAmount of ["0x10", "1e3", "+1", String(Number.MAX_SAFE_INTEGER), "0.000001"]) {
623623
const container = renderView({
624624
createOpen: true,
625625
form: { ...DEFAULT_CRON_FORM, scheduleKind: "every", everyAmount },

‎ui/src/pages/cron/view.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import "../../components/web-awesome.ts";
2626
import "../../components/web-awesome-popover.ts";
2727
import { t } from "../../i18n/index.ts";
2828
import { isCronJobActiveFailure, resolveCronJobLastRunStatus } from "../../lib/cron-status.ts";
29-
import { parseCronPositiveDecimal } from "../../lib/cron/decimal.ts";
29+
import { parseCronEveryMs } from "../../lib/cron/decimal.ts";
3030
import type {
3131
CronFieldErrors,
3232
CronFieldKey,
@@ -1236,7 +1236,7 @@ function renderGeneralSection(props: CronProps) {
12361236
function describeFormSchedule(form: CronFormState): string | null {
12371237
if (form.scheduleKind === "every") {
12381238
const amount = form.everyAmount.trim();
1239-
if (parseCronPositiveDecimal(amount) === undefined) {
1239+
if (parseCronEveryMs(amount, form.everyUnit) === undefined) {
12401240
return null;
12411241
}
12421242
if (Number(amount) === 1) {

0 commit comments

Comments
 (0)