[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Python3 unicode error with AuthorizedClient



commit 444182f50e99fc27194f7074abc43590b80a93b1
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Dec 4 12:48:03 2019 -0800

    Python3 unicode error with AuthorizedClient
    
    Great catch from asn that with python3 our AuthorizedClient construction embeds
    extra b'', malforming descriptor output...
    
      https://trac.torproject.org/projects/tor/ticket/31823#comment:19
    
    Fixing this is a general fashion such that AuthorizedClient both normalizes its
    field and can be constructed with unsupplied values randomized (so, for
    example, you can create a AuthorizedClient with only an iv).
---
 stem/descriptor/hidden_service.py         | 25 +++++++++++++++++++------
 test/unit/descriptor/hidden_service_v3.py |  4 ++--
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/stem/descriptor/hidden_service.py b/stem/descriptor/hidden_service.py
index 13fae6ba..249822f1 100644
--- a/stem/descriptor/hidden_service.py
+++ b/stem/descriptor/hidden_service.py
@@ -412,7 +412,7 @@ class IntroductionPointV3(collections.namedtuple('IntroductionPointV3', ['link_s
     return not self == other
 
 
-class AuthorizedClient(collections.namedtuple('AuthorizedClient', ['id', 'iv', 'cookie'])):
+class AuthorizedClient(object):
   """
   Client authorized to use a v3 hidden service.
 
@@ -423,6 +423,23 @@ class AuthorizedClient(collections.namedtuple('AuthorizedClient', ['id', 'iv', '
   :var str cookie: base64 encoded authentication cookie
   """
 
+  def __init__(self, id = None, iv = None, cookie = None):
+    self.id = stem.util.str_tools._to_unicode(id if id else base64.b64encode(os.urandom(8)).rstrip(b'='))
+    self.iv = stem.util.str_tools._to_unicode(iv if iv else base64.b64encode(os.urandom(16)).rstrip(b'='))
+    self.cookie = stem.util.str_tools._to_unicode(cookie if cookie else base64.b64encode(os.urandom(16)).rstrip(b'='))
+
+  def __str__(self):
+    return '%s %s %s' % (self.id, self.iv, self.cookie)
+
+  def __hash__(self):
+    return stem.util._hash_attr(self, 'id', 'iv', 'cookie', cache = True)
+
+  def __eq__(self, other):
+    return hash(self) == hash(other) if isinstance(other, AuthorizedClient) else False
+
+  def __ne__(self, other):
+    return not self == other
+
 
 def _parse_file(descriptor_file, desc_type = None, validate = False, **kwargs):
   """
@@ -1228,11 +1245,7 @@ class OuterLayer(Descriptor):
         pass  # caller is providing raw auth-client lines through the attr
       else:
         for i in range(16):
-          client_id = base64.b64encode(os.urandom(8)).rstrip(b'=')
-          iv = base64.b64encode(os.urandom(16)).rstrip(b'=')
-          cookie = base64.b64encode(os.urandom(16)).rstrip(b'=')
-
-          authorized_clients.append(AuthorizedClient(client_id, iv, cookie))
+          authorized_clients.append(AuthorizedClient())
 
     return _descriptor_content(attr, exclude, [
       ('desc-auth-type', 'x25519'),
diff --git a/test/unit/descriptor/hidden_service_v3.py b/test/unit/descriptor/hidden_service_v3.py
index 180493c1..33507314 100644
--- a/test/unit/descriptor/hidden_service_v3.py
+++ b/test/unit/descriptor/hidden_service_v3.py
@@ -383,8 +383,8 @@ class TestHiddenServiceDescriptorV3(unittest.TestCase):
     self.assertEqual('-----BEGIN MESSAGE-----\nmalformed block\n-----END MESSAGE-----', desc.encrypted)
 
     self.assertEqual({
-      '1D8VBAh9hdM': AuthorizedClient(id = '1D8VBAh9hdM', iv = '6K/uO3sRqBp6URrKC7GB6Q', cookie = 'ElwRj5+6SN9kb8bRhiiQvA'),
-      'JNil86N07AA': AuthorizedClient(id = 'JNil86N07AA', iv = 'epkaL79NtajmgME/egi8oA', cookie = 'qosYH4rXisxda3X7p9b6fw'),
+      '1D8VBAh9hdM': AuthorizedClient(id = b'1D8VBAh9hdM', iv = b'6K/uO3sRqBp6URrKC7GB6Q', cookie = b'ElwRj5+6SN9kb8bRhiiQvA'),
+      'JNil86N07AA': AuthorizedClient(id = b'JNil86N07AA', iv = b'epkaL79NtajmgME/egi8oA', cookie = b'qosYH4rXisxda3X7p9b6fw'),
     }, desc.clients)
 
     self.assertEqual(EXPECTED_OUTER_LAYER, str(desc))

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits