[tor-commits] [stem/master] Fallback to GETCONF for getting exit policies

atagar at torproject.org atagar at torproject.org
Wed Apr 18 18:17:24 UTC 2018


commit f61e91314c81949bb6fb199f14afa0f56d4b4557
Author: Damian Johnson <atagar at torproject.org>
Date:   Wed Apr 18 10:17:08 2018 -0700

    Fallback to GETCONF for getting exit policies
    
    As pointed out by dmr on #25423 when tor is unable to determine our address
    'GETINFO exit-policy/full' fails. I was able to easily repro this by running
    our integ tests when disconnected...
    
      ======================================================================
      ERROR: test_get_exit_policy
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/require.py", line 58, in wrapped
          return func(self, *args, **kwargs)
        File "/home/atagar/Desktop/stem/test/integ/control/controller.py", line 278, in test_get_exit_policy
          self.assertEqual(ExitPolicy('reject *:*'), controller.get_exit_policy())
        File "/home/atagar/Desktop/stem/stem/control.py", line 481, in wrapped
          return func(self, *args, **kwargs)
        File "/home/atagar/Desktop/stem/stem/control.py", line 1289, in get_exit_policy
          policy = stem.exit_policy.ExitPolicy(*self.get_info('exit-policy/full').splitlines())
        File "/home/atagar/Desktop/stem/stem/control.py", line 481, in wrapped
          return func(self, *args, **kwargs)
        File "/home/atagar/Desktop/stem/stem/control.py", line 1186, in get_info
          stem.response.convert('GETINFO', response)
        File "/home/atagar/Desktop/stem/stem/response/__init__.py", line 124, in convert
          message._parse_message(**kwargs)
        File "/home/atagar/Desktop/stem/stem/response/getinfo.py", line 40, in _parse_message
          raise stem.ProtocolError("GETINFO response didn't have an OK status:\n%s" % self)
      ProtocolError: GETINFO response didn't have an OK status:
      router_get_my_routerinfo returned NULL
    
    I'll shoot tor a ticket in a bit, but in the meantime when this happens
    we'll now fall back to our prior method of getting the policy. One small
    fix for the legacy method is that we now account for 'ExitRelay 0' in our
    the torrc (otherwise our tests wouldn't pass).
---
 stem/control.py | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/stem/control.py b/stem/control.py
index a91e9588..9229f07c 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -1286,7 +1286,33 @@ class Controller(BaseController):
     policy = self._get_cache('exit_policy')
 
     if not policy:
-      policy = stem.exit_policy.ExitPolicy(*self.get_info('exit-policy/full').splitlines())
+      try:
+        policy = stem.exit_policy.ExitPolicy(*self.get_info('exit-policy/full').splitlines())
+      except stem.ProtocolError:
+        # When tor is unable to determine our address 'GETINFO
+        # exit-policy/full' fails with...
+        #
+        #   ProtocolError: GETINFO response didn't have an OK status:
+        #     router_get_my_routerinfo returned NULL
+        #
+        # Failing back to the legacy method we used for getting our exit
+        # policy.
+
+        rules = []
+
+        if self.get_conf('ExitRelay') == '0':
+          rules.append('reject *:*')
+
+        if self.get_conf('ExitPolicyRejectPrivate') == '1':
+          rules.append('reject private:*')
+
+        for policy_line in self.get_conf('ExitPolicy', multiple = True):
+          rules += policy_line.split(',')
+
+        rules += self.get_info('exit-policy/default').split(',')
+
+        policy = stem.exit_policy.get_config_policy(rules, self.get_info('address', None))
+
       self._set_cache({'exit_policy': policy})
 
     return policy



More information about the tor-commits mailing list