fix: reject unsafe memory duration values · openclaw/openclaw@1ac8c71
steipete
·
2026-05-29
·
via Recent Commits to openclaw:main
File tree
packages/memory-host-sdk/src/host
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseDurationMs } from "./config-utils.js"; |
| 3 | + |
| 4 | +describe("parseDurationMs", () => { |
| 5 | +it("parses decimal durations into milliseconds", () => { |
| 6 | +expect(parseDurationMs("1.5s")).toBe(1_500); |
| 7 | +expect(parseDurationMs("1h30m")).toBe(5_400_000); |
| 8 | +}); |
| 9 | + |
| 10 | +it("rejects unsafe millisecond results", () => { |
| 11 | +expect(() => parseDurationMs("9007199254740993ms")).toThrow(/invalid duration/u); |
| 12 | +expect(() => parseDurationMs("9007199254740990ms10ms")).toThrow(/invalid duration/u); |
| 13 | +}); |
| 14 | +}); |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -156,6 +156,14 @@ const DURATION_MULTIPLIERS: Record<string, number> = {
|
156 | 156 | d: 86_400_000, |
157 | 157 | }; |
158 | 158 | |
| 159 | +function roundDurationMs(raw: string, value: number): number { |
| 160 | +const rounded = Math.round(value); |
| 161 | +if (!Number.isSafeInteger(rounded)) { |
| 162 | +throw new Error(`invalid duration: ${raw}`); |
| 163 | +} |
| 164 | +return rounded; |
| 165 | +} |
| 166 | + |
159 | 167 | export function normalizeAgentId(value: string | undefined | null): string { |
160 | 168 | const trimmed = (value ?? "").trim(); |
161 | 169 | if (!trimmed) { |
@@ -344,7 +352,7 @@ export function parseDurationMs(
|
344 | 352 | throw new Error(`invalid duration: ${raw}`); |
345 | 353 | } |
346 | 354 | const unit = single[2] ?? opts?.defaultUnit ?? "ms"; |
347 | | -return Math.round(value * (DURATION_MULTIPLIERS[unit] ?? 1)); |
| 355 | +return roundDurationMs(raw, value * (DURATION_MULTIPLIERS[unit] ?? 1)); |
348 | 356 | } |
349 | 357 | |
350 | 358 | let totalMs = 0; |
@@ -367,5 +375,5 @@ export function parseDurationMs(
|
367 | 375 | if (consumed !== trimmed.length || consumed === 0) { |
368 | 376 | throw new Error(`invalid duration: ${raw}`); |
369 | 377 | } |
370 | | -return Math.round(totalMs); |
| 378 | +return roundDurationMs(raw, totalMs); |
371 | 379 | } |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。