Skip to content

Commit 61ab548

Browse files
committed
Address open redirect vulnerability
1 parent 505140f commit 61ab548

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

jupyter_server/base/handlers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,12 @@ class TrailingSlashHandler(web.RequestHandler):
776776
"""
777777

778778
def get(self):
779-
uri = self.request.path.rstrip("/")
780-
if uri:
781-
self.redirect('?'.join((uri, self.request.query)))
779+
path, *rest = self.request.uri.partition("?")
780+
# trim trailing *and* leading /
781+
# to avoid misinterpreting repeated '//'
782+
path = "/" + path.strip("/")
783+
new_uri = "".join([path, *rest])
784+
self.redirect(new_uri)
782785

783786
post = put = get
784787

tests/test_paths.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
2-
2+
import pytest
3+
import tornado
34
from jupyter_server.base.handlers import path_regex
45

56

@@ -29,3 +30,29 @@ def test_path_regex_bad():
2930
'/y/x/foo',
3031
):
3132
assert re.match(path_pat, path) is None
33+
34+
35+
@pytest.mark.parametrize(
36+
'uri,expected',
37+
[
38+
("/notebooks/mynotebook/", "/notebooks/mynotebook"),
39+
("////foo///", "/foo"),
40+
("//example.com/", "/example.com"),
41+
("/has/param/?hasparam=true", "/has/param?hasparam=true"),
42+
]
43+
)
44+
async def test_trailing_slash(uri, expected, http_server_client, auth_header, base_url):
45+
# http_server_client raises an exception when follow_redirects=False
46+
with pytest.raises(tornado.httpclient.HTTPClientError) as err:
47+
await http_server_client.fetch(
48+
uri,
49+
headers=auth_header,
50+
request_timeout=20,
51+
follow_redirects=False
52+
)
53+
# Capture the response from the raised exception value.
54+
response = err.value.response
55+
assert response.code == 302
56+
assert "Location" in response.headers
57+
assert response.headers["Location"] == expected
58+
assert False

0 commit comments

Comments
 (0)