|
2 | 2 | Traitlets based configuration for jupyter_server_proxy |
3 | 3 | """ |
4 | 4 | from notebook.utils import url_path_join as ujoin |
5 | | -from traitlets import Any, Dict |
| 5 | +from traitlets import Dict, List, Union, default |
6 | 6 | from traitlets.config import Configurable |
7 | 7 | from .handlers import SuperviseAndProxyHandler, AddSlashHandler |
8 | 8 | import pkg_resources |
9 | 9 | from collections import namedtuple |
10 | 10 | from .utils import call_with_asked_args |
11 | 11 |
|
| 12 | +try: |
| 13 | + # Traitlets >= 4.3.3 |
| 14 | + from traitlets import Callable |
| 15 | +except ImportError: |
| 16 | + from .utils import Callable |
| 17 | + |
12 | 18 | def _make_serverproxy_handler(name, command, environment, timeout, absolute_url, port): |
13 | 19 | """ |
14 | 20 | Create a SuperviseAndProxyHandler subclass with given parameters |
@@ -163,24 +169,29 @@ class ServerProxy(Configurable): |
163 | 169 | config=True |
164 | 170 | ) |
165 | 171 |
|
166 | | - host_whitelist_hook = Any( |
167 | | - lambda handler, host: host in ['localhost', '127.0.0.1'], |
| 172 | + host_whitelist = Union( |
| 173 | + trait_types=[List(), Callable()], |
168 | 174 | help=""" |
169 | | - Verify that a host should be proxied. |
| 175 | + List of allowed hosts. |
| 176 | + Can also be a function that decides whether a host can be proxied. |
170 | 177 |
|
171 | | - This should be a callable that checks whether a host should be proxied |
172 | | - and returns True if so (False otherwise). It could be a very simple |
173 | | - check that the host is present in a list of allowed hosts, or it could |
174 | | - be a more complex verification against a regular expression. It should |
175 | | - probably not be a slow check against an external service. Here is an |
176 | | - example that could be placed in a site-wide Jupyter notebook config: |
| 178 | + If implemented as a function, this should return True if a host should |
| 179 | + be proxied and False if it should not. Such a function could verify |
| 180 | + that the host matches a particular regular expression pattern or falls |
| 181 | + into a specific subnet. It should probably not be a slow check against |
| 182 | + some external service. Here is an example that could be placed in a |
| 183 | + site-wide Jupyter notebook config: |
177 | 184 |
|
178 | | - def hook(handler, host): |
| 185 | + def host_whitelist(handler, host): |
179 | 186 | handler.log.info("Request to proxy to host " + host) |
180 | 187 | return host.startswith("10.") |
181 | | - c.ServerProxy.host_whitelist_hook = hook |
182 | | - |
183 | | - The default check is to return True if the host is localhost. |
184 | | - """, |
| 188 | + c.ServerProxy.host_whitelist = host_whitelist |
| 189 | +
|
| 190 | + Defaults to a list of ["localhost", "127.0.0.1"]. |
| 191 | + """, |
185 | 192 | config=True |
186 | 193 | ) |
| 194 | + |
| 195 | + @default("host_whitelist") |
| 196 | + def _host_whitelist_default(self): |
| 197 | + return ["localhost", "127.0.0.1"] |
0 commit comments