fix: normalize admin basepath to prevent double slashes in URLs

Strip trailing slashes from adminBasepath in AdminController window context
and wouter Router base to prevent paths like /admin//data when users
configure adminBasepath with a trailing slash.

Amp-Thread-ID: https://ampcode.com/threads/T-019ca537-4cc4-7174-bf9a-5325d782f097
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Cameron Pak
2026-02-28 11:11:44 -06:00
parent 6321a9935a
commit 9008f9c6a3
3 changed files with 59 additions and 2 deletions

View File

@@ -203,6 +203,63 @@ describe("AppReduced", () => {
});
});
describe("withBasePath - double slash fix (admin_basepath with trailing slash)", () => {
it("should not produce double slashes when admin_basepath has trailing slash", () => {
const options: BkndAdminProps["config"] = {
basepath: "/",
admin_basepath: "/admin/",
logo_return_path: "/",
};
appReduced = new AppReduced(mockAppJson, options);
const result = appReduced.withBasePath("/data");
expect(result).toBe("/admin/data");
expect(result).not.toContain("//");
});
it("should work correctly when admin_basepath has no trailing slash", () => {
const options: BkndAdminProps["config"] = {
basepath: "/",
admin_basepath: "/admin",
logo_return_path: "/",
};
appReduced = new AppReduced(mockAppJson, options);
const result = appReduced.withBasePath("/data");
expect(result).toBe("/admin/data");
});
it("should handle absolute paths with admin_basepath trailing slash", () => {
const options: BkndAdminProps["config"] = {
basepath: "/",
admin_basepath: "/admin/",
logo_return_path: "/",
};
appReduced = new AppReduced(mockAppJson, options);
const result = appReduced.getAbsolutePath("data");
expect(result).toBe("~/admin/data");
expect(result).not.toContain("//");
});
it("should handle settings path with admin_basepath trailing slash", () => {
const options: BkndAdminProps["config"] = {
basepath: "/",
admin_basepath: "/admin/",
logo_return_path: "/",
};
appReduced = new AppReduced(mockAppJson, options);
const result = appReduced.getSettingsPath(["general"]);
expect(result).toBe("~/admin/settings/general");
expect(result).not.toContain("//");
});
});
describe("edge cases", () => {
it("should handle undefined basepath", () => {
const options: BkndAdminProps["config"] = {

View File

@@ -123,7 +123,7 @@ export class AdminController extends Controller {
const obj: AdminBkndWindowContext = {
user: c.get("auth")?.user,
logout_route: authRoutes.logout,
admin_basepath: this.options.adminBasepath,
admin_basepath: this.options.adminBasepath.replace(/\/+$/, ""),
theme: this.options.theme,
logo_return_path: this.options.logoReturnPath,
};

View File

@@ -33,7 +33,7 @@ export function Routes({
}) {
const { theme } = useTheme();
const ctx = useBkndWindowContext();
const actualBasePath = basePath || ctx.admin_basepath;
const actualBasePath = (basePath || ctx.admin_basepath).replace(/\/+$/, "");
return (
<div id="bknd-admin" className={theme + " antialiased"}>