Skip to content

Commit 5e4f0fc

Browse files
committed
Add xsrf exception to Tensorboard POST requests.
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.
1 parent ecb70d9 commit 5e4f0fc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

jupyter_tensorboard/handlers.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,34 @@ def post(self, name, path):
9191
else:
9292
raise web.HTTPError(404)
9393

94+
def check_xsrf_cookie(self):
95+
"""Expand xsrf check exception for POST requests.
96+
97+
Expand xsrf_cookie exceptions, normally only applied to GET and HEAD
98+
requests, to POST requests for tensorboard api.
99+
100+
Provides support for hparams plugin, which uses POST to retrieve
101+
experiment information but can't be trivially extended to include xsrf
102+
information in these POST requests.
103+
104+
"""
105+
106+
try:
107+
return super(TensorboardHandler, self).check_xsrf_cookie()
108+
except web.HTTPError:
109+
if self.request.method in {"GET", "POST", "HEAD"}:
110+
# Consider Referer a sufficient cross-origin check for GET requests
111+
# Extended to post for Tensorboard API
112+
if not self.check_referer():
113+
referer = self.request.headers.get("Referer")
114+
if referer:
115+
msg = "Blocking Cross Origin request from {}.".format(referer)
116+
else:
117+
msg = "Blocking request from unknown origin"
118+
raise web.HTTPError(403, msg)
119+
else:
120+
raise
121+
94122

95123
class TensorboardErrorHandler(IPythonHandler):
96124
pass

0 commit comments

Comments
 (0)