Skip to content

Commit 7810e5a

Browse files
maria-rcksclaude
andcommitted
fix(server): harden the GitHub media proxy and fall back to the direct URL
Review follow-ups on the private repository media fix: the cache key held a literal NUL byte, which made the new module a binary blob to git; a name no `decodeURIComponent` accepts killed asset issuance; and the proxied bytes carried GitHub's content type without the `nosniff` and SVG policy every other asset gets. The proxy now serves only pictures and recordings, naming them from the file when the raw host says `application/octet-stream`, forwards an upstream refusal instead of turning it into 502, and follows GitHub's redirect itself so the credential provably stops at GitHub. A signed URL that cannot be minted — an older server, or one with no route to GitHub — now falls back to the direct link instead of a dead placeholder, so public media keeps working exactly as it did. Authored attributes, the original link, and the legacy attachment and Git LFS hosts are handled too. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent dfd5009 commit 7810e5a

5 files changed

Lines changed: 78 additions & 14 deletions

File tree

‎apps/server/src/assets/AssetAccess.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,10 +1089,24 @@ describe("AssetAccess", () => {
10891089
url: "https://raw.githubusercontent.com/owner/repo/main/docs/shot.png",
10901090
});
10911091

1092+
// The pre-`user-attachments` form, Git LFS bytes, and a name no `decodeURIComponent`
1093+
// accepts all arrive from real bodies.
1094+
const legacy = yield* issue("https://github.com/owner/repo/assets/45952064/1a1842fb");
1095+
expect(yield* resolve(legacy.relativeUrl)).toMatchObject({
1096+
url: "https://github.com/owner/repo/assets/45952064/1a1842fb",
1097+
});
1098+
const lfs = yield* issue("https://media.githubusercontent.com/media/owner/repo/main/a.mp4");
1099+
expect(yield* resolve(lfs.relativeUrl)).toMatchObject({
1100+
url: "https://media.githubusercontent.com/media/owner/repo/main/a.mp4",
1101+
});
1102+
const awkward = yield* issue("https://raw.githubusercontent.com/o/r/main/100%.png");
1103+
expect(awkward.relativeUrl.endsWith("/100%25.png")).toBe(true);
1104+
10921105
for (const url of [
10931106
"https://example.com/shot.png",
10941107
"http://github.com/user-attachments/assets/1a1842fb",
10951108
"https://github.com/owner/repo/pull/1",
1109+
"https://github.com/owner/repo/blob/main/",
10961110
]) {
10971111
expect((yield* issue(url).pipe(Effect.flip))._tag).toBe(
10981112
"AssetGitHubMediaUrlValidationError",
1.48 KB
Binary file not shown.

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,16 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
15881588
/** Caps the box height in rem while keeping the image's ratio; 30 by default. */
15891589
readonly maxHeightRem?: number | undefined;
15901590
readonly style?: CSSProperties | undefined;
1591+
readonly className?: string | undefined;
1592+
/** Sanitized authored attributes (`id`, `align`, …) that fragment links and layout rely on. */
1593+
readonly imageProps?:
1594+
| Omit<ComponentProps<"img">, "src" | "alt" | "className" | "style">
1595+
| undefined;
1596+
/** Where the media also lives on the web, for the failure state's escape hatch. */
1597+
readonly originalUrl?: string | undefined;
1598+
/** Loaded instead of the failure state when no URL can be signed, such as against a server
1599+
too old to know this resource. Only safe when the client can reach it directly. */
1600+
readonly fallbackSrc?: string | undefined;
15911601
readonly workspaceRoot?: string | undefined;
15921602
readonly onImageExpand?: ((preview: ExpandedImagePreview) => void) | undefined;
15931603
}) {
@@ -1600,9 +1610,15 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
16001610
: resource._tag === "workspace-file" && props.workspaceRoot
16011611
? `${props.workspaceRoot.replace(/[\\/]+$/, "")}/${resource.path}`
16021612
: undefined;
1603-
const reference = path ? mediaFileReference(path, props.workspaceRoot) : undefined;
1604-
const relativePath = reference?.relativePath;
1605-
const src = assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : null;
1613+
const reference = path
1614+
? mediaFileReference(path, props.workspaceRoot)
1615+
: props.originalUrl
1616+
? mediaUrlReference(props.originalUrl)
1617+
: undefined;
1618+
const relativePath = reference?.kind === "file" ? reference.relativePath : undefined;
1619+
const fallbackSrc = assetUrl._tag === "Failure" ? props.fallbackSrc : undefined;
1620+
const src =
1621+
assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : (fallbackSrc ?? null);
16061622
// The server reads the pixel size from the file header, so the slot can be
16071623
// the image's final box instead of a 16:9 guess. An authored size wins; a
16081624
// caller's height cap shrinks the box while keeping the ratio.
@@ -1638,9 +1654,10 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
16381654
return (
16391655
<ChatMarkdownVideo
16401656
src={src}
1641-
sourceFailed={assetUrl._tag === "Failure"}
1657+
sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
16421658
alt={props.alt}
16431659
copyMarkdown={props.copyMarkdown}
1660+
originalUrl={props.originalUrl}
16441661
style={props.style}
16451662
mediaIdentity={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
16461663
onRetry={refreshAssetUrl}
@@ -1653,13 +1670,15 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
16531670
<ChatMarkdownImage
16541671
key={JSON.stringify([props.environmentId, props.resource, props.srcFragment])}
16551672
src={src}
1656-
sourceFailed={assetUrl._tag === "Failure"}
1673+
sourceFailed={assetUrl._tag === "Failure" && fallbackSrc === undefined}
16571674
alt={props.alt}
16581675
copyMarkdown={props.copyMarkdown}
16591676
standalone={props.standalone ?? true}
1660-
className={CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME}
1677+
className={cn(CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME, props.className)}
16611678
style={style}
1679+
imageProps={props.imageProps}
16621680
actionsSource={actionsSource}
1681+
originalUrl={props.originalUrl}
16631682
onImageExpand={props.onImageExpand}
16641683
/>
16651684
);
@@ -3137,7 +3156,13 @@ const CHAT_MARKDOWN_COMPONENTS = {
31373156
kind={kind}
31383157
copyMarkdown={copyMarkdown}
31393158
standalone={standalone}
3159+
className={className}
31403160
style={authoredSizeStyle}
3161+
imageProps={imageProps}
3162+
originalUrl={imageSource.uri}
3163+
// A server too old to sign this resource, or one with no route to GitHub, still leaves
3164+
// the public half of these working exactly as it did before.
3165+
fallbackSrc={imageSource.uri}
31413166
onImageExpand={imageExpand}
31423167
/>
31433168
);

‎apps/web/src/components/pullRequest/PullRequestMarkdown.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ function PullRequestGitHubVideo({
3434
const resource = useMemo<AssetResource>(() => ({ _tag: "github-media", cwd, url }), [cwd, url]);
3535
const assetUrl = useAssetUrlState(environmentId, resource);
3636
const refreshAssetUrl = useAssetUrlRefresh(environmentId, resource);
37+
// A server too old to sign this resource, or one with no route to GitHub, still leaves a
38+
// public repository's video playing exactly as it did before.
39+
const src = assetUrl._tag === "Success" ? assetUrl.url : assetUrl._tag === "Failure" ? url : null;
3740
return (
3841
<MediaVideoPlayer
39-
src={assetUrl._tag === "Success" ? assetUrl.url : null}
40-
sourceFailed={assetUrl._tag === "Failure"}
42+
src={src}
4143
originalUrl={url}
4244
label="Pull request video"
4345
className="w-full"

‎packages/shared/src/githubMedia.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44
* becomes `github.com/user-attachments/assets/<id>`, and a file committed alongside the code
55
* becomes a `raw.githubusercontent.com` path — so the renderer, which carries no GitHub session,
66
* draws a broken image where the reviewer expects the evidence. The server can fetch these with
7-
* the repository's `gh` credential and hand the bytes back.
7+
* the `gh` credential and hand the bytes back.
88
*
99
* Only hosts where a credential is what decides the answer belong here. `objects.githubusercontent.com`
1010
* and the `private-user-images` links GitHub's own HTML carries are already signed and load on their
1111
* own, and a token does nothing for them once that signature expires.
1212
*/
1313

1414
const RAW_HOST = "raw.githubusercontent.com";
15+
/** Git LFS pointers resolve here, which is where an LFS-tracked screenshot's bytes live. */
16+
const LFS_HOST = "media.githubusercontent.com";
1517
const ATTACHMENT_PATH_PATTERN = /^\/user-attachments\/assets\/[\w-]+$/u;
18+
/** What GitHub wrote into a body before `user-attachments`; older descriptions still carry it. */
19+
const LEGACY_ATTACHMENT_PATH_PATTERN = /^\/[^/]+\/[^/]+\/assets\/\d+\/[\w-]+$/u;
1620
/** `blob` and `raw` both address file bytes; `raw` is the one the token is honoured on. */
17-
const REPOSITORY_FILE_PATTERN = /^\/([^/]+)\/([^/]+)\/(?:raw|blob)\/(.+)$/u;
21+
const REPOSITORY_FILE_PATTERN = /^\/([^/]+)\/([^/]+)\/(?:raw|blob)\/(.*[^/])$/u;
22+
23+
/** Port, userinfo, and fragment say nothing about which bytes GitHub will serve. */
24+
function canonicalUrl(host: string, url: URL): string {
25+
return `https://${host}${url.pathname}${url.search}`;
26+
}
1827

1928
/**
2029
* The URL to fetch with a GitHub credential for `source`, or null when the source is not
@@ -29,19 +38,33 @@ export function githubMediaFetchUrl(source: string): string | null {
2938
}
3039
if (url.protocol !== "https:") return null;
3140
const host = url.hostname.toLowerCase();
32-
if (host === RAW_HOST) return url.toString();
41+
if (host === RAW_HOST || host === LFS_HOST) return canonicalUrl(host, url);
3342
if (host !== "github.com" && host !== "www.github.com") return null;
34-
if (ATTACHMENT_PATH_PATTERN.test(url.pathname)) return `https://github.com${url.pathname}`;
43+
if (
44+
ATTACHMENT_PATH_PATTERN.test(url.pathname) ||
45+
LEGACY_ATTACHMENT_PATH_PATTERN.test(url.pathname)
46+
) {
47+
return `https://github.com${url.pathname}`;
48+
}
3549
const repositoryFile = REPOSITORY_FILE_PATTERN.exec(url.pathname);
3650
// `?raw=true` is how the web UI spells "the bytes, not the page"; the raw host needs no query.
3751
return repositoryFile
3852
? `https://${RAW_HOST}/${repositoryFile[1]}/${repositoryFile[2]}/${repositoryFile[3]}`
3953
: null;
4054
}
4155

42-
/** Last path segment, for the signed URL's display name and the download filename. */
56+
/**
57+
* Last path segment, for the signed URL's display name. A percent sequence GitHub accepts but
58+
* `decodeURIComponent` rejects is left encoded rather than failing the whole asset.
59+
*/
4360
export function githubMediaFileName(fetchUrl: string): string {
4461
const segment = new URL(fetchUrl).pathname.split("/").pop() ?? "";
45-
const name = decodeURIComponent(segment).replace(/[\\/\0]/gu, "");
62+
let decoded: string;
63+
try {
64+
decoded = decodeURIComponent(segment);
65+
} catch {
66+
decoded = segment;
67+
}
68+
const name = decoded.replace(/[\p{Cc}\\/]/gu, "");
4669
return name.length > 0 ? name : "github-media";
4770
}

0 commit comments

Comments
 (0)