clean-up: uploads and images: one guarded open in avatar_decode, an attachment sweep that does not grow with every attachment
tsk-29gg66b9ep80 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.
These two items carry medium risk (the image parser guard, and the race reasoning of the attachment sweep), so they are apart from the low-risk Area 4 card filed at the same time.
Order: none between the two. Item 6 touches avatar_decode.py only; if the low-risk card's finding 5 (moving decode() into an image_decode.py) is in flight, rebase rather than conflict. tsk-x2gd2w0146jm (security hardening, open) changes the decode refund path; keep the two PRs apart.
6. Duplication: avatar_decode repeats the guarded open sequence
process()(avatar_decode.py:149-167) andprocess_file()(261-278) repeat the same steps: header dimensions, side/pixel check, PNG text walk, Pillow limits,Image.openwith one format, two size checks aroundload(). Only the limits and the format table differ.- The 16-bit grey conversion is written twice (142-143 in
_rgba, 282-283). - "Rebuild from raw pixels, save as PNG" is written twice (176-179, 197-200).
- Proposal: one
_open_checked(data, content_type, side, pixels, formats)and one_png_from_raw(image). This is the hub's most exposed parser, so a single copy of the guard is also the safer shape. - Effort: S-M. Risk: medium, because the code is security-relevant. Run test_avatars.py, test_attachment_images.py and test_avatar_public.py.
9. Debt: the attachment sweep's cost grows with every attachment ever stored
- Every 5 minutes (
SWEEP_EVERY, attachments.py:439),sweep()does two things that grow without bound: - It reads every tombstoned row ever (
attachments.py:572-576) and asks the store about each one (store.age, 579-586). Tombstones are never removed, so this costs one stat or S3 HEAD per deleted or redacted attachment, forever. - For every blob in the store (587-600),
remove_stray(499-535) callsstore.age, one HEAD per object on s3 (attachment_store.py:274-281). Any blob older thanBLOB_GRACE, which is nearly every blob, then opens its own transaction and takes its project's lock (522) just to find that its row exists. - Proposal:
- Prefilter each project's keys with one unlocked
SELECT id ... WHERE id IN (...). Skipping a key that has a row is safe; a later pass catches it. Only keys without a row go through the locked check. - For s3, take the age from
list_objects_v2'sLastModified, already in the listing atattachment_store.py:297-304, instead of a HEAD. - Bound the tombstone pass, for example to
deleted_atwithin a window, or record that the bytes are gone. Mind docs/api.md:12925 ("a removal that fails is retried by the sweep"). - Effort: M. Risk: medium, because the race reasoning in hub/context/attachments.md 7dcd4850 must still hold.
Effort sum: S-M + M = about 5 points.