🐛 Strip interface tags from addresses on local check
Closes #3143 and Sentry SERVER-11B
This commit is contained in:
1 parent
07423712a6
commit
b03d43156d
2 files changed
+40
-8
No files matched your search
@@ -46,9 +46,8 @@ else:
|
||||
|
||||
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():
|
||||
@@ -71,10 +70,7 @@ def is_lan_address(address, additional_private=None):
|
||||
# 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():
|
||||
@@ -103,3 +99,17 @@ def is_lan_address(address, additional_private=None):
|
||||
# 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
|
||||
+23
-1
@@ -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)
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user