[tor-commits] [bridgedb/master] Check that a networkstatus nickname matches tor-spec.

isis at torproject.org isis at torproject.org
Sun Jan 12 06:06:32 UTC 2014


commit 1c96f4bbf504af86a4254078f0c0cbf01b7124d5
Author: Isis Lovecruft <isis at torproject.org>
Date:   Sat Nov 16 00:34:14 2013 +0000

    Check that a networkstatus nickname matches tor-spec.
---
 lib/bridgedb/parse/networkstatus.py |   26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/lib/bridgedb/parse/networkstatus.py b/lib/bridgedb/parse/networkstatus.py
index 9b1bb01..6e2c0d6 100644
--- a/lib/bridgedb/parse/networkstatus.py
+++ b/lib/bridgedb/parse/networkstatus.py
@@ -41,13 +41,32 @@ class InvalidNetworkstatusRouterIdentity(ValueError):
 class InvalidNetworkstatusDescriptorDigest(ValueError):
     """Descriptor digest of a networkstatus document 'r'-line is invalid."""
 
+class InvalidRouterNickname(ValueError):
+    """Router nickname doesn't follow tor-spec."""
+
+
+ALPHANUMERIC = string.letters + string.digits
+
 
 def isValidRouterNickname(nickname):
     """Determine if a router's given nickname meets the specification.
 
     :param string nickname: An OR's nickname.
     """
-    
+    try:
+        if not (1 <= len(nickname) <= 19):
+            raise InvalidRouterNickname(
+                "Nicknames must be between 1 and 19 characters: %r" % nickname)
+        for letter in nickname:
+            if not letter in ALPHANUMERIC:
+                raise InvalidRouterNickname(
+                    "Nicknames must only use [A-Za-z0-9]: %r" % nickname)
+    except Exception as error:
+        logging.exception(error)
+    else:
+        return True
+
+    raise InvalidRouterNickname
 
 def parseRLine(line):
     """Parse an 'r'-line from a networkstatus document.
@@ -90,6 +109,8 @@ def parseRLine(line):
     try:
         nickname, ID = fields[:2]
 
+        isValidRouterNickname(nickname)
+
         if ID.endswith('='):
             raise InvalidNetworkstatusRouterIdentity(
                 "Skipping networkstatus parsing for router with nickname %r:"\
@@ -109,6 +130,9 @@ def parseRLine(line):
 
     except IndexError as error:
         logging.error(error.message)
+    except InvalidRouterNickname as error:
+        logging.error(error.message)
+        nickname = None
     except InvalidNetworkstatusRouterIdentity as error:
         logging.error(error.message)
         ID = None





More information about the tor-commits mailing list