Skip to content

fix(pg): deprecate invalid Date query params instead of serializing NaN string#3706

Closed
momomuchu wants to merge 1 commit into
brianc:masterfrom
momomuchu:fix/invalid-date-serialization-3318
Closed

fix(pg): deprecate invalid Date query params instead of serializing NaN string#3706
momomuchu wants to merge 1 commit into
brianc:masterfrom
momomuchu:fix/invalid-date-serialization-3318

Conversation

@momomuchu

Copy link
Copy Markdown

Summary

Fixes #3318. Passing an invalid Date (e.g. new Date(undefined), whose .getTime() is NaN) as a query parameter was serialized by prepareValue() as the literal string "0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN", which Postgres cannot parse.

Per @brianc's approved direction in the issue thread (deprecate in pg8, throw in pg9), invalid dates now emit a DeprecationWarning and serialize to NULL in pg8; pg9 will throw.

Root cause

packages/pg/lib/utils.js, the isDate(val) branch of prepareValue() — an invalid Date fell through to dateToString(...) and produced the NaN string.

Fix

  • In prepareValue, an invalid Date (isNaN(val.getTime())) now emits a one-time DeprecationWarning (via the same util.deprecate pattern already used elsewhere in this codebase) and returns null instead of the garbage string.
  • Also fixes a crash this surfaced in arrayString(): it called escapeElement(prepareValue(item)) and only null-guarded the raw item, not prepareValue's return value — so an array containing an invalid Date would throw TypeError: Cannot read properties of null (reading 'replace'). It now null-checks the prepared value.

Test evidence

Two new unit tests in packages/pg/test/unit/utils-tests.js (given/when/then): an invalid Date scalar and an invalid Date inside an array both (a) never produce a NaN string and (b) emit the deprecation warning. Verified RED before the fix (both emit the garbage string) → GREEN after. Full packages/pg unit suite passes with no regressions; no live Postgres required.

…aN string

new Date(undefined) (and any Date whose .getTime() is NaN) was serialized
by prepareValue() as the literal string "0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN",
which Postgres cannot meaningfully parse.

Per the maintainer-approved direction in brianc#3318 (charmander/brianc,
2026-04-16), this follows the staged deprecation used elsewhere in this
codebase (see client.js's nodeUtils.deprecate() notices targeting pg@9.0):
invalid dates now serialize to NULL and emit a DeprecationWarning in pg@8,
and will throw an error in pg@9.

Also fixes a latent bug this surfaced in arrayString(): it called
escapeElement(prepareValue(item)) without checking whether prepareValue's
*result* was null, which crashed once prepareValue could return null for
a non-null input (an invalid Date). It now treats a null prepared value
the same as a null item.

Fixes brianc#3318

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@charmander charmander closed this Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not serialize new Date(undefined) as "0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN"

2 participants