oliver-zehentleitner / maity
clean-up: uploads and images: one image toolbox (sniff, caps, headers, decode), dead code and small duplicates
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()anddecode_attachmentout 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
_matchesimport 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-62sniff(PNG/JPEG/WebP/GIF) andimages.py:52-60sniff(PNG/JPEG/WebP, with alen >= 12guard). The first is the second plus GIF. - Content-Type parsers:
attachments.py:65-68declared_type; the same split/strip/lower is written inline atroutes/avatars.py:126androutes/project_images.py:75. The spec wants one difference: only attachments mapimage/jpgto JPEG (docs/api.md:12846-12847, §6.10 at 8513-8516). - The CLI's own copy (
cli/src/maity/cli.py:1656-1662, onavatar_typeat 3361) is a separate package and stays. - Proposal: one
sniff(data, allow=(...)), with attachments passing GIF, and onedeclared_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:36CAP(1 MiB) repeatsmiddleware.py:55IMAGE_CAP. Both are in use: the routes checkimages.CAPatroutes/avatars.py:127androutes/project_images.py:80; the middleware checks its own copy.attachments.py:33FILE_CAP(10 MiB) is read by no code; the enforced cap ismiddleware.py:65ATTACHMENT_CAP.attachments.py:44IMAGE_TYPEShas no reader in hub, cli or web.- Proposal: delete
IMAGE_TYPES. Have the middleware import the caps fromattachments/images(or the reverse), so each number exists once. - The docs drift around
FILE_CAPin 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-242current()has no caller in hub/src or tests;stored()(245) replaced it.attachment_store.py:208-212S3Store.digest()is called only byhub/tests/test_attachments_s3.py:494-496. Production code (copy_local_to_s3) usesstat(), 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-432drop_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-623writes the same deletes inline:avatar_image, thenavatar_size, by owner.- Proposal: add
avatars.remove_all(conn, owner_id), which deletes the image rows and callsdrop_sizes(conn, owner_id), and call it from account_deletion. Or drop the unusedsource=Nonebranch. - 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) anddecode_attachment(641-651). The generic child-process runnerdecode()(73-127) serves avatars, attachments and sizes alike. Its settings are namedavatar_decode_*andavatar_decodes, but they also govern attachment decodes.- The rule "a GIF is stored as PNG" is written twice:
avatars.STORED_TYPE(633-638) andattachments.EXTENSION(47), plusclean_name(attachments.py:87-89). images.pyis 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 androutes/avatars.py:127-129import itsHEADERS,etag,sniffandCAP.routes/attachments.py:40-45HEADERSis identical toavatars.HEADERS(avatars.py:48, which isimages.HEADERSwithprivate, no-cache). (From audit finding 7; the If-None-Match part of 7 is on tsk-k25qqhygend8.)Reencodedis a hand-written__slots__class where aNamedTupleor frozen dataclass would do.- Proposal: move
decode()anddecode_attachmentinto a parent-side module next toavatar_decode(for exampleimage_decode.py). Movesniff,etag, the sharedHEADERS(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) andused_total(169-177) are the same SUM; only theproject_idfilter differs. One function withproject_id: str | Nonewould do.attachments.bind(attachments.py:340-345) rebuilds theattachments_not_configuredApiError, message included, thatattachment_store.store_of(374-379) builds. Proposal: oneattachment_store.not_configured().routes/attachments.py:87,271,363callblobs.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_closedpart 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) andS3Store.age(274-290) each run their ownhead_objectwith 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._matchesimported 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.