Skip to content

Commit

Permalink
Merge pull request #8051 from ThomasWaldmann/corrupted-key-errmsg-1.4
Browse files Browse the repository at this point in the history
better error msg for corrupted key data, fixes #8016
  • Loading branch information
ThomasWaldmann committed Jan 18, 2024
2 parents 06f6136 + dbbccf9 commit 319441e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/internals/frontends.rst
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ Errors
Failed to encode filename "{}" into file system encoding "{}". Consider configuring the LANG environment variable.

KeyfileInvalidError rc: 40 traceback: no
Invalid key file for repository {} found in {}.
Invalid key data for repository {} found in {}.
KeyfileMismatchError rc: 41 traceback: no
Mismatch between repository {} and key file {}.
KeyfileNotFoundError rc: 42 traceback: no
Expand Down
16 changes: 11 additions & 5 deletions src/borg/crypto/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class KeyfileNotFoundError(Error):


class KeyfileInvalidError(Error):
"""Invalid key file for repository {} found in {}."""
"""Invalid key data for repository {} found in {}."""
exit_mcode = 40


Expand Down Expand Up @@ -689,8 +689,14 @@ def load(self, target, passphrase):
raise NotImplementedError

def _load(self, key_data, passphrase):
cdata = binascii.a2b_base64(key_data)
data = self.decrypt_key_file(cdata, passphrase)
try:
key = binascii.a2b_base64(key_data)
except (ValueError, binascii.Error):
raise KeyfileInvalidError(self.repository._location.canonical_path(), "(repokey)") from None
if len(key) < 20:
# this is in no way a precise check, usually we have about 400b key data.
raise KeyfileInvalidError(self.repository._location.canonical_path(), "(repokey)")
data = self.decrypt_key_file(key, passphrase)
if data:
data = msgpack.unpackb(data)
key = Key(internal_dict=data)
Expand Down Expand Up @@ -805,9 +811,9 @@ def sanity_check(self, filename, id):
key_b64 = ''.join(lines[1:])
try:
key = binascii.a2b_base64(key_b64)
except binascii.Error:
except (ValueError, binascii.Error):
logger.warning(f"borg key sanity check: key line 2+ does not look like base64. [{filename}]")
raise KeyfileInvalidError(self.repository._location.canonical_path(), filename)
raise KeyfileInvalidError(self.repository._location.canonical_path(), filename) from None
if len(key) < 20:
# this is in no way a precise check, usually we have about 400b key data.
logger.warning(f"borg key sanity check: binary encrypted key data from key line 2+ suspiciously short."
Expand Down

0 comments on commit 319441e

Please sign in to comment.