Repository navigation
fix: signed media URLs in Studio previews (#383) - #384
Merged
Merged
Conversation
Studio previews pointed blob images at the site's `/media/<sha16>/<slug>.<ext>`, which only the site's host serves; on Studio's host the SPA answered with HTML, so previews showed no images whenever Studio and the site have different hosts. Previews now name a short-lived signed URL Studio serves itself: `GET /api/sites/:siteId/preview-media/:assetId/<slug>.<ext>?v=&exp=&sig=`. The signature is the credential: the served preview is sandboxed (opaque origin), so the Lax session cookie is not sent and `CORP: same-origin` cannot match, which rules out the private `/assets/:id/content` route. - core-ts: `preview-media.ts`, HMAC-SHA256 over `site|asset|sha16|exp` with a key derived by HKDF from the preview secret or else the auth secret; 6-hour bucketed expiry so re-renders keep the same URL. - api: the public `preview-media` route (constant-time verify, exp in the future and at most a day ahead, asset of the site, sha prefix, blob only, 410 after a takedown, bare 403/404 otherwise; CORP cross-origin, private max-age up to the expiry, no-referrer, noindex); `previewMediaFor` loads the assets a node refers to and presigns them for entry, draft, fragment, page (Build view) and term-archive previews; the preview CSP also allows a local http Studio's own origin. - astro-renderer: `MediaContext.previewAssetUrl`, applied in one place (`preview-media.ts`) for asset images, links, players, covers, section images and plain `/media/` paths in Markdown and legacy `src`. - cloudflare: the admin Worker passes CLOUDHAL_PREVIEW_SECRET or BETTER_AUTH_SECRET; the playground signs per session and its Worker routes these URLs by a `ps` token, since the sandboxed preview sends no cookie. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #383
Problem
Preview HTML pointed blob images at the site-relative
/media/<sha16>/<slug>.<ext>. Only the site's host serves that path; on Studio's host (studio.cloudhal.com, and a localcloudhal studio) the SPA fallback answered with HTML. So previews showed no cover, body or section images whenever Studio and the site have different hosts.Why not Studio's private
/assets/:id/contentrouteGET …/preview) hassandboxin its CSP, and the editor'ssrcdocframe usessandbox="". Both give the document an opaque origin.SameSite=Laxsession cookie is not sent and the private route answers 401.Cross-Origin-Resource-Policy: same-origin, which can never match an opaque origin either.Design: the signature is the credential
Other products either make asset URLs public (Sanity) or sign them for a short time (Storyblok, Contentful embargoed assets, Cloudflare Images). This PR takes the second approach, served from Studio's own origin.
@cloudhal/core-ts,preview-media.ts, WebCrypto only):site|assetId|sha16|exp, encoded as base64url.info: "cloudhal preview-media v1") fromCLOUDHAL_PREVIEW_SECRET, or elseBETTER_AUTH_SECRET.cloudhal studio, tests) it uses a random key for the process.exp = ceil(now/6h)*6h + 6h. Re-renders give identical URLs, so the browser cache hits. A URL is valid for 6 to 12 hours.GET /api/sites/:siteId/preview-media/:assetId/<slug>.<ext>?v=&exp=&sig=:publicinROUTE_SCOPES, so no session is needed.verify, constant time);expin the future and at most 24 h ahead; the asset belongs to the site;vequals its sha prefix; blob storage only./content(assetBytesResponse, which also shrinksassets.ts).CORP: cross-origin,cache-control: private, max-age=<remaining, ≤ 1 day>,referrer-policy: no-referrer,x-robots-tag: noindex,nosniff, plus the existing SVG sandbox CSP and disposition rules. Nothing goes in a shared cache.@cloudhal/astro-renderer):MediaContext.previewAssetUrlhook, applied in one place,src/preview-media.ts:shownAssetUrlcovers asset images, links, video and audio, covers and section images.previewMediaSrccovers plain/media/…paths (Markdown images and links, legacysrc, term images). It resolves them to their asset with the existingMEDIA_ROUTEparser, after a length check.previewMediaForcollectsassetIds,asset:<id>references (Markdown, directives, surfaces) and/media/<prefix>/paths from a node's JSON. It uses linear scans withindexOf, then loads the assets and presigns their URLs.preview-fragment, page previews (which also feed the Build view'ssrcdoc) and term-archive previews.srcdoc.img-srcgains'self' http://localhost:* http://127.0.0.1:*. The served preview also gets the request's own origin when that origin is http, so a local Studio loads the images.https:already covers deployments.PLAYGROUND_SECRETand the session id, so they survive an evicted Durable Object./api/sites/*/preview-media/*GETs that carry no cookie by aps=<ulid>.<mac>token, which has its own MAC key. It is accepted on that path only and never works as the cookie.Deviations and notes for review
CLOUDHAL_PREVIEW_SECRETstill signs preview media.ApiDeps.previewMedia { secret?, query? }is new. The admin Worker passesCLOUDHAL_PREVIEW_SECRET || BETTER_AUTH_SECRET, so the auth secret itself never entersApiDeps.previewDoc, used for Build chat'slook()for the vision model and for MCPpreview_page. It renders for an agent or a headless browser, not for Studio, and prefers the signed site URL.build-eval.ts, which renders without assets.srcdocpreview on a plain-http host other than localhost (for examplehttp://nas:4321) still blocks these images through the document's meta CSP. The served preview allows them through the header. Deployments are https.Tests (run locally, single files)
core-ts/test/preview-media.test.ts: round trip; tampered site, asset, sha, exp or signature; another key; expired; more than a day ahead; bucket stability; no secret.api/test/preview-media-api.test.ts, on sqlite and Miniflare D1 (12/12):/media/…image name only signed URLs and no bare/media/src. The page preview signs section images.CORP: cross-originand a private max-age of 6 to 12 h. A second instance with the same secret signs the same URL.exp, an expired URL or another key gives a bare 403. Another site's asset gives a bare 404. A taken-down file gives 410. The process-key fallback works.astro-renderer/test/preview-media.test.ts: a cover byassetId, Markdown/media/plus anasset:image and link, section images by id and by legacy src; external, unknown and.sha256paths are untouched; no hook or no preview means no change; the CSP is checked.cloudflare/test/playground.test.ts:psrouting works on preview-media GETs only, refuses POST, refuses use as a cookie or with the cookie's MAC, and refuses expired sessions.route-scopes,api/playground,assets-api(sqlite), and the renderer'smedia,preview,carousel,fieldsandsurfacestests. Typecheck is clean in core-ts, astro-renderer, api and cloudflare.size:checkis OK (baseline tightened:assets.ts833 → 825).🤖 Generated with Claude Code