From cbfabad4c02e3edd26343db05b1b127a5bc9be77 Mon Sep 17 00:00:00 2001 From: Marceli Pawlinski Date: Wed, 29 Jul 2026 20:27:06 +0100 Subject: [PATCH] fix: throw on invalid Date serialization instead of producing NaN string new Date(undefined) (an invalid Date) was serialized as '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN' which is meaningless and harmful for Postgres queries. Now throws a clear error. Closes #3318 --- packages/pg/lib/utils.js | 3 +++ packages/pg/test/unit/utils-tests.js | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/packages/pg/lib/utils.js b/packages/pg/lib/utils.js index 638b43970..e7b7af06f 100644 --- a/packages/pg/lib/utils.js +++ b/packages/pg/lib/utils.js @@ -54,6 +54,9 @@ const prepareValue = function (val, seen) { return Buffer.from(val.buffer, val.byteOffset, val.byteLength) } if (isDate(val)) { + if (isNaN(val.getTime())) { + throw new Error('Invalid Date value provided to query parameter') + } if (defaults.parseInputDatesAsUTC) { return dateToStringUTC(val) } else { diff --git a/packages/pg/test/unit/utils-tests.js b/packages/pg/test/unit/utils-tests.js index 5f75f6c2d..825b5580f 100644 --- a/packages/pg/test/unit/utils-tests.js +++ b/packages/pg/test/unit/utils-tests.js @@ -78,6 +78,18 @@ test('prepareValues: BC date prepared properly', function () { helper.resetTimezoneOffset() }) +test('prepareValues: invalid date throws error', function () { + assert.throws(function () { + utils.prepareValue(new Date(undefined)) + }, /Invalid Date/) + assert.throws(function () { + utils.prepareValue(new Date(NaN)) + }, /Invalid Date/) + assert.throws(function () { + utils.prepareValue(new Date('not a date')) + }, /Invalid Date/) +}) + test('prepareValues: 1 BC date prepared properly', function () { helper.setTimezoneOffset(-330)