Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): preserve reverse proxy subpaths in share link generation (closes #10588)

jayhormes5742ae113ab8
diff --git a/apps/client/src/services/share_link.spec.ts b/apps/client/src/services/share_link.spec.ts
index e81db9761c..9bd5d9110a 100644
--- a/apps/client/src/services/share_link.spec.ts
+++ b/apps/client/src/services/share_link.spec.ts
@@ -7,6 +7,7 @@ describe("buildShareLink", () => {
afterEach(() => {
window.glob = originalGlob;
+ history.replaceState({}, "", "/");
});
function setGlob(patch: Record<string, unknown>) {
@@ -31,11 +32,34 @@ describe("buildShareLink", () => {
expect(buildShareLink("abc123", null)).toBe("http://localhost:3000/share/abc123");
});
+ it("keeps the browser deployment subpath with or without a trailing slash", () => {
+ setGlob({});
+
+ history.replaceState({}, "", "/trilium");
+ expect(buildShareLink("abc123", null)).toBe("http://localhost:3000/trilium/share/abc123");
+
+ history.replaceState({}, "", "/trilium/");
+ expect(buildShareLink("abc123", null)).toBe("http://localhost:3000/trilium/share/abc123");
+ });
+
it("resolves an empty shareId (share root) to the /share/ base", () => {
setGlob({ httpBaseUrl: "http://127.0.0.1:37742" });
expect(buildShareLink("", undefined)).toBe("http://127.0.0.1:37742/share/");
});
+ it("keeps the subpath when the sync server is reverse-proxied below the domain root", () => {
+ setGlob({ httpBaseUrl: "http://127.0.0.1:37742" });
+ expect(buildShareLink("abc123", "https://notes.example.com/trilium")).toBe("https://notes.example.com/trilium/share/abc123");
+ expect(buildShareLink("abc123", "https://notes.example.com/trilium/")).toBe("https://notes.example.com/trilium/share/abc123");
+ expect(buildShareLink("abc123", "https://notes.example.com/trilium///")).toBe("https://notes.example.com/trilium/share/abc123");
+ expect(buildShareLink("abc123", "https://notes.example.com/a/b")).toBe("https://notes.example.com/a/b/share/abc123");
+ });
+
+ it("drops a query or fragment carried by the configured sync server address", () => {
+ setGlob({ httpBaseUrl: "http://127.0.0.1:37742" });
+ expect(buildShareLink("abc123", "https://notes.example.com/trilium?x=1#y")).toBe("https://notes.example.com/trilium/share/abc123");
+ });
+
it("percent-encodes a shareId carrying characters that are not path-safe", () => {
// A shareAlias is free-form text, so it can hold spaces, quotes, separators and anything
// else a title can. Each branch must produce one path segment the /share/:shareId route
diff --git a/apps/client/src/services/share_link.ts b/apps/client/src/services/share_link.ts
index a7c26ecd8f..fe60a8e1ef 100644
--- a/apps/client/src/services/share_link.ts
+++ b/apps/client/src/services/share_link.ts
@@ -13,12 +13,24 @@ export function buildShareLink(shareId: string, syncServerHost: string | null |
const encodedShareId = encodeURIComponent(shareId);
if (syncServerHost) {
- return new URL(`/share/${encodedShareId}`, syncServerHost).href;
+ return appendSharePath(syncServerHost, encodedShareId);
}
if (window.glob.httpBaseUrl) {
- return new URL(`/share/${encodedShareId}`, window.glob.httpBaseUrl).href;
+ return appendSharePath(window.glob.httpBaseUrl, encodedShareId);
}
- return `${location.protocol}//${location.host}${location.pathname}share/${encodedShareId}`;
+ return appendSharePath(location.href, encodedShareId);
+}
+
+// `new URL("/share/x", base)` cannot stand in here: the leading slash makes the path absolute
+// and drops the base's own path, which an instance reverse-proxied under a subpath needs.
+function appendSharePath(base: string, encodedShareId: string): string {
+ const url = new URL(base);
+
+ url.search = "";
+ url.hash = "";
+ url.pathname = `${url.pathname.replace(/\/+$/, "")}/share/${encodedShareId}`;
+
+ return url.href;
}