[tor-commits] [stem/master] Recognize additional representations of 'localhost' in get_ports()

atagar at torproject.org atagar at torproject.org
Tue Oct 31 17:08:09 UTC 2017


commit 66a9b77e165dfe47e3b32a84080b59f47cc85dc1
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Oct 31 10:01:35 2017 -0700

    Recognize additional representations of 'localhost' in get_ports()
    
    Tim and Sebastian pointed out that we're still not doing correct localhost
    detection...
    
      https://trac.torproject.org/projects/tor/ticket/24085
---
 stem/control.py                 | 14 +++++++++++++-
 test/unit/control/controller.py | 10 +++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index 19619190..7c67cd0d 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -1323,7 +1323,19 @@ class Controller(BaseController):
       and no default was provided
     """
 
-    return [port for (addr, port) in self.get_listeners(listener_type) if addr in ('127.0.0.1', '0.0.0.0')]
+    def is_localhost(address):
+      if stem.util.connection.is_valid_ipv4_address(address):
+        return address == '0.0.0.0' or address.startswith('127.')
+      elif stem.util.connection.is_valid_ipv6_address(address):
+        return stem.util.connection.expand_ipv6_address(address) in (
+          '0000:0000:0000:0000:0000:0000:0000:0000',
+          '0000:0000:0000:0000:0000:0000:0000:0001',
+        )
+      else:
+        log.info("Request for %s ports got an address that's neither IPv4 or IPv6: %s" % (listener_type, address))
+        return False
+
+    return [port for (addr, port) in self.get_listeners(listener_type) if is_localhost(addr)]
 
   @with_default()
   def get_listeners(self, listener_type, default = UNDEFINED):
diff --git a/test/unit/control/controller.py b/test/unit/control/controller.py
index a9a7e73d..929adcfc 100644
--- a/test/unit/control/controller.py
+++ b/test/unit/control/controller.py
@@ -227,7 +227,7 @@ class TestControl(unittest.TestCase):
     self.assertEqual([], self.controller.get_ports(Listener.CONTROL))
     self.controller.clear_cache()
 
-    # Exercise via the GETINFO option.
+    # exercise via the GETINFO option
 
     get_info_mock.side_effect = None
     get_info_mock.return_value = '"127.0.0.1:1112" "127.0.0.1:1114"'
@@ -240,6 +240,14 @@ class TestControl(unittest.TestCase):
     self.assertEqual([1112, 1114], self.controller.get_ports(Listener.CONTROL))
     self.controller.clear_cache()
 
+    # with all localhost addresses, including a couple that aren't
+
+    get_info_mock.side_effect = None
+    get_info_mock.return_value = '"27.4.4.1:1113" "127.0.0.5:1114" "0.0.0.0:1115" "[::]:1116" "[::1]:1117" "[10::]:1118"'
+
+    self.assertEqual([1114, 1115, 1116, 1117], self.controller.get_ports(Listener.OR))
+    self.controller.clear_cache()
+
     # IPv6 address
 
     get_info_mock.return_value = '"0.0.0.0:9001" "[fe80:0000:0000:0000:0202:b3ff:fe1e:8329]:9001"'



More information about the tor-commits mailing list