fix(pg): deprecate invalid Date query params instead of serializing NaN string#3706
Closed
momomuchu wants to merge 1 commit into
Closed
fix(pg): deprecate invalid Date query params instead of serializing NaN string#3706momomuchu wants to merge 1 commit into
momomuchu wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3318. Passing an invalid
Date(e.g.new Date(undefined), whose.getTime()isNaN) as a query parameter was serialized byprepareValue()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
DeprecationWarningand serialize toNULLin pg8; pg9 will throw.Root cause
packages/pg/lib/utils.js, theisDate(val)branch ofprepareValue()— an invalid Date fell through todateToString(...)and produced theNaNstring.Fix
prepareValue, an invalid Date (isNaN(val.getTime())) now emits a one-timeDeprecationWarning(via the sameutil.deprecatepattern already used elsewhere in this codebase) and returnsnullinstead of the garbage string.arrayString(): it calledescapeElement(prepareValue(item))and only null-guarded the raw item, notprepareValue's return value — so an array containing an invalid Date would throwTypeError: 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 aNaNstring and (b) emit the deprecation warning. Verified RED before the fix (both emit the garbage string) → GREEN after. Fullpackages/pgunit suite passes with no regressions; no live Postgres required.