Skip to content

Commit 6b1d1ba

Browse files
maria-rcksclaude
andcommitted
perf(server): let a client keep GitHub media for as long as its signed URL lives
An upload GitHub hosts never changes under its URL, so the only thing a cached copy must not outlive is the signed URL that granted it. The proxy now answers with that URL's own remaining life instead of `no-store`, which is what a body full of screenshots costs on every remount, and what a video seek costs on every range request. The credential is now attached only on the four hosts it is for, rather than on anything under a GitHub domain; the absence of a credential is cached too, so an unauthenticated machine stops spawning `gh` per request; and the refusal paths carry this server's own headers. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 47854df commit 6b1d1ba

5 files changed

Lines changed: 53 additions & 17 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,8 @@ describe("AssetAccess", () => {
10811081
kind: "github-media",
10821082
url: "https://github.com/user-attachments/assets/1a1842fb-6383-492f-873c-57aa0033fa6c",
10831083
cwd: "/repo",
1084+
// The signed URL's own expiry, which is how long a client may keep the bytes.
1085+
expiresAt: attachment.expiresAt,
10841086
});
10851087

10861088
// A `blob` link addresses the page; only the raw host answers a credential with bytes.

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ export type ResolvedAsset =
165165
readonly kind: "github-media";
166166
readonly url: string;
167167
readonly cwd: string;
168+
/** When the signed URL that granted this stops working, which bounds how long a client
169+
may keep the bytes it fetched with it. */
170+
readonly expiresAt: number;
168171
};
169172

170173
function decodeClaims(encodedPayload: string): AssetClaims | null {
@@ -789,7 +792,12 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
789792
}
790793

791794
if (claims.kind === "github-media") {
792-
return { kind: "github-media", url: claims.url, cwd: claims.cwd } satisfies ResolvedAsset;
795+
return {
796+
kind: "github-media",
797+
url: claims.url,
798+
cwd: claims.cwd,
799+
expiresAt: claims.expiresAt,
800+
} satisfies ResolvedAsset;
793801
}
794802

795803
if (claims.kind === "native-app-icon") {

‎apps/server/src/assets/GitHubMediaFetch.ts‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@ import {
1313

1414
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
1515

16-
/** Hosts the credential is for. A redirect off them is answered with its own signature instead. */
17-
const GITHUB_HOST_PATTERN = /^(?:[\w-]+\.)*github(?:usercontent)?\.com$/iu;
18-
const isGitHubHost = (url: string) => {
16+
/**
17+
* Exactly the hosts the credential is for. Everything a redirect leads to — the presigned
18+
* object stores GitHub hands assets off to — authorizes with its own signature, and some of
19+
* them reject a request that also carries a bearer token.
20+
*/
21+
const CREDENTIALED_HOSTS = new Set([
22+
"github.com",
23+
"www.github.com",
24+
"raw.githubusercontent.com",
25+
"media.githubusercontent.com",
26+
]);
27+
const isCredentialedHost = (url: string) => {
1928
try {
20-
return GITHUB_HOST_PATTERN.test(new URL(url).hostname);
29+
return CREDENTIALED_HOSTS.has(new URL(url).hostname.toLowerCase());
2130
} catch {
2231
return false;
2332
}
@@ -51,14 +60,18 @@ const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inlin
5160
* The token is what `gh auth token` would print again on the next call, and it is held no longer
5261
* than a signed asset URL lives.
5362
*/
54-
const tokenCache = new Map<string, { readonly at: number; readonly token: Redacted.Redacted }>();
63+
const tokenCache = new Map<
64+
string,
65+
{ readonly at: number; readonly token: Redacted.Redacted | null }
66+
>();
5567

5668
const githubToken = Effect.fn("GitHubMediaFetch.githubToken")(function* (input: {
5769
readonly cwd: string;
5870
readonly host: string;
5971
}) {
6072
// `gh` stores a token per host, not per repository, so the directory it runs in is not part
61-
// of the answer and must not fragment the cache a client could otherwise churn.
73+
// of the answer and must not fragment the cache a client could otherwise churn. This route
74+
// pins no credential; if it ever does, the pin belongs in this key.
6275
const key = input.host;
6376
const now = yield* Clock.currentTimeMillis;
6477
const cached = tokenCache.get(key);
@@ -76,11 +89,12 @@ const githubToken = Effect.fn("GitHubMediaFetch.githubToken")(function* (input:
7689
Effect.map((output) => output.stdout.trim()),
7790
Effect.orElseSucceed(() => ""),
7891
);
79-
if (token.length === 0) return null;
8092
if (tokenCache.size >= TOKEN_CACHE_MAX_ENTRIES) {
8193
tokenCache.delete(tokenCache.keys().next().value!);
8294
}
83-
const redacted = Redacted.make(token);
95+
// The absence of a credential is cached too, or an unauthenticated machine spawns `gh` again
96+
// for every image and every video range request.
97+
const redacted = token.length === 0 ? null : Redacted.make(token);
8498
tokenCache.set(key, { at: now, token: redacted });
8599
return redacted;
86100
});
@@ -102,7 +116,7 @@ const fetchFollowingRedirects = Effect.fn("GitHubMediaFetch.fetchFollowingRedire
102116
// object URL that authorizes on its own, and the store it lives in has no business seeing
103117
// a token — deciding that from the target, not from the hop count, is what makes it so.
104118
const authorization =
105-
token !== null && isGitHubHost(target) ? `Bearer ${Redacted.value(token)}` : null;
119+
token !== null && isCredentialedHost(target) ? `Bearer ${Redacted.value(token)}` : null;
106120
const response: HttpClientResponse.HttpClientResponse = yield* httpClient
107121
.execute(
108122
HttpClientRequest.get(target).pipe(
@@ -130,7 +144,7 @@ const fetchFollowingRedirects = Effect.fn("GitHubMediaFetch.fetchFollowingRedire
130144
* only thing that distinguishes a readable private attachment from a 404.
131145
*/
132146
export const githubMediaResponse = Effect.fn("GitHubMediaFetch.githubMediaResponse")(function* (
133-
asset: { readonly url: string; readonly cwd: string },
147+
asset: { readonly url: string; readonly cwd: string; readonly expiresAt: number },
134148
requestHeaders: Record<string, string | undefined>,
135149
) {
136150
// Both media hosts are served by github.com's account, which is the host `gh` stores it under.
@@ -141,9 +155,12 @@ export const githubMediaResponse = Effect.fn("GitHubMediaFetch.githubMediaRespon
141155
if (value !== undefined) forwarded[name] = value;
142156
}
143157
const response = yield* fetchFollowingRedirects(asset.url, forwarded, token);
158+
// An upload GitHub hosts never changes under its URL, so the only thing a cached copy must
159+
// not outlive is the signed URL that granted it — which is the same bound the URL itself has.
160+
const remainingSeconds = Math.floor((asset.expiresAt - (yield* Clock.currentTimeMillis)) / 1000);
144161
const headers: Record<string, string> = {
145-
// The signed asset URL is the grant; a cached copy must not outlive it.
146-
"cache-control": "private, no-store",
162+
"cache-control":
163+
remainingSeconds > 0 ? `private, max-age=${remainingSeconds}` : "private, no-store",
147164
"x-content-type-options": "nosniff",
148165
};
149166
if (response === null) return HttpServerResponse.empty({ status: 502, headers });
@@ -167,7 +184,7 @@ export const githubMediaResponse = Effect.fn("GitHubMediaFetch.githubMediaRespon
167184
? upstreamType
168185
: (Mime.getType(githubMediaFileName(asset.url))?.toLowerCase() ?? "");
169186
if (!MEDIA_CONTENT_TYPE_PATTERN.test(contentType)) {
170-
return HttpServerResponse.empty({ status: 415 });
187+
return HttpServerResponse.empty({ status: 415, headers });
171188
}
172189
headers["content-type"] = contentType;
173190
if (contentType === SVG_CONTENT_TYPE) {

‎apps/server/src/http.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,12 @@ export const assetRouteLayer = HttpRouter.add(
395395
Effect.tapError((cause) =>
396396
Effect.logWarning("Failed to fetch GitHub media.", { url: asset.url, cause }),
397397
),
398-
Effect.orElseSucceed(() => HttpServerResponse.empty({ status: 502 })),
398+
Effect.orElseSucceed(() =>
399+
HttpServerResponse.empty({
400+
status: 502,
401+
headers: { "cache-control": "private, no-store", "x-content-type-options": "nosniff" },
402+
}),
403+
),
399404
);
400405
}
401406
return yield* assetFileResponse(

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,11 @@ export const ChatMarkdownAssetImage = memo(function ChatMarkdownAssetImage(props
16201620
const relativePath = reference?.kind === "file" ? reference.relativePath : undefined;
16211621
const fallbackSrc = assetUrl._tag === "Failure" ? props.fallbackSrc : undefined;
16221622
const src =
1623-
assetUrl._tag === "Success" ? assetUrl.url + (props.srcFragment ?? "") : (fallbackSrc ?? null);
1623+
assetUrl._tag === "Success"
1624+
? assetUrl.url + (props.srcFragment ?? "")
1625+
: fallbackSrc === undefined
1626+
? null
1627+
: fallbackSrc + (props.srcFragment ?? "");
16241628
// The server reads the pixel size from the file header, so the slot can be
16251629
// the image's final box instead of a 16:9 guess. An authored size wins; a
16261630
// caller's height cap shrinks the box while keeping the ratio.
@@ -3168,7 +3172,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
31683172
style={authoredSizeStyle}
31693173
imageProps={imageProps}
31703174
srcFragment={markdownImageSourceFragment(classifiedSrc)}
3171-
originalUrl={directUri}
3175+
originalUrl={resolveProtocolRelativeMediaUrl(directUri)}
31723176
// A pull request body draws its own boxes; keep the author's, not the workspace frame.
31733177
framed={false}
31743178
// A server too old to sign this resource, or one with no route to GitHub, still leaves

0 commit comments

Comments
 (0)