Skip to content

[ISSUE]: JS security helper depends on DOMPurify presence but can silently fall back insecurely #526

Closed

Description

@Aditya8369

Description

initializeDOMPurify() returns null if DOMPurify is undefined and sanitizeForDisplay() falls back to HTML.escape(dirty). Escaping is safer for pure text, but the function is documented as “Sanitize user-generated content for display (removes all HTML)”. If dirty is later inserted with setElementContent(..., asHTML=true), it uses sanitizeForDisplay() and sets innerHTML = ... using the returned value. If DOMPurify is missing:
The fallback is escaped text, but then assigning escaped text to innerHTML still treats it as HTML entities. This is usually safe for escaped strings, but the risk is that developers may incorrectly assume sanitizeForDisplay always strips tags rather than only escaping; and other functions like sanitizeHTML() directly call DOMPurify.sanitize(...) without guarding DOMPurify existence.

Additional Context

Steps to reproduce:
Ensure DOMPurify is not loaded (e.g., remove/disable the script tag in the frontend page).
Call sanitizeHTML(dirty) somewhere in the app (or any code path using that function).
Observe runtime error or inconsistent sanitization behavior (depending on code path).
Also test setElementContent(element, "", true) and verify whether behavior is safe across app flows.

Activity

  1. Aditya8369 commented on May 15, 2026

    @Aditya8369
    ContributorAuthor

    @devanshi14malhotra please assign me under GSSoC

  2. added
    level:advancedGSSoC'26 advanced level PR/issue
    GSSoC'26For all issues/PRs related to GSSoC'2026 open source event
    NSoC'26For issues and PRs which are a part of the Nexus Spring of Code open source event
    on May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

GSSoC'26For all issues/PRs related to GSSoC'2026 open source eventNSoC'26For issues and PRs which are a part of the Nexus Spring of Code open source eventlevel310 pointslevel:advancedGSSoC'26 advanced level PR/issue

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions