diff --git a/src/cli/parser.test.ts b/src/cli/parser.test.ts index 1d5630c..5348da8 100644 --- a/src/cli/parser.test.ts +++ b/src/cli/parser.test.ts @@ -177,6 +177,15 @@ describe("option helpers", () => { expect(parseDelegationExpireHours(2)).toBe(2); expect(parseHoseExpireSeconds(2)).toBe(7200); }); + + test("sub-second input stays a usage error on both --expire flags", () => { + // Regression: the shared tokenizer understands `ms` for the simulation + // windows, and without a per-caller unit set that grammar leaked into + // --expire, silently rounding 500ms up to a 1h / 1s link. + expect(() => parseDelegationExpireHours("500ms")).toThrow("必须为有效时长格式"); + expect(() => parseHoseExpireSeconds("500ms")).toThrow("必须为有效时长"); + expect(() => parseHoseExpireSeconds("200ms")).toThrow("必须为有效时长"); + }); }); describe("flag registry cross-check", () => { diff --git a/src/cli/parser.ts b/src/cli/parser.ts index 5ed41f7..5a246a9 100644 --- a/src/cli/parser.ts +++ b/src/cli/parser.ts @@ -1,7 +1,7 @@ import type { WebhookMode } from "../domains/seal/types.js"; import type { MaterialPackFilePolicyPatch } from "../domains/seal/material-pack/policy.js"; import type { ParsedArgs } from "./types.js"; -import { type DurationToken, expireSeconds, tokenizeDuration } from "../core/duration.js"; +import { type DurationToken, EXPIRE_UNITS, expireSeconds, tokenizeDuration } from "../core/duration.js"; import { usageError } from "./errors.js"; /** @@ -453,7 +453,7 @@ function readExpireToken( `Invalid --expire: ${value}。必须为大于 0 的有效时长${options.unitlessHint ? `,推荐使用 ${options.unitlessHint}` : ""}。` ); } - const token = tokenizeDuration(value); + const token = tokenizeDuration(value, { units: EXPIRE_UNITS }); if (!token) throw usageError(`Invalid --expire: ${value}。`); return token; } @@ -462,7 +462,7 @@ function readExpireToken( throw usageError(`Invalid --expire: ${String(value)}。`); } - const token = tokenizeDuration(value); + const token = tokenizeDuration(value, { units: EXPIRE_UNITS }); if (!token) { throw usageError(`Invalid --expire: '${value}'。必须为有效时长格式${options.formatHint}`); } diff --git a/src/core/duration.ts b/src/core/duration.ts index 503f6b3..26e518b 100644 --- a/src/core/duration.ts +++ b/src/core/duration.ts @@ -42,6 +42,15 @@ const UNIT_ALIASES: Record = { const DURATION_PATTERN = /^(\d+(?:\.\d+)?)\s*([a-z]+)?$/i; +export interface TokenizeDurationOptions { + /** + * Units this caller accepts. Restricting the set keeps a shared tokenizer + * from quietly widening a flag's grammar: sub-second input was a usage error + * for `--expire` before the parsers were unified, and must stay one. + */ + units?: readonly DurationUnit[]; +} + /** * Parse a duration literal. Returns `undefined` when the input cannot be read * at all — callers decide whether that is a usage error or a default. @@ -49,7 +58,10 @@ const DURATION_PATTERN = /^(\d+(?:\.\d+)?)\s*([a-z]+)?$/i; * A unitless number keeps whatever magnitude it had; pair it with * {@link expireSeconds} or an explicit `unitless` argument to resolve it. */ -export function tokenizeDuration(value: string | number): DurationToken | undefined { +export function tokenizeDuration( + value: string | number, + options: TokenizeDurationOptions = {} +): DurationToken | undefined { if (typeof value === "number") { return Number.isFinite(value) ? { value, unit: undefined } : undefined; } @@ -71,9 +83,18 @@ export function tokenizeDuration(value: string | number): DurationToken | undefi if (!unit) { return undefined; } + if (options.units && !options.units.includes(unit)) { + return undefined; + } return { value: parseFloat(match[1]), unit }; } +/** + * Units the `--expire` flags accept. Sub-second precision is meaningless for + * an assistance link and was rejected before the parsers were shared. + */ +export const EXPIRE_UNITS: readonly DurationUnit[] = ["d", "h", "m", "s"]; + /** Convert a token to seconds, resolving a unitless value as `unitless`. */ export function durationSeconds(token: DurationToken, unitless: DurationUnit): number { switch (token.unit ?? unitless) {