Skip to content

Commit a8c6b8b

Browse files
minrkgnestor
authored andcommitted
Fix some errors caused by raising 403 in get_current_user (jupyter#2919)
get_current_user is called in a few places that really shouldn’t raise move the raising to `get_login_url`, which is called in `@web.authenticated`, where we want to replace redirect logic with 403.
1 parent 2ee51ab commit a8c6b8b

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

notebook/base/handlers.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def skip_check_origin(self):
104104
For example: in the default LoginHandler, if a request is token-authenticated,
105105
origin checking should be skipped.
106106
"""
107+
if self.request.method == 'OPTIONS':
108+
# no origin-check on options requests, which are used to check origins!
109+
return True
107110
if self.login_handler is None or not hasattr(self.login_handler, 'should_check_origin'):
108111
return False
109112
return not self.login_handler.should_check_origin(self)
@@ -476,10 +479,16 @@ def get_current_user(self):
476479
if hasattr(self, '_user_cache'):
477480
return self._user_cache
478481
self._user_cache = user = super(APIHandler, self).get_current_user()
479-
if user is None:
480-
raise web.HTTPError(403)
481482
return user
482483

484+
def get_login_url(self):
485+
# if get_login_url is invoked in an API handler,
486+
# that means @web.authenticated is trying to trigger a redirect.
487+
# instead of redirecting, raise 403 instead.
488+
if not self.current_user:
489+
raise web.HTTPError(403)
490+
return super(APIHandler, self).get_login_url()
491+
483492
@property
484493
def content_security_policy(self):
485494
csp = '; '.join([
@@ -494,7 +503,7 @@ def content_security_policy(self):
494503
def update_api_activity(self):
495504
"""Update last_activity of API requests"""
496505
# record activity of authenticated requests
497-
if self._track_activity and self.get_current_user():
506+
if self._track_activity and getattr(self, '_user_cache', None):
498507
self.settings['api_last_activity'] = utcnow()
499508

500509
def finish(self, *args, **kwargs):
@@ -507,7 +516,6 @@ def options(self, *args, **kwargs):
507516
'accept, content-type, authorization, x-xsrftoken')
508517
self.set_header('Access-Control-Allow-Methods',
509518
'GET, PUT, POST, PATCH, DELETE, OPTIONS')
510-
self.finish()
511519

512520

513521
class Template404(IPythonHandler):

0 commit comments

Comments
 (0)