[PR #5327] [MERGED] fix: respect additionalFields returned config for user data when setting cookie cache #5930

Closed
opened 2026-03-13 12:41:42 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5327
Author: @ahmed-abdat
Created: 10/15/2025
Status: Merged
Merged: 10/15/2025
Merged by: @Bekacru

Base: canaryHead: fix/cookie-cache-field-filtering-5293


📝 Commits (3)

  • 977d408 fix(cookies): respect additionalFields returned config for user data
  • 3179081 chore: update test
  • e0f4019 chore: lint

📊 Changes

2 files changed (+264 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/cookies/cookies.test.ts (+260 -0)
📝 packages/better-auth/src/cookies/index.ts (+4 -1)

📄 Description

Fixes #5293

Problem

The setCookieCache function includes ALL user fields in the cookie cache, even when additionalFields[key].returned: false is configured. This causes:

  • Cookie size limit issues (>4093 bytes) with large fields
  • Security concerns (sensitive data in cookies)
  • Inconsistent behavior (session fields ARE filtered correctly)

Solution

Applied parseUserOutput filtering to user fields, matching the existing pattern used for session fields (lines 110-120) and in internal-adapter.ts (line 406).

Changes

  • Import parseUserOutput from db/schema.ts
  • Filter user data before adding to cookie cache: parseUserOutput(ctx.context.options, session.user)
  • Maintains backward compatibility: unknown fields still included

Tests

  • User fields with returned: false excluded from cookie cache
  • Multiple user fields with mixed returned configuration filtered correctly
  • Cookie size reduced when large fields excluded
  • Session field filtering unchanged (regression check)
  • Backward compatibility maintained for unknown fields
  • All existing tests pass

Summary by cubic

Filters user fields in the session cookie cache to respect the returned config in additionalFields, preventing oversized cookies and sensitive data exposure. Aligns user filtering with session filtering and resolves #5293.

  • Bug Fixes
    • Apply parseUserOutput to user data before caching.
    • Exclude fields with returned: false; keep unknown/standard fields for backward compatibility.
    • Leave existing session field filtering unchanged.

🔄 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/5327 **Author:** [@ahmed-abdat](https://github.com/ahmed-abdat) **Created:** 10/15/2025 **Status:** ✅ Merged **Merged:** 10/15/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/cookie-cache-field-filtering-5293` --- ### 📝 Commits (3) - [`977d408`](https://github.com/better-auth/better-auth/commit/977d4087ea5dea9681a7ba6250ddb346cbd54960) fix(cookies): respect additionalFields returned config for user data - [`3179081`](https://github.com/better-auth/better-auth/commit/3179081cd15c1124a47b20a743d9ea4ba7b147c5) chore: update test - [`e0f4019`](https://github.com/better-auth/better-auth/commit/e0f401961be08b2ac3deede0d9deb072bd59cab0) chore: lint ### 📊 Changes **2 files changed** (+264 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/cookies/cookies.test.ts` (+260 -0) 📝 `packages/better-auth/src/cookies/index.ts` (+4 -1) </details> ### 📄 Description Fixes #5293 ### Problem The `setCookieCache` function includes ALL user fields in the cookie cache, even when `additionalFields[key].returned: false` is configured. This causes: - Cookie size limit issues (>4093 bytes) with large fields - Security concerns (sensitive data in cookies) - Inconsistent behavior (session fields ARE filtered correctly) ### Solution Applied `parseUserOutput` filtering to user fields, matching the existing pattern used for session fields (lines 110-120) and in `internal-adapter.ts` (line 406). ### Changes - Import `parseUserOutput` from `db/schema.ts` - Filter user data before adding to cookie cache: `parseUserOutput(ctx.context.options, session.user)` - Maintains backward compatibility: unknown fields still included ### Tests - [x] User fields with `returned: false` excluded from cookie cache - [x] Multiple user fields with mixed `returned` configuration filtered correctly - [x] Cookie size reduced when large fields excluded - [x] Session field filtering unchanged (regression check) - [x] Backward compatibility maintained for unknown fields - [x] All existing tests pass <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Filters user fields in the session cookie cache to respect the returned config in additionalFields, preventing oversized cookies and sensitive data exposure. Aligns user filtering with session filtering and resolves #5293. - **Bug Fixes** - Apply parseUserOutput to user data before caching. - Exclude fields with returned: false; keep unknown/standard fields for backward compatibility. - Leave existing session field filtering unchanged. <!-- End of auto-generated description by cubic. --> --- <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 12:41:42 -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#5930