Jason R. Coombs avatar Jason R. Coombs committed 454aa98

Fixed issue with low bytes not being escaped consistently

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')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.