oliver-zehentleitner / maity
clean-up: secrets and signing: secretbox as the one AEAD user, raw-key accessors named, its own tests, packaging.md
tsk-xkwfxqp240kd Open
From the clean-up audit tsk-hgnbapyw7hnh, Area 1 (Secrets and signing), read at 80647565. Line numbers are at 80647565; check each one against current main before changing it. Finding numbers are the audit's (A1-n).
This card makes secretbox the one place for sealing and gives it its own tests. The sibling card for ids, signing and redaction helpers was filed at the same time.
1. Notification positions re-implement secretbox's AES-GCM (A1-3, duplication)
- Files:
notifications/positions.py:36, 85-104(NONCE_BYTES,_aad,_seal,_open) build nonce || AESGCM(key).encrypt(nonce, data, aad) with a random 96-bit nonce. That is exactlysecretbox.seal/open_sealed(secretbox.py:20, 42-53), withf"{human_id}|{PURPOSE}"as the field. - Proposal:
_seal= base64url(secretbox.seal(key, f"{human_id}|{PURPOSE}", json));_opencatchessecretbox.SealedError. secretbox is then the only AESGCM user. - Effort: S. Risk: low. Byte layout and AAD stay identical, so positions already issued keep opening.
2. Raw stored keys go through accessors named "sealed" (rest of A1-4, design)
- The HKDF part of A1-4 (three copies of "HKDF of MAITY_SECRET_KEY, else a stored random key", and known_sources reading
mail.address.LOCK_PEPPER) is covered by tsk-8vdegd61y70v item 4. Not repeated here. - Left:
notifications/positions.py,mail/address.pyandauth/known_sources.pystore and read raw, unsealed keys throughinstance_settings.sealed/add_sealed(instance_settings.py:95-140). Its docstring says "a stored secret assecretboxsealed it"; the callers' own comments (positions.py:37-39,address.py:58-60) say "not sealed". - Proposal: name the raw-key accessors for what they are (e.g.
stored_key/add_stored_key), or let the fallback-key helper of tsk-8vdegd61y70v item 4 own them. When doing so, give known_sources its own purpose over the shared helper, notLOCK_PEPPER, if that card has not already. - Effort: S. Risk: low (rename). The stored rows and derived bytes must not change.
3. packaging.md says secretbox is the only user of cryptography (A1-10, docs drift)
hub/context/packaging.md:166-171: "maity_hub.secretboxis the only module that uses it".cryptographyis also imported bynotifications/positions.py:22-25(AESGCM, HKDF),mail/address.py:18-19(HKDF),auth/known_sources.py:23-24(HKDF) andgithub_app.py:25-26(RSA for the app JWT).- Proposal: list the uses: AEAD in secretbox, HKDF for derived keys, RSA in github_app. After item 1 and tsk-8vdegd61y70v item 4 the list gets shorter, so write it last.
- Effort: S. Risk: none.
4. secretbox has no test module, and one branch is untested (A1-11, debt)
- The only direct unit test is inside
hub/tests/test_forge_webhook.py:599-607(test_a_sealed_value_opens_only_with_its_key_and_field).parse_keyis covered only through Settings (test_forge_webhook.py:628,test_mail_settings.py:128). No test reaches the "not a sealed value" branch (secretbox.py:48-49, input of 12 bytes or fewer).codes.pyhas no unit test either; only the join and pairing routes cover it. - Proposal: add
hub/tests/test_secretbox.py; move the round-trip test there and add the short-input, wrong-length-key and unset-key cases. Optionally a smalltest_codes.pyfornormalise_code/code_matches(or for the shared reader of tsk-j967n09yz6ps item 2). - Effort: S. Risk: none.
Order
- Items 1 and 2 after tsk-8vdegd61y70v item 4, which rewrites the key handling in the same files (positions.py, address.py, known_sources.py). tsk-k25qqhygend8 item 3 also swaps the base64url codec in positions.py; rebase rather than conflict.
- Item 3 last.
- Item 4 is independent.
Covered elsewhere
- A1-1 (Crockford codes made and read in three places): tsk-j967n09yz6ps item 2.
- A1-4, HKDF part: tsk-8vdegd61y70v item 4.
- A1-2 (sha256 token digest copies) is a hub-wide pattern; the lead files it on a shared card (most copies are already in tsk-j967n09yz6ps item 3).
Effort sum: 4S.