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(' {
+ it('leaves normal colors alone', () => {
+ expect(escapeCss('transparent')).toBe('transparent');
+ expect(escapeCss('#ff00aa')).toBe('#ff00aa');
+ 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(/[;{}<>]/);
+ });
+});
+
+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('
+