fix(mermaid): separate diagram errors from frame failures and validate the frame protocol

Review findings on #1040:

- A bad fence no longer poisons the document. `renderMermaidDiagram` used to
  treat every `renderInFrame` rejection as a frame failure, so a mermaid
  syntax error (a normal `mermaid-error` answer) was retried and then flipped
  `frameUnavailable` — every later diagram failed until the page reloaded.
  Frame answers are now classified: `MermaidDiagramError` is final for that
  one diagram (its block shows the message + source), while
  `MermaidFrameError` (load/handshake/timeout/protocol) gets the single retry
  and then the page-level fast-fail breaker.
- Both directions of postMessage are validated. Parent side binds
  `event.source` to the frame's `contentWindow` plus an opaque-origin
  allowlist and validates id/svg/message; the inlined frame script binds
  `event.source` to `window.parent` and validates id/code/theme/length before
  rendering.
- Visibility gating now flushes on return: the 80ms inline-HTML pass remembers
  a skipped pass and replays it on `visibilitychange`, and the preview handle
  also pauses/resumes on document visibility (not just IntersectionObserver).
- Tests: new `tests/mermaid-frame.spec.ts` covers the error classes
  (diagram error does not break later renders; transport retry; breaker after
  two failures) and the message validator (wrong window/origin, malformed
  payloads); the asset spec pins the frame-side sender/payload checks.
This commit is contained in:
OCILY committed 2026-10-28 20:20:50 +08:00
1 parent 7c1e67193d
commit d7925a87d9
6 files changed
+290 -29

No files matched your search

+10 -1
View File
@@ -58,6 +58,7 @@ const mermaidSource = readFileSync(mermaidBundlePath(), 'utf8')
const FRAME_API = `(function () {
'use strict';
var seq = 0;
var MAX_SOURCE = 500000;
function configure(theme) {
window.mermaid.initialize({
startOnLoad: false,
@@ -77,9 +78,17 @@ const FRAME_API = `(function () {
window.__dshMermaidFrame = { render: render };
if (window.parent !== window) {
window.addEventListener('message', function (event) {
// The parent is the only legitimate sender; the opaque-origin sandbox
// cannot compare origins, so identity plus a strict payload schema is
// the boundary (the host document must never be able to drive this
// renderer with arbitrary content).
if (event.source !== window.parent) return;
var data = event.data;
if (!data || typeof data !== 'object' || data.type !== 'mermaid-render') return;
render(String(data.code == null ? '' : data.code), data.theme).then(function (svg) {
if (typeof data.id !== 'number' || !isFinite(data.id)) return;
if (typeof data.code !== 'string' || data.code.length > MAX_SOURCE) return;
var theme = data.theme === 'dark' ? 'dark' : 'default';
render(data.code, theme).then(function (svg) {
window.parent.postMessage({ type: 'mermaid-result', id: data.id, svg: svg }, '*');
}).catch(function (error) {
window.parent.postMessage({ type: 'mermaid-error', id: data.id, message: String((error && error.message) || error) }, '*');