fix(encryption): don't be strict on enc format when no enc headers are missing.

This fixes problem with migration of OLD unencrypted DBs into a encryption format
This commit is contained in:
RhodeCode Admin 2024-05-06 14:54:21 +02:00
parent a970700222
commit 960d6c5218
2 changed files with 8 additions and 3 deletions

View file

@ -43,6 +43,7 @@ class InvalidDecryptedValue(str):
content = f'<{cls.__name__}({content[:16]}...)>'
return str.__new__(cls, content)
KEY_FORMAT = b'enc$aes_hmac${1}'

View file

@ -29,12 +29,16 @@ class Encryptor(object):
@classmethod
def detect_enc_algo(cls, enc_data: bytes):
parts = enc_data.split(b'$', 3)
if len(parts) != 3:
raise ValueError(f'Encrypted Data has invalid format, expected {cls.key_format}, got {parts}')
if b'enc$aes_hmac$' in enc_data:
# we expect this data is encrypted, so validate the header
if len(parts) != 3:
raise ValueError(f'Encrypted Data has invalid format, expected {cls.key_format}, got `{parts}`')
return 'aes'
elif b'enc2$salt' in enc_data:
# we expect this data is encrypted, so validate the header
if len(parts) != 3:
raise ValueError(f'Encrypted Data has invalid format, expected {cls.key_format}, got `{parts}`')
return 'fernet'
return None
@ -65,7 +69,7 @@ class Encryptor(object):
def _get_parts(self, enc_data):
parts = enc_data.split(b'$', 3)
if len(parts) != 3:
raise ValueError(f'Encrypted Data has invalid format, expected {self.key_format}, got {parts}')
raise ValueError(f'Encrypted Data has invalid format, expected {self.key_format}, got `{parts}`')
prefix, salt, enc_data = parts
try: