From b51935504af974e18c70538d91424b1f31939ba7 Mon Sep 17 00:00:00 2001 From: Arnel Robles Date: Thu, 12 Nov 2026 23:23:58 +0800 Subject: [PATCH 1/2] escape mermaid-export theme and background, reject unknown themes --- .changeset/mermaid-export-escape.md | 5 ++ packages/mermaid-export/README.md | 8 ++ packages/mermaid-export/src/html.test.ts | 64 ++++++++++++++++ packages/mermaid-export/src/html.ts | 95 ++++++++++++++++++++++++ packages/mermaid-export/src/renderer.ts | 63 +--------------- 5 files changed, 175 insertions(+), 60 deletions(-) create mode 100644 .changeset/mermaid-export-escape.md create mode 100644 packages/mermaid-export/src/html.test.ts create mode 100644 packages/mermaid-export/src/html.ts diff --git a/.changeset/mermaid-export-escape.md b/.changeset/mermaid-export-escape.md new file mode 100644 index 0000000..50faa59 --- /dev/null +++ b/.changeset/mermaid-export-escape.md @@ -0,0 +1,5 @@ +--- +"mermaid-export": patch +--- + +Escape `theme` and `backgroundColor` before they go into the render page, reject unknown themes, and document that diagrams and options must be trusted. diff --git a/packages/mermaid-export/README.md b/packages/mermaid-export/README.md index d881989..2f5b285 100644 --- a/packages/mermaid-export/README.md +++ b/packages/mermaid-export/README.md @@ -133,6 +133,14 @@ Generic export function that supports all formats. Close the shared browser instance. Call this when done with all exports. +## Security + +Only render diagrams you trust, and only pass trusted option values. The diagram runs in headless Chromium with Mermaid in `securityLevel: 'loose'`, which allows HTML labels and click handlers, and the page can reach the network. An untrusted diagram can make requests from your machine and put script into the SVG you get back. Do not embed that SVG in a page without sanitizing it. + +`theme` must be one of `default`, `forest`, `dark`, `neutral`, `base`. Any other value throws. `backgroundColor` is escaped before it goes into the page's CSS. + +Chromium is launched with `--no-sandbox`. That flag is needed when running as root in a container, but it turns off a layer of browser isolation, so do not run untrusted diagrams this way. + ## Supported Diagram Types All Mermaid diagram types are supported: diff --git a/packages/mermaid-export/src/html.test.ts b/packages/mermaid-export/src/html.test.ts new file mode 100644 index 0000000..89024c3 --- /dev/null +++ b/packages/mermaid-export/src/html.test.ts @@ -0,0 +1,64 @@ +import { describe, it, expect } from 'vitest'; +import { generateHtml, validateTheme, escapeCss, escapeJsString } from './html.js'; + +describe('validateTheme', () => { + it('accepts the supported themes', () => { + for (const t of ['default', 'forest', 'dark', 'neutral', 'base']) { + expect(() => validateTheme(t)).not.toThrow(); + } + }); + + it('rejects an unknown theme', () => { + expect(() => validateTheme("x'});window.pwned=1;//")).toThrow(/Unknown theme/); + expect(() => validateTheme(undefined)).toThrow(/Unknown theme/); + }); +}); + +describe('escapeJsString', () => { + it('keeps a quote breakout inside the string', () => { + const out = escapeJsString("x'});window.pwned=1;//"); + expect(out).toBe(JSON.stringify("x'});window.pwned=1;//")); + expect(() => new Function(`return ${out}`)()).not.toThrow(); + expect(new Function(`return ${out}`)()).toBe("x'});window.pwned=1;//"); + }); + + it('does not let a value close the script tag', () => { + expect(escapeJsString(''); + expect(out).not.toMatch(/[;{}<>]/); + }); +}); + +describe('generateHtml', () => { + it('does not put raw option text into the page', () => { + const html = generateHtml( + 'graph TD; A-->B', + "x'});window.pwned=1;//", + 'red;} ' + ); + expect(html).not.toContain("theme: 'x'"); + expect(html).toContain('theme: "x\'});window.pwned=1;//"'); + expect(html).not.toContain(' + +`; +} + +/** + * Escape HTML special characters + */ +function escapeHtml(str: string): string { + return str + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + diff --git a/packages/mermaid-export/src/renderer.ts b/packages/mermaid-export/src/renderer.ts index 799090a..22679ba 100644 --- a/packages/mermaid-export/src/renderer.ts +++ b/packages/mermaid-export/src/renderer.ts @@ -1,8 +1,6 @@ import puppeteer, { type Browser } from 'puppeteer'; import type { RasterExportOptions, SvgExportOptions } from './types.js'; - -/** Mermaid CDN URL for rendering */ -const MERMAID_CDN = 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs'; +import { generateHtml, validateTheme } from './html.js'; /** Browser instance for reuse */ let browserInstance: Browser | null = null; @@ -30,63 +28,6 @@ export async function closeBrowser(): Promise { } } -/** - * Generate HTML template for rendering Mermaid diagram - */ -function generateHtml(diagram: string, theme: string, backgroundColor: string): string { - return ` - - - - - - -
-
${escapeHtml(diagram)}
-
- - -`; -} - -/** - * Escape HTML special characters - */ -function escapeHtml(str: string): string { - return str - .replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"') - .replace(/'/g, '''); -} - /** * Render a Mermaid diagram and return SVG string */ @@ -95,6 +36,7 @@ export async function renderToSvg( options: SvgExportOptions = {} ): Promise { const { backgroundColor = 'transparent', theme = 'default' } = options; + validateTheme(theme); const browser = await getBrowser(); const page = await browser.newPage(); @@ -145,6 +87,7 @@ export async function renderToBuffer( theme = 'default', scale = 1, } = options; + validateTheme(theme); const browser = await getBrowser(); const page = await browser.newPage(); From aecd965b8415b1167128b090724e44cf8b2c8053 Mon Sep 17 00:00:00 2001 From: Arnel Robles Date: Thu, 12 Nov 2026 23:34:49 +0800 Subject: [PATCH 2/2] keep a slash in escapeCss unless it opens a comment --- packages/mermaid-export/src/html.test.ts | 8 ++++++++ packages/mermaid-export/src/html.ts | 7 +++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/mermaid-export/src/html.test.ts b/packages/mermaid-export/src/html.test.ts index 89024c3..161b869 100644 --- a/packages/mermaid-export/src/html.test.ts +++ b/packages/mermaid-export/src/html.test.ts @@ -34,6 +34,14 @@ describe('escapeCss', () => { expect(escapeCss('rgb(1, 2, 3)')).toBe('rgb(1, 2, 3)'); }); + it('keeps the modern rgb syntax valid', () => { + expect(escapeCss('rgb(0 0 0 / 50%)')).toBe('rgb(0 0 0 / 50%)'); + }); + + it('still escapes a comment opener', () => { + expect(escapeCss('red/*x')).toBe('red\\2f \\2a x'); + }); + it('escapes characters that end a declaration or the style tag', () => { const out = escapeCss('red;} '); expect(out).not.toMatch(/[;{}<>]/); diff --git a/packages/mermaid-export/src/html.ts b/packages/mermaid-export/src/html.ts index 8cd8970..d293f10 100644 --- a/packages/mermaid-export/src/html.ts +++ b/packages/mermaid-export/src/html.ts @@ -26,13 +26,16 @@ export function escapeJsString(value: string): string { /** * A value for a CSS declaration. Anything outside letters, digits and - * the characters colors use (# ( ) , . % space -) is hex escaped, so it + * the characters colors use (# ( ) , . % space - and / unless it opens a comment) is hex escaped, so it * cannot end the declaration, open a block or close the style tag. */ export function escapeCss(value: string): string { return String(value).replace( /[^A-Za-z0-9#(),.% -]/gu, - (ch) => `\\${ch.codePointAt(0)!.toString(16)} ` + (ch, offset, str) => + ch === '/' && str[offset + 1] !== '*' + ? ch + : `\\${ch.codePointAt(0)!.toString(16)} ` ); }