Skip to content

fix(core): recognize cross-realm Date references - #672

Open
maxazure wants to merge 1 commit into
wanasit:masterfrom
maxazure:fix-cross-realm-date-reference
Open

maxazure wants to merge 1 commit into
wanasit:masterfrom
maxazure:fix-cross-realm-date-reference

Conversation

@maxazure

Copy link
Copy Markdown

Closes #603.

A Date created in another realm fails the local instanceof Date check and is treated as a ParsingReference object, so a direct date reference silently falls back to the current time. Recognize dates using the intrinsic Date.prototype.getTime check instead. Invalid dates remain date references, while reference objects retain their instant and timezone; Symbol.toStringTag, getTime, and coercion methods alone do not turn an object into a date.

Reproduced through both built package entry points on Node 22.22.3 with TZ=UTC:

const chrono = require("chrono-node");
const { runInNewContext } = require("node:vm");
const instant = "2025-06-07T12:34:56.789Z";
const local = new Date(instant);
const foreign = runInNewContext("new Date(instant)", { instant });

chrono.parseDate("1 day ago", local).toISOString();
chrono.parseDate("1 day ago", foreign).toISOString();
// Both now return "2025-06-06T12:34:56.789Z".
// Before the change, the foreign date uses the current time minus one day.

Seven regression/control tests cover public parse/parseDate results and match boundaries, invalid local and foreign dates, a genuine date with an overridden tag/method, timezone-bearing reference objects, spoofed date tags, wrapped values, and omitted references. The direct foreign-date and invalid foreign-date cases failed before the fix; the existing-behavior controls passed.

Validation:

  • Pre-commit hook: 760 tests passed with one Jest worker-exit warning at default concurrency. A subsequent serial full run with --detectOpenHandles passed all 760 tests without a handle warning.
  • UTC complete coverage suite: 159 suites / 760 tests passed (pristine baseline: 158 / 753).
  • New York reference/result tests: 16 passed. Actual built CJS and ESM APIs passed under UTC, America/New_York, and Pacific/Auckland.
  • tsc --noEmit, CJS/ESM builds, required npm run prettier, full Prettier check, and git diff --check passed.
  • Pacific/Auckland complete suite has the same 8 existing DST failures as pristine master; all new tests pass there.
  • npm run eslint has the same pre-existing failure on master and this change: ESLint 9 expects a flat config, while the repository provides legacy configuration in package.json. No configuration/dependency changes are included.

The cross-realm reproduction uses Node vm; BigQuery and browser iframe execution were not tested directly. Public types and accepted text formats are unchanged.

Copilot AI balanced review requested due to automatic review settings October 11, 2026 19:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

This change alters core reference-date semantics shared by every locale and parser, so it warrants human sign-off despite no objective defects being found.

0 open findings

What changed in this PR

This PR fixes a cross-realm Date detection bug (issue #603) where a Date created in another JavaScript realm (e.g., a BigQuery UDF or an iframe) fails the local instanceof Date check in ReferenceWithTimezone.fromInput. As a result, such a reference date was misinterpreted as a ParsingReference object with no instant, silently falling back to the current time. The fix replaces the instanceof check with an intrinsic-based isDate helper, so genuine Dates from any realm are recognized while reference objects and date-like decoys are not.

Changes:

  • Added an isDate helper in src/results.ts that uses Date.prototype.getTime.call(input) (intrinsic slot check) to detect Dates across realms, including invalid Dates.
  • Switched ReferenceWithTimezone.fromInput to use isDate instead of input instanceof Date.
  • Added test/reference.test.ts with regression and control coverage for foreign/local, valid/invalid, spoofed-tag, wrapped, and omitted reference inputs.
File Description
src/​results.ts Introduces the realm-agnostic isDate helper and uses it in fromInput to correctly classify cross-realm Date references.
test/​reference.test.ts New tests covering cross-realm dates, invalid dates, timezone-bearing reference objects, spoofed Date tags/wrappers, and default-to-now fallbacks.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

This branch has not been deployed

No deployments
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.

parseDate function is not working as expected when used in Bigquery UDF

2 participants