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:

  1. Voice channel attachments were refused although voice text chat is not end-to-end encrypted.
  2. isE2EEMedia was an invented heuristic testing prefixes that do not exist.
  3. Media serving was forked into a duplicate streaming implementation without CSP, cache-control, ETag, or Range support.
  4. Several authorization rules were unpinned by tests.
  5. Bot payload url pointed to the member /api/media/{id} route.
  6. Attachment metadata aliases (filename, content_type, size) required documentation and pointer serialization for 0-byte attachments.
  7. Bandwidth rate limit notes.

All round 1 findings were addressed in the revised implementation.

Adversarial Verification & Shared Serve Path

  1. Shared Serve Pipeline (serveMedia):

    • Authorization is unified under h.authorizeServed(ctx, sub, m, isBot).
    • The bot rules apply strictly before calling serveMedia and before http.ServeContent.
    • On denial, authorizeServed returns an error converted to 404 via hideDenied, 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, serveMedia enforces:
      • 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.
      • ETag generation and Range (bytes=...) serving.
  2. Member Access Integrity:

    • Member access rules in authorizeServed remain intact: staged uploads restricted to uploader, channel attachments guarded by ViewChannel, wiki attachments guarded by ViewWiki, and deleted media returns 410 (StatusGone).
    • All existing member media integration tests in internal/media and internal/server pass without regressions.
  3. 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.
  4. 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}.

Mutation Testing

Every access check and response condition was systematically mutated and verified against test failure:

MutationTest Failure / ResultVerdict
Remove ViewChannel checkTestBotDownloadAttachment_WithoutView fails (404 != 200)Caught
Remove m.messageID == nil checkTestBotDownloadAttachment_MessageIDNil fails on cover-shaped row (404 != 200)Caught
Remove m.staged checkTestBotDownloadAttachment_MessageIDNil fails on staged row with message ID (404 != 200)Caught
Remove m.deleted checkTestBotDownloadAttachment_AdminDeleted fails (404 != 200)Caught
Remove expires_at SQL filterTestBotDownloadAttachment_EndedTimedMessage fails (404 != 200)Caught
Remove bot missing object 404 checkTestBotDownloadAttachment_MissingObject fails (404 != 410)Caught
Remove Gateway botEventPayload rewriteTestBotDownloadAttachment_GatewayPayloadURL fails (mismatched URL)Caught
Remove forced attachment dispositionTestBotDownloadAttachment_HappyPath fails (Content-Disposition inline)Caught
Send If-None-Match or Range on hidden channelTestBotDownloadAttachment_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.

Something wrong on this page? Tell us.