Summary
A stored cross-site scripting vulnerability in SiYuan's CSS snippet rendering allows crafted snippet bodies to break out of <style> tags and inject arbitrary JavaScript into the renderer.
When attackers can write to a synced workspace, malicious snippets execute on every device that pulls the workspace; on Electron desktop builds with nodeIntegration:true, this JavaScript can be escalated to remote code execution on the host.
Affected context
The vulnerability occurs in SiYuan's handling of CSS snippets, specifically when renderSnippet() interpolates snippet content into the DOM using insertAdjacentHTML inside a <style> tag.
Any environment where snippet bodies are treated as trusted HTML fragments and synchronized via the workspace repository is at risk, especially Electron desktop builds configured with nodeIntegration:true in the renderer.
Risk emerges when collaborators or automation can commit or modify snippet content in a workspace that is later synchronized across multiple devices, including systems where users have deliberately disabled JavaScript for safety but still allow CSS.
Exploit preconditions
- Attack surface: SiYuan workspace snippets rendered in the desktop Electron application and any web-based renderers that process CSS snippets via
renderSnippet(). - Required access: Ability to write or modify CSS snippet bodies in a synced workspace repository (e.g., collaborator access, compromised account, or malicious sync source).
- Trigger point: Viewing or rendering the affected snippet so that
renderSnippet()callsinsertAdjacentHTMLwith a payload containing</style>and script-capable markup. - Why exploitation is possible: The renderer directly interpolates untrusted snippet content as HTML into a
<style>tag without escaping or context-aware sanitization, allowing the payload to terminate the style element and inject executable JavaScript. In Electron withnodeIntegration:true, that JavaScript can access Node APIs such asrequire('child_process'), bridging XSS to host-level RCE. The design also violates the intended separation between enabled CSS and disabled JavaScript, implicitly trusting CSS content.
Why this happens
The underlying flaw is an unsafe HTML injection pattern where untrusted content is interpolated into the DOM using APIs like insertAdjacentHTML without encoding for the specific context (style element) and without sanitization.
Snippet bodies are treated as raw HTML inside a <style> tag, so a </style> token closes the element and reopens the parser in a context where script-capable markup is interpreted.
This matches CWE-79: Improper Neutralization of Input During Web Page Generation (Cross-site Scripting), because user-controlled data enters the page without proper neutralization and leads to script execution.
It is also related to CWE-1188: Insecure Default Initialization of Resource, as the renderer runs with nodeIntegration:true, exposing Node.js capabilities by default and amplifying the impact from XSS to full remote code execution on the host.
Potential impact
- Malicious JavaScript may execute in the SiYuan renderer context whenever an affected snippet is rendered.
- Attackers could read or modify notes, documents, and other data managed by SiYuan.
- Workspace credentials, session tokens, or API keys accessible in the renderer may be exfiltrated.
- On Electron desktop builds, injected JavaScript can access Node.js APIs and could execute arbitrary OS commands via
child_process, leading to host compromise. - Attackers may persist across devices by planting payloads in synchronized workspaces, causing widespread compromise among all users pulling the repository.
- User security preferences regarding enabled CSS versus disabled JavaScript can be bypassed, undermining trust in configuration safeguards.
General mitigation guidance
Effective mitigation focuses on treating snippet content as untrusted and constraining how it is injected into the DOM.
Pattern-based defensive approaches include:
- Avoiding direct use of
insertAdjacentHTMLor similar DOM APIs with unescaped, user-controlled content, especially inside context-sensitive elements such as<style>or<script>. - Encoding snippet bodies according to their intended context (e.g., CSS text rather than HTML fragments), using APIs or templates that treat them as text rather than markup.
- Introducing robust sanitization for any feature that allows custom CSS or HTML, ensuring that constructs capable of breaking out of tags or introducing scripts are rejected or neutralized.
- Revisiting default Electron configuration to minimize exposed capabilities—avoid
nodeIntegration:truein renderers unless strictly necessary and isolate untrusted content to hardened, sandboxed contexts. - Enforcing configuration semantics consistently so that disabling JavaScript truly prevents execution paths that originate from CSS or other non-script inputs.
- Applying least-privilege principles to renderer processes and filesystem access so that, even if XSS occurs, escalation to host-level command execution is constrained.
How teams detect issues like this
High-level detection can combine code-level and configuration-focused practices:
- Static analysis rules that flag use of
insertAdjacentHTML,innerHTML, or similar APIs with untrusted data, especially within<style>or<script>tags. - Code review checklists that require developers to justify any dynamic HTML injection and verify proper context-aware encoding, sanitization, and separation of HTML, CSS, and script content.
- Security linters or automated review tools configured to identify Electron renderers running with
nodeIntegration:trueand other dangerous defaults, prompting architectural reconsideration. - Runtime telemetry and logging around snippet rendering and exceptions in the renderer, which may reveal unexpected HTML structures or script execution originating from snippet content.
- Policy enforcement in build or CI pipelines that prohibit unsafe DOM injection patterns and ensure that features allowing custom CSS/HTML adhere to a documented threat model.

