Skip to content

Serve editor media of a Private document only to its owner #439

Closed

Description

@HMarzban

Summary

Decision needed first: the maintainer must choose how a media request proves who asks. Nobody should write code before that choice. The smallest option found is in the fix plan below.

Editor media of a Private document stays readable to anyone who once saw its URL. The media route cannot apply resolvePrivateAccess, because an image request has no identity. The editor loads media with a plain src attribute, so the request carries no token. The Supabase session cookie is host-only on docs.plus and never reaches prodback.docs.plus.

Where

  • Route, public by design: apps/hocuspocus.server/src/api/routers/hypermultimedia.router.ts:30-35.
  • getMedia calls the store with no metadata, privacy or Trash check: apps/hocuspocus.server/src/api/controllers/documents.controller.ts:410-421 and apps/hocuspocus.server/src/api/services/media.service.ts:14-15. Upload does check (documents.controller.ts:431-451).
  • Responses carry Cache-Control: public, max-age=31536000, immutable: apps/hocuspocus.server/src/lib/storage/storage.s3.ts:116 and apps/hocuspocus.server/src/lib/storage/storage.local.ts:80.
  • The editor stores ${NEXT_PUBLIC_RESTAPI_URL}/plugins/hypermultimedia/<documentId>/<file> and renders it as a plain src: apps/webapp/src/components/TipTap/mediaPopovers/uploadMediaFile.ts:195.
  • Duplicate copies every object the snapshot names, under any prefix: apps/hocuspocus.server/src/api/services/documents.service.ts:851-861, media.service.ts:39-68.

Fix plan

Decision (maintainer)

Pick the credential before coding. The smallest option found:

  • The webapp calls a new POST /api/plugins/hypermultimedia/:documentId/session with its Authorization header when it opens a Private document.
  • REST runs resolvePrivateAccess and sets a short-lived, signed, HttpOnly, Secure, SameSite=Lax cookie with Path=/api/plugins/hypermultimedia/<documentId>. The two hosts are same-site, so image requests then carry it. CORS already allows credentials.
  • getMedia serves a Private document's media only with a valid cookie for that document, and sends Cache-Control: private, no-cache.
  • No change to the published @docs.plus/extension-hypermultimedia node views.

Duplicate

#408 step 3 drops references whose prefix document is trashed. Extend that filter here. Also drop references whose prefix document is Private and not owned by the requester (resolvePrivateAccess).

Layer: the gate belongs in the REST media controller (getMedia), next to the upload gate. Do not put access logic in lib/storage/*; those adapters only move bytes. Keep the media-read rate-limit bucket as it is.

Out of scope

Acceptance criteria

  • After a Private flip, a non-owner's request for a known media URL returns 403 or 404.
  • The owner still sees every image in their Private document after a reload.

Verify

  • Local stack: as the owner, flip a document with an image to Private, then reload. Every image must load.
  • Request the same media URL as another account, or with curl -i and no cookie. Expect 403 or 404.

Related


Generated by Claude Code

Activity

  1. added theissue type on Oct 6, 2026
  2. added
    bug
    Editor
    Security
    on Oct 6, 2026
  3. HMarzban commented on Oct 9, 2026

    @HMarzban
    CollaboratorAuthor

    Built but held, not in production. The media gate fails closed when MEDIA_COOKIE_SECRET is unset, so every owner's Private images would 404 if that env line were missed. Maintainer decision owed: set the secret first and ship as built, or approve the gate being off while the secret is unset. The cookie TTL is already 1 hour in the patch.

  4. HMarzban commented on Oct 19, 2026

    @HMarzban
    CollaboratorAuthor

    Maintainer ruling, 2026-10-19

    Set MEDIA_COOKIE_SECRET (32 or more characters) on rest-api first. Then ship the fail-closed gate as built, with the 1-hour cookie.

  5. HMarzban commented on Oct 21, 2026

    @HMarzban
    CollaboratorAuthor

    Status 2026-10-21: built, committed on main, not pushed yet.

    • Gate: getMedia serves a Private document's media only with the owner's dp_media cookie, with Cache-Control: private, no-cache. Anyone else gets 404, as for Trash. The cookie is HttpOnly, SameSite=Lax, one hour, from POST /api/plugins/hypermultimedia/session. DELETE on the same route expires it.
    • A race found and fixed. A pad can paint images from its local copy before the cookie lands, and a failed image never retries. Reproduced on the full local stack with a forced 1.5 s delay on the session POST. The webapp now retries a failed docs.plus media element once per cookie that REST sets. A failed POST spends no retry. With the retry switched off, the image stayed broken.
    • DOCX export gets the owner's cookie through a mediaCookieFor dependency, and sends it only to media-route paths. A duplicate skips media of a Private document the requester cannot read.
    • Deploy safety: MEDIA_COOKIE_SECRET is set on production (64 hex characters). The prod and staging workflows now refuse a missing or short secret, and both env templates carry the line.
    • Docs: API.md, the OpenAPI spec, ENV.md, the self-hosting guide and CONTEXT.md (Private media).
    • Tests: backend 878/878, webapp 390/390. The new gate, cookie-scope and retry tests were each proven by sabotage.

    After the push (Deploy A): an owner's Private images load after a reload, and a private window gets 404 for the same URL. Tabs open across the deploy run old code until a reload.

  6. HMarzban commented on Oct 21, 2026

    @HMarzban
    CollaboratorAuthor

    Deployed to production on 2026-10-21 and checked.

    • Both rest-api replicas have MEDIA_COOKIE_SECRET set (64 characters), and the logs show no "not set" warning.
    • A media request on a Private document with no cookie gets 404 "Document not found". That answer comes from the gate, before the store. The same request on a public document gets the store's "File not found".
    • A forged dp_media cookie on a Private document also gets 404.
    • POST /api/plugins/hypermultimedia/session with no token gets 401.
    • DELETE on the same route gets 204 and expires the cookie (Max-Age=0; HttpOnly; Secure; SameSite=Lax).

    Not checked on production yet: an owner loading a real Private image. Today, neither Private document on production holds media. That path passed in a local browser check, including the cold-load retry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    EditorSecuritybug

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions