oliver-zehentleitner / maity

clean-up: sign-in and accounts: shared helpers for codes, hashes, addresses and lookups; dead code

Sign in to report

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:34 normalise_code and auth/mail_factor.py:48 normalise_code are identical; both drop only spaces.
  • auth/totp.py:48 normalised drops any whitespace.
  • Code generation f"{secrets.randbelow(1_000_000):06d}" appears at auth/email_links.py:106, auth/mail_factor.py:146 and :248.
  • What is wrong: one rule written three times, and the copies already disagree on tabs and newlines.
  • Proposal: keep one normalise_six() and one new_six() (for example in auth/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:28 ALPHABET, :39 new_code, :43 normalise.
  • auth/second_factor.py:55-56 _CROCKFORD and _CROCKFORD_READ, used at :353 and :363.
  • codes.py:23-31, built on ids.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.CROCKFORD everywhere and give codes.py a checked normalise(text, length) that device_codes and the recovery codes call. The length and alphabet check that only device_codes.normalise has 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:52 digest
  • auth/email_links.py:26 digest, imported by auth/password_resets.py:22
  • auth/second_factor_reset.py:43 _nonce_id
  • auth/caller.py:147 session_id_hash
  • auth/sessions.py:410 and :429, inline
  • registration_mail.py:81, inline
  • account_deletion.py:211, inline
  • codes.py:13 digest already exists for this.
  • Proposal: use codes.digest everywhere (or rename it, for example hashing.sha256_hex). session_id_hash may 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-70 address_of
  • auth/second_factor_reset.py:70-79, inline
  • account_deletion.py:158-166 _verified_address
  • routes/password_reset.py:100-110 and routes/email_sign_in.py:117-126, inline, both with a row lock
  • notifications/mailer.py:159
  • What is wrong: all of them run the same select on notification_settings for address_status == "verified" with a non-null address.
  • Proposal: one helper in notifications/preferences with a lock flag; move mail_factor.address_of there.
  • Effort: S
  • Risk: low.

5. Void open device codes in one place (audit #7)

  • Files:
  • auth/device_codes.py:31 OPEN and :250 void_open(conn, which).
  • auth/sessions.py:211-217 (inside end_where at :190) repeats the status tuple and the update.
  • account_deletion.py:555-562 does it again by human_id.
  • What is wrong: the open statuses are listed three times. The account_deletion copy also looks redundant, because sessions.end_where at account_deletion.py:536 has already ended every session and voided its codes; this is not verified.
  • Proposal: call device_codes.void_open. device_codes imports sessions, so either move OPEN and void_open into sessions or import late. Drop the account_deletion copy once the redundancy is confirmed.
  • Effort: S
  • Risk: low.

6. One address normalisation (audit #8)

  • Files:
  • registration.py:54-57 normalized; its docstring says it is "the same as mail.address.normalized" (mail/address.py:52-55).
  • registration.py:77, inline.
  • auth/email_links.py:251 and auth/password_resets.py:172 key their limiters on .strip().casefold(), which differs from .lower() (for example ß becomes ss).
  • Proposal: delete registration.normalized and use mail.address.normalized everywhere, the limiter keys included. The other callers are registration_accounts.py:96, routes/registration.py:193 and :332, routes/invitations.py:176 and invitation_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-61 facts_of and middleware.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"") in auth/caller.py, used by both.
  • Effort: S
  • Risk: low.

8. Repetition inside second_factor and the private _handle (audit #13)

  • Files:
  • The test "required_admins applies to this human" is written three times in auth/second_factor.py: :138-140, :745-747 and :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, :170 and :189 call the private second_factor._handle.
  • Proposal: add _admins_must_have_it(conn, hub, human_id) and a public handles.of_human(conn, human_id).
  • Effort: S
  • Risk: low.

9. Dead code and leftovers (audit #16)

  • Items:
  • auth/credentials.py:23 EMPTY_BODY_SHA256 has no reader.
  • auth/password_limits.py:32 ALL has no reader.
  • The comment at auth/passwords.py:57-58 says 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-620 has_password compares against DUMMY_HASH, a random hash made per process and never stored, so that half of the test can never be false.
  • middleware.py:355 imports Response again; it is already imported at :26.
  • type: ignore[no-any-return] at auth/nonces.py:42 and :50 and auth_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_links and password_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_accounts importing from the route module routes.registration (audit #10), are cross-area. They are filed separately by the coordinator.