|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # Copyright (c) 2017-2019, Shengpeng Liu. All rights reserved. |
| 3 | +# Copyright (c) 2019, Alex Ford. All rights reserved. |
3 | 4 | # Copyright (c) 2020-2021, NVIDIA CORPORATION. All rights reserved. |
4 | 5 |
|
5 | 6 | from tornado import web |
|
8 | 9 | from notebook.utils import url_path_join as ujoin |
9 | 10 | from notebook.base.handlers import path_regex |
10 | 11 |
|
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 | | - |
28 | 12 | notebook_dir = None |
29 | 13 |
|
30 | 14 | def load_jupyter_server_extension(nb_app): |
@@ -70,7 +54,6 @@ def load_jupyter_server_extension(nb_app): |
70 | 54 |
|
71 | 55 | class TensorboardHandler(IPythonHandler): |
72 | 56 |
|
73 | | - |
74 | 57 | def _impl(self, name, path): |
75 | 58 |
|
76 | 59 | self.request.path = path |
@@ -105,6 +88,48 @@ def post(self, name, path): |
105 | 88 |
|
106 | 89 | self._impl(name, path) |
107 | 90 |
|
| 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 | + |
108 | 133 |
|
109 | 134 | class TbFontHandler(IPythonHandler): |
110 | 135 |
|
|
0 commit comments