Skip to content

Commit ffa7e26

Browse files
committed
Merge lspvic#54
Extends XSRF header exceptions to POST requests per discussion in tensorflow/tensorboard#4685 (comment) Signed-off-by: Cliff Woolley <jwoolley@nvidia.com>
2 parents 130792b + dfa2a17 commit ffa7e26

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

jupyter_tensorboard/handlers.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# Copyright (c) 2017-2019, Shengpeng Liu. All rights reserved.
3+
# Copyright (c) 2019, Alex Ford. All rights reserved.
34
# Copyright (c) 2020-2021, NVIDIA CORPORATION. All rights reserved.
45

56
from tornado import web
@@ -8,23 +9,6 @@
89
from notebook.utils import url_path_join as ujoin
910
from notebook.base.handlers import path_regex
1011

11-
try:
12-
import wrapt
13-
@wrapt.patch_function_wrapper(web.RequestHandler, 'check_xsrf_cookie')
14-
def translate_check_xsrf_cookie(wrapped, instance, args, kwargs):
15-
16-
if ((instance.request.headers.get("X-XSRF-TOKEN")) and
17-
not (instance.get_argument("_xsrf", None)
18-
or instance.request.headers.get("X-Xsrftoken")
19-
or instance.request.headers.get("X-Csrftoken"))):
20-
21-
instance.request.headers.add("X-Xsrftoken", instance.request.headers.get("X-XSRF-TOKEN"))
22-
23-
wrapped(*args, **kwargs)
24-
except:
25-
pass
26-
27-
2812
notebook_dir = None
2913

3014
def load_jupyter_server_extension(nb_app):
@@ -70,7 +54,6 @@ def load_jupyter_server_extension(nb_app):
7054

7155
class TensorboardHandler(IPythonHandler):
7256

73-
7457
def _impl(self, name, path):
7558

7659
self.request.path = path
@@ -105,6 +88,48 @@ def post(self, name, path):
10588

10689
self._impl(name, path)
10790

91+
def check_xsrf_cookie(self):
92+
"""Expand XSRF check exceptions for POST requests.
93+
94+
Provides support for TensorBoard plugins that use POST to retrieve
95+
experiment information.
96+
97+
Expands check_xsrf_cookie exceptions, normally only applied to GET
98+
and HEAD requests, to POST requests, as TensorBoard POST endpoints
99+
do not modify state, so TensorBoard doesn't handle XSRF checks.
100+
101+
See https://github.com/tensorflow/tensorboard/issues/4685
102+
103+
"""
104+
105+
# Check XSRF token
106+
try:
107+
return super(TensorboardHandler, self).check_xsrf_cookie()
108+
109+
except web.HTTPError:
110+
# Note: GET and HEAD exceptions are already handled in
111+
# IPythonHandler.check_xsrf_cookie and will not normally throw 403
112+
113+
# For TB POSTs, we must loosen our expectations a bit. IPythonHandler
114+
# has some existing exceptions to consider a matching Referer as
115+
# sufficient for GET and HEAD requests; we extend that here to POST
116+
117+
if self.request.method in {"POST"}:
118+
# Consider Referer a sufficient cross-origin check, mirroring
119+
# logic in IPythonHandler.check_xsrf_cookie.
120+
if not self.check_referer():
121+
referer = self.request.headers.get("Referer")
122+
if referer:
123+
msg = (
124+
"Blocking Cross Origin request from {}."
125+
.format(referer)
126+
)
127+
else:
128+
msg = "Blocking request from unknown origin"
129+
raise web.HTTPError(403, msg)
130+
else:
131+
raise
132+
108133

109134
class TbFontHandler(IPythonHandler):
110135

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
-e .
22
flake8
33
pytest
4-
wrapt

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def run(self):
7474
]],
7575
install_requires=[
7676
'notebook>=5.0',
77-
'wrapt>=1.12',
7877
],
7978
classifiers=[
8079
'Intended Audience :: Developers',

0 commit comments

Comments
 (0)