diff --git a/web/backend/crypto_storage.py b/web/backend/crypto_storage.py index 9318c73..c289fd6 100644 --- a/web/backend/crypto_storage.py +++ b/web/backend/crypto_storage.py @@ -13,6 +13,7 @@ from Crypto.Cipher import AES from sqlalchemy import text from sqlalchemy.engine import Engine from sqlalchemy.types import Text, TypeDecorator +from loguru import logger _PREFIX = "enc:v1:" @@ -164,7 +165,10 @@ def _table_exists(engine: Engine, table_name: str) -> bool: def encrypt_existing_sensitive_data(engine: Engine) -> int: - """把历史明文敏感字段迁移成密文;已加密但非主密钥的值会重加密。""" + """把历史明文敏感字段迁移成密文;已加密但非主密钥的值会重加密。 + + 解密失败的值(密钥丢失/变更)会被跳过并记录警告。 + """ primary_name = _key_candidates()[0].name changed = 0 @@ -179,7 +183,15 @@ def encrypt_existing_sensitive_data(engine: Engine) -> int: raw_value = row[column_name] if raw_value is None or raw_value == "": continue - plain_value, key_name = decrypt_value_with_key_name(str(raw_value)) + try: + plain_value, key_name = decrypt_value_with_key_name(str(raw_value)) + except ValueError: + # 解密失败(密钥变更/丢失),跳过该值,避免启动阻塞 + logger.warning( + f"跳过无法解密的字段 {table_name}.{column_name}[id={row['id']}]," + f"可能是加密密钥已变更" + ) + continue if is_encrypted(str(raw_value)) and key_name == primary_name: continue encrypted = encrypt_value(plain_value)