1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-06 11:40:31 +00:00

When probing key files, do binary reads

This commit is contained in:
Marian Beermann 2016-05-18 17:37:47 +02:00
parent eaf0f62022
commit 24cd303ca9
No known key found for this signature in database
GPG key ID: 9B8450B91D1362C1

View file

@ -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())