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_pagesor 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_pagesrouting 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_pagesintegrations 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

