callbackURL with query params not encoded correctly in email verification #2051

Closed
opened 2026-03-13 09:23:12 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @phuctm97 on GitHub (Oct 1, 2025).

Description

When using email verification (sign up flow), if the callbackURL contains query parameters or other non-URL-friendly characters, it is not being encoded correctly. This causes incorrect redirects after users click the verification link in their email.

Note: Password reset does not have this issue.

Expected Behavior

The callbackURL parameter should be properly URL-encoded (using encodeURIComponent or similar) when being included as a query parameter in the email verification URL.

Actual Behavior

The callbackURL is passed without proper encoding, which breaks when it contains:

  • Query parameters (e.g., ?redirect=/dashboard&tab=settings)
  • Special characters (e.g., #, &, =, etc.)

This causes the redirect URL to be malformed after email verification, leading to incorrect navigation or broken links.

Steps to Reproduce

  1. Initiate sign up with email verification using a callbackURL that includes query parameters
  2. Example: callbackURL = "https://example.com/app?redirect=/dashboard&tab=settings"
  3. Receive the verification email
  4. Click the link in the email
  5. Observe that the redirect after verification is incorrect due to malformed URL parsing

Environment

  • Better Auth version: 1.3.24
  • Affected feature: Email verification (sign up)
Originally created by @phuctm97 on GitHub (Oct 1, 2025). ## Description When using email verification (sign up flow), if the `callbackURL` contains query parameters or other non-URL-friendly characters, it is not being encoded correctly. This causes incorrect redirects after users click the verification link in their email. **Note**: Password reset does not have this issue. ## Expected Behavior The `callbackURL` parameter should be properly URL-encoded (using `encodeURIComponent` or similar) when being included as a query parameter in the email verification URL. ## Actual Behavior The `callbackURL` is passed without proper encoding, which breaks when it contains: - Query parameters (e.g., `?redirect=/dashboard&tab=settings`) - Special characters (e.g., `#`, `&`, `=`, etc.) This causes the redirect URL to be malformed after email verification, leading to incorrect navigation or broken links. ## Steps to Reproduce 1. Initiate sign up with email verification using a `callbackURL` that includes query parameters 2. Example: `callbackURL = "https://example.com/app?redirect=/dashboard&tab=settings"` 3. Receive the verification email 4. Click the link in the email 5. Observe that the redirect after verification is incorrect due to malformed URL parsing ## Environment - Better Auth version: 1.3.24 - Affected feature: Email verification (sign up)
GiteaMirror added the bug label 2026-03-13 09:23:12 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 1, 2025):

This is a known issue in Better Auth versions around 1.3.24: if the callbackURL contains query parameters or special characters, it may not be properly encoded in verification and password reset emails, leading to malformed redirects. The root cause is that the backend decodes the callbackURL before including it in the email link, which breaks URLs with &, =, etc. This has been discussed and confirmed in GitHub issue #4780.

A fix was implemented in a later beta version (see PR #3863), but it may not be present in 1.3.24. If you are on 1.3.24 and still experiencing this, you can:

  • Double-encode the callbackURL before sending it to the backend, then decode it twice on the frontend.
  • Manually parse the callbackURL on the frontend to handle any splitting issues.

Upgrading to a version that includes the fix from PR #3863 is the recommended long-term solution. For now, ensure you use encodeURIComponent when constructing the callbackURL and decodeURIComponent when reading it, as you suggested.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Oct 1, 2025): <!-- Answer --> This is a known issue in Better Auth versions around 1.3.24: if the callbackURL contains query parameters or special characters, it may not be properly encoded in verification and password reset emails, leading to malformed redirects. The root cause is that the backend decodes the callbackURL before including it in the email link, which breaks URLs with `&`, `=`, etc. This has been discussed and confirmed in [GitHub issue #4780](https://github.com/better-auth/better-auth/issues/4780). A fix was implemented in a later beta version (see [PR #3863](https://github.com/better-auth/better-auth/pull/3863)), but it may not be present in 1.3.24. If you are on 1.3.24 and still experiencing this, you can: - Double-encode the callbackURL before sending it to the backend, then decode it twice on the frontend. - Manually parse the callbackURL on the frontend to handle any splitting issues. Upgrading to a version that includes the fix from PR #3863 is the recommended long-term solution. For now, ensure you use `encodeURIComponent` when constructing the callbackURL and `decodeURIComponent` when reading it, as you suggested. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/31b46f24-cd74-4f28-8b72-6cd2af7acb89?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/31b46f24-cd74-4f28-8b72-6cd2af7acb89?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/31b46f24-cd74-4f28-8b72-6cd2af7acb89?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/31b46f24-cd74-4f28-8b72-6cd2af7acb89?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/31b46f24-cd74-4f28-8b72-6cd2af7acb89?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/31b46f24-cd74-4f28-8b72-6cd2af7acb89?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/31b46f24-cd74-4f28-8b72-6cd2af7acb89?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/5015)
Author
Owner

@phuctm97 commented on GitHub (Oct 1, 2025):

Found the buggy code:

f6cbdcc84e/packages/better-auth/src/api/routes/sign-up.ts (L279-L281)

19f39cb400/packages/better-auth/src/api/routes/email-verification.ts (L54-L56)

And many other places in those 2 files

@phuctm97 commented on GitHub (Oct 1, 2025): Found the buggy code: https://github.com/better-auth/better-auth/blob/f6cbdcc84ee5d2971fdcc8b23ff7c174f88cf45b/packages/better-auth/src/api/routes/sign-up.ts#L279-L281 https://github.com/better-auth/better-auth/blob/19f39cb400d1b7afe925528bb58bccb8c3f077c3/packages/better-auth/src/api/routes/email-verification.ts#L54-L56 And many other places in those 2 files
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2051