handlers.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. """Base Tornado handlers for the notebook server."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import datetime
  5. import functools
  6. import ipaddress
  7. import json
  8. import mimetypes
  9. import os
  10. import re
  11. import sys
  12. import traceback
  13. import types
  14. import warnings
  15. try:
  16. # py3
  17. from http.client import responses
  18. from http.cookies import Morsel
  19. except ImportError:
  20. from httplib import responses
  21. from Cookie import Morsel
  22. try:
  23. from urllib.parse import urlparse # Py 3
  24. except ImportError:
  25. from urlparse import urlparse # Py 2
  26. from jinja2 import TemplateNotFound
  27. from tornado import web, gen, escape, httputil
  28. from tornado.log import app_log
  29. import prometheus_client
  30. from notebook._sysinfo import get_sys_info
  31. from traitlets.config import Application
  32. from ipython_genutils.path import filefind
  33. from ipython_genutils.py3compat import string_types, PY3
  34. import notebook
  35. from notebook._tz import utcnow
  36. from notebook.i18n import combine_translations
  37. from notebook.utils import is_hidden, url_path_join, url_is_absolute, url_escape
  38. from notebook.services.security import csp_report_uri
  39. #-----------------------------------------------------------------------------
  40. # Top-level handlers
  41. #-----------------------------------------------------------------------------
  42. non_alphanum = re.compile(r'[^A-Za-z0-9]')
  43. _sys_info_cache = None
  44. def json_sys_info():
  45. global _sys_info_cache
  46. if _sys_info_cache is None:
  47. _sys_info_cache = json.dumps(get_sys_info())
  48. return _sys_info_cache
  49. def log():
  50. if Application.initialized():
  51. return Application.instance().log
  52. else:
  53. return app_log
  54. class AuthenticatedHandler(web.RequestHandler):
  55. """A RequestHandler with an authenticated user."""
  56. @property
  57. def content_security_policy(self):
  58. """The default Content-Security-Policy header
  59. Can be overridden by defining Content-Security-Policy in settings['headers']
  60. """
  61. if 'Content-Security-Policy' in self.settings.get('headers', {}):
  62. # user-specified, don't override
  63. return self.settings['headers']['Content-Security-Policy']
  64. return '; '.join([
  65. "frame-ancestors 'self'",
  66. # Make sure the report-uri is relative to the base_url
  67. "report-uri " + self.settings.get('csp_report_uri', url_path_join(self.base_url, csp_report_uri)),
  68. ])
  69. def set_default_headers(self):
  70. headers = {}
  71. headers["X-Content-Type-Options"] = "nosniff"
  72. headers.update(self.settings.get('headers', {}))
  73. headers["Content-Security-Policy"] = self.content_security_policy
  74. # Allow for overriding headers
  75. for header_name, value in headers.items():
  76. try:
  77. self.set_header(header_name, value)
  78. except Exception as e:
  79. # tornado raise Exception (not a subclass)
  80. # if method is unsupported (websocket and Access-Control-Allow-Origin
  81. # for example, so just ignore)
  82. self.log.debug(e)
  83. def force_clear_cookie(self, name, path="/", domain=None):
  84. """Deletes the cookie with the given name.
  85. Tornado's cookie handling currently (Jan 2018) stores cookies in a dict
  86. keyed by name, so it can only modify one cookie with a given name per
  87. response. The browser can store multiple cookies with the same name
  88. but different domains and/or paths. This method lets us clear multiple
  89. cookies with the same name.
  90. Due to limitations of the cookie protocol, you must pass the same
  91. path and domain to clear a cookie as were used when that cookie
  92. was set (but there is no way to find out on the server side
  93. which values were used for a given cookie).
  94. """
  95. name = escape.native_str(name)
  96. expires = datetime.datetime.utcnow() - datetime.timedelta(days=365)
  97. morsel = Morsel()
  98. morsel.set(name, '', '""')
  99. morsel['expires'] = httputil.format_timestamp(expires)
  100. morsel['path'] = path
  101. if domain:
  102. morsel['domain'] = domain
  103. self.add_header("Set-Cookie", morsel.OutputString())
  104. def clear_login_cookie(self):
  105. cookie_options = self.settings.get('cookie_options', {})
  106. path = cookie_options.setdefault('path', self.base_url)
  107. self.clear_cookie(self.cookie_name, path=path)
  108. if path and path != '/':
  109. # also clear cookie on / to ensure old cookies are cleared
  110. # after the change in path behavior (changed in notebook 5.2.2).
  111. # N.B. This bypasses the normal cookie handling, which can't update
  112. # two cookies with the same name. See the method above.
  113. self.force_clear_cookie(self.cookie_name)
  114. def get_current_user(self):
  115. if self.login_handler is None:
  116. return 'anonymous'
  117. return self.login_handler.get_user(self)
  118. def skip_check_origin(self):
  119. """Ask my login_handler if I should skip the origin_check
  120. For example: in the default LoginHandler, if a request is token-authenticated,
  121. origin checking should be skipped.
  122. """
  123. if self.request.method == 'OPTIONS':
  124. # no origin-check on options requests, which are used to check origins!
  125. return True
  126. if self.login_handler is None or not hasattr(self.login_handler, 'should_check_origin'):
  127. return False
  128. return not self.login_handler.should_check_origin(self)
  129. @property
  130. def token_authenticated(self):
  131. """Have I been authenticated with a token?"""
  132. if self.login_handler is None or not hasattr(self.login_handler, 'is_token_authenticated'):
  133. return False
  134. return self.login_handler.is_token_authenticated(self)
  135. @property
  136. def cookie_name(self):
  137. default_cookie_name = non_alphanum.sub('-', 'username-{}'.format(
  138. self.request.host
  139. ))
  140. return self.settings.get('cookie_name', default_cookie_name)
  141. @property
  142. def logged_in(self):
  143. """Is a user currently logged in?"""
  144. user = self.get_current_user()
  145. return (user and not user == 'anonymous')
  146. @property
  147. def login_handler(self):
  148. """Return the login handler for this application, if any."""
  149. return self.settings.get('login_handler_class', None)
  150. @property
  151. def token(self):
  152. """Return the login token for this application, if any."""
  153. return self.settings.get('token', None)
  154. @property
  155. def login_available(self):
  156. """May a user proceed to log in?
  157. This returns True if login capability is available, irrespective of
  158. whether the user is already logged in or not.
  159. """
  160. if self.login_handler is None:
  161. return False
  162. return bool(self.login_handler.get_login_available(self.settings))
  163. class IPythonHandler(AuthenticatedHandler):
  164. """IPython-specific extensions to authenticated handling
  165. Mostly property shortcuts to IPython-specific settings.
  166. """
  167. @property
  168. def ignore_minified_js(self):
  169. """Wether to user bundle in template. (*.min files)
  170. Mainly use for development and avoid file recompilation
  171. """
  172. return self.settings.get('ignore_minified_js', False)
  173. @property
  174. def config(self):
  175. return self.settings.get('config', None)
  176. @property
  177. def log(self):
  178. """use the IPython log by default, falling back on tornado's logger"""
  179. return log()
  180. @property
  181. def jinja_template_vars(self):
  182. """User-supplied values to supply to jinja templates."""
  183. return self.settings.get('jinja_template_vars', {})
  184. #---------------------------------------------------------------
  185. # URLs
  186. #---------------------------------------------------------------
  187. @property
  188. def version_hash(self):
  189. """The version hash to use for cache hints for static files"""
  190. return self.settings.get('version_hash', '')
  191. @property
  192. def mathjax_url(self):
  193. url = self.settings.get('mathjax_url', '')
  194. if not url or url_is_absolute(url):
  195. return url
  196. return url_path_join(self.base_url, url)
  197. @property
  198. def mathjax_config(self):
  199. return self.settings.get('mathjax_config', 'TeX-AMS-MML_HTMLorMML-full,Safe')
  200. @property
  201. def base_url(self):
  202. return self.settings.get('base_url', '/')
  203. @property
  204. def default_url(self):
  205. return self.settings.get('default_url', '')
  206. @property
  207. def ws_url(self):
  208. return self.settings.get('websocket_url', '')
  209. @property
  210. def contents_js_source(self):
  211. self.log.debug("Using contents: %s", self.settings.get('contents_js_source',
  212. 'services/contents'))
  213. return self.settings.get('contents_js_source', 'services/contents')
  214. #---------------------------------------------------------------
  215. # Manager objects
  216. #---------------------------------------------------------------
  217. @property
  218. def kernel_manager(self):
  219. return self.settings['kernel_manager']
  220. @property
  221. def contents_manager(self):
  222. return self.settings['contents_manager']
  223. @property
  224. def session_manager(self):
  225. return self.settings['session_manager']
  226. @property
  227. def terminal_manager(self):
  228. return self.settings['terminal_manager']
  229. @property
  230. def kernel_spec_manager(self):
  231. return self.settings['kernel_spec_manager']
  232. @property
  233. def config_manager(self):
  234. return self.settings['config_manager']
  235. #---------------------------------------------------------------
  236. # CORS
  237. #---------------------------------------------------------------
  238. @property
  239. def allow_origin(self):
  240. """Normal Access-Control-Allow-Origin"""
  241. return self.settings.get('allow_origin', '')
  242. @property
  243. def allow_origin_pat(self):
  244. """Regular expression version of allow_origin"""
  245. return self.settings.get('allow_origin_pat', None)
  246. @property
  247. def allow_credentials(self):
  248. """Whether to set Access-Control-Allow-Credentials"""
  249. return self.settings.get('allow_credentials', False)
  250. def set_default_headers(self):
  251. """Add CORS headers, if defined"""
  252. super(IPythonHandler, self).set_default_headers()
  253. if self.allow_origin:
  254. self.set_header("Access-Control-Allow-Origin", self.allow_origin)
  255. elif self.allow_origin_pat:
  256. origin = self.get_origin()
  257. if origin and self.allow_origin_pat.match(origin):
  258. self.set_header("Access-Control-Allow-Origin", origin)
  259. elif (
  260. self.token_authenticated
  261. and "Access-Control-Allow-Origin" not in
  262. self.settings.get('headers', {})
  263. ):
  264. # allow token-authenticated requests cross-origin by default.
  265. # only apply this exception if allow-origin has not been specified.
  266. self.set_header('Access-Control-Allow-Origin',
  267. self.request.headers.get('Origin', ''))
  268. if self.allow_credentials:
  269. self.set_header("Access-Control-Allow-Credentials", 'true')
  270. def set_attachment_header(self, filename):
  271. """Set Content-Disposition: attachment header
  272. As a method to ensure handling of filename encoding
  273. """
  274. escaped_filename = url_escape(filename)
  275. self.set_header('Content-Disposition',
  276. 'attachment;'
  277. " filename*=utf-8''{utf8}"
  278. .format(
  279. utf8=escaped_filename,
  280. )
  281. )
  282. def get_origin(self):
  283. # Handle WebSocket Origin naming convention differences
  284. # The difference between version 8 and 13 is that in 8 the
  285. # client sends a "Sec-Websocket-Origin" header and in 13 it's
  286. # simply "Origin".
  287. if "Origin" in self.request.headers:
  288. origin = self.request.headers.get("Origin")
  289. else:
  290. origin = self.request.headers.get("Sec-Websocket-Origin", None)
  291. return origin
  292. # origin_to_satisfy_tornado is present because tornado requires
  293. # check_origin to take an origin argument, but we don't use it
  294. def check_origin(self, origin_to_satisfy_tornado=""):
  295. """Check Origin for cross-site API requests, including websockets
  296. Copied from WebSocket with changes:
  297. - allow unspecified host/origin (e.g. scripts)
  298. - allow token-authenticated requests
  299. """
  300. if self.allow_origin == '*' or self.skip_check_origin():
  301. return True
  302. host = self.request.headers.get("Host")
  303. origin = self.request.headers.get("Origin")
  304. # If no header is provided, let the request through.
  305. # Origin can be None for:
  306. # - same-origin (IE, Firefox)
  307. # - Cross-site POST form (IE, Firefox)
  308. # - Scripts
  309. # The cross-site POST (XSRF) case is handled by tornado's xsrf_token
  310. if origin is None or host is None:
  311. return True
  312. origin = origin.lower()
  313. origin_host = urlparse(origin).netloc
  314. # OK if origin matches host
  315. if origin_host == host:
  316. return True
  317. # Check CORS headers
  318. if self.allow_origin:
  319. allow = self.allow_origin == origin
  320. elif self.allow_origin_pat:
  321. allow = bool(self.allow_origin_pat.match(origin))
  322. else:
  323. # No CORS headers deny the request
  324. allow = False
  325. if not allow:
  326. self.log.warning("Blocking Cross Origin API request for %s. Origin: %s, Host: %s",
  327. self.request.path, origin, host,
  328. )
  329. return allow
  330. def check_referer(self):
  331. """Check Referer for cross-site requests.
  332. Disables requests to certain endpoints with
  333. external or missing Referer.
  334. If set, allow_origin settings are applied to the Referer
  335. to whitelist specific cross-origin sites.
  336. Used on GET for api endpoints and /files/
  337. to block cross-site inclusion (XSSI).
  338. """
  339. host = self.request.headers.get("Host")
  340. referer = self.request.headers.get("Referer")
  341. if not host:
  342. self.log.warning("Blocking request with no host")
  343. return False
  344. if not referer:
  345. self.log.warning("Blocking request with no referer")
  346. return False
  347. referer_url = urlparse(referer)
  348. referer_host = referer_url.netloc
  349. if referer_host == host:
  350. return True
  351. # apply cross-origin checks to Referer:
  352. origin = "{}://{}".format(referer_url.scheme, referer_url.netloc)
  353. if self.allow_origin:
  354. allow = self.allow_origin == origin
  355. elif self.allow_origin_pat:
  356. allow = bool(self.allow_origin_pat.match(origin))
  357. else:
  358. # No CORS settings, deny the request
  359. allow = False
  360. if not allow:
  361. self.log.warning("Blocking Cross Origin request for %s. Referer: %s, Host: %s",
  362. self.request.path, origin, host,
  363. )
  364. return allow
  365. def check_xsrf_cookie(self):
  366. """Bypass xsrf cookie checks when token-authenticated"""
  367. if self.token_authenticated or self.settings.get('disable_check_xsrf', False):
  368. # Token-authenticated requests do not need additional XSRF-check
  369. # Servers without authentication are vulnerable to XSRF
  370. return
  371. try:
  372. return super(IPythonHandler, self).check_xsrf_cookie()
  373. except web.HTTPError as e:
  374. if self.request.method in {'GET', 'HEAD'}:
  375. # Consider Referer a sufficient cross-origin check for GET requests
  376. if not self.check_referer():
  377. referer = self.request.headers.get('Referer')
  378. if referer:
  379. msg = "Blocking Cross Origin request from {}.".format(referer)
  380. else:
  381. msg = "Blocking request from unknown origin"
  382. raise web.HTTPError(403, msg)
  383. else:
  384. raise
  385. def check_host(self):
  386. """Check the host header if remote access disallowed.
  387. Returns True if the request should continue, False otherwise.
  388. """
  389. if self.settings.get('allow_remote_access', False):
  390. return True
  391. # Remove port (e.g. ':8888') from host
  392. host = re.match(r'^(.*?)(:\d+)?$', self.request.host).group(1)
  393. # Browsers format IPv6 addresses like [::1]; we need to remove the []
  394. if host.startswith('[') and host.endswith(']'):
  395. host = host[1:-1]
  396. if not PY3:
  397. # ip_address only accepts unicode on Python 2
  398. host = host.decode('utf8', 'replace')
  399. try:
  400. addr = ipaddress.ip_address(host)
  401. except ValueError:
  402. # Not an IP address: check against hostnames
  403. allow = host in self.settings.get('local_hostnames', ['localhost'])
  404. else:
  405. allow = addr.is_loopback
  406. if not allow:
  407. self.log.warning(
  408. ("Blocking request with non-local 'Host' %s (%s). "
  409. "If the notebook should be accessible at that name, "
  410. "set NotebookApp.allow_remote_access to disable the check."),
  411. host, self.request.host
  412. )
  413. return allow
  414. def prepare(self):
  415. if not self.check_host():
  416. raise web.HTTPError(403)
  417. return super(IPythonHandler, self).prepare()
  418. #---------------------------------------------------------------
  419. # template rendering
  420. #---------------------------------------------------------------
  421. def get_template(self, name):
  422. """Return the jinja template object for a given name"""
  423. return self.settings['jinja2_env'].get_template(name)
  424. def render_template(self, name, **ns):
  425. ns.update(self.template_namespace)
  426. template = self.get_template(name)
  427. return template.render(**ns)
  428. @property
  429. def template_namespace(self):
  430. return dict(
  431. base_url=self.base_url,
  432. default_url=self.default_url,
  433. ws_url=self.ws_url,
  434. logged_in=self.logged_in,
  435. allow_password_change=self.settings.get('allow_password_change'),
  436. login_available=self.login_available,
  437. token_available=bool(self.token),
  438. static_url=self.static_url,
  439. sys_info=json_sys_info(),
  440. contents_js_source=self.contents_js_source,
  441. version_hash=self.version_hash,
  442. ignore_minified_js=self.ignore_minified_js,
  443. xsrf_form_html=self.xsrf_form_html,
  444. token=self.token,
  445. xsrf_token=self.xsrf_token.decode('utf8'),
  446. nbjs_translations=json.dumps(combine_translations(
  447. self.request.headers.get('Accept-Language', ''))),
  448. **self.jinja_template_vars
  449. )
  450. def get_json_body(self):
  451. """Return the body of the request as JSON data."""
  452. if not self.request.body:
  453. return None
  454. # Do we need to call body.decode('utf-8') here?
  455. body = self.request.body.strip().decode(u'utf-8')
  456. try:
  457. model = json.loads(body)
  458. except Exception:
  459. self.log.debug("Bad JSON: %r", body)
  460. self.log.error("Couldn't parse JSON", exc_info=True)
  461. raise web.HTTPError(400, u'Invalid JSON in body of request')
  462. return model
  463. def write_error(self, status_code, **kwargs):
  464. """render custom error pages"""
  465. exc_info = kwargs.get('exc_info')
  466. message = ''
  467. status_message = responses.get(status_code, 'Unknown HTTP Error')
  468. exception = '(unknown)'
  469. if exc_info:
  470. exception = exc_info[1]
  471. # get the custom message, if defined
  472. try:
  473. message = exception.log_message % exception.args
  474. except Exception:
  475. pass
  476. # construct the custom reason, if defined
  477. reason = getattr(exception, 'reason', '')
  478. if reason:
  479. status_message = reason
  480. # build template namespace
  481. ns = dict(
  482. status_code=status_code,
  483. status_message=status_message,
  484. message=message,
  485. exception=exception,
  486. )
  487. self.set_header('Content-Type', 'text/html')
  488. # render the template
  489. try:
  490. html = self.render_template('%s.html' % status_code, **ns)
  491. except TemplateNotFound:
  492. html = self.render_template('error.html', **ns)
  493. self.write(html)
  494. class APIHandler(IPythonHandler):
  495. """Base class for API handlers"""
  496. def prepare(self):
  497. if not self.check_origin():
  498. raise web.HTTPError(404)
  499. return super(APIHandler, self).prepare()
  500. def write_error(self, status_code, **kwargs):
  501. """APIHandler errors are JSON, not human pages"""
  502. self.set_header('Content-Type', 'application/json')
  503. message = responses.get(status_code, 'Unknown HTTP Error')
  504. reply = {
  505. 'message': message,
  506. }
  507. exc_info = kwargs.get('exc_info')
  508. if exc_info:
  509. e = exc_info[1]
  510. if isinstance(e, HTTPError):
  511. reply['message'] = e.log_message or message
  512. reply['reason'] = e.reason
  513. else:
  514. reply['message'] = 'Unhandled error'
  515. reply['reason'] = None
  516. reply['traceback'] = ''.join(traceback.format_exception(*exc_info))
  517. self.log.warning(reply['message'])
  518. self.finish(json.dumps(reply))
  519. def get_current_user(self):
  520. """Raise 403 on API handlers instead of redirecting to human login page"""
  521. # preserve _user_cache so we don't raise more than once
  522. if hasattr(self, '_user_cache'):
  523. return self._user_cache
  524. self._user_cache = user = super(APIHandler, self).get_current_user()
  525. return user
  526. def get_login_url(self):
  527. # if get_login_url is invoked in an API handler,
  528. # that means @web.authenticated is trying to trigger a redirect.
  529. # instead of redirecting, raise 403 instead.
  530. if not self.current_user:
  531. raise web.HTTPError(403)
  532. return super(APIHandler, self).get_login_url()
  533. @property
  534. def content_security_policy(self):
  535. csp = '; '.join([
  536. super(APIHandler, self).content_security_policy,
  537. "default-src 'none'",
  538. ])
  539. return csp
  540. # set _track_activity = False on API handlers that shouldn't track activity
  541. _track_activity = True
  542. def update_api_activity(self):
  543. """Update last_activity of API requests"""
  544. # record activity of authenticated requests
  545. if self._track_activity and getattr(self, '_user_cache', None):
  546. self.settings['api_last_activity'] = utcnow()
  547. def finish(self, *args, **kwargs):
  548. self.update_api_activity()
  549. self.set_header('Content-Type', 'application/json')
  550. return super(APIHandler, self).finish(*args, **kwargs)
  551. def options(self, *args, **kwargs):
  552. if 'Access-Control-Allow-Headers' in self.settings.get('headers', {}):
  553. self.set_header('Access-Control-Allow-Headers', self.settings['headers']['Access-Control-Allow-Headers'])
  554. else:
  555. self.set_header('Access-Control-Allow-Headers',
  556. 'accept, content-type, authorization, x-xsrftoken')
  557. self.set_header('Access-Control-Allow-Methods',
  558. 'GET, PUT, POST, PATCH, DELETE, OPTIONS')
  559. # if authorization header is requested,
  560. # that means the request is token-authenticated.
  561. # avoid browser-side rejection of the preflight request.
  562. # only allow this exception if allow_origin has not been specified
  563. # and notebook authentication is enabled.
  564. # If the token is not valid, the 'real' request will still be rejected.
  565. requested_headers = self.request.headers.get('Access-Control-Request-Headers', '').split(',')
  566. if requested_headers and any(
  567. h.strip().lower() == 'authorization'
  568. for h in requested_headers
  569. ) and (
  570. # FIXME: it would be even better to check specifically for token-auth,
  571. # but there is currently no API for this.
  572. self.login_available
  573. ) and (
  574. self.allow_origin
  575. or self.allow_origin_pat
  576. or 'Access-Control-Allow-Origin' in self.settings.get('headers', {})
  577. ):
  578. self.set_header('Access-Control-Allow-Origin',
  579. self.request.headers.get('Origin', ''))
  580. class Template404(IPythonHandler):
  581. """Render our 404 template"""
  582. def prepare(self):
  583. raise web.HTTPError(404)
  584. class AuthenticatedFileHandler(IPythonHandler, web.StaticFileHandler):
  585. """static files should only be accessible when logged in"""
  586. @property
  587. def content_security_policy(self):
  588. # In case we're serving HTML/SVG, confine any Javascript to a unique
  589. # origin so it can't interact with the notebook server.
  590. return super(AuthenticatedFileHandler, self).content_security_policy + \
  591. "; sandbox allow-scripts"
  592. @web.authenticated
  593. def head(self, path):
  594. self.check_xsrf_cookie()
  595. return super(AuthenticatedFileHandler, self).head(path)
  596. @web.authenticated
  597. def get(self, path):
  598. self.check_xsrf_cookie()
  599. if os.path.splitext(path)[1] == '.ipynb' or self.get_argument("download", False):
  600. name = path.rsplit('/', 1)[-1]
  601. self.set_attachment_header(name)
  602. return web.StaticFileHandler.get(self, path)
  603. def get_content_type(self):
  604. path = self.absolute_path.strip('/')
  605. if '/' in path:
  606. _, name = path.rsplit('/', 1)
  607. else:
  608. name = path
  609. if name.endswith('.ipynb'):
  610. return 'application/x-ipynb+json'
  611. else:
  612. cur_mime = mimetypes.guess_type(name)[0]
  613. if cur_mime == 'text/plain':
  614. return 'text/plain; charset=UTF-8'
  615. else:
  616. return super(AuthenticatedFileHandler, self).get_content_type()
  617. def set_headers(self):
  618. super(AuthenticatedFileHandler, self).set_headers()
  619. # disable browser caching, rely on 304 replies for savings
  620. if "v" not in self.request.arguments:
  621. self.add_header("Cache-Control", "no-cache")
  622. def compute_etag(self):
  623. return None
  624. def validate_absolute_path(self, root, absolute_path):
  625. """Validate and return the absolute path.
  626. Requires tornado 3.1
  627. Adding to tornado's own handling, forbids the serving of hidden files.
  628. """
  629. abs_path = super(AuthenticatedFileHandler, self).validate_absolute_path(root, absolute_path)
  630. abs_root = os.path.abspath(root)
  631. if is_hidden(abs_path, abs_root) and not self.contents_manager.allow_hidden:
  632. self.log.info("Refusing to serve hidden file, via 404 Error, use flag 'ContentsManager.allow_hidden' to enable")
  633. raise web.HTTPError(404)
  634. return abs_path
  635. def json_errors(method):
  636. """Decorate methods with this to return GitHub style JSON errors.
  637. This should be used on any JSON API on any handler method that can raise HTTPErrors.
  638. This will grab the latest HTTPError exception using sys.exc_info
  639. and then:
  640. 1. Set the HTTP status code based on the HTTPError
  641. 2. Create and return a JSON body with a message field describing
  642. the error in a human readable form.
  643. """
  644. warnings.warn('@json_errors is deprecated in notebook 5.2.0. Subclass APIHandler instead.',
  645. DeprecationWarning,
  646. stacklevel=2,
  647. )
  648. @functools.wraps(method)
  649. def wrapper(self, *args, **kwargs):
  650. self.write_error = types.MethodType(APIHandler.write_error, self)
  651. return method(self, *args, **kwargs)
  652. return wrapper
  653. #-----------------------------------------------------------------------------
  654. # File handler
  655. #-----------------------------------------------------------------------------
  656. # to minimize subclass changes:
  657. HTTPError = web.HTTPError
  658. class FileFindHandler(IPythonHandler, web.StaticFileHandler):
  659. """subclass of StaticFileHandler for serving files from a search path"""
  660. # cache search results, don't search for files more than once
  661. _static_paths = {}
  662. def set_headers(self):
  663. super(FileFindHandler, self).set_headers()
  664. # disable browser caching, rely on 304 replies for savings
  665. if "v" not in self.request.arguments or \
  666. any(self.request.path.startswith(path) for path in self.no_cache_paths):
  667. self.set_header("Cache-Control", "no-cache")
  668. def initialize(self, path, default_filename=None, no_cache_paths=None):
  669. self.no_cache_paths = no_cache_paths or []
  670. if isinstance(path, string_types):
  671. path = [path]
  672. self.root = tuple(
  673. os.path.abspath(os.path.expanduser(p)) + os.sep for p in path
  674. )
  675. self.default_filename = default_filename
  676. def compute_etag(self):
  677. return None
  678. @classmethod
  679. def get_absolute_path(cls, roots, path):
  680. """locate a file to serve on our static file search path"""
  681. with cls._lock:
  682. if path in cls._static_paths:
  683. return cls._static_paths[path]
  684. try:
  685. abspath = os.path.abspath(filefind(path, roots))
  686. except IOError:
  687. # IOError means not found
  688. return ''
  689. cls._static_paths[path] = abspath
  690. log().debug("Path %s served from %s"%(path, abspath))
  691. return abspath
  692. def validate_absolute_path(self, root, absolute_path):
  693. """check if the file should be served (raises 404, 403, etc.)"""
  694. if absolute_path == '':
  695. raise web.HTTPError(404)
  696. for root in self.root:
  697. if (absolute_path + os.sep).startswith(root):
  698. break
  699. return super(FileFindHandler, self).validate_absolute_path(root, absolute_path)
  700. class APIVersionHandler(APIHandler):
  701. def get(self):
  702. # not authenticated, so give as few info as possible
  703. self.finish(json.dumps({"version":notebook.__version__}))
  704. class TrailingSlashHandler(web.RequestHandler):
  705. """Simple redirect handler that strips trailing slashes
  706. This should be the first, highest priority handler.
  707. """
  708. def get(self):
  709. self.redirect(self.request.uri.rstrip('/'))
  710. post = put = get
  711. class FilesRedirectHandler(IPythonHandler):
  712. """Handler for redirecting relative URLs to the /files/ handler"""
  713. @staticmethod
  714. def redirect_to_files(self, path):
  715. """make redirect logic a reusable static method
  716. so it can be called from other handlers.
  717. """
  718. cm = self.contents_manager
  719. if cm.dir_exists(path):
  720. # it's a *directory*, redirect to /tree
  721. url = url_path_join(self.base_url, 'tree', url_escape(path))
  722. else:
  723. orig_path = path
  724. # otherwise, redirect to /files
  725. parts = path.split('/')
  726. if not cm.file_exists(path=path) and 'files' in parts:
  727. # redirect without files/ iff it would 404
  728. # this preserves pre-2.0-style 'files/' links
  729. self.log.warning("Deprecated files/ URL: %s", orig_path)
  730. parts.remove('files')
  731. path = '/'.join(parts)
  732. if not cm.file_exists(path=path):
  733. raise web.HTTPError(404)
  734. url = url_path_join(self.base_url, 'files', url_escape(path))
  735. self.log.debug("Redirecting %s to %s", self.request.path, url)
  736. self.redirect(url)
  737. def get(self, path=''):
  738. return self.redirect_to_files(self, path)
  739. class RedirectWithParams(web.RequestHandler):
  740. """Sam as web.RedirectHandler, but preserves URL parameters"""
  741. def initialize(self, url, permanent=True):
  742. self._url = url
  743. self._permanent = permanent
  744. def get(self):
  745. sep = '&' if '?' in self._url else '?'
  746. url = sep.join([self._url, self.request.query])
  747. self.redirect(url, permanent=self._permanent)
  748. class PrometheusMetricsHandler(IPythonHandler):
  749. """
  750. Return prometheus metrics for this notebook server
  751. """
  752. @web.authenticated
  753. def get(self):
  754. self.set_header('Content-Type', prometheus_client.CONTENT_TYPE_LATEST)
  755. self.write(prometheus_client.generate_latest(prometheus_client.REGISTRY))
  756. #-----------------------------------------------------------------------------
  757. # URL pattern fragments for re-use
  758. #-----------------------------------------------------------------------------
  759. # path matches any number of `/foo[/bar...]` or just `/` or ''
  760. path_regex = r"(?P<path>(?:(?:/[^/]+)+|/?))"
  761. #-----------------------------------------------------------------------------
  762. # URL to handler mappings
  763. #-----------------------------------------------------------------------------
  764. default_handlers = [
  765. (r".*/", TrailingSlashHandler),
  766. (r"api", APIVersionHandler),
  767. (r'/(robots\.txt|favicon\.ico)', web.StaticFileHandler),
  768. (r'/metrics', PrometheusMetricsHandler)
  769. ]