Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update redirect logic and tests #6511

Merged
merged 2 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions notebook/auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import os

from urllib.parse import urlparse
from urllib.parse import urlparse, urlunparse

import uuid

Expand Down Expand Up @@ -42,15 +42,18 @@ def _redirect_safe(self, url, default=None):
# instead of %5C, causing `\\` to behave as `//`
url = url.replace("\\", "%5C")
parsed = urlparse(url)
if parsed.netloc or not (parsed.path + '/').startswith(self.base_url):
path_only = urlunparse(parsed._replace(netloc='', scheme=''))
if url != path_only or not (parsed.path + '/').startswith(self.base_url):
# require that next_url be absolute path within our path
allow = False
# OR pass our cross-origin check
if parsed.netloc:
if url != path_only:
# if full URL, run our cross-origin check:
origin = f'{parsed.scheme}://{parsed.netloc}'
origin = origin.lower()
if self.allow_origin:
if origin == f'{self.request.protocol}://{self.request.host}':
allow = True
elif self.allow_origin:
allow = self.allow_origin == origin
elif self.allow_origin_pat:
allow = bool(self.allow_origin_pat.match(origin))
Expand Down
10 changes: 8 additions & 2 deletions notebook/auth/tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def test_next_bad(self):
"//host" + self.url_prefix + "tree",
"https://google.com",
"/absolute/not/base_url",
"///jupyter.org",
"/\\some-host",
):
url = self.login(next=bad_next)
self.assertEqual(url, self.url_prefix)
Expand All @@ -39,10 +41,14 @@ def test_next_bad(self):
def test_next_ok(self):
for next_path in (
"tree/",
"//" + self.url_prefix + "tree",
self.base_url() + "has/host",
"notebooks/notebook.ipynb",
"tree//something",
):
expected = self.url_prefix + next_path
if "://" in next_path:
expected = next_path
else:
expected = self.url_prefix + next_path

actual = self.login(next=expected)
self.assertEqual(actual, expected)