From 24cd303ca97d750de55ca17e7bb9a8de5554d6d9 Mon Sep 17 00:00:00 2001 From: Marian Beermann Date: Wed, 18 May 2016 17:37:47 +0200 Subject: [PATCH] When probing key files, do binary reads --- borg/key.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/borg/key.py b/borg/key.py index d52c1fd2d..be79dfc14 100644 --- a/borg/key.py +++ b/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())