Browse Source

Merge pull request #1058 from enkore/issue/1056

When probing key files, do binary reads
TW 9 years ago
parent
commit
5c6cfc6329
1 changed files with 6 additions and 4 deletions
  1. 6 4
      borg/key.py

+ 6 - 4
borg/key.py

@@ -394,13 +394,15 @@ class KeyfileKey(KeyfileKeyBase):
     FILE_ID = 'BORG_KEY'
 
     def find_key(self):
-        id = hexlify(self.repository.id).decode('ascii')
+        file_id = self.FILE_ID.encode()
+        first_line = file_id + b' ' + hexlify(self.repository.id)
         keys_dir = get_keys_dir()
         for name in os.listdir(keys_dir):
             filename = os.path.join(keys_dir, name)
-            with open(filename, 'r') as fd:
-                line = fd.readline().strip()
-                if line.startswith(self.FILE_ID) and line[len(self.FILE_ID) + 1:] == id:
+            # we do the magic / id check in binary mode to avoid stumbling over
+            # decoding errors if somebody has binary files in the keys dir for some reason.
+            with open(filename, 'rb') as fd:
+                if fd.read(len(first_line)) == first_line:
                     return filename
         raise KeyfileNotFoundError(self.repository._location.canonical_path(), get_keys_dir())