Skip to content

fix: prevent double encoded cookie#8133

Merged
himself65 merged 3 commits intobetter-auth:canaryfrom
Oluwatobi-Mustapha:fix/custom-session-cookie-double-encode-8127
Mar 3, 2026
Merged

fix: prevent double encoded cookie#8133
himself65 merged 3 commits intobetter-auth:canaryfrom
Oluwatobi-Mustapha:fix/custom-session-cookie-double-encode-8127

Conversation

@Oluwatobi-Mustapha
Copy link
Copy Markdown
Contributor

@Oluwatobi-Mustapha Oluwatobi-Mustapha commented Feb 24, 2026

Fix #8127

Decode forwarded Set-Cookie values in customSession before calling ctx.setCookie(...) so /get-session refresh does not double-encode session cookies (%25).

  • decode parsed cookie value before re-setting cookie
  • add regression test for refresh cookie encoding

Tests

  • corepack pnpm --dir packages/better-auth exec vitest run src/plugins/custom-session/custom-session.test.ts --maxWorkers=1
  • corepack pnpm --dir packages/better-auth exec vitest run src/cookies/cookies.test.ts --maxWorkers=1

Summary by cubic

Prevents double-encoding of session cookies during get-session refresh in the customSession plugin, fixing #8127. Cookie values are decoded once when parsing Set-Cookie so better-auth.session_token stays unchanged on refresh.

  • Bug Fixes
    • Centralize decoding in parseSetCookieHeader using a safe tryDecode only when "%" is present; add a unit test for URI-decoded values.
    • Remove redundant decode calls in Next.js, SvelteKit, and TanStack Start integrations.
    • Add a regression test to ensure the session token is identical after refresh and contains no %25.

Written for commit 1d34572. Summary will update on new commits.

@vercel
Copy link
Copy Markdown

vercel bot commented Feb 24, 2026

@Oluwatobi-Mustapha is attempting to deploy a commit to the better-auth Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 24, 2026

Open in StackBlitz

@better-auth/api-key

npm i https://pkg.pr.new/@better-auth/api-key@8133

better-auth

npm i https://pkg.pr.new/better-auth@8133

auth

npm i https://pkg.pr.new/auth@8133

@better-auth/core

npm i https://pkg.pr.new/@better-auth/core@8133

@better-auth/drizzle-adapter

npm i https://pkg.pr.new/@better-auth/drizzle-adapter@8133

@better-auth/electron

npm i https://pkg.pr.new/@better-auth/electron@8133

@better-auth/expo

npm i https://pkg.pr.new/@better-auth/expo@8133

@better-auth/i18n

npm i https://pkg.pr.new/@better-auth/i18n@8133

@better-auth/kysely-adapter

npm i https://pkg.pr.new/@better-auth/kysely-adapter@8133

@better-auth/memory-adapter

npm i https://pkg.pr.new/@better-auth/memory-adapter@8133

@better-auth/mongo-adapter

npm i https://pkg.pr.new/@better-auth/mongo-adapter@8133

@better-auth/oauth-provider

npm i https://pkg.pr.new/@better-auth/oauth-provider@8133

@better-auth/passkey

npm i https://pkg.pr.new/@better-auth/passkey@8133

@better-auth/prisma-adapter

npm i https://pkg.pr.new/@better-auth/prisma-adapter@8133

@better-auth/redis-storage

npm i https://pkg.pr.new/@better-auth/redis-storage@8133

@better-auth/scim

npm i https://pkg.pr.new/@better-auth/scim@8133

@better-auth/sso

npm i https://pkg.pr.new/@better-auth/sso@8133

@better-auth/stripe

npm i https://pkg.pr.new/@better-auth/stripe@8133

@better-auth/telemetry

npm i https://pkg.pr.new/@better-auth/telemetry@8133

@better-auth/test-utils

npm i https://pkg.pr.new/@better-auth/test-utils@8133

commit: 5c90e67

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 2 files

Comment on lines +1 to +8
function tryDecode(str: string): string {
try {
return decodeURIComponent(str);
} catch {
return str;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we probably try to decode on the top level

@himself65 himself65 changed the title Fix #8127: prevent double-encoded customSession cookie on refresh fix: prevent double-encoded cookie Feb 25, 2026
@himself65 himself65 changed the title fix: prevent double-encoded cookie fix: prevent double encoded cookie Feb 25, 2026
@himself65 himself65 force-pushed the fix/custom-session-cookie-double-encode-8127 branch 2 times, most recently from 47b1ac3 to 3846367 Compare February 26, 2026 03:12
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 26, 2026

CLA assistant check
All committers have signed the CLA.

@himself65 himself65 mentioned this pull request Mar 3, 2026
@himself65 himself65 force-pushed the fix/custom-session-cookie-double-encode-8127 branch from aded47b to 1d34572 Compare March 3, 2026 04:12
@himself65 himself65 added this pull request to the merge queue Mar 3, 2026
Merged via the queue into better-auth:canary with commit 4992110 Mar 3, 2026
19 of 21 checks passed
@Oluwatobi-Mustapha
Copy link
Copy Markdown
Contributor Author

Thank you @himself65 🙏🏼

himself65 added a commit that referenced this pull request Mar 3, 2026
Co-authored-by: Alex Yang <himself65@outlook.com>
himself65 added a commit that referenced this pull request Mar 3, 2026
Co-authored-by: Alex Yang <himself65@outlook.com>
@better-auth better-auth locked as resolved and limited conversation to collaborators Apr 1, 2026
@bytaesu bytaesu added the locked Locked conversations after being closed for 7 days label Apr 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked Locked conversations after being closed for 7 days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

customSession plugin double-encodes Set-Cookie values during session refresh

4 participants