fix(crypto): raise clear EncryptionError for invalid Fernet key (BIT-33939) #11

Closed
bug-squasher wants to merge 1 commits from task/BIT-33939-fernet-key-validation into main
pull from: task/BIT-33939-fernet-key-validation
Member

Context

BIT-33939 — ValueError: Fernet key must be 32 url-safe base64-encoded bytes. crashing the WebDAV sync loop.

Root cause

get_fernet() (src/forwarder/crypto.py) passed ENCRYPTION_KEY straight to Fernet() without guarding construction. When the key is malformed — e.g. the literal change_me_fernet_key placeholder shipped in .env.example — Fernet() raises a raw, cryptic ValueError that bubbles up through resolve_webdav_config → sync_webdav_account and gets reported to the error hub on every sync tick (error loop).

Fix

  • src/forwarder/crypto.py: wrap Fernet() construction in try/except and raise EncryptionError with an actionable message (points at Fernet.generate_key()) instead of leaking the raw ValueError.
  • .env.example: blank the invalid change_me_fernet_key placeholder so it cannot be copied verbatim into production; added the generate command as a comment.
  • tests/test_crypto.py: prove a malformed key raises EncryptionError (not ValueError), an empty key raises EncryptionError, and a generated key is accepted.

Production follow-up (not in this PR — infra/secret)

The code fix makes the error actionable but does not repair the running instance: production must have a valid ENCRYPTION_KEY. If encrypted WebDAV passwords already exist in the DB, the key must be the original one used to encrypt them — generating a fresh key would orphan existing ciphertext. This is a sysadmin/Infisical task for @nelson-baptista.

Paperclip-Issue: BIT-33939

## Context BIT-33939 — `ValueError: Fernet key must be 32 url-safe base64-encoded bytes.` crashing the WebDAV sync loop. ## Root cause `get_fernet()` (src/forwarder/crypto.py) passed `ENCRYPTION_KEY` straight to `Fernet()` without guarding construction. When the key is malformed — e.g. the literal `change_me_fernet_key` placeholder shipped in `.env.example` — `Fernet()` raises a raw, cryptic `ValueError` that bubbles up through `resolve_webdav_config` → `sync_webdav_account` and gets reported to the error hub on every sync tick (error loop). ## Fix - `src/forwarder/crypto.py`: wrap `Fernet()` construction in try/except and raise `EncryptionError` with an actionable message (points at `Fernet.generate_key()`) instead of leaking the raw `ValueError`. - `.env.example`: blank the invalid `change_me_fernet_key` placeholder so it cannot be copied verbatim into production; added the generate command as a comment. - `tests/test_crypto.py`: prove a malformed key raises `EncryptionError` (not `ValueError`), an empty key raises `EncryptionError`, and a generated key is accepted. ## Production follow-up (not in this PR — infra/secret) The code fix makes the error actionable but does not repair the running instance: production must have a **valid** `ENCRYPTION_KEY`. If encrypted WebDAV passwords already exist in the DB, the key must be the **original** one used to encrypt them — generating a fresh key would orphan existing ciphertext. This is a sysadmin/Infisical task for @nelson-baptista. Paperclip-Issue: BIT-33939
bug-squasher added 1 commit 2026-12-04 10:16:58 +00:00
get_fernet() let Fernet() raise a raw ValueError when ENCRYPTION_KEY was
malformed (e.g. the 'change_me_fernet_key' placeholder shipped in
.env.example), crashing the WebDAV sync loop with a cryptic
'Fernet key must be 32 url-safe base64-encoded bytes.' message.

Guard the Fernet() construction so an invalid key raises EncryptionError
with actionable guidance pointing at Fernet.generate_key(). Also blank
the invalid placeholder in .env.example so it cannot be copied verbatim
into production.

Paperclip-Issue: BIT-33939
Member

@cto — Gitea autopilot needs a CTO decision on BitReport.pt/forwarder#11: the PR has merge conflicts.

  • Head: 1243c7db57f7
  • The bot did not approve or schedule a merge for this head.
  • Fix the branch, approve as CTO, or close the PR. The bot re-checks on the next run.
@cto — Gitea autopilot needs a CTO decision on BitReport.pt/forwarder#11: the PR has merge conflicts. - Head: `1243c7db57f7` - The bot did **not** approve or schedule a merge for this head. - Fix the branch, approve as CTO, or close the PR. The bot re-checks on the next run. <!-- gitea-autopilot-escalation kind=conflict sha=1243c7db57f7 -->
Owner

Closing as superseded: main already has the same fix (969dbe0, BIT-33940) — get_fernet catches ValueError and raises EncryptionError, with equivalent tests. The only delta here is TypeError handling and a longer message; not worth a conflicting rebase. Reopen/new PR if TypeError coverage is wanted.

Closing as superseded: main already has the same fix (969dbe0, BIT-33940) — get_fernet catches ValueError and raises EncryptionError, with equivalent tests. The only delta here is TypeError handling and a longer message; not worth a conflicting rebase. Reopen/new PR if TypeError coverage is wanted.
spark closed this pull request 2026-12-04 10:32:33 +00:00

Pull request closed

Please reopen this pull request to perform a merge.
Sign in to join this conversation.