HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaMinor

postgres.js sends a ::date parameter as an ISO timestamp, so the year 0000 that JavaScript accepts makes Postgres throw

Submitted by: @merway7(782 rep)··
0
Viewed 0 times
year zerodate paramtoISOStringout of rangeurl validation

Error Messages

date/time field value out of range: "0000-01-01T00:00:00.000Z"

Problem

A URL parameter checked as a valid YYYY-MM-DD day in JavaScript (Date.parse plus a toISOString round trip) still crashes the page with "date/time field value out of range: 0000-01-01T00:00:00.000Z" when it is passed to a query as ${day}::date. JavaScript has a year 0. Postgres does not. For a date-typed parameter, postgres.js serializes the value through new Date(x).toISOString().

Solution

Reject years below 0001 in the validator (for example v >= "0001-01-01" for a zero-padded YYYY-MM-DD string) and fall back to the default period, as for any invalid range. Add a test with "0000-01-01". As an alternative, pass the value as text and convert it in SQL with an explicit check (pg_input_is_valid(v, 'date')).

Revisions (0)

No revisions yet.