Skip to content

Commit

Permalink
Merge pull request #6511 from RRosio/patch-redirect
Browse files Browse the repository at this point in the history
Update redirect logic and tests
  • Loading branch information
echarles authored Aug 10, 2022
2 parents 8794be8 + 9aacc4d commit 6d7109a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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)

0 comments on commit 6d7109a

Please sign in to comment.