Skip to content

fix(core): use textContent instead of innerHTML for pathname display in BaseUrlIssueBanner #12216

Closed

Description

@paragao

Description

The insertBanner() function in packages/docusaurus/src/client/BaseUrlIssueBanner/index.tsx uses innerHTML to display a value derived from window.location.pathname:

suggestionContainer.innerHTML = suggestedBaseUrl;

While the practical exploitability is low (browsers percent-encode <> in pathname), this pattern causes every Docusaurus project to be flagged by automated security scanners (SAST/DAST) as a DOM-based XSS vulnerability — user-controlled input flowing into a dangerous DOM sink without sanitization.

CWE: CWE-79 — Improper Neutralization of Input During Web Page Generation (Cross-site Scripting), DOM-based variant

Impact

  • Downstream scanner noise: Tools like Snyk, SonarQube, Semgrep, CodeQL, and AWS ACAT flag this in the generated build output of every Docusaurus site with baseUrlIssueBanner: true (the default). This creates security tickets across thousands of organizations.
  • Defense-in-depth: Even though browsers currently percent-encode <> in URLs, relying on browser encoding behavior for XSS prevention is not a recognized secure coding practice. Future browser changes, non-standard URL handling, or server-side URL rewriting could alter this assumption.
  • Semantic correctness: suggestedBaseUrl is always a URL path string (e.g., /my-site/). Displaying text via innerHTML is a code smell — textContent is the semantically correct API.

Reproduction

  1. Build any Docusaurus site (e.g., npx create-docusaurus@latest)
  2. Run a SAST scanner against the generated build/ output
  3. Scanner identifies innerHTML assignment of window.location.pathname as DOM-based XSS (CWE-79)

Suggested Fix

One-line change — replace innerHTML with textContent:

-  suggestionContainer.innerHTML = suggestedBaseUrl;
+  suggestionContainer.textContent = suggestedBaseUrl;

This renders identically (the value is always a plain path string) with zero functional side effects.

Note: The bannerContainer.innerHTML = bannerHtml assignment on line 57 should remain unchanged — bannerHtml is a trusted HTML template generated at build time containing DOM elements (<div>, <p>, <a>) that must be parsed as HTML.

Environment

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions