Skip to content

Commit

Permalink
allow token-authenticated requests cross-origin by default
Browse files Browse the repository at this point in the history
we already apply this logic in our server-side checks,
but browsers check `Access-Control-Allow-Origin` headers themselves as well,
meaning that token-authenticated requests can’t be made cross-origin without CORS headers from browsers,
only scripts.

This makes default browser and server-side origin checks consistent
  • Loading branch information
minrk committed Oct 10, 2017
1 parent 0141d82 commit 12fcd69
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions notebook/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ def set_default_headers(self):
origin = self.get_origin()
if origin and self.allow_origin_pat.match(origin):
self.set_header("Access-Control-Allow-Origin", origin)
elif (
self.token_authenticated
and "Access-Control-Allow-Origin" not in
self.settings.get('headers', {})
):
# allow token-authenticated requests cross-origin by default.
# only apply this exception if allow-origin has not been specified.
self.set_header('Access-Control-Allow-Origin',
self.request.headers.get('Origin', ''))

if self.allow_credentials:
self.set_header("Access-Control-Allow-Credentials", 'true')

Expand Down Expand Up @@ -516,6 +526,17 @@ def options(self, *args, **kwargs):
'accept, content-type, authorization, x-xsrftoken')
self.set_header('Access-Control-Allow-Methods',
'GET, PUT, POST, PATCH, DELETE, OPTIONS')
# if authorization header is requested,
# that means the request is token-authenticated.
# avoid browser-side rejection of the preflight request.
# only allow this exception if allow_origin has not been specified.
if not (
self.allow_origin
or self.allow_origin_pat
or 'Access-Control-Allow-Origin' in self.settings.get('headers', {})
):
self.set_header('Access-Control-Allow-Origin',
self.request.headers.get('Origin', ''))


class Template404(IPythonHandler):
Expand Down

0 comments on commit 12fcd69

Please sign in to comment.