Skip to content

Comments

fix(security): prevent XSS in Mermaid WebView#678

Open
ex3ndr-bot wants to merge 1 commit intoslopus:mainfrom
ex3ndr-bot:fix/mermaid-xss-668
Open

fix(security): prevent XSS in Mermaid WebView#678
ex3ndr-bot wants to merge 1 commit intoslopus:mainfrom
ex3ndr-bot:fix/mermaid-xss-668

Conversation

@ex3ndr-bot
Copy link

Summary

Fixes XSS vulnerability in the Mermaid diagram WebView renderer.

Problem

User/AI-supplied mermaid diagram content was interpolated directly into an HTML template without sanitisation:

<div id="mermaid-container" class="mermaid">
    ${props.content}
</div>

A payload like </div><img src=x onerror=alert(1)> could break out of the mermaid container and execute arbitrary JavaScript in the WebView context.

Solution

Instead of HTML interpolation, the content is now:

  1. Passed via JSON.stringify() which safely escapes all special characters
  2. Parsed in JavaScript and rendered using mermaid.render() explicitly
  3. Error messages are also escaped to prevent XSS via error output

This approach:

  • Prevents HTML injection attacks
  • Maintains full mermaid diagram functionality
  • Handles errors gracefully with escaped output

Testing

Tested with:

  • Normal mermaid diagrams (flowcharts, sequence diagrams)
  • XSS payloads like </div><script>alert(1)</script>
  • Diagrams containing special characters (<, >, ", ')

Closes #668

Previously, mermaid diagram content was interpolated directly into the
WebView HTML template using ${props.content}, allowing XSS attacks with
payloads like </div><script>alert(1)</script>.

The fix:
- Pass content via JSON.stringify() which safely escapes all special chars
- Use mermaid.render() explicitly instead of startOnLoad to process the
  JSON-parsed content
- Add error handling that also escapes error messages

This prevents HTML injection while maintaining full mermaid diagram
functionality.

Closes slopus#668
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Low: unsanitised Mermaid content interpolated into WebView HTML

1 participant