Skip to content

Commit 6d7109a

Browse files
authored
Merge pull request #6511 from RRosio/patch-redirect
Update redirect logic and tests
2 parents 8794be8 + 9aacc4d commit 6d7109a

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

notebook/auth/login.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
import os
88

9-
from urllib.parse import urlparse
9+
from urllib.parse import urlparse, urlunparse
1010

1111
import uuid
1212

@@ -42,15 +42,18 @@ def _redirect_safe(self, url, default=None):
4242
# instead of %5C, causing `\\` to behave as `//`
4343
url = url.replace("\\", "%5C")
4444
parsed = urlparse(url)
45-
if parsed.netloc or not (parsed.path + '/').startswith(self.base_url):
45+
path_only = urlunparse(parsed._replace(netloc='', scheme=''))
46+
if url != path_only or not (parsed.path + '/').startswith(self.base_url):
4647
# require that next_url be absolute path within our path
4748
allow = False
4849
# OR pass our cross-origin check
49-
if parsed.netloc:
50+
if url != path_only:
5051
# if full URL, run our cross-origin check:
5152
origin = f'{parsed.scheme}://{parsed.netloc}'
5253
origin = origin.lower()
53-
if self.allow_origin:
54+
if origin == f'{self.request.protocol}://{self.request.host}':
55+
allow = True
56+
elif self.allow_origin:
5457
allow = self.allow_origin == origin
5558
elif self.allow_origin_pat:
5659
allow = bool(self.allow_origin_pat.match(origin))

notebook/auth/tests/test_login.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_next_bad(self):
3131
"//host" + self.url_prefix + "tree",
3232
"https://google.com",
3333
"/absolute/not/base_url",
34+
"///jupyter.org",
35+
"/\\some-host",
3436
):
3537
url = self.login(next=bad_next)
3638
self.assertEqual(url, self.url_prefix)
@@ -39,10 +41,14 @@ def test_next_bad(self):
3941
def test_next_ok(self):
4042
for next_path in (
4143
"tree/",
42-
"//" + self.url_prefix + "tree",
44+
self.base_url() + "has/host",
4345
"notebooks/notebook.ipynb",
4446
"tree//something",
4547
):
46-
expected = self.url_prefix + next_path
48+
if "://" in next_path:
49+
expected = next_path
50+
else:
51+
expected = self.url_prefix + next_path
52+
4753
actual = self.login(next=expected)
4854
self.assertEqual(actual, expected)

0 commit comments

Comments
 (0)