Skip to content

Commit ae20f77

Browse files
Merge commit from fork
* escape URL to avoid arbitrary code execution * add pytest
1 parent 139c1dd commit ae20f77

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

nicegui/functions/navigate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Any, Callable, Union
22
from urllib.parse import urlparse
33

4-
from .. import background_tasks
4+
from .. import background_tasks, json
55
from ..client import Client
66
from ..context import context
77
from ..element import Element
@@ -89,7 +89,7 @@ def push(self, url: str) -> None:
8989
9090
:param url: relative or absolute URL
9191
"""
92-
run_javascript(f'history.pushState({{}}, "", "{url}");')
92+
run_javascript(f'history.pushState({{}}, "", {json.dumps(url)});')
9393

9494
def replace(self, url: str) -> None:
9595
"""Replace the current URL in the browser history.
@@ -100,7 +100,7 @@ def replace(self, url: str) -> None:
100100
101101
:param url: relative or absolute URL
102102
"""
103-
run_javascript(f'history.replaceState({{}}, "", "{url}");')
103+
run_javascript(f'history.replaceState({{}}, "", {json.dumps(url)});')
104104

105105

106106
navigate = Navigate()

tests/test_navigate.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,14 @@ def page():
7777
screen.click('Send mail')
7878
screen.wait(0.5)
7979
assert screen.selenium.execute_script('return window.__open_calls') == [['mailto:test@example.com', '_self']]
80+
81+
82+
def test_xss_via_history_push(screen: Screen):
83+
@ui.page('/')
84+
def page():
85+
ui.button('Push', on_click=lambda: ui.navigate.history.push('/");console.log("XSS");//'))
86+
87+
screen.open('/')
88+
screen.click('Push')
89+
screen.wait(1)
90+
assert 'XSS' not in screen.render_js_logs()

0 commit comments

Comments
 (0)