Review of PR #851: "feat: bots can download attachments of messages they can see"
From mnema-talk v3.3.13, rendered on this site.
Second independent adversarial security review. Final verdict: MERGE.
Context & Prior Findings (Round 1)
Round 1 identified 4 medium and 3 low findings:
- Voice channel attachments were refused although voice text chat is not end-to-end encrypted.
isE2EEMediawas an invented heuristic testing prefixes that do not exist.- Media serving was forked into a duplicate streaming implementation without CSP, cache-control, ETag, or Range support.
- Several authorization rules were unpinned by tests.
- Bot payload
urlpointed to the member/api/media/{id}route. - Attachment metadata aliases (
filename,content_type,size) required documentation and pointer serialization for 0-byte attachments. - Bandwidth rate limit notes.
All round 1 findings were addressed in the revised implementation.
Adversarial Verification & Shared Serve Path
Shared Serve Pipeline (
serveMedia):- Authorization is unified under
h.authorizeServed(ctx, sub, m, isBot). - The bot rules apply strictly before calling
serveMediaand beforehttp.ServeContent. - On denial,
authorizeServedreturns an error converted to 404 viahideDenied, ensuring uniform 404 existence privacy (no 403 or timing oracles). - Range requests (206) and conditional revalidations (
If-None-Match-> 304) never execute for unauthorized IDs; they consistently return 404. - For authorized bots,
serveMediaenforces:Content-Disposition: attachment; filename="..."(forced attachment disposition; never inline).Content-Security-Policy: default-src 'none'; sandbox.X-Content-Type-Options: nosniff.Cache-Control: private, no-cache.ETaggeneration and Range (bytes=...) serving.
- Authorization is unified under
Member Access Integrity:
- Member access rules in
authorizeServedremain intact: staged uploads restricted to uploader, channel attachments guarded byViewChannel, wiki attachments guarded byViewWiki, and deleted media returns 410 (StatusGone). - All existing member media integration tests in
internal/mediaandinternal/serverpass without regressions.
- Member access rules in
Storage E2EE Verification:
- Audited the entire repository and database schema for encrypted blobs in object storage.
- Real-time voice and video RTP payloads are client-side MLS encrypted in memory by the browser and forwarded by the Pion SFU. No calls or RTP streams are written to S3/object storage.
- Attachments, avatars, and covers stored in object storage are standard media files inspected with MIME sniffers.
- The claim "there are no encrypted blobs in object storage" holds unconditionally.
Bot Payload URLs:
- Both REST (
loadAttachments) and Gateway WebSocket (botEventPayload) rewrite attachment URLs to/api/bot/v1/media/{id}for bots. - Member routes and payloads retain
/api/media/{id}.
- Both REST (
Mutation Testing
Every access check and response condition was systematically mutated and verified against test failure:
| Mutation | Test Failure / Result | Verdict |
|---|---|---|
Remove ViewChannel check | TestBotDownloadAttachment_WithoutView fails (404 != 200) | Caught |
Remove m.messageID == nil check | TestBotDownloadAttachment_MessageIDNil fails on cover-shaped row (404 != 200) | Caught |
Remove m.staged check | TestBotDownloadAttachment_MessageIDNil fails on staged row with message ID (404 != 200) | Caught |
Remove m.deleted check | TestBotDownloadAttachment_AdminDeleted fails (404 != 200) | Caught |
Remove expires_at SQL filter | TestBotDownloadAttachment_EndedTimedMessage fails (404 != 200) | Caught |
| Remove bot missing object 404 check | TestBotDownloadAttachment_MissingObject fails (404 != 410) | Caught |
Remove Gateway botEventPayload rewrite | TestBotDownloadAttachment_GatewayPayloadURL fails (mismatched URL) | Caught |
| Remove forced attachment disposition | TestBotDownloadAttachment_HappyPath fails (Content-Disposition inline) | Caught |
Send If-None-Match or Range on hidden channel | TestBotDownloadAttachment_WithoutView asserts 404 (never 304 or 206) | Caught |
Verdict
MERGE. The implementation is robust, adheres to zero secret exposure and existence privacy, shares the hardened streaming pipeline, and is fully pinned by integration and mutation tests.