Commits

Jason R. Coombs committed 454aa98

Fixed issue with low bytes not being escaped consistently

  • Participants
  • Parent commits 7dede23

Comments (0)

Files changed (2)

keyring/tests/test_util.py

 
     def test_low_byte(self):
         """
-        The current encoding allows low bytes (less than hex 16) to encode
-        as two bytes. For example '\n' (hex A) will encode as '_A', which
+        Ensure that encoding low bytes (ordinal less than hex F) encode as
+        as three bytes to avoid ambiguity. For example '\n' (hex A) should
+        encode as '_0A' and not '_A', the latter of which
         isn't matched by the inverse operation.
         """
         self.check_escape_unescape('\n')
-
-    def test_ambiguous_string(self):
-        """
-        The current encoding encodes each non-alphanumeric byte to _XX where
-        XX is the hex code for that byte. However, it doesn't encode dual-
-        digits, so '\x00' encodes to '_0'. Thus, if one tries to escape the
-        string '\x000' (the null byte followed by the number 0), it will be
-        encoded to '_00', which decodes to '\x00'.
-        """
         self.check_escape_unescape('\x000')
 
 def test_suite():

keyring/util/escape.py

     or getattr(string, 'ascii_letters') # Python 3
 ) + string.digits
 
-ESCAPE_FMT = "_%X"
+ESCAPE_FMT = "_%02X"
 
 def _escape_char(c):
     "Single char escape. Return the char, escaped if not already legal"
     """
     re_esc = re.compile(
         # the pattern must be bytes to operate on bytes
-        ESCAPE_FMT.replace('%X', '(?P<code>[0-9A-F]{2})').encode('ascii')
+        ESCAPE_FMT.replace('%02X', '(?P<code>[0-9A-F]{2})').encode('ascii')
     )
     return re_esc.sub(_unescape_code, value.encode('ascii')).decode('utf-8')