[GH-ISSUE #5060] withPath is missing edge cases #18786

Closed
opened 2026-04-15 17:26:17 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @tnspacetime on GitHub (Oct 3, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/5060

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

The withPath function in the packages/better-auth/src/utils/url.ts file is missing the edge cases of a basePath of "" or "/". If basePath is any of these values, the result is a double slash "//" in the final url, causing an auth error.

Current vs. Expected behavior

The withPath function in the packages/better-auth/src/utils/url.ts file is missing the edge cases of a basePath of "" or "/". These two cases may be needed in the case the auth is deployed as a separate service, distinct from the api service of an app. I propose the following solution:

function withPath(url: string, path = "/api/auth") {
	const hasPath = checkHasPath(url);
	if (hasPath) {
		return url;
	}

	const cleanedUrl = url.replace(/\/+$/, "");

	if (!path || path === "/") {
		return cleanedUrl;
	}

	path = path.startsWith("/") ? path : `/${path}`;
	return `${cleanedUrl}${path}`;
}

What version of Better Auth are you using?

1.3.24

System info

Mac

Which area(s) are affected? (Select all that apply)

Client, Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @tnspacetime on GitHub (Oct 3, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/5060 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce The `withPath` function in the `packages/better-auth/src/utils/url.ts file` is missing the edge cases of a `basePath` of "" or "/". If `basePath` is any of these values, the result is a double slash "//" in the final url, causing an auth error. ### Current vs. Expected behavior The `withPath` function in the `packages/better-auth/src/utils/url.ts file` is missing the edge cases of a `basePath` of "" or "/". These two cases may be needed in the case the auth is deployed as a separate service, distinct from the api service of an app. I propose the following solution: ```ts function withPath(url: string, path = "/api/auth") { const hasPath = checkHasPath(url); if (hasPath) { return url; } const cleanedUrl = url.replace(/\/+$/, ""); if (!path || path === "/") { return cleanedUrl; } path = path.startsWith("/") ? path : `/${path}`; return `${cleanedUrl}${path}`; } ``` ### What version of Better Auth are you using? 1.3.24 ### System info ```bash Mac ``` ### Which area(s) are affected? (Select all that apply) Client, Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-15 17:26:17 -05:00
Author
Owner

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

Hi @tnspacetime! I'm Dosu and I’m helping the better-auth team.

Yes, this is well-suited for GitHub as a bug report. The current implementation of withPath in packages/better-auth/src/utils/url.ts does not explicitly handle the edge cases where the path is "" or "/", which can result in double slashes in the final URL if the base URL ends with a slash. This matches your observation and can cause auth errors in certain deployments. There are no dedicated test cases for these scenarios, and recent changes to the function have not addressed this gap. Your proposed solution would improve the function's robustness and maintainability by handling these edge cases directly.
See source

To reply, just mention @dosu.


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

<!-- gh-comment-id:3363688038 --> @dosubot[bot] commented on GitHub (Oct 3, 2025): <!-- Greeting --> Hi @tnspacetime! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> Yes, this is well-suited for GitHub as a bug report. The current implementation of withPath in packages/better-auth/src/utils/url.ts does not explicitly handle the edge cases where the path is "" or "/", which can result in double slashes in the final URL if the base URL ends with a slash. This matches your observation and can cause auth errors in certain deployments. There are no dedicated test cases for these scenarios, and recent changes to the function have not addressed this gap. Your proposed solution would improve the function's robustness and maintainability by handling these edge cases directly. [See source](https://github.com/better-auth/better-auth/blob/8fbb921fb319ba2e9d079ead0de545643fd595b9/packages/better-auth/src/utils/url.ts) <!-- 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/181a744c-6b57-440e-8cbb-6b0577af7c2f?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/181a744c-6b57-440e-8cbb-6b0577af7c2f?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/181a744c-6b57-440e-8cbb-6b0577af7c2f?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/181a744c-6b57-440e-8cbb-6b0577af7c2f?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/181a744c-6b57-440e-8cbb-6b0577af7c2f?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/181a744c-6b57-440e-8cbb-6b0577af7c2f?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/181a744c-6b57-440e-8cbb-6b0577af7c2f?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/5060)
Author
Owner

@surafel58 commented on GitHub (Oct 4, 2025):

Hi, I want to work on this. I will add tests for path "" and "/". Then I will update withPath to return the base URL for those cases, and keep current behavior for other paths. I will open a PR shortly. Thanks.

<!-- gh-comment-id:3368482368 --> @surafel58 commented on GitHub (Oct 4, 2025): Hi, I want to work on this. I will add tests for path "" and "/". Then I will update withPath to return the base URL for those cases, and keep current behavior for other paths. I will open a PR shortly. Thanks.
Author
Owner

@tnspacetime commented on GitHub (Oct 4, 2025):

Sure go ahead. I think the test file is here btw packages/better-auth/src/init.test.ts.

<!-- gh-comment-id:3368497637 --> @tnspacetime commented on GitHub (Oct 4, 2025): Sure go ahead. I think the test file is here btw `packages/better-auth/src/init.test.ts`.
Author
Owner

@surafel58 commented on GitHub (Oct 5, 2025):

PR #5091

<!-- gh-comment-id:3368910698 --> @surafel58 commented on GitHub (Oct 5, 2025): PR #5091
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#18786