Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(docs): allow ampersands in doc names so "Import & Export" pages render
Elian Doranc9a9b231e50b
diff --git a/apps/client/src/services/doc_renderer.spec.ts b/apps/client/src/services/doc_renderer.spec.tsindex 11086b3880..4669492b1b 100644--- a/apps/client/src/services/doc_renderer.spec.ts+++ b/apps/client/src/services/doc_renderer.spec.ts@@ -45,6 +45,7 @@ describe("isValidDocName", () => {expect(isValidDocName("quick_start_guide")).toBe(true);expect(isValidDocName("quick-start-guide")).toBe(true);expect(isValidDocName("User Guide/User Guide/Advanced Usage/Text Extraction (OCR)")).toBe(true);+ expect(isValidDocName("User Guide/Basic Concepts and Features/Import & Export/Microsoft OneNote")).toBe(true);});it("rejects path traversal attacks", () => {@@ -115,6 +116,12 @@ describe("renderDoc", () => {expect(loadSpy.mock.calls[0][0]).toBe("assets/doc_notes/en/User%20Guide/Quick%20Start.html");});+ it("percent-encodes ampersands in the docName (e.g. \"Import & Export\")", async () => {+ const loadSpy = stubLoad(() => {});+ await renderDoc(fakeNote("User Guide/Basic Concepts and Features/Import & Export/Microsoft OneNote"));+ expect(loadSpy.mock.calls[0][0]).toBe("assets/doc_notes/en/User%20Guide/Basic%20Concepts%20and%20Features/Import%20%26%20Export/Microsoft%20OneNote.html");+ });+it("falls back to the English doc when the localized load errors", async () => {mockedLanguage = "de";const requested: string[] = [];diff --git a/apps/client/src/services/doc_renderer.ts b/apps/client/src/services/doc_renderer.tsindex bdb08fdfa0..f53a03b764 100644--- a/apps/client/src/services/doc_renderer.ts+++ b/apps/client/src/services/doc_renderer.ts@@ -9,8 +9,8 @@ import { formatCodeBlocks } from "./syntax_highlight.js";* but blocks traversal sequences and URL manipulation characters.*/export function isValidDocName(docName: string): boolean {- // Allow alphanumeric characters, spaces, underscores, hyphens, and forward slashes.- const validDocNameRegex = /^[a-zA-Z0-9_/\- ()]+$/;+ // Allow alphanumeric characters, spaces, underscores, hyphens, ampersands, and forward slashes.+ const validDocNameRegex = /^[a-zA-Z0-9_/\- ()&]+$/;return validDocNameRegex.test(docName);}@@ -74,6 +74,8 @@ function getUrl(docNameValue: string | null, language: string) {// Cannot have spaces in the URL due to how JQuery.load works.docNameValue = docNameValue.replaceAll(" ", "%20");+ // Percent-encode ampersands (e.g. in "Import & Export") so they aren't misread when fetching the doc.+ docNameValue = docNameValue.replaceAll("&", "%26");// The user guide is available only in English, so make sure we are requesting correctly since 404s in standalone client are treated differently.if (docNameValue.includes("User%20Guide")) language = "en";return `${getBasePath()}/doc_notes/${language}/${docNameValue}.html`;