fix(pg): throw on invalid Date instead of serializing NaN string#3657
Closed
terminalchai wants to merge 1 commit intobrianc:masterfrom
Closed
fix(pg): throw on invalid Date instead of serializing NaN string#3657terminalchai wants to merge 1 commit intobrianc:masterfrom
terminalchai wants to merge 1 commit intobrianc:masterfrom
Conversation
new Date(undefined) and new Date('invalid') produce an invalid Date
object whose getTime() returns NaN. Previously prepareValue() would
pass such a date straight through to dateToString() / dateToStringUTC()
which format each NaN component with padStart(), producing the
meaningless string '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN' that Postgres
cannot parse.
Add an isNaN(val.getTime()) guard in the isDate branch of prepareValue()
and throw an informative error immediately, so callers discover the bug
at the JS level rather than receiving a cryptic Postgres error.
Fixes brianc#3318
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
Problem
Passing an invalid Date (e.g.
ew Date(undefined) or
ew Date('not a date')) to a query currently produces the string '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN'\ which Postgres cannot parse in any useful way, resulting in a confusing server-side error far from the real problem.
\\js
// before
client.query('SELECT ::timestamptz', [new Date(undefined)])
// → sends literal string '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN' to Postgres
\\
Root cause
In \packages/pg/lib/utils.js, \prepareValue()\ detects \Date\ objects via \isDate(val)\ and immediately passes them to \dateToString()\ / \dateToStringUTC(). Those functions call \date.getFullYear(), \date.getMonth(), etc., which all return \NaN\ for an invalid Date. \String(NaN).padStart(2, '0')\ produces 'NaN', so the resulting timestamp string is garbage.
Fix
Add an \isNaN(val.getTime())\ guard at the top of the \isDate\ branch and throw a clear error:
\\js
if (isDate(val)) {
if (isNaN(val.getTime())) {
throw new Error('date is invalid')
}
// ...
}
\\
\\js
// after
client.query('SELECT ::timestamptz', [new Date(undefined)])
// → throws Error: date is invalid ← caught immediately in JS
\\
Tests
Added two tests to \packages/pg/test/unit/utils-tests.js:
ew Date(undefined)\
ew Date('not a date')\
All existing date tests continue to pass.