Author SHA1 Message Date
spark 948b6ba2c6 Merge pull request 'fix(crypto): catch ValueError on malformed Fernet key, raise EncryptionError (BIT-33940)' (#12) from fix/BIT-33940-fernet-key-validation into main
ci / verify (push) Successful in 32s
2026-12-04 10:32:08 +00:00
spark 54c317e452 fix(deploy): configurable health check URL for deploy-hook container (BIT-33898) (#10)
ci / verify (push) Successful in 53s
2026-12-04 10:19:41 +00:00
Xika 969dbe08ce fix(crypto): catch ValueError on malformed Fernet key, raise EncryptionError (BIT-33940)
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
2026-12-04 10:18:29 +00:00
Xika 77ee80d3f1 fix(deploy): configurable health check URL for deploy-hook container (BIT-33898)
ci / verify (pull_request) Successful in 35s
The deploy-pull.sh health check used 127.0.0.1:8000 which doesn't
reach the API from inside the deploy-hook container (separate network
namespace). Add HEALTH_CHECK_URL env var so the container can use
http://api:8000 via Docker DNS. Also add .git-credentials to .gitignore
for the credential file mounted into the deploy-hook container.

Paperclip-Issue: BIT-33898
Paperclip-Agent: nelson-baptista
2026-12-04 10:16:28 +00:00
5 changed files with 38 additions and 43 deletions

No files matched your search

+1 -2
View File
@@ -8,8 +8,7 @@ MYSQL_PASSWORD=change_me_db
# App
DATABASE_URL=mysql+pymysql://forwarder:change_me_db@mysql:3306/forwarder?charset=utf8mb4
# Generate with: python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
ENCRYPTION_KEY=
ENCRYPTION_KEY=change_me_fernet_key
APP_SECRET=change_me_jwt_secret
ADMIN_USERNAME=admin
ADMIN_PASSWORD=change_me
+3
View File
@@ -50,6 +50,9 @@ processed_emails.json
storage/deploy-logs/*
!storage/deploy-logs/.gitkeep
# Git credentials (deploy-hook container)
.git-credentials
# OS
.DS_Store
Thumbs.db
+3 -2
View File
@@ -66,12 +66,13 @@ log "Building API + worker ($GIT_SHA)"
$COMPOSE build --build-arg GIT_SHA="$GIT_SHA" web
remove_stale_service_containers web
$COMPOSE up -d --force-recreate web
HEALTH_URL="${HEALTH_CHECK_URL:-http://127.0.0.1:8000/api/health}"
for i in 1 2 3 4 5; do
curl -sf --max-time 8 http://127.0.0.1:8000/api/health >/dev/null && break
curl -sf --max-time 8 "$HEALTH_URL" >/dev/null && break
log "API not ready yet (attempt $i/5), retrying in 5s..."
sleep 5
done
curl -sf --max-time 8 http://127.0.0.1:8000/api/health >/dev/null
curl -sf --max-time 8 "$HEALTH_URL" >/dev/null
write_status "ok $GIT_SHA $(date -Iseconds)"
log "Deploy OK ($GIT_SHA)"
log "Refreshing deploy-hook (self-restart — runs detached)"
+3 -7
View File
@@ -13,15 +13,11 @@ def get_fernet() -> Fernet:
key = get_settings().encryption_key.strip()
if not key:
raise EncryptionError("ENCRYPTION_KEY is not configured")
raw = key.encode() if isinstance(key, str) else key
try:
return Fernet(raw)
except (ValueError, TypeError) as exc:
return Fernet(key.encode() if isinstance(key, str) else key)
except ValueError as exc:
raise EncryptionError(
"ENCRYPTION_KEY is not a valid Fernet key: it must be 32 url-safe "
"base64-encoded bytes (44 chars). Generate one with: "
"python -c \"from cryptography.fernet import Fernet; "
"print(Fernet.generate_key().decode())\""
"ENCRYPTION_KEY must be 32 url-safe base64-encoded bytes"
) from exc
+28 -32
View File
@@ -1,51 +1,47 @@
"""Fernet key validation tests."""
"""Crypto / Fernet key tests."""
from __future__ import annotations
from unittest.mock import MagicMock, patch
from unittest.mock import patch
import pytest
from cryptography.fernet import Fernet
from forwarder.crypto import EncryptionError, decrypt_password, get_fernet
from forwarder.crypto import EncryptionError, decrypt_password, encrypt_password, get_fernet
def _settings_with_key(key: str):
settings = MagicMock()
settings.encryption_key = key
return settings
def _make_settings(key: str):
fake = type("FakeSettings", (), {"encryption_key": key})()
return fake
def test_get_fernet_raises_encryption_error_when_missing():
with patch("forwarder.crypto.get_settings", return_value=_settings_with_key(" ")):
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_raises_encryption_error_for_invalid_key():
with patch("forwarder.crypto.get_settings", return_value=_settings_with_key("change_me_fernet_key")):
with pytest.raises(EncryptionError, match="valid Fernet key"):
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_get_fernet_does_not_leak_value_error_for_invalid_key():
with patch("forwarder.crypto.get_settings", return_value=_settings_with_key("change_me_fernet_key")):
try:
get_fernet()
except ValueError as exc:
pytest.fail(f"raw ValueError leaked instead of EncryptionError: {exc}")
except EncryptionError:
pass
def test_get_fernet_accepts_generated_key():
valid = Fernet.generate_key().decode()
with patch("forwarder.crypto.get_settings", return_value=_settings_with_key(valid)):
f = get_fernet()
assert isinstance(f, Fernet)
def test_decrypt_password_raises_encryption_error_for_invalid_key():
with patch("forwarder.crypto.get_settings", return_value=_settings_with_key("not-a-fernet-key")):
with pytest.raises(EncryptionError, match="valid Fernet key"):
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"