[PR #5344] [CLOSED] fix: properly encode callbackURL parameters throughout codebase #14191

Closed
opened 2026-04-13 09:21:11 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5344
Author: @shaug
Created: 10/15/2025
Status: Closed

Base: v1.3.28Head: v1.3.28


📝 Commits (10+)

📊 Changes

276 files changed (+6848 additions, -4265 deletions)

View changed files

📝 docs/app/global.css (+9 -3)
📝 docs/app/layout.tsx (+2 -0)
📝 docs/components/ai-chat-modal.tsx (+1 -1)
docs/components/anchor-scroll-fix.tsx (+107 -0)
📝 docs/components/docs/layout/nav.tsx (+4 -1)
📝 docs/content/docs/authentication/gitlab.mdx (+26 -0)
📝 docs/content/docs/plugins/passkey.mdx (+4 -0)
📝 packages/better-auth/package.json (+1 -0)
📝 packages/better-auth/src/adapters/adapter-factory/index.ts (+26 -19)
📝 packages/better-auth/src/adapters/adapter-factory/test/adapter-factory.test.ts (+3 -2)
📝 packages/better-auth/src/adapters/adapter-factory/types.ts (+21 -274)
📝 packages/better-auth/src/adapters/create-test-suite.ts (+13 -12)
📝 packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts (+8 -4)
📝 packages/better-auth/src/adapters/drizzle-adapter/test/generate-schema.ts (+3 -2)
📝 packages/better-auth/src/adapters/kysely-adapter/dialect.ts (+1 -1)
📝 packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts (+7 -3)
📝 packages/better-auth/src/adapters/kysely-adapter/test/adapter.kysely.mssql.test.ts (+1 -1)
📝 packages/better-auth/src/adapters/kysely-adapter/test/adapter.kysely.pg.test.ts (+1 -1)
📝 packages/better-auth/src/adapters/kysely-adapter/test/node-sqlite-dialect.test.ts (+1 -1)
📝 packages/better-auth/src/adapters/memory-adapter/memory-adapter.ts (+7 -4)

...and 80 more files

📄 Description

Fixes #5340 - Improper callbackURL Parameter Encoding Throughout Codebase

Problem:
When callbackURL contains special characters like ?, &, #, or spaces, generated authentication URLs become malformed and break authentication flows.

Example of broken URL:

/verify-email?token=abc123&callbackURL=https://example.com/dashboard?tab=settings&user=123

Solution:
Apply encodeURIComponent() to all callbackURL parameters before including them in query strings to ensure proper URL encoding.

Files Fixed:

  • src/api/routes/email-verification.ts (2 instances)
  • src/api/routes/sign-up.ts (1 instance)
  • src/api/routes/update-user.ts (3 instances)
  • src/api/routes/sign-in.ts (1 instance)
  • src/plugins/username/index.ts (1 instance)
  • src/oauth2/link-account.ts (1 instance)

Changes:

  • Before: callbackURL=${ctx.body.callbackURL || "/"}
  • After: callbackURL=${encodeURIComponent(ctx.body.callbackURL || "/")}

Impact:
Fixes authentication flows for users with complex callback URLs containing query parameters, fragments, or special characters. Affects email verification, password reset, OAuth2 account linking, and user account management flows.


Summary by cubic

Properly URL-encode callbackURL across auth flows to prevent malformed links when callbacks include query strings or special characters. This fixes broken redirects in email verification, sign-in/up, account linking, and delete/change email flows.

  • Bug Fixes
    • Wrapped callbackURL with encodeURIComponent in all places building verification/callback links.
    • Updated email verification, sign-in, sign-up, delete account, change email, OAuth2 link-account, and username plugin.
    • Keeps default "/" when no callback is provided.

🔄 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/5344 **Author:** [@shaug](https://github.com/shaug) **Created:** 10/15/2025 **Status:** ❌ Closed **Base:** `v1.3.28` ← **Head:** `v1.3.28` --- ### 📝 Commits (10+) - [`b4e3ec4`](https://github.com/better-auth/better-auth/commit/b4e3ec46b85ee0db2db2a1cbd319c47ff18c8cad) refactor: move RateLimit to core (#5129) - [`98fa63d`](https://github.com/better-auth/better-auth/commit/98fa63dccd5a3f93ede40e32287cc7c65de93ebc) refactor: move env related to core (#5130) - [`664dae9`](https://github.com/better-auth/better-auth/commit/664dae9eaae7bea495a01adcb9f5276c408e11a9) refactor: move SecondaryStorage type to core (#5132) - [`1a7a8bb`](https://github.com/better-auth/better-auth/commit/1a7a8bb90985a66992cb9cca2fb36dead38d59cc) refactor: move oauth2 type to core (#5133) - [`89a4db8`](https://github.com/better-auth/better-auth/commit/89a4db8290a24a6e98ccfff68af053320833879e) refactor: move oauth2 to core (#5135) - [`e559c6b`](https://github.com/better-auth/better-auth/commit/e559c6bd82ff04765ffc2d28e3d9b68b6aff0633) refactor: move telemetry to single package (#5152) - [`d9565c6`](https://github.com/better-auth/better-auth/commit/d9565c698046ecc20b5970c3e1f5c0ba0470f9c6) refactor: move social-providers to core (#5153) - [`2a70d53`](https://github.com/better-auth/better-auth/commit/2a70d53fec54d2158986e46e7343b65cfe90729d) fix(gitlab): fix the token endpoint (#5147) - [`92736c8`](https://github.com/better-auth/better-auth/commit/92736c885b3d3329b42370f6638eee0ed5281109) fix(passkey): atom listeners not working (#5096) - [`45dac14`](https://github.com/better-auth/better-auth/commit/45dac148c0e5bcc436741291463ff7015f4368dc) chore: backport for old `./social-providers` endpoint (#5155) ### 📊 Changes **276 files changed** (+6848 additions, -4265 deletions) <details> <summary>View changed files</summary> 📝 `docs/app/global.css` (+9 -3) 📝 `docs/app/layout.tsx` (+2 -0) 📝 `docs/components/ai-chat-modal.tsx` (+1 -1) ➕ `docs/components/anchor-scroll-fix.tsx` (+107 -0) 📝 `docs/components/docs/layout/nav.tsx` (+4 -1) 📝 `docs/content/docs/authentication/gitlab.mdx` (+26 -0) 📝 `docs/content/docs/plugins/passkey.mdx` (+4 -0) 📝 `packages/better-auth/package.json` (+1 -0) 📝 `packages/better-auth/src/adapters/adapter-factory/index.ts` (+26 -19) 📝 `packages/better-auth/src/adapters/adapter-factory/test/adapter-factory.test.ts` (+3 -2) 📝 `packages/better-auth/src/adapters/adapter-factory/types.ts` (+21 -274) 📝 `packages/better-auth/src/adapters/create-test-suite.ts` (+13 -12) 📝 `packages/better-auth/src/adapters/drizzle-adapter/drizzle-adapter.ts` (+8 -4) 📝 `packages/better-auth/src/adapters/drizzle-adapter/test/generate-schema.ts` (+3 -2) 📝 `packages/better-auth/src/adapters/kysely-adapter/dialect.ts` (+1 -1) 📝 `packages/better-auth/src/adapters/kysely-adapter/kysely-adapter.ts` (+7 -3) 📝 `packages/better-auth/src/adapters/kysely-adapter/test/adapter.kysely.mssql.test.ts` (+1 -1) 📝 `packages/better-auth/src/adapters/kysely-adapter/test/adapter.kysely.pg.test.ts` (+1 -1) 📝 `packages/better-auth/src/adapters/kysely-adapter/test/node-sqlite-dialect.test.ts` (+1 -1) 📝 `packages/better-auth/src/adapters/memory-adapter/memory-adapter.ts` (+7 -4) _...and 80 more files_ </details> ### 📄 Description Fixes #5340 - Improper `callbackURL` Parameter Encoding Throughout Codebase **Problem:** When callbackURL contains special characters like `?`, `&`, `#`, or spaces, generated authentication URLs become malformed and break authentication flows. **Example of broken URL:** ``` /verify-email?token=abc123&callbackURL=https://example.com/dashboard?tab=settings&user=123 ``` **Solution:** Apply `encodeURIComponent()` to all callbackURL parameters before including them in query strings to ensure proper URL encoding. **Files Fixed:** - `src/api/routes/email-verification.ts` (2 instances) - `src/api/routes/sign-up.ts` (1 instance) - `src/api/routes/update-user.ts` (3 instances) - `src/api/routes/sign-in.ts` (1 instance) - `src/plugins/username/index.ts` (1 instance) - `src/oauth2/link-account.ts` (1 instance) **Changes:** - Before: `callbackURL=${ctx.body.callbackURL || "/"}` - After: `callbackURL=${encodeURIComponent(ctx.body.callbackURL || "/")}` **Impact:** Fixes authentication flows for users with complex callback URLs containing query parameters, fragments, or special characters. Affects email verification, password reset, OAuth2 account linking, and user account management flows. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Properly URL-encode callbackURL across auth flows to prevent malformed links when callbacks include query strings or special characters. This fixes broken redirects in email verification, sign-in/up, account linking, and delete/change email flows. - **Bug Fixes** - Wrapped callbackURL with encodeURIComponent in all places building verification/callback links. - Updated email verification, sign-in, sign-up, delete account, change email, OAuth2 link-account, and username plugin. - Keeps default "/" when no callback is provided. <!-- 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-04-13 09:21:11 -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#14191