Skip to content

Commit 0950252

Browse files
committed
Escape invalid host when displayed in html
1 parent ecb8143 commit 0950252

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

jupyter_server_proxy/handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from jupyter_server.utils import ensure_async, url_path_join
1717
from simpervisor import SupervisedProcess
1818
from tornado import httpclient, httputil, web
19+
from tornado.escape import xhtml_escape
1920
from tornado.simple_httpclient import SimpleAsyncHTTPClient
2021
from traitlets import Bytes, Dict, Instance, Integer, Unicode, Union, default, observe
2122
from traitlets.traitlets import HasTraits
@@ -327,7 +328,7 @@ async def proxy(self, host, port, proxied_path):
327328
self.write(
328329
"Host '{host}' is not allowed. "
329330
"See https://jupyter-server-proxy.readthedocs.io/en/latest/arbitrary-ports-hosts.html for info.".format(
330-
host=host
331+
host=xhtml_escape(host)
331332
)
332333
)
333334
return

tests/test_proxies.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ def test_server_proxy_host_absolute(a_server_port_and_token: Tuple[int, str]) ->
255255
assert "X-Proxycontextpath" not in s
256256

257257

258+
def test_server_proxy_host_invalid(a_server_port_and_token: Tuple[int, str]) -> None:
259+
PORT, TOKEN = a_server_port_and_token
260+
r = request_get(PORT, "/proxy/absolute/<invalid>:54321/", TOKEN)
261+
assert r.code == 403
262+
s = r.read().decode("ascii")
263+
assert s.startswith("Host '&lt;invalid&gt;' is not allowed.")
264+
265+
258266
def test_server_proxy_port_non_service_rewrite_response(
259267
a_server_port_and_token: Tuple[int, str]
260268
) -> None:

0 commit comments

Comments
 (0)