Repository navigation
Description
Context
packages/api/routes/previewMediaRoutes.ts contains the only code that can securely load a published visual preview (screenshot/video) attached to a pull request or completion comment: it checks the repository has visual previews enabled, reads the PR/comment through the user's GitHub credential (proving access), verifies the asset is one ProPR published (parsePublishedVisualPreviews), resolves GitHub's signed URL from body_html, follows at most 3 redirects to trusted hosts only, validates the content type, and bounds the body (10 MiB images, 100 MiB videos).
The upcoming MCP preview tools need exactly the same pipeline, but the logic is private to the Express handler (serve) and writes directly to res. Duplicating it would fork a security boundary. This issue extracts it into a service with no behaviour change for the HTTP routes.
Requirements
- New
packages/api/services/previewMediaFetch.tsexporting:PreviewAssociation({ kind: 'pull' | 'comment'; repository: string; number: number }),loadPublishedPreview({ association, assetId, token, octokit, fetch, reader, maxBytes? })→{ preview: PublishedVisualPreview, contentType, body: Buffer }or throwsPreviewMediaError(status, code, message),listPublishedPreviews({ association, octokit, reader })→PublishedVisualPreview[]withassetIdfor each (only trusted GitHub attachment URLs),- the existing helpers
signedMediaUrl,trustedFetchTarget,fetchMedia,boundedBodymoved unchanged.
PreviewMediaErrorcarries a stable code:PREVIEW_NOT_FOUND(404),PREVIEWS_DISABLED(404 — do not reveal policy),PREVIEW_SOURCE_REJECTED(502),PREVIEW_TOO_LARGE(413),PREVIEW_INVALID_TYPE(502),PREVIEW_UNAVAILABLE(502).previewMediaRoutes.tsbecomes a thin adapter: parse params →loadPublishedPreview→ set the same headers and send; errors map to the same status codes and JSON bodies as today ({ error: message }), and GitHub access errors still go throughhandleGitHubRepositoryAccessError.maxBytesdefaults to the current limits by media type; callers may pass a lower bound (never higher).- All existing tests in
packages/api/test/previewMediaRoutes.test.tsandpreviewMediaProjection.test.tspass unchanged; addpackages/api/test/previewMediaFetch.test.tsfor the service directly (listing, not-found, disabled repository, oversized body).
Implementation Specification
- New
packages/api/services/previewMediaFetch.ts. - Edit
packages/api/routes/previewMediaRoutes.tsto delegate. - New tests as above.
Acceptance Criteria
-
previewMediaRoutes.test.tspasses without modification. -
listPublishedPreviewsfor a PR body with two published previews and one unpublished attachment returns exactly the two, each withassetId,title,type,description. -
loadPublishedPreviewwithmaxBytes: 1024on a 2 KiB image throwsPREVIEW_TOO_LARGE. - A repository with previews disabled throws
PREVIEWS_DISABLEDwithout calling GitHub. - No new code path forwards the user's
Authorizationheader to non-github.comhosts (existing redirect test still passes).
Created by ProPR AI Planner
Activity
propr-dev commentedon Sep 28, 2026
Suggested Implementation:
Implementation Approach
Pure refactor: move the security-sensitive pipeline into a service, keep the HTTP route as an adapter. We return a
Buffer(not a stream) because both consumers already need the full bounded body (the route sends it withContent-Length; the MCP tool will downscale/base64-encode it). Keeping the existing byte limits as ceilings that callers may only lower preserves the route's guarantees while letting MCP ask for less.File:
packages/api/services/previewMediaFetch.ts(new file)Why this file: One implementation of "load a preview this user may see", shared by HTTP and MCP.
File:
packages/api/routes/previewMediaRoutes.tsWhy this file: Becomes a thin HTTP adapter; response headers and status codes are unchanged.
File:
packages/api/test/previewMediaFetch.test.ts(new file)Why this file: Tests the service contract that the MCP tools will rely on.
propr-dev commentedon Sep 29, 2026
🤖 AI processing has started for this issue using codex agent with gpt-5.6-sol model.
I'll analyze the problem and work on a solution. This may take a few minutes.
Processing Details:
codexgpt-5.6-sol2603/gpt-5.6-sol-extract-preview-media-loa-20260929-2118-az32593-epic-mcp-observability-cf6issue-2603-20260929-2118-gpt-5.6-sol-az3🔍 Track Task Execution
fix(ai): Resolve issue #2603 - Extract preview media loading from pre…
fix(ai): Resolve issue #2603 - Extract preview media loading from pre…