[PR #5341] [CLOSED] fix: properly encode callbackURL parameters throughout codebase #5941

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

📋 Pull Request Information

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

Base: v1.3.x-latestHead: v1.3.x-latest


📝 Commits (3)

  • f37822a fix: properly encode callbackURL parameters throughout codebase
  • 7e03c00 Remove previous diff
  • c19c064 Rename rest to additionalFields

📊 Changes

7 files changed (+16 additions, -16 deletions)

View changed files

📝 packages/better-auth/src/adapters/adapter-factory/index.ts (+0 -4)
📝 packages/better-auth/src/api/routes/email-verification.ts (+2 -2)
📝 packages/better-auth/src/api/routes/sign-in.ts (+1 -1)
📝 packages/better-auth/src/api/routes/sign-up.ts (+4 -4)
📝 packages/better-auth/src/api/routes/update-user.ts (+5 -3)
📝 packages/better-auth/src/oauth2/link-account.ts (+3 -1)
📝 packages/better-auth/src/plugins/username/index.ts (+1 -1)

📄 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 encode callbackURL in auth URLs to prevent malformed links when the callback has query params, fragments, or spaces. This restores email verification, account linking, and user update flows that were breaking.

  • Bug Fixes
    • Apply encodeURIComponent() to callbackURL before adding it to query strings across auth routes and plugins (email verification, sign-in, sign-up, delete account, change email, OAuth link, username).
    • Keep "/" as the default when callbackURL is missing.

🔄 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/5341 **Author:** [@shaug](https://github.com/shaug) **Created:** 10/15/2025 **Status:** ❌ Closed **Base:** `v1.3.x-latest` ← **Head:** `v1.3.x-latest` --- ### 📝 Commits (3) - [`f37822a`](https://github.com/better-auth/better-auth/commit/f37822a05ccd6a02698b3adfdfe8f5bde3e13f9f) fix: properly encode callbackURL parameters throughout codebase - [`7e03c00`](https://github.com/better-auth/better-auth/commit/7e03c00058b8c5f8c285dde96c0bce70dbd07cdd) Remove previous diff - [`c19c064`](https://github.com/better-auth/better-auth/commit/c19c06432f411a663e28cd78a58cde17241a960f) Rename `rest` to `additionalFields` ### 📊 Changes **7 files changed** (+16 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/adapters/adapter-factory/index.ts` (+0 -4) 📝 `packages/better-auth/src/api/routes/email-verification.ts` (+2 -2) 📝 `packages/better-auth/src/api/routes/sign-in.ts` (+1 -1) 📝 `packages/better-auth/src/api/routes/sign-up.ts` (+4 -4) 📝 `packages/better-auth/src/api/routes/update-user.ts` (+5 -3) 📝 `packages/better-auth/src/oauth2/link-account.ts` (+3 -1) 📝 `packages/better-auth/src/plugins/username/index.ts` (+1 -1) </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 encode callbackURL in auth URLs to prevent malformed links when the callback has query params, fragments, or spaces. This restores email verification, account linking, and user update flows that were breaking. - **Bug Fixes** - Apply encodeURIComponent() to callbackURL before adding it to query strings across auth routes and plugins (email verification, sign-in, sign-up, delete account, change email, OAuth link, username). - Keep "/" as the default when callbackURL is missing. <!-- 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:42: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#5941