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
chore(core): align CSP for SVGs
Elian Doran6531acf359b0
diff --git a/apps/server/src/share/routes.ts b/apps/server/src/share/routes.tsindex d40745e9b9..bbd2111e03 100644--- a/apps/server/src/share/routes.ts+++ b/apps/server/src/share/routes.ts@@ -150,7 +150,7 @@ function register(router: Router) {// so the instance owner is deliberately opting in to serve their own scriptable// content. Restricting it would break legitimate self-contained HTML pages.if (note.mime === "image/svg+xml") {- res.setHeader("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; img-src * data:; font-src * data:");+ res.setHeader("Content-Security-Policy", utils.SVG_CONTENT_SECURITY_POLICY);res.setHeader("X-Content-Type-Options", "nosniff");}@@ -241,7 +241,8 @@ function register(router: Router) {const sanitized = utils.sanitizeSvg(svgContent);res.set("Content-Type", "image/svg+xml");res.set("Cache-Control", "no-cache, no-store, must-revalidate");- res.set("Content-Security-Policy", "script-src 'none'");+ res.set("Content-Security-Policy", utils.SVG_CONTENT_SECURITY_POLICY);+ res.set("X-Content-Type-Options", "nosniff");res.send(sanitized);} else {res.set("Content-Type", image.mime);@@ -277,7 +278,8 @@ function register(router: Router) {const sanitized = utils.sanitizeSvg(svgContent);res.set("Content-Type", "image/svg+xml");res.set("Cache-Control", "no-cache, no-store, must-revalidate");- res.set("Content-Security-Policy", "script-src 'none'");+ res.set("Content-Security-Policy", utils.SVG_CONTENT_SECURITY_POLICY);+ res.set("X-Content-Type-Options", "nosniff");res.send(sanitized);} else {res.set("Content-Type", attachment.mime);diff --git a/packages/trilium-core/src/routes/api/image.spec.ts b/packages/trilium-core/src/routes/api/image.spec.tsindex 3350592003..80f8c4a792 100644--- a/packages/trilium-core/src/routes/api/image.spec.ts+++ b/packages/trilium-core/src/routes/api/image.spec.ts@@ -113,7 +113,7 @@ describe("Image API (core)", () => {const res = await api.get<string>(`/api/images/${noteId}/file.svg`);expect(res.status).toBe(200);expect(res.headers["Content-Type"]).toBe("image/svg+xml");- expect(res.headers["Content-Security-Policy"]).toBe("script-src 'none'");+ expect(res.headers["Content-Security-Policy"]).toContain("default-src 'none'");expect(res.body).not.toContain("alert(1)");expect(res.body).toContain("<svg");});@@ -131,7 +131,7 @@ describe("Image API (core)", () => {const res = await api.get<string>(`/api/images/${noteId}/file.svg`);expect(res.status).toBe(200);expect(res.headers["Content-Type"]).toBe("image/svg+xml");- expect(res.headers["Content-Security-Policy"]).toBe("script-src 'none'");+ expect(res.headers["Content-Security-Policy"]).toContain("default-src 'none'");expect(res.body).toContain(type);expect(res.body).toContain("<svg");expect(res.body).not.toContain("alert(9)");@@ -238,7 +238,7 @@ describe("Image API (core)", () => {const res = await api.get<string>(`/api/attachments/${attachmentId}/image/file.svg`);expect(res.status).toBe(200);expect(res.headers["Content-Type"]).toBe("image/svg+xml");- expect(res.headers["Content-Security-Policy"]).toBe("script-src 'none'");+ expect(res.headers["Content-Security-Policy"]).toContain("default-src 'none'");expect(unwrapStringOrBuffer(res.body as never)).not.toContain("alert(2)");});});@@ -285,7 +285,7 @@ describe("Image API (core)", () => {});it("sets Content-Security-Policy header on SVG responses", () => {- const parentNote = note("note").note;+ const parentNote = mockNote("note").note;const response = new MockResponse();renderSvgAttachment(parentNote, response as any, "attachment");expect(response.headers["Content-Security-Policy"]).toBeDefined();@@ -293,7 +293,7 @@ describe("Image API (core)", () => {});it("sets X-Content-Type-Options header on SVG responses", () => {- const parentNote = note("note").note;+ const parentNote = mockNote("note").note;const response = new MockResponse();renderSvgAttachment(parentNote, response as any, "attachment");expect(response.headers["X-Content-Type-Options"]).toBe("nosniff");diff --git a/packages/trilium-core/src/routes/api/image.ts b/packages/trilium-core/src/routes/api/image.tsindex 06496c6e4a..536f66a80f 100644--- a/packages/trilium-core/src/routes/api/image.ts+++ b/packages/trilium-core/src/routes/api/image.ts@@ -7,7 +7,7 @@ import becca from "../../becca/becca.js";import type BNote from "../../becca/entities/bnote.js";import type BRevision from "../../becca/entities/brevision.js";import imageService from "../../services/image.js";-import { sanitizeSvg } from "../../services/utils/index.js";+import { sanitizeSvg, SVG_CONTENT_SECURITY_POLICY } from "../../services/utils/index.js";import { unwrapStringOrBuffer } from "../../services/utils/binary.js";function returnImageFromNote(req: Request<{ noteId: string }>, res: Response) {@@ -147,7 +147,7 @@ export default {function sendSanitizedSvg(res: Response, content: string | Uint8Array) {const svgString = unwrapStringOrBuffer(content);- res.set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:");+ res.set("Content-Security-Policy", SVG_CONTENT_SECURITY_POLICY);res.set("X-Content-Type-Options", "nosniff");res.send(sanitizeSvg(svgString));}diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.tsindex 0869e28bce..78a2ede2b2 100644--- a/packages/trilium-core/src/services/utils/index.ts+++ b/packages/trilium-core/src/services/utils/index.ts@@ -146,6 +146,15 @@ export function sanitizeSqlIdentifier(str: string) {return str.replace(/[^A-Za-z0-9_]/g, "");}+/**+ * Canonical Content-Security-Policy used whenever SVG content is served to a browser+ * (image notes, attachments, raw-shared SVG). Acts as defense-in-depth alongside+ * {@link sanitizeSvg}: blocks scripts via `default-src 'none'`, while still allowing+ * inline styles and same-origin/`data:` images and fonts so legitimate SVGs render.+ * Keep all SVG-serving routes pointed at this constant so the policy can't drift.+ */+export const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:";+/*** Sanitize SVG to remove potentially dangerous elements and attributes.* This prevents XSS via script injection in SVG content.