Skip to content

Commit

Permalink
Address open redirect vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-bates committed Nov 13, 2020
1 parent 505140f commit 61ab548
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
9 changes: 6 additions & 3 deletions jupyter_server/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,12 @@ class TrailingSlashHandler(web.RequestHandler):
"""

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

post = put = get

Expand Down
29 changes: 28 additions & 1 deletion tests/test_paths.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re

import pytest
import tornado
from jupyter_server.base.handlers import path_regex


Expand Down Expand Up @@ -29,3 +30,29 @@ def test_path_regex_bad():
'/y/x/foo',
):
assert re.match(path_pat, path) is None


@pytest.mark.parametrize(
'uri,expected',
[
("/notebooks/mynotebook/", "/notebooks/mynotebook"),
("////foo///", "/foo"),
("//example.com/", "/example.com"),
("/has/param/?hasparam=true", "/has/param?hasparam=true"),
]
)
async def test_trailing_slash(uri, expected, http_server_client, auth_header, base_url):
# http_server_client raises an exception when follow_redirects=False
with pytest.raises(tornado.httpclient.HTTPClientError) as err:
await http_server_client.fetch(
uri,
headers=auth_header,
request_timeout=20,
follow_redirects=False
)
# Capture the response from the raised exception value.
response = err.value.response
assert response.code == 302
assert "Location" in response.headers
assert response.headers["Location"] == expected
assert False

0 comments on commit 61ab548

Please sign in to comment.