Skip to content

Commit

Permalink
馃悰 Strip interface tags from addresses on local check
Browse files Browse the repository at this point in the history
Closes #3143 and Sentry SERVER-11B
  • Loading branch information
foosel committed May 15, 2019
1 parent 0742371 commit b03d431
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/octoprint/util/net.py
Expand Up @@ -46,9 +46,8 @@ def f():

def is_lan_address(address, additional_private=None):
try:
if address.lower().startswith("::ffff:") and "." in address:
# ipv6 mapped ipv4 address, unmap
address = address[len("::ffff:"):]
address = unmap_v4_as_v6(address)
address = strip_interface_tag(address)

ip = netaddr.IPAddress(address)
if ip.is_private() or ip.is_loopback():
Expand All @@ -71,10 +70,7 @@ def to_ipnetwork(address):
# v6 notation in netifaces output, e.g. "ffff:ffff:ffff:ffff::/64"
_, prefix = prefix.split("/")

addr = address["addr"]
if "%" in addr:
# interface comment in netifaces output, e.g. "fe80::457f:bbee:d579:1063%wlan0"
addr = addr[:addr.find("%")]
addr = strip_interface_tag(address["addr"])
return netaddr.IPNetwork("{}/{}".format(addr, prefix))

for interface in netifaces.interfaces():
Expand Down Expand Up @@ -103,3 +99,17 @@ def to_ipnetwork(address):
# we are extra careful here since an unhandled exception in this method will effectively nuke the whole UI
logging.getLogger(__name__).exception("Error while trying to determine whether {} is a local address".format(address))
return True


def strip_interface_tag(address):
if "%" in address:
# interface comment, e.g. "fe80::457f:bbee:d579:1063%wlan0"
address = address[:address.find("%")]
return address


def unmap_v4_as_v6(address):
if address.lower().startswith("::ffff:") and "." in address:
# ipv6 mapped ipv4 address, unmap
address = address[len("::ffff:"):]
return address
24 changes: 23 additions & 1 deletion tests/util/test_net.py
Expand Up @@ -43,7 +43,8 @@ class UtilNetTest(unittest.TestCase):
("11.1.2.3", [], False),
("11.1.2.3", ["11/8"], True),
("12.1.1.123", [], True),
("2a01:4f8:1c0c:6958::1:23", [], True)
("2a01:4f8:1c0c:6958::1:23", [], True),
("fe80::89f3:31bb:ced0:2093%wlan0", [], True)
)
@ddt.unpack
@mock.patch("netifaces.interfaces", side_effect=patched_interfaces)
Expand All @@ -52,3 +53,24 @@ class UtilNetTest(unittest.TestCase):
def test_is_lan_address(self, input_address, input_additional, expected, nifa, nifs):
actual = octoprint.util.net.is_lan_address(input_address, additional_private=input_additional)
self.assertEqual(expected, actual)

@ddt.data(
("fe80::89f3:31bb:ced0:2093%wlan0", "fe80::89f3:31bb:ced0:2093"),
("2a01:4f8:1c0c:6958::1:23", "2a01:4f8:1c0c:6958::1:23"),
("10.1.2.3", "10.1.2.3")
)
@ddt.unpack
def test_strip_interface_tag(self, address, expected):
actual = octoprint.util.net.strip_interface_tag(address)
self.assertEqual(expected, actual)

@ddt.data(
("::ffff:192.168.1.1", "192.168.1.1"),
("::ffff:2a01:4f8", "::ffff:2a01:4f8"),
("2a01:4f8:1c0c:6958::1:23", "2a01:4f8:1c0c:6958::1:23"),
("11.1.2.3", "11.1.2.3")
)
@ddt.unpack
def test_unmap_v4_in_v6(self, address, expected):
actual = octoprint.util.net.unmap_v4_as_v6(address)
self.assertEqual(expected, actual)

0 comments on commit b03d431

Please sign in to comment.