Skip to content

Reject out-of-range fields in ISO date strings - #2327

Open
0z5a wants to merge 1 commit into
meta:static_hfrom
0z5a:date-parse-field-range
Open

0z5a wants to merge 1 commit into
meta:static_hfrom
0z5a:date-parse-field-range

Conversation

@0z5a

@0z5a 0z5a commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1148 (and its closed duplicate #1291).

Date.parse and new Date(string) accept an ISO date-time string with an out-of-range field and roll it over instead of returning NaN:

Input Before After V8
new Date('2024-13-08T00:00:00Z') 2025-01-08 NaN NaN
new Date('1999-00-00') 1998-11-30 NaN NaN
Date.parse('2020-01-32') Feb 1 NaN NaN
Date.parse('2020-01-01T25:00Z') Jan 2, 01:00 NaN NaN
Date.parse('2020-01-01T23:59:60Z') Jan 2, 00:00 NaN NaN
Date.parse('2020-01-01T00:00+25:00') a time shifted by 25 h NaN NaN

parseISODate reads every field with scanInt and passes it straight to makeDay / makeTime, which carry any excess into the next unit. The Date Time String Format section (ES2024 21.4.1.32) says "A string containing out-of-bounds or nonconforming elements is not a valid instance of this format", and the earlier comment on #1148 said that the V8-style validation is what the spec seems to require.

This change adds three range checks in parseISODate, in the existing early-return style:

  • month 1–12 and day 1–31, right after the date part;
  • hour 0–24, minute and second 0–59, and hour 24 only as exactly 24:00:00.000, right after the time part (before the local-time return, so strings without an offset are covered too);
  • UTC offset hours 0–23 and minutes 0–59, checked before the sign is applied.

When parseISODate returns NaN, parseDate still tries parseESDate. None of the inputs above parse there, including the space-separated form ('2020-01-01 25:00'), so the result stays NaN.

The day is only checked against 31, not against the length of the month, so 2020-02-30 still rolls over to March 1. This matches V8. The #1148 comment noted that matching JSC and SpiderMonkey, which reject it, would take more work, so I left that out. Other ISO-format differences I saw (single-digit fields such as 2020-1-01, the -000000 year) are separate and not changed here.

Behaviour that changes besides the rollover cases: a leap second (:60), hour 24 with a non-zero remainder, and offsets of 24:00 or more now give NaN, as in V8.

How I found it

I was running the same set of built-in calls through the hermes CLI and node v24.14.1 and diffing the output. Date.parse('2020-13-01') printed a time in 2021 under hermes and NaN under node. Reading parseISODate in lib/VM/JSLib/DateUtil.cpp showed there is no range check on any field. Then I found #1148, which reports the same thing for 1999-00-00.

Where this can show up in practice

This is my estimate; I have no usage data. React Native apps often check a date that a user typed or that came from an API with isNaN(new Date(s).getTime()) or isNaN(Date.parse(s)). On Hermes, a string like 2024-13-08 passes that check and silently becomes a date in the next year, while the same code on JSC or in a browser rejects it. #1148 and #1291 are two reports of this from React Native users.

Test Plan

  • New test/hermes/date-parse-field-range.js (same RUN lines as date-parse-time-clip.js): each out-of-range field gives NaN, including the strings from new Date() not throwing an Invalid Date error #1148 and Date constructor accept wrong ISO 8601 date #1291, a local-time string, a space-separated string and a negative offset. The largest valid value of each field still parses, and 2020-02-30 still rolls over. I checked every expected value against node v24.14.1 with TZ=MST+7.
  • hermes-lit on all of test/hermes (the check-hermes command, pointed at that directory): 663 passed, 3 expected failures, 45 unsupported, 0 failed. The new test passes on both RUN lines (hermes -O and shermes -exec).
  • The diff run that found this (608 cases over built-ins): the only output that changed with this patch is the :60 seconds case, which now prints NaN like node.
  • clang-format reports no changes for DateUtil.cpp.
  • Release build with Apple clang on macOS arm64, on static_h @ e5d8795.

parseISODate passed every field straight to makeDay/makeTime, so an
out-of-range month, day, hour, minute, second or UTC offset rolled over
instead of making the string invalid: new Date('2024-13-08T00:00:00Z')
was 2025-01-08 and new Date('1999-00-00') was 1998-11-30. The Date Time
String Format says a string with out-of-bounds elements is not a valid
instance of the format, and V8 returns NaN for these.

Check month 1-12, day 1-31, hour 0-24 (24 only as 24:00:00.000), minute
and second 0-59, and offset hours 0-23 and minutes 0-59. As in V8, the
day is not checked against the length of the month, so 2020-02-30 still
rolls over into March.

Fixes meta#1148
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Nov 19, 2026
@0z5a
0z5a deployed to npm-publish November 19, 2026 05:12 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
npm-publish — fc10f17a Deployed Nov 19, 2026 by 0z5a via publish #1768
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new Date() not throwing an Invalid Date error

1 participant