[PR #8328] fix(secondary-storage): improve JSON parse error handling #24803

Open
opened 2026-04-15 22:34:04 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8328
Author: @himself65
Created: 3/3/2026
Status: 🔄 Open

Base: mainHead: fix/secondary-storage-json-error-handling


📝 Commits (1)

  • 9ca9b4f fix(secondary-storage): improve JSON parse error handling

📊 Changes

3 files changed (+91 additions, -49 deletions)

View changed files

📝 packages/better-auth/src/db/internal-adapter.ts (+33 -48)
📝 packages/better-auth/src/db/secondary-storage.test.ts (+50 -0)
📝 packages/core/src/utils/json.ts (+8 -1)

📄 Description

Summary

  • Replace raw JSON.parse calls in listSessions and findSessions with safeJSONParse, so corrupt or double-parsed values no longer crash the server
  • Improve the error message in safeJSONParse to guide users when their secondaryStorage.get() returns unexpected types (e.g. already-parsed objects instead of JSON strings)
  • Add a test covering the common mistake of double-parsing JSON in a custom secondaryStorage.get() implementation

Test plan

  • Existing secondary storage tests pass (string return + object return)
  • New double-parse scenario test passes end-to-end (sign in, get session, list sessions, revoke)
  • Internal adapter tests pass (33 tests including corrupt data scenarios)
  • Magic link secondary storage tests pass
  • pnpm typecheck passes

🔄 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/8328 **Author:** [@himself65](https://github.com/himself65) **Created:** 3/3/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/secondary-storage-json-error-handling` --- ### 📝 Commits (1) - [`9ca9b4f`](https://github.com/better-auth/better-auth/commit/9ca9b4fa547807a58ef31d2d746028b2d82c323a) fix(secondary-storage): improve JSON parse error handling ### 📊 Changes **3 files changed** (+91 additions, -49 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/internal-adapter.ts` (+33 -48) 📝 `packages/better-auth/src/db/secondary-storage.test.ts` (+50 -0) 📝 `packages/core/src/utils/json.ts` (+8 -1) </details> ### 📄 Description ## Summary - Replace raw `JSON.parse` calls in `listSessions` and `findSessions` with `safeJSONParse`, so corrupt or double-parsed values no longer crash the server - Improve the error message in `safeJSONParse` to guide users when their `secondaryStorage.get()` returns unexpected types (e.g. already-parsed objects instead of JSON strings) - Add a test covering the common mistake of double-parsing JSON in a custom `secondaryStorage.get()` implementation ## Test plan - [x] Existing secondary storage tests pass (string return + object return) - [x] New double-parse scenario test passes end-to-end (sign in, get session, list sessions, revoke) - [x] Internal adapter tests pass (33 tests including corrupt data scenarios) - [x] Magic link secondary storage tests pass - [x] `pnpm typecheck` passes --- <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-04-15 22:34:04 -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#24803