fix(crypto): catch ValueError on malformed Fernet key, raise EncryptionError (BIT-33940)
ci / verify (pull_request) Successful in 49s
ci / verify (pull_request) Successful in 49s
get_fernet() only checked for an empty ENCRYPTION_KEY. A non-empty but malformed key (not 32 url-safe base64 bytes) let Fernet() raise a raw ValueError that crashed sync_account. Wrap the constructor and re-raise as EncryptionError with a clear message. Paperclip-BIT-33940
This commit is contained in:
1 parent
c389503fb9
commit
969dbe08ce
2 files changed
+52
No files matched your search
@@ -13,7 +13,12 @@ def get_fernet() -> Fernet:
|
|||||||
key = get_settings().encryption_key.strip()
|
key = get_settings().encryption_key.strip()
|
||||||
if not key:
|
if not key:
|
||||||
raise EncryptionError("ENCRYPTION_KEY is not configured")
|
raise EncryptionError("ENCRYPTION_KEY is not configured")
|
||||||
|
try:
|
||||||
return Fernet(key.encode() if isinstance(key, str) else key)
|
return Fernet(key.encode() if isinstance(key, str) else key)
|
||||||
|
except ValueError as exc:
|
||||||
|
raise EncryptionError(
|
||||||
|
"ENCRYPTION_KEY must be 32 url-safe base64-encoded bytes"
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
|
||||||
def encrypt_password(plain: str) -> str:
|
def encrypt_password(plain: str) -> str:
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
"""Crypto / Fernet key tests."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from cryptography.fernet import Fernet
|
||||||
|
|
||||||
|
from forwarder.crypto import EncryptionError, decrypt_password, encrypt_password, get_fernet
|
||||||
|
|
||||||
|
|
||||||
|
def _make_settings(key: str):
|
||||||
|
fake = type("FakeSettings", (), {"encryption_key": key})()
|
||||||
|
return fake
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_fernet_with_valid_key():
|
||||||
|
key = Fernet.generate_key().decode()
|
||||||
|
with patch("forwarder.crypto.get_settings", return_value=_make_settings(key)):
|
||||||
|
f = get_fernet()
|
||||||
|
assert isinstance(f, Fernet)
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_fernet_empty_key_raises_encryption_error():
|
||||||
|
with patch("forwarder.crypto.get_settings", return_value=_make_settings("")):
|
||||||
|
with pytest.raises(EncryptionError, match="not configured"):
|
||||||
|
get_fernet()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_fernet_invalid_key_raises_encryption_error():
|
||||||
|
with patch("forwarder.crypto.get_settings", return_value=_make_settings("not-a-valid-fernet-key")):
|
||||||
|
with pytest.raises(EncryptionError, match="32 url-safe base64"):
|
||||||
|
get_fernet()
|
||||||
|
|
||||||
|
|
||||||
|
def test_decrypt_password_with_invalid_key_raises_encryption_error():
|
||||||
|
with patch("forwarder.crypto.get_settings", return_value=_make_settings("bad-key")):
|
||||||
|
with pytest.raises(EncryptionError, match="32 url-safe base64"):
|
||||||
|
decrypt_password("some-token")
|
||||||
|
|
||||||
|
|
||||||
|
def test_encrypt_then_decrypt_roundtrip():
|
||||||
|
key = Fernet.generate_key().decode()
|
||||||
|
with patch("forwarder.crypto.get_settings", return_value=_make_settings(key)):
|
||||||
|
token = encrypt_password("secret123")
|
||||||
|
assert decrypt_password(token) == "secret123"
|
||||||
Reference in new issue
Block a user