Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check 'Host' header for local connections #3714

Merged
merged 5 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions notebook/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import datetime
import functools
import ipaddress
import json
import mimetypes
import os
Expand Down Expand Up @@ -411,6 +412,43 @@ def check_xsrf_cookie(self):
return
return super(IPythonHandler, self).check_xsrf_cookie()

def check_host(self):
"""Check the host header if remote access disallowed.

Returns True if the request should continue, False otherwise.
"""
if self.settings.get('allow_remote_access', False):
return True

# Remove port (e.g. ':8888') from host
host = re.match(r'^(.*?)(:\d+)?$', self.request.host).group(1)

# Browsers format IPv6 addresses like [::1]; we need to remove the []
if host.startswith('[') and host.endswith(']'):
host = host[1:-1]

try:
addr = ipaddress.ip_address(host)
except ValueError:
# Not an IP address: check against hostnames
allow = host in self.settings.get('local_hostnames', [])
else:
allow = addr.is_loopback

if not allow:
self.log.warning(
("Blocking request with non-local 'Host' %s (%s). "
"If the notebook should be accessible at that name, "
"set NotebookApp.allow_remote_access to disable the check."),
host, self.request.host
)
return allow

def prepare(self):
if not self.check_host():
raise web.HTTPError(403)
return super(IPythonHandler, self).prepare()

#---------------------------------------------------------------
# template rendering
#---------------------------------------------------------------
Expand Down
43 changes: 43 additions & 0 deletions notebook/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import hmac
import importlib
import io
import ipaddress
import json
import logging
import mimetypes
Expand Down Expand Up @@ -252,6 +253,8 @@ def init_settings(self, jupyter_app, kernel_manager, contents_manager,
password=jupyter_app.password,
xsrf_cookies=True,
disable_check_xsrf=jupyter_app.disable_check_xsrf,
allow_remote_access=jupyter_app.allow_remote_access,
local_hostnames=jupyter_app.local_hostnames,

# managers
kernel_manager=kernel_manager,
Expand Down Expand Up @@ -831,6 +834,46 @@ def _token_changed(self, change):
"""
)

allow_remote_access = Bool(config=True,
help="""Allow requests where the Host header doesn't point to a local server

By default, requests get a 403 forbidden response if the 'Host' header
shows that the browser thinks it's on a non-local domain.
Setting this option to True disables this check.

This protects against 'DNS rebinding' attacks, where a remote web server
serves you a page and then changes its DNS to send later requests to a
local IP, bypassing same-origin checks.

Local IP addresses (such as 127.0.0.1 and ::1) are allowed as local,
along with hostnames configured in local_hostnames.
""")

@default('allow_remote_access')
def _default_allow_remote(self):
"""Disallow remote access if we're listening only on loopback addresses"""
try:
addr = ipaddress.ip_address(self.ip)
except ValueError:
# Address is a hostname
for info in socket.getaddrinfo(self.ip, self.port, 0, socket.SOCK_STREAM):
addr = info[4][0]
if not py3compat.PY3:
addr = addr.decode('ascii')
if not ipaddress.ip_address(addr).is_loopback:
return True
return False
else:
return not addr.is_loopback

local_hostnames = List(Unicode(), ['localhost'], config=True,
help="""Hostnames to allow as local when allow_remote_access is False.

Local IP addresses (such as 127.0.0.1 and ::1) are automatically accepted
as local as well.
"""
)

open_browser = Bool(True, config=True,
help="""Whether to open in a browser after starting.
The specific browser used is platform dependent and
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
'prometheus_client'
],
extras_require = {
':python_version == "2.7"': ['ipaddress'],
'test:python_version == "2.7"': ['mock'],
'test': ['nose', 'coverage', 'requests', 'nose_warnings_filters',
'nbval', 'nose-exclude'],
Expand Down