oliver-zehentleitner / maity

clean-up: uploads and images: one image toolbox (sniff, caps, headers, decode), dead code and small duplicates

Sign in to report

tsk-thx8zrjc708f Open

From the clean-up audit tsk-hgnbapyw7hnh, Area 4 (Uploads and images), read at 80647565. Line numbers are at that SHA; check them against current main first. Paths are under hub/src/maity_hub/. Finding numbers are the audit's.

Order:

  • Do 1, 2 and 5 in one PR, or 5 first: 1 and 2 put the sniffer and the caps into the image helper module that 5 creates.
  • 5 moves decode() and decode_attachment out of avatars.py. tsk-x2gd2w0146jm (open, security hardening) changes the refund on a refused decode in routes/attachments.py and maybe the decode slots in avatars.py. Do 5 after it.
  • tsk-vvyqmk9vwg8e (in progress) changes caching in routes/avatars.py, and tsk-k25qqhygend8 item 1 replaces the private _matches import in routes/attachments.py and routes/avatars.py. Rebase on both.
  • 3, 4, 10 and 11 have no order.

1. Duplication: three image sniffers and three Content-Type parsers

  • Sniffers: attachments.py:52-62 sniff (PNG/JPEG/WebP/GIF) and images.py:52-60 sniff (PNG/JPEG/WebP, with a len >= 12 guard). The first is the second plus GIF.
  • Content-Type parsers: attachments.py:65-68 declared_type; the same split/strip/lower is written inline at routes/avatars.py:126 and routes/project_images.py:75. The spec wants one difference: only attachments map image/jpg to JPEG (docs/api.md:12846-12847, §6.10 at 8513-8516).
  • The CLI's own copy (cli/src/maity/cli.py:1656-1662, on avatar_type at 3361) is a separate package and stays.
  • Proposal: one sniff(data, allow=(...)), with attachments passing GIF, and one declared_type(header, *, jpg_alias=False), both in the shared image helper module of finding 5.
  • Effort: S. Risk: low; test_project_images.py:99 and the attachment tests cover the sniff cases.

2. Debt and duplication: byte caps defined twice, one constant dead

  • images.py:36 CAP (1 MiB) repeats middleware.py:55 IMAGE_CAP. Both are in use: the routes check images.CAP at routes/avatars.py:127 and routes/project_images.py:80; the middleware checks its own copy.
  • attachments.py:33 FILE_CAP (10 MiB) is read by no code; the enforced cap is middleware.py:65 ATTACHMENT_CAP.
  • attachments.py:44 IMAGE_TYPES has no reader in hub, cli or web.
  • Proposal: delete IMAGE_TYPES. Have the middleware import the caps from attachments/images (or the reverse), so each number exists once.
  • The docs drift around FILE_CAP in the S3 copy (attachment_store.py:412, hub/context/replicas.md:599) is on its own card, tsk-8axntrksjmwx, because it may concern security.
  • Effort: S. Risk: low.

3. Debt: dead functions

  • avatars.py:239-242 current() has no caller in hub/src or tests; stored() (245) replaced it.
  • attachment_store.py:208-212 S3Store.digest() is called only by hub/tests/test_attachments_s3.py:494-496. Production code (copy_local_to_s3) uses stat(), so the test covers code nothing runs.
  • Proposal: remove both, and change the test to assert on stat().
  • Effort: S. Risk: none.

4. Duplication: avatar size clean-up rewritten outside avatars

  • avatars.py:427-432 drop_sizes(conn, owner_id, source=None) has a path for dropping every source. No caller uses it; both callers pass "uploaded" (436, 444).
  • account_deletion.py:622-623 writes the same deletes inline: avatar_image, then avatar_size, by owner.
  • Proposal: add avatars.remove_all(conn, owner_id), which deletes the image rows and calls drop_sizes(conn, owner_id), and call it from account_deletion. Or drop the unused source=None branch.
  • Effort: S. Risk: low.

5. Design: avatars.py and images.py have outgrown their purposes

  • avatars.py ("a member's picture") also holds the attachment image path: Reencoded (620-630), STORED_TYPE (633-638) and decode_attachment (641-651). The generic child-process runner decode() (73-127) serves avatars, attachments and sizes alike. Its settings are named avatar_decode_* and avatar_decodes, but they also govern attachment decodes.
  • The rule "a GIF is stored as PNG" is written twice: avatars.STORED_TYPE (633-638) and attachments.EXTENSION (47), plus clean_name (attachments.py:87-89).
  • images.py is the project logo/banner module, yet it is also the generic image toolbox: avatars.py:48,52,272,299,437, motifs.py:134,143,148, the avatar_size migration and routes/avatars.py:127-129 import its HEADERS, etag, sniff and CAP.
  • routes/attachments.py:40-45 HEADERS is identical to avatars.HEADERS (avatars.py:48, which is images.HEADERS with private, no-cache). (From audit finding 7; the If-None-Match part of 7 is on tsk-k25qqhygend8.)
  • Reencoded is a hand-written __slots__ class where a NamedTuple or frozen dataclass would do.
  • Proposal: move decode() and decode_attachment into a parent-side module next to avatar_decode (for example image_decode.py). Move sniff, etag, the shared HEADERS (public and private variants) and the caps into one small image helper. Keep one type table that gives both the stored type and the extension.
  • Effort: M. Risk: low (moves only), but the import sites touch many files.

10. Duplication: the same SQL and the same error written twice

  • attachments.used (159-166) and used_total (169-177) are the same SUM; only the project_id filter differs. One function with project_id: str | None would do.
  • attachments.bind (attachments.py:340-345) rebuilds the attachments_not_configured ApiError, message included, that attachment_store.store_of (374-379) builds. Proposal: one attachment_store.not_configured().
  • routes/attachments.py:87,271,363 call blobs.store_of(settings) only to check that a store is configured, and discard the store. attachment_store.configured() plus that error says what is meant.
  • (The audit's project_closed part of this finding is hub-wide, 13 sites, and left to a shared card.)
  • Effort: S. Risk: low.

11. Duplication: small repeats inside single modules

  • attachment_store.py: S3Store.stat (214-225) and S3Store.age (274-290) each run their own head_object with the same missing-object and error handling. Proposal: one _head(key).
  • images.py: _state(_shown(_rows(conn, [project_id]), kind)) is spelled out 9 times (132, 178, 192, 202, 210, 218, 225, 370, 378). Proposal: _shown_state(conn, project_id, kind).
  • avatars.py:84-85: max(1, math.ceil(settings.avatar_decode_seconds)) is computed twice, one line apart.
  • avatars.announce (491-496) and _projects_of (609-617) build the same "active memberships of this member" query.
  • Effort: S. Risk: none.

Covered elsewhere

  • Finding 7, If-None-Match (routes/project_images._matches imported by routes/attachments.py and routes/avatars.py, and the other copies): tsk-k25qqhygend8 item 1.
  • Finding 8, the copied background Worker (attachments.py:603-633, images.py:413-450): tsk-dzpzpn5xrm5j item 1.

Effort sum: S+S+S+S+M+S+S = 6S+1M = 9 points.