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..161b869 --- /dev/null +++ b/packages/mermaid-export/src/html.test.ts @@ -0,0 +1,72 @@ +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();