Skip to content

Commit

Permalink
Add xsrf exception to Tensorboard POST requests.
Browse files Browse the repository at this point in the history
Expand xsrf_cookie exceptions, normally only applied to GET and HEAD
requests in the IPythonHandler, to POST requests in TensorboardHandler.

Provides support for hparams plugin, which uses POST to retrieve
experiment information but can't be trivially extended to include xsrf
information in these POST requests. Mirrors existing IPythonHandler
behavior, falling back to Referer header rather than form parameters.
  • Loading branch information
asford committed Aug 20, 2019
1 parent ecb70d9 commit 5e4f0fc
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions jupyter_tensorboard/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ def post(self, name, path):
else:
raise web.HTTPError(404)

def check_xsrf_cookie(self):
"""Expand xsrf check exception for POST requests.
Expand xsrf_cookie exceptions, normally only applied to GET and HEAD
requests, to POST requests for tensorboard api.
Provides support for hparams plugin, which uses POST to retrieve
experiment information but can't be trivially extended to include xsrf
information in these POST requests.
"""

try:
return super(TensorboardHandler, self).check_xsrf_cookie()
except web.HTTPError:
if self.request.method in {"GET", "POST", "HEAD"}:
# Consider Referer a sufficient cross-origin check for GET requests
# Extended to post for Tensorboard API
if not self.check_referer():
referer = self.request.headers.get("Referer")
if referer:
msg = "Blocking Cross Origin request from {}.".format(referer)
else:
msg = "Blocking request from unknown origin"
raise web.HTTPError(403, msg)
else:
raise


class TensorboardErrorHandler(IPythonHandler):
pass

0 comments on commit 5e4f0fc

Please sign in to comment.