[PR #5091] [MERGED] fix(url): handle empty and root path in withPath, prevent double slashes, add tests #5768

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5091
Author: @surafel58
Created: 10/4/2025
Status: Merged
Merged: 10/6/2025
Merged by: @himself65

Base: canaryHead: fix/withpath-edge-cases


📝 Commits (5)

  • 21f6368 fix(url): handle empty and root path in withPath, prevent double slashes, add tests
  • 4ace798 chore: rename
  • 7213e5d Merge branch 'canary' into fix/withpath-edge-cases
  • b7d0257 Merge branch 'canary' into fix/withpath-edge-cases
  • d659f97 Merge branch 'canary' into fix/withpath-edge-cases

📊 Changes

2 files changed (+36 additions, -2 deletions)

View changed files

📝 packages/better-auth/src/init.test.ts (+26 -0)
📝 packages/better-auth/src/utils/url.ts (+10 -2)

📄 Description

Problem

withPath returns a double slash when the base URL ends with a slash and basePath is "" or "/". This breaks auth URLs.

Reproduction

Base URL http://localhost:5147/

A) basePath "" → bad URL with //
B) basePath "/" → bad URL with //

Fix

  • Trim trailing slashes from the base URL.
  • If path is empty or "/", return the cleaned base URL.
  • For other paths, ensure a leading slash and join.
  • No behavior change for URLs that already include a path.

Tests

Added init tests that cover:

Closes #5060

Credit goes to the reporter tnspacetime for the proposed approach.


Summary by cubic

Fixes URL joining in withPath to prevent double slashes and broken auth URLs. Empty or "/" basePath now returns the cleaned base URL; other paths are normalized.

  • Bug Fixes
    • Trim trailing slashes from baseURL.
    • Return cleaned baseURL when basePath is "" or "/".
    • Normalize other paths with a single leading slash.
    • Leave URLs unchanged if a path already exists.
    • Added init tests covering empty/root basePath and trailing-slash cases.

🔄 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/5091 **Author:** [@surafel58](https://github.com/surafel58) **Created:** 10/4/2025 **Status:** ✅ Merged **Merged:** 10/6/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/withpath-edge-cases` --- ### 📝 Commits (5) - [`21f6368`](https://github.com/better-auth/better-auth/commit/21f6368cb5848e54f00728eec75198d0edd63bde) fix(url): handle empty and root path in withPath, prevent double slashes, add tests - [`4ace798`](https://github.com/better-auth/better-auth/commit/4ace798e975e7e724674ab83cd26d4ecdc34c133) chore: rename - [`7213e5d`](https://github.com/better-auth/better-auth/commit/7213e5d478e014bbffe7a4b6b877d55269fa1dfd) Merge branch 'canary' into fix/withpath-edge-cases - [`b7d0257`](https://github.com/better-auth/better-auth/commit/b7d0257eddb4f062c568a4d746b0dab2526b98d4) Merge branch 'canary' into fix/withpath-edge-cases - [`d659f97`](https://github.com/better-auth/better-auth/commit/d659f970f937bc3037cc8dae3918708b0c208804) Merge branch 'canary' into fix/withpath-edge-cases ### 📊 Changes **2 files changed** (+36 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/init.test.ts` (+26 -0) 📝 `packages/better-auth/src/utils/url.ts` (+10 -2) </details> ### 📄 Description ### Problem withPath returns a double slash when the base URL ends with a slash and basePath is "" or "/". This breaks auth URLs. ### Reproduction Base URL http://localhost:5147/ A) basePath "" → bad URL with // B) basePath "/" → bad URL with // ### Fix - Trim trailing slashes from the base URL. - If path is empty or "/", return the cleaned base URL. - For other paths, ensure a leading slash and join. - No behavior change for URLs that already include a path. ### Tests Added init tests that cover: - empty basePath → http://localhost:5147/ - root basePath → http://localhost:5147/ - trailing slashes with default path → http://localhost:5147/api/auth - unchanged when path already present Closes #5060 Credit goes to the reporter [tnspacetime](https://github.com/tnspacetime) for the proposed approach. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes URL joining in withPath to prevent double slashes and broken auth URLs. Empty or "/" basePath now returns the cleaned base URL; other paths are normalized. - **Bug Fixes** - Trim trailing slashes from baseURL. - Return cleaned baseURL when basePath is "" or "/". - Normalize other paths with a single leading slash. - Leave URLs unchanged if a path already exists. - Added init tests covering empty/root basePath and trailing-slash cases. <!-- 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:35:05 -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#5768