Skip to content

Commit 41366d3

Browse files
committed
fix: ignore unsafe timestamp values
1 parent bd773d2 commit 41366d3

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/agents/date-time.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from "vitest";
2+
import { normalizeTimestamp } from "./date-time.js";
3+
4+
describe("normalizeTimestamp", () => {
5+
it("normalizes numeric second and millisecond timestamps", () => {
6+
expect(normalizeTimestamp("1700000000")).toEqual({
7+
timestampMs: 1_700_000_000_000,
8+
timestampUtc: "2023-11-14T22:13:20.000Z",
9+
});
10+
expect(normalizeTimestamp("1700000000000")).toEqual({
11+
timestampMs: 1_700_000_000_000,
12+
timestampUtc: "2023-11-14T22:13:20.000Z",
13+
});
14+
});
15+
16+
it("ignores unsafe or out-of-range numeric timestamp strings", () => {
17+
expect(normalizeTimestamp("9007199254740993")).toBeUndefined();
18+
expect(normalizeTimestamp("999999999999999999999999")).toBeUndefined();
19+
});
20+
});

src/agents/date-time.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ export type ResolvedTimeFormat = "12" | "24";
55

66
let cachedTimeFormat: ResolvedTimeFormat | undefined;
77

8+
function buildNormalizedTimestamp(
9+
timestampMs: number,
10+
): { timestampMs: number; timestampUtc: string } | undefined {
11+
if (!Number.isSafeInteger(timestampMs)) {
12+
return undefined;
13+
}
14+
const timestampUtc = new Date(timestampMs).toISOString();
15+
return { timestampMs, timestampUtc };
16+
}
17+
818
export function resolveUserTimezone(configured?: string): string {
919
const trimmed = configured?.trim();
1020
if (trimmed) {
@@ -69,7 +79,11 @@ export function normalizeTimestamp(
6979
if (timestampMs === undefined || !Number.isFinite(timestampMs)) {
7080
return undefined;
7181
}
72-
return { timestampMs, timestampUtc: new Date(timestampMs).toISOString() };
82+
try {
83+
return buildNormalizedTimestamp(timestampMs);
84+
} catch {
85+
return undefined;
86+
}
7387
}
7488

7589
export function withNormalizedTimestamp<T extends Record<string, unknown>>(

0 commit comments

Comments
 (0)