Skip to content

Commit fa3abf2

Browse files
committed
Allow remote access by default when we're listening on external addresses
1 parent 7f1bba6 commit fa3abf2

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

notebook/notebookapp.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import hmac
1616
import importlib
1717
import io
18+
import ipaddress
1819
import json
1920
import logging
2021
import mimetypes
@@ -833,7 +834,7 @@ def _token_changed(self, change):
833834
"""
834835
)
835836

836-
allow_remote_access = Bool(False, config=True,
837+
allow_remote_access = Bool(config=True,
837838
help="""Allow requests where the Host header doesn't point to a local server
838839
839840
By default, requests get a 403 forbidden response if the 'Host' header
@@ -848,6 +849,21 @@ def _token_changed(self, change):
848849
along with hostnames configured in local_hostnames.
849850
""")
850851

852+
@default('allow_remote_access')
853+
def _default_allow_remote(self):
854+
"""Disallow remote access if we're listening only on loopback addresses"""
855+
try:
856+
addr = ipaddress.ip_address(self.ip)
857+
except ValueError:
858+
# Address is a hostname
859+
for info in socket.getaddrinfo(self.ip, self.port, type=socket.SOCK_STREAM):
860+
addr = ipaddress.ip_address(info[4][0])
861+
if not addr.is_loopback:
862+
return True
863+
return False
864+
else:
865+
return not addr.is_loopback
866+
851867
local_hostnames = List(Unicode(), ['localhost'], config=True,
852868
help="""Hostnames to allow as local when allow_remote_access is False.
853869

0 commit comments

Comments
 (0)