Summary
SiYuan versions 3.6.3 and below render Mermaid diagrams with a permissive security level and inject the resulting SVG into the DOM using innerHTML, allowing attacker-controlled javascript: URLs to persist in rendered output.
On Electron-based desktop builds with nodeIntegration enabled and contextIsolation disabled, this stored XSS can escalate to arbitrary code execution when a victim opens a malicious note and interacts with the rendered diagram.
Affected context
The vulnerability occurs in SiYuan's rendering pipeline for Mermaid diagrams inside notes, where:
- Mermaid is configured with
securityLevelset toloose - Generated SVG content is inserted into the document using
innerHTML - On desktop builds, the application runs in an Electron environment with
nodeIntegrationenabled andcontextIsolationdisabled
The issue becomes relevant when users open or sync notes that may contain untrusted Mermaid code blocks, such as from shared workspaces, synced folders, or imported documents.
Exploit preconditions
- Attack surface: Mermaid diagram rendering in SiYuan notes (preview/rendered mode).
- Required access: Ability to create, edit, sync, or otherwise cause a note to contain attacker-controlled Mermaid code (e.g., shared workspace, imported notes, compromised sync source).
- User interaction: A victim must open the note so it renders, and on desktop builds click or interact with the malicious diagram node that contains a
javascript:URL. - Trigger point: Rendering of Mermaid diagrams with
securityLevel = "loose"and injection of the resulting SVG viainnerHTMLinto a privileged Electron renderer. - Why exploitation is possible: The permissive Mermaid configuration allows unsafe links/attributes, and the Electron window is configured with
nodeIntegrationenabled andcontextIsolationdisabled, so JavaScript execution can directly access Node.js APIs and the host environment.
Why this happens
The core issue is a combination of unsafe content rendering and overly permissive runtime configuration:
- Mermaid is configured with
securityLevel: "loose", which tolerates potentially dangerousjavascript:URLs and other untrusted attributes in diagram output. - The resulting SVG is injected into the DOM using
innerHTML, a pattern that directly interprets attacker-controlled markup and script-related attributes (mapping to CWE-79: Improper Neutralization of Input During Web Page Generation (Cross-Site Scripting)). - In the Electron desktop environment, windows are created with
nodeIntegrationenabled andcontextIsolationdisabled, violating isolation best practices and allowing browser JavaScript to reach Node.js APIs, effectively bridging a web XSS into system-level code execution (connected to CWE-94: Improper Control of Generation of Code when arbitrary script gains access to a code-capable environment).
Abstractly, this is an unsafe mixing of untrusted markup with privileged renderer capabilities, without applying strict sanitization or containment.
Potential impact
- Execution of arbitrary JavaScript in the SiYuan renderer context when a malicious note is opened.
- Escalation to arbitrary code execution on the victim's machine in desktop/Electron builds via Node.js APIs.
- Reading, modifying, or exfiltrating note contents, including sensitive personal knowledge or private documents.
- Tampering with application configuration or local files accessible to the Electron process.
- Use of the compromised client as a foothold for broader system compromise, depending on OS-level protections and user privileges.
General mitigation guidance
Effective mitigation focuses on both safe content handling and hardened desktop runtime configuration:
- Prefer stricter Mermaid configurations (e.g., secure
securityLevelsettings) that disallowjavascript:URLs and unsafe link targets. - Avoid inserting diagram-rendered SVG or HTML directly with
innerHTML; instead, use APIs or libraries that sanitize or safely construct DOM nodes. - Introduce a robust sanitization layer for all rendered rich-content blocks (including Markdown/HTML/Mermaid) that strips dangerous protocols, event handlers, and script-capable attributes before DOM insertion.
- In Electron environments, follow secure-by-default practices:
- Disable
nodeIntegrationin renderer windows unless absolutely necessary. - Enable
contextIsolationso untrusted page scripts cannot directly access privileged objects. - Use preload scripts and explicit IPC channels to mediate any required privileged operations.
- Treat synced and imported notes as untrusted content and render them within the same hardened, sandboxed pipeline as remotely sourced data.
How teams detect issues like this
Detection can leverage a mix of code-level and configuration-level reviews:
- Static analysis and linting:
- Search for
innerHTML,outerHTML, or similar APIs used with content derived from user-editable notes or imported files, especially in rendering pipelines. - Flag uses of Mermaid configuration where
securityLevelis set to"loose"or other permissive modes. - Identify Electron window creation with
nodeIntegration: trueorcontextIsolation: false, especially for windows that load user-controlled or rich text content. - Code review patterns:
- Review all Markdown/diagram rendering features for direct DOM injection, protocol handling for links, and lack of sanitization.
- Check that all content transformers (Markdown → HTML, Mermaid → SVG, etc.) enforce safe default configurations and do not rely on client-side trust.
- Policy and automated checks:
- Enforce project policies that ban dangerous Electron flags in production builds unless tightly sandboxed.
- Integrate security-focused linters or SAST tools into CI to surface insecure DOM sinks and unsafe Electron practices.
- Log and, where feasible, monitor usage of high-risk rendering paths in test builds to ensure no unsafe patterns are reintroduced.

