Repository navigation
Description
Summary
encode() accepts an Invalid Date (a Date whose getTime() is NaN) with no error. It writes the timestamp 0 in the timestamp 96 format: c7 0c ff 00 00 00 00 00 00 00 00 00 00 00 00. Thus decode() gives the valid date 1970-01-01T00:00:00.000Z, and the receiver cannot see that the value was not a date.
The cause: in encodeDateToTimeSpec(), sec and nsec are NaN. encodeTimeSpecToTimestamp() then selects the timestamp 96 format, because each comparison with NaN is false. DataView writes NaN as 0.
For comparison, Date.prototype.toISOString() throws a RangeError for an Invalid Date, and JSON.stringify() writes null.
Version: 3.1.3. src/timestamp.ts on main (6d4b666) is the same.
Repro
npm install @msgpack/msgpack@3.1.3, then node repro.ts with Node.js 22.18 or later:
// F0138: @msgpack/msgpack 3.1.3 encodes an Invalid Date (getTime() is NaN) with no error. encodeDateToTimeSpec gives NaN
// for sec and nsec, encodeTimeSpecToTimestamp then writes the timestamp 96 format, and DataView writes NaN as 0.
// Thus decode() gives the valid date 1970-01-01T00:00:00.000Z.
import { decode, encode } from "@msgpack/msgpack";
const hex = (bytes: Uint8Array): string => Buffer.from(bytes).toString("hex").replace(/(..)/g, "$1 ").trim();
const roundTrip = (date: Date): string => {
let bytes: Uint8Array;
try {
bytes = encode(date);
} catch (error) {
return `encode() throws: ${String(error)}`;
}
const back = decode(bytes) as Date;
return `bytes ${hex(bytes)}, decode() gives ${Number.isNaN(back.getTime()) ? "Invalid Date" : back.toISOString()}`;
};
const control = new Date("2026-11-17T12:34:56.789Z");
console.log(`control ${control.toISOString()}: ${roundTrip(control)}`);
const invalid = new Date(Number.NaN);
const result = roundTrip(invalid);
console.log(`Invalid Date: ${result}`);
const parsed = new Date("not a date");
console.log(`new Date("not a date"): ${roundTrip(parsed)}`);
if (result.includes("decode() gives 1970-01-01T00:00:00.000Z")) {
console.log("BUG PRESENT: encode() writes an Invalid Date as the timestamp 0, so decode() gives the valid date 1970-01-01T00:00:00.000Z.");
process.exit(1);
}
console.log("OK: an Invalid Date does not become a valid date.");Output
control 2026-11-17T12:34:56.789Z: bytes d7 ff bc 1c bd 00 6a fc 49 f0, decode() gives 2026-11-17T12:34:56.789Z
Invalid Date: bytes c7 0c ff 00 00 00 00 00 00 00 00 00 00 00 00, decode() gives 1970-01-01T00:00:00.000Z
new Date("not a date"): bytes c7 0c ff 00 00 00 00 00 00 00 00 00 00 00 00, decode() gives 1970-01-01T00:00:00.000Z
BUG PRESENT: encode() writes an Invalid Date as the timestamp 0, so decode() gives the valid date 1970-01-01T00:00:00.000Z.
The CI run of the repro (Node.js on Ubuntu): https://github.com/mark1russell7/wirelab/actions/runs/44229138782
Expected
encode() does not change an Invalid Date into a valid date. For example, it throws a RangeError, as toISOString() does.
Possible fix
encodeTimestampExtension() (or encodeDateToTimeSpec()) can throw a RangeError when Number.isNaN(date.getTime()). I can send a pull request with a test if you want one, or with a different behavior if you prefer one.
An AI coding assistant (Claude Code) found this fault, wrote the repro and wrote this text.
Activity