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(client): validate docName attribute in doc renderer
Elian Doranff06c8e7bd42
diff --git a/apps/client/src/services/doc_renderer.spec.ts b/apps/client/src/services/doc_renderer.spec.tsnew file mode 100644index 0000000000..c36e3e80b9--- /dev/null+++ b/apps/client/src/services/doc_renderer.spec.ts@@ -0,0 +1,30 @@+import { describe, expect, it } from "vitest";++import { isValidDocName } from "./doc_renderer.js";++describe("isValidDocName", () => {+ it("accepts valid docNames", () => {+ expect(isValidDocName("launchbar_intro")).toBe(true);+ expect(isValidDocName("User Guide/Quick Start")).toBe(true);+ expect(isValidDocName("User Guide/User Guide/Quick Start")).toBe(true);+ expect(isValidDocName("Quick Start Guide")).toBe(true);+ expect(isValidDocName("quick_start_guide")).toBe(true);+ expect(isValidDocName("quick-start-guide")).toBe(true);+ });++ it("rejects path traversal attacks", () => {+ expect(isValidDocName("..")).toBe(false);+ expect(isValidDocName("../etc/passwd")).toBe(false);+ expect(isValidDocName("foo/../bar")).toBe(false);+ expect(isValidDocName("../../../../api/notes/_malicious/open")).toBe(false);+ expect(isValidDocName("..\\etc\\passwd")).toBe(false);+ expect(isValidDocName("foo\\bar")).toBe(false);+ });++ it("rejects URL manipulation attacks", () => {+ expect(isValidDocName("../../../../api/notes/_malicious/open?x=")).toBe(false);+ expect(isValidDocName("foo#bar")).toBe(false);+ expect(isValidDocName("%2e%2e")).toBe(false);+ expect(isValidDocName("%2e%2e%2f%2e%2e%2fapi")).toBe(false);+ });+});diff --git a/apps/client/src/services/doc_renderer.ts b/apps/client/src/services/doc_renderer.tsindex 1ae60fb9ca..d2b2140847 100644--- a/apps/client/src/services/doc_renderer.ts+++ b/apps/client/src/services/doc_renderer.ts@@ -3,6 +3,22 @@ import { applyReferenceLinks } from "../widgets/type_widgets/text/read_only_helpimport { getCurrentLanguage } from "./i18n.js";import { formatCodeBlocks } from "./syntax_highlight.js";+/**+ * Validates a docName to prevent path traversal attacks.+ * Allows forward slashes for subdirectories (e.g., "User Guide/Quick Start")+ * but blocks traversal sequences and URL manipulation characters.+ */+export function isValidDocName(docName: string): boolean {+ if (docName.includes("..") ||+ docName.includes("\\") ||+ docName.includes("?") ||+ docName.includes("#") ||+ docName.includes("%")) {+ return false;+ }+ return true;+}+export default function renderDoc(note: FNote) {return new Promise<JQuery<HTMLElement>>((resolve) => {let docName = note.getLabelValue("docName");@@ -49,6 +65,11 @@ async function processContent(url: string, $content: JQuery<HTMLElement>) {}function getUrl(docNameValue: string, language: string) {+ if (!isValidDocName(docNameValue)) {+ console.error(`Invalid docName: ${docNameValue}`);+ return "";+ }+// Cannot have spaces in the URL due to how JQuery.load works.docNameValue = docNameValue.replaceAll(" ", "%20");