/

CVE-2026-21873

Severity:

HIGH RISK

(7.2)

Cross-Site Scripting (XSS) in NiceGUI sub_pages

CVE-2026-21873

Analyzed

Vulnerability type

Category:

Cross-Site Scripting (XSS)

CWE:

CWE-79

Impact:

Cross-Site Scripting and Client-Side Code Execution

Severity:

HIGH RISK

(7.2)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

NiceGUI's pushstate event listener for ui.sub_pages from versions 2.22.0 to 3.4.1 unsafely processes the URL fragment (hash) component, enabling script injection via manipulated fragment identifiers.

An attacker can exploit this behavior cross-site using an iframe, potentially executing arbitrary JavaScript in the context of the vulnerable NiceGUI application when a victim loads a crafted page.

Affected context

The vulnerability occurs in NiceGUI applications that:

  • Use ui.sub_pages or related routing features relying on the pushstate event listener
  • Interpret or reflect the URL fragment (#...) into client-side state, DOM, or templates
  • Are embedded or can be indirectly loaded in iframes controlled by an attacker

These conditions allow untrusted fragment data to influence the front-end logic in a way that can be turned into script execution.

Exploit preconditions

  • Attack surface: Browser-based UI of a NiceGUI application using ui.sub_pages
  • Required access: Ability to get a victim to visit an attacker-controlled page (e.g., phishing, malicious link) that embeds the NiceGUI app in an iframe
  • Trigger point: Manipulation of the URL fragment identifier (#fragment) which the pushstate listener processes without adequate sanitization or isolation
  • Why exploitation is possible: The client-side handling of the fragment trusts and incorporates attacker-controlled values into DOM or script-relevant contexts, matching the pattern of reflected or DOM-based XSS (CWE-79)

Why this happens

This issue stems from client-side routing logic that treats the URL fragment as trusted input.

The pushstate event listener used by ui.sub_pages reacts to changes in the fragment identifier but does not robustly sanitize, encode, or strictly parse the fragment before using it in UI state or DOM updates.

Because the fragment can be controlled cross-site via iframes and similar mechanisms, this creates a classic XSS condition aligned with CWE-79: Improper Neutralization of Input During Web Page Generation, where untrusted data can influence browser-executed code.

Potential impact

  • Execution of arbitrary JavaScript in the context of the vulnerable NiceGUI application
  • Theft or manipulation of data visible in the web UI (e.g., session-based data exposed to the front-end)
  • Unauthorized actions performed on behalf of the user within the application's browser session, subject to existing permissions
  • UI redressing, phishing overlays, or injection of misleading content in the application interface
  • Potential pivot into broader account compromise if tokens or sensitive identifiers are accessible to front-end scripts

General mitigation guidance

Mitigations focus on treating URL-derived data, including fragments, as untrusted and limiting its influence on UI rendering:

  • Apply strict encoding and sanitization when incorporating any URL components into HTML, attributes, or JavaScript contexts
  • Restrict ui.sub_pages routing logic to a fixed, known-safe set of paths rather than interpreting raw fragment content
  • Avoid directly evaluating or concatenating fragment values into JavaScript or HTML; use framework mechanisms that auto-escape content
  • Introduce a parsing layer that validates the fragment against an expected schema (e.g., enumerated route names, numeric IDs) before use
  • Reduce the privileges of front-end pages by minimizing sensitive data exposed client-side and separating highly privileged views from publicly embeddable routes

How teams detect issues like this

Detection typically involves a combination of code-level and behavior-level review:

  • Static analysis: Search for code paths where URL fragments, window.location.hash, or pushstate events are read and then used in DOM APIs (innerHTML, outerHTML, insertAdjacentHTML) or dynamic script construction
  • Code review: Examine ui.sub_pages integrations and routing logic for direct use of fragment values without escaping or validation; look for string concatenation into templates or script blocks
  • Security testing: Use automated or manual XSS testing tools to mutate the fragment identifier and observe whether payloads appear in the rendered DOM or event handlers
  • Policy enforcement: Establish frontend development guidelines requiring encoding libraries or templating mechanisms that default to escaping, and add checks in CI to flag unsafe DOM sinks and URL-derived data flows

Sources

SourceLink
github.comgithub.com/zauberzeug/nicegui/releases/tag/v3.5.0
github.comgithub.com/zauberzeug/nicegui/security/advisories/GHSA-mhpg-c27v-6mxr

Frequently asked

questions

Yes. An attacker can host a malicious page that embeds the vulnerable NiceGUI application in an iframe and manipulates the URL fragment, potentially leading to script execution when a victim's browser loads the page.

Not necessarily. If the vulnerable routes are publicly accessible, attackers only need to induce a victim to visit a crafted page. If the NiceGUI UI is behind authentication, the impact is constrained by whether the victim is logged in and what data or actions their session permits.

Treat any URL-derived value, including the fragment, as untrusted input: strictly parse it into known-safe route tokens, apply context-aware encoding before rendering, and avoid using raw fragments in HTML or JavaScript construction. Prefer high-level routing APIs that abstract away direct hash manipulation.

Adopt a secure-by-default client-side architecture: use templating systems that auto-escape content, prohibit direct use of innerHTML or dynamic script creation with untrusted data, establish clear patterns for handling URL parameters and fragments, and integrate security-focused review or linting rules into the development workflow.

Find out what's already
exploitable in your codebase.