oliver-zehentleitner / maity
clean-up: sign-in and accounts: shared helpers for codes, hashes, addresses and lookups; dead code
tsk-j967n09yz6ps Done An agent
From the clean-up audit tsk-hgnbapyw7hnh, area 14 (sign-in and accounts), read at 80647565. Every line number is at 80647565; check each one against current main before changing it. The "audit #N" numbers are those of the findings sent to the lead (seq 125426 and 125444).
This card groups the small, low-risk helper cleanups. Each item is S. None of them changes behaviour, except item 6 (limiter keys) as noted there.
1. One helper for six-digit codes (audit #1)
- Files:
auth/email_links.py:34normalise_codeandauth/mail_factor.py:48normalise_codeare identical; both drop only spaces.auth/totp.py:48normaliseddrops any whitespace.- Code generation
f"{secrets.randbelow(1_000_000):06d}"appears atauth/email_links.py:106,auth/mail_factor.py:146and:248. - What is wrong: one rule written three times, and the copies already disagree on tabs and newlines.
- Proposal: keep one
normalise_six()and onenew_six()(for example inauth/totp.py), called from all three modules. - Effort: S
- Risk: low. A pasted code with a tab or newline is then read the same way on every path.
2. One Crockford base32 alphabet and reading rule (audit #2)
- Files:
auth/device_codes.py:28ALPHABET,:39new_code,:43normalise.auth/second_factor.py:55-56_CROCKFORDand_CROCKFORD_READ, used at:353and:363.codes.py:23-31, built onids.CROCKFORD(ids.py:12).- What is wrong: the device code is the same 12-character, three-groups-of-four code as
codes.new_code, yet its alphabet and reading rule are written out again, and a third time for recovery codes. - Proposal: use
ids.CROCKFORDeverywhere and givecodes.pya checkednormalise(text, length)thatdevice_codesand the recovery codes call. The length and alphabet check that onlydevice_codes.normalisehas today moves into the shared helper. - Effort: S
- Risk: low to medium. Device-code hashes are stored over the normalised form (upper case, no hyphens); the shared helper must return exactly that.
3. One sha256-hex helper (audit #3)
- Copies:
auth/device_codes.py:52digestauth/email_links.py:26digest, imported byauth/password_resets.py:22auth/second_factor_reset.py:43_nonce_idauth/caller.py:147session_id_hashauth/sessions.py:410and:429, inlineregistration_mail.py:81, inlineaccount_deletion.py:211, inlinecodes.py:13digestalready exists for this.- Proposal: use
codes.digesteverywhere (or rename it, for examplehashing.sha256_hex).session_id_hashmay stay as a named wrapper. - Effort: S
- Risk: low. The output is identical.
4. One reader for the verified address of a human (audit #6)
- Copies:
auth/mail_factor.py:64-70address_ofauth/second_factor_reset.py:70-79, inlineaccount_deletion.py:158-166_verified_addressroutes/password_reset.py:100-110androutes/email_sign_in.py:117-126, inline, both with a row locknotifications/mailer.py:159- What is wrong: all of them run the same select on
notification_settingsforaddress_status == "verified"with a non-null address. - Proposal: one helper in
notifications/preferenceswith alockflag; movemail_factor.address_ofthere. - Effort: S
- Risk: low.
5. Void open device codes in one place (audit #7)
- Files:
auth/device_codes.py:31OPENand:250void_open(conn, which).auth/sessions.py:211-217(insideend_whereat:190) repeats the status tuple and the update.account_deletion.py:555-562does it again byhuman_id.- What is wrong: the open statuses are listed three times. The
account_deletioncopy also looks redundant, becausesessions.end_whereataccount_deletion.py:536has already ended every session and voided its codes; this is not verified. - Proposal: call
device_codes.void_open.device_codesimportssessions, so either moveOPENandvoid_openintosessionsor import late. Drop theaccount_deletioncopy once the redundancy is confirmed. - Effort: S
- Risk: low.
6. One address normalisation (audit #8)
- Files:
registration.py:54-57normalized; its docstring says it is "the same asmail.address.normalized" (mail/address.py:52-55).registration.py:77, inline.auth/email_links.py:251andauth/password_resets.py:172key their limiters on.strip().casefold(), which differs from.lower()(for example ß becomes ss).- Proposal: delete
registration.normalizedand usemail.address.normalizedeverywhere, the limiter keys included. The other callers areregistration_accounts.py:96,routes/registration.py:193and:332,routes/invitations.py:176andinvitation_mail.py:47. - Effort: S
- Risk: low. Changing the limiter key empties the live in-memory buckets once.
7. Build RequestFacts in one place (audit #9)
- Files:
deps.py:49-61facts_ofandmiddleware.py:294-304(in_precheck). - What is wrong: two builders of the same structure. A new field has to be added to both, or the precheck sees something different from the full check.
- Proposal: one
RequestFacts.from_scope(scope, settings, body=b"")inauth/caller.py, used by both. - Effort: S
- Risk: low.
8. Repetition inside second_factor and the private _handle (audit #13)
- Files:
- The test "
required_adminsapplies to this human" is written three times inauth/second_factor.py::138-140,:745-747and:796-797. - The handle of a human is read three ways in the same file:
:660,:816-817(_handle) and:846. auth/second_factor_reset.py:111,:170and:189call the privatesecond_factor._handle.- Proposal: add
_admins_must_have_it(conn, hub, human_id)and a publichandles.of_human(conn, human_id). - Effort: S
- Risk: low.
9. Dead code and leftovers (audit #16)
- Items:
auth/credentials.py:23EMPTY_BODY_SHA256has no reader.auth/password_limits.py:32ALLhas no reader.- The comment at
auth/passwords.py:57-58says DUMMY_HASH is used "when the handle does not exist". Login has been by verified address since 01029c7f (context/sign-in.md). auth/second_factor.py:619-620has_passwordcompares againstDUMMY_HASH, a random hash made per process and never stored, so that half of the test can never be false.middleware.py:355importsResponseagain; it is already imported at:26.type: ignore[no-any-return]atauth/nonces.py:42and:50andauth_pages.py:112.- Proposal: delete the dead constants and the comparison, fix the comment, drop the second import, and replace the ignores with a typed cast.
- Effort: S
- Risk: none.
Order
- Item 6 (the limiter keys in
email_linksandpassword_resets): do it after tsk-q4zynccy5t0b, which adds per-recipient caps to the same anonymous-mail limits. - Item 8: do it after tsk-8mgb85ne0nrq, which is changing
auth/second_factor.py. - Item 9 (
auth/credentials.py): tsk-bg5f4cv0894w and tsk-d9r02hn5n32y also edit that file; rebase rather than conflict. - Otherwise the items are independent and fit one PR.
Left out
- The background Worker class copied in about 18 modules (audit #18), and
registration_accountsimporting from the route moduleroutes.registration(audit #10), are cross-area. They are filed separately by the coordinator.