[PR #6789] [MERGED] fix: preserve = padding in parsed cookies #6884

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6789
Author: @Shridhad
Created: 12/16/2025
Status: Merged
Merged: 12/16/2025
Merged by: @himself65

Base: canaryHead: fix/parse-cookies


📝 Commits (1)

  • f238277 fix: securely parse request cookies

📊 Changes

2 files changed (+37 additions, -2 deletions)

View changed files

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

📄 Description

Sometimes the signed cookies end with signature padding =, which causes issue with parseCookies to exclude them while parsing request cookies.

The current implementation splits each cookie with = to format into key-value pair, however, if cookie value contains = it is split into into three parts, ignoring the last part. Example breaking tests:

better-auth:test:  FAIL  src/cookies/cookies.test.ts > parse cookies > should securely parse the signed cookies with padding
better-auth:test: AssertionError: expected 'session-token.signature' to be 'session-token.signature=' // Object.is equality
better-auth:test:
better-auth:test: Expected: "session-token.signature="
better-auth:test: Received: "session-token.signature"
better-auth:test:
better-auth:test:  ❯ src/cookies/cookies.test.ts:1121:58
better-auth:test:     1119|   const parsedCookies = parseCookies(cookieHeader);
better-auth:test:     1120|
better-auth:test:     1121|   expect(parsedCookies.get("better-auth.session_token")).toBe("session-token.signature=");
better-auth:test:        |                                                          ^
better-auth:test:     1122|   expect(parsedCookies.get("better-auth.session_data")).toBe("session-data.signature=");
better-auth:test:     1123|  })

This change splits the individual cookie by first = ensure only two parts.


Summary by cubic

Fix cookie parsing to preserve "=" padding in signed cookies by splitting on the first "=". This prevents truncation and ensures session cookies are read correctly from request headers.

  • Bug Fixes
    • Updated parseCookies to split on the first "=" (using /(=(.*)/s)) so values with padding are preserved.
    • Added tests for signed cookies with "=" padding to prevent regressions.

Written for commit f238277de1. Summary will update automatically on new commits.


🔄 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/6789 **Author:** [@Shridhad](https://github.com/Shridhad) **Created:** 12/16/2025 **Status:** ✅ Merged **Merged:** 12/16/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/parse-cookies` --- ### 📝 Commits (1) - [`f238277`](https://github.com/better-auth/better-auth/commit/f238277de147a81d19a570d92391e8e3cc8b937d) fix: securely parse request cookies ### 📊 Changes **2 files changed** (+37 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/cookies/cookies.test.ts` (+36 -1) 📝 `packages/better-auth/src/cookies/index.ts` (+1 -1) </details> ### 📄 Description Sometimes the signed cookies end with signature padding `=`, which causes issue with `parseCookies` to exclude them while parsing request cookies. The current implementation splits each cookie with `=` to format into key-value pair, however, if cookie value contains `=` it is split into into three parts, ignoring the last part. Example breaking tests: ```sh better-auth:test: FAIL src/cookies/cookies.test.ts > parse cookies > should securely parse the signed cookies with padding better-auth:test: AssertionError: expected 'session-token.signature' to be 'session-token.signature=' // Object.is equality better-auth:test: better-auth:test: Expected: "session-token.signature=" better-auth:test: Received: "session-token.signature" better-auth:test: better-auth:test: ❯ src/cookies/cookies.test.ts:1121:58 better-auth:test: 1119| const parsedCookies = parseCookies(cookieHeader); better-auth:test: 1120| better-auth:test: 1121| expect(parsedCookies.get("better-auth.session_token")).toBe("session-token.signature="); better-auth:test: | ^ better-auth:test: 1122| expect(parsedCookies.get("better-auth.session_data")).toBe("session-data.signature="); better-auth:test: 1123| }) ``` This change splits the individual cookie by first `=` ensure only two parts. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fix cookie parsing to preserve "=" padding in signed cookies by splitting on the first "=". This prevents truncation and ensures session cookies are read correctly from request headers. - **Bug Fixes** - Updated parseCookies to split on the first "=" (using /(=(.*)/s)) so values with padding are preserved. - Added tests for signed cookies with "=" padding to prevent regressions. <sup>Written for commit f238277de147a81d19a570d92391e8e3cc8b937d. Summary will update automatically on new commits.</sup> <!-- 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 13:15:26 -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#6884