[PR #8248] [MERGED] fix(core): revive date strings in safeJSONParse for pre-parsed objects #7851

Closed
opened 2026-03-13 13:51:05 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8248
Author: @himself65
Created: 3/1/2026
Status: Merged
Merged: 3/1/2026
Merged by: @himself65

Base: canaryHead: fix/safe-json-parse-dates


📝 Commits (2)

  • f71a1c3 fix(core): revive date strings in safeJSONParse for pre-parsed objects
  • c8504a7 fix(test): await runMigrations in safeJSONParse tests

📊 Changes

2 files changed (+187 additions, -12 deletions)

View changed files

📝 packages/better-auth/src/db/internal-adapter.test.ts (+144 -0)
📝 packages/core/src/utils/json.ts (+43 -12)

📄 Description

Summary

  • Fix safeJSONParse to convert ISO 8601 date strings to Date instances when the input is an already-parsed object (e.g., from Redis clients that auto-parse JSON)
  • Previously, the date reviver only ran on JSON.parse string input — pre-parsed objects were returned as-is with date fields still as strings
  • This caused silent failures in 40+ call sites doing expiresAt < new Date(), since string < Date coerces to NaN and always returns false — expired tokens were never detected as expired

Test plan

  • Added 4 new tests in internal-adapter.test.ts under "safeJSONParse date revival in secondary storage":
    • Verifies findVerificationValue returns Date instances when storage returns pre-parsed objects
    • Verifies expiration comparisons work correctly (expiresAt > new Date())
    • Verifies Date objects are consistent across multiple reads
    • Verifies non-date string fields are preserved (not converted)
  • All existing tests pass (internal-adapter: 33/33, secondary-storage: 2/2, magic-link: 16/16, email-otp: 70/70, two-factor: 34/34, password: 16/16, one-time-token: 13/13)
  • pnpm typecheck, pnpm lint, pnpm build all pass

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8248 **Author:** [@himself65](https://github.com/himself65) **Created:** 3/1/2026 **Status:** ✅ Merged **Merged:** 3/1/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/safe-json-parse-dates` --- ### 📝 Commits (2) - [`f71a1c3`](https://github.com/better-auth/better-auth/commit/f71a1c309e2510dd12b47a0baa2353347880c9ae) fix(core): revive date strings in safeJSONParse for pre-parsed objects - [`c8504a7`](https://github.com/better-auth/better-auth/commit/c8504a789b8eba7116e1f6a5f1551d7347fb4a3c) fix(test): await runMigrations in safeJSONParse tests ### 📊 Changes **2 files changed** (+187 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/internal-adapter.test.ts` (+144 -0) 📝 `packages/core/src/utils/json.ts` (+43 -12) </details> ### 📄 Description ## Summary - Fix `safeJSONParse` to convert ISO 8601 date strings to `Date` instances when the input is an already-parsed object (e.g., from Redis clients that auto-parse JSON) - Previously, the date reviver only ran on `JSON.parse` string input — pre-parsed objects were returned as-is with date fields still as strings - This caused silent failures in 40+ call sites doing `expiresAt < new Date()`, since `string < Date` coerces to `NaN` and always returns `false` — expired tokens were never detected as expired ## Test plan - [x] Added 4 new tests in `internal-adapter.test.ts` under `"safeJSONParse date revival in secondary storage"`: - Verifies `findVerificationValue` returns `Date` instances when storage returns pre-parsed objects - Verifies expiration comparisons work correctly (`expiresAt > new Date()`) - Verifies `Date` objects are consistent across multiple reads - Verifies non-date string fields are preserved (not converted) - [x] All existing tests pass (internal-adapter: 33/33, secondary-storage: 2/2, magic-link: 16/16, email-otp: 70/70, two-factor: 34/34, password: 16/16, one-time-token: 13/13) - [x] `pnpm typecheck`, `pnpm lint`, `pnpm build` all pass --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:51:05 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#7851