Skip to content

Commit b9c8c4e

Browse files
committed
fix(web): show pull request images and videos from private repositories
GitHub serves user-attachments uploads from a private repository only to a signed-in reader, so the bare <img> the PR body carries answered 404 and the panel showed an "Image unavailable" chip. Videos dropped as bare links failed the same way. The web client now asks the environment for a signed asset URL for such links. The server reads the gh token through the CLI, asks GitHub for the upload with it, and answers with a redirect to GitHub's own short-lived download, so no bytes are relayed and remote clients fetch straight from GitHub's storage. Without a token the request is anonymous, which keeps public repositories working exactly as before.
1 parent 061543e commit b9c8c4e

14 files changed

Lines changed: 460 additions & 23 deletions

File tree

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts";
2222
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
2323
import { assetFileResponse } from "../http.ts";
2424
import { ASSET_ROUTE_PREFIX, issueAssetUrl, resolveAsset } from "./AssetAccess.ts";
25+
import * as GitHubAttachmentResolver from "./GitHubAttachmentResolver.ts";
2526
import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
2627
import { openMediaFile } from "./MediaFile.ts";
2728
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
@@ -43,10 +44,44 @@ const testLayer = Layer.mergeAll(
4344
Layer.provide(T3ProjectFileLoader.layer),
4445
),
4546
NativeAppIconResolver.layer.pipe(Layer.provide(configLayer)),
47+
Layer.succeed(GitHubAttachmentResolver.GitHubAttachmentResolver, {
48+
resolve: (url) =>
49+
Effect.succeed(
50+
url.endsWith("missing")
51+
? null
52+
: `https://signed.example/download?for=${encodeURIComponent(url)}`,
53+
),
54+
}),
4655
ServerSecretStore.layer.pipe(Layer.provide(configLayer)),
4756
).pipe(Layer.provideMerge(NodeServices.layer));
4857

58+
function splitAssetUrl(relativeUrl: string): { token: string; name: string } {
59+
const suffix = relativeUrl.slice(`${ASSET_ROUTE_PREFIX}/`.length);
60+
const separator = suffix.indexOf("/");
61+
return { token: suffix.slice(0, separator), name: suffix.slice(separator + 1) };
62+
}
63+
4964
describe("AssetAccess", () => {
65+
it.effect("redirects a GitHub attachment to the download its resolver found", () =>
66+
Effect.gen(function* () {
67+
const url = "https://github.com/user-attachments/assets/0b1f6f2e-3c4d-4e5f-8a9b-0c1d2e3f4a5b";
68+
const issued = yield* issueAssetUrl({ resource: { _tag: "github-attachment", url } });
69+
const { token, name } = splitAssetUrl(issued.relativeUrl);
70+
expect(name).toBe("0b1f6f2e-3c4d-4e5f-8a9b-0c1d2e3f4a5b");
71+
expect(yield* resolveAsset(token, name)).toEqual({
72+
kind: "redirect",
73+
location: `https://signed.example/download?for=${encodeURIComponent(url)}`,
74+
});
75+
expect(yield* resolveAsset(`${token}tampered`, name)).toBeNull();
76+
77+
const missing = yield* issueAssetUrl({
78+
resource: { _tag: "github-attachment", url: `${url}-missing` },
79+
});
80+
const missingUrl = splitAssetUrl(missing.relativeUrl);
81+
expect(yield* resolveAsset(missingUrl.token, missingUrl.name)).toBeNull();
82+
}).pipe(Effect.provide(testLayer)),
83+
);
84+
5085
it.effect("issues exact URLs for media and browser documents outside the workspace", () =>
5186
Effect.gen(function* () {
5287
const fs = yield* FileSystem.FileSystem;
@@ -228,7 +263,7 @@ describe("AssetAccess", () => {
228263
suffix.slice(0, separator),
229264
suffix.slice(separator + 1),
230265
);
231-
if (!asset) throw new Error("Expected a resolved media file");
266+
if (asset?.kind !== "file") throw new Error("Expected a resolved media file");
232267

233268
yield* fs.rename(filePath, savedPath);
234269
yield* fs.symlink(secretPath, filePath);
@@ -391,7 +426,7 @@ describe("AssetAccess", () => {
391426
const name = suffix.slice(separator + 1);
392427
yield* fs.writeFileString(filePath, "in-place edit");
393428
const edited = yield* resolveAsset(token, name);
394-
if (!edited) throw new Error("Expected the edited media file");
429+
if (edited?.kind !== "file") throw new Error("Expected the edited media file");
395430
const editedResponse = HttpServerResponse.toWeb(yield* assetFileResponse(edited));
396431
expect(yield* Effect.promise(() => editedResponse.text())).toBe("in-place edit");
397432

@@ -407,7 +442,7 @@ describe("AssetAccess", () => {
407442
renewedSuffix.slice(0, renewedSeparator),
408443
renewedSuffix.slice(renewedSeparator + 1),
409444
);
410-
if (!renewedAsset) throw new Error("Expected the replacement media file");
445+
if (renewedAsset?.kind !== "file") throw new Error("Expected the replacement media file");
411446
const renewedResponse = HttpServerResponse.toWeb(yield* assetFileResponse(renewedAsset));
412447
expect(yield* Effect.promise(() => renewedResponse.text())).toBe("replacement");
413448
yield* fs.remove(filePath);

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { parseAttachmentFileExtension, resolveAttachmentPathById } from "../atta
4848
import * as ServerConfig from "../config.ts";
4949
import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts";
5050
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
51+
import * as GitHubAttachmentResolver from "./GitHubAttachmentResolver.ts";
5152
import * as NativeAppIconResolver from "./NativeAppIconResolver.ts";
5253
import { openMediaFile, readMediaFileHeader, type OpenMediaFile } from "./MediaFile.ts";
5354

@@ -133,21 +134,30 @@ const AssetClaimsSchema = Schema.Union([
133134
app: ToolActivityNativeAppReference,
134135
expiresAt: Schema.Number,
135136
}),
137+
Schema.Struct({
138+
version: Schema.Literal(1),
139+
kind: Schema.Literal("github-attachment"),
140+
url: Schema.String,
141+
expiresAt: Schema.Number,
142+
}),
136143
]);
137144
type AssetClaims = typeof AssetClaimsSchema.Type;
138145

139146
const AssetClaimsJson = Schema.fromJsonString(AssetClaimsSchema);
140147
const decodeAssetClaims = Schema.decodeUnknownOption(AssetClaimsJson);
141148
const encodeAssetClaims = Schema.encodeSync(AssetClaimsJson);
142149

143-
export type ResolvedAsset = {
144-
readonly kind: "file";
145-
readonly path: string;
146-
readonly download?: boolean;
147-
readonly fileName?: string;
148-
readonly mimeType?: string;
149-
readonly file?: OpenMediaFile;
150-
};
150+
export type ResolvedAsset =
151+
| {
152+
readonly kind: "file";
153+
readonly path: string;
154+
readonly download?: boolean;
155+
readonly fileName?: string;
156+
readonly mimeType?: string;
157+
readonly file?: OpenMediaFile;
158+
}
159+
// The bytes live elsewhere; the client is sent to a URL that is valid for a short time.
160+
| { readonly kind: "redirect"; readonly location: string };
151161

152162
function decodeClaims(encodedPayload: string): AssetClaims | null {
153163
try {
@@ -577,6 +587,16 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
577587
fileName = "native-app-icon.png";
578588
break;
579589
}
590+
case "github-attachment": {
591+
claims = {
592+
version: 1,
593+
kind: "github-attachment",
594+
url: input.resource.url,
595+
expiresAt,
596+
};
597+
fileName = input.resource.url.slice(input.resource.url.lastIndexOf("/") + 1);
598+
break;
599+
}
580600
}
581601

582602
const secretStore = yield* ServerSecretStore.ServerSecretStore;
@@ -683,6 +703,12 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
683703
return iconPath ? ({ kind: "file", path: iconPath } satisfies ResolvedAsset) : null;
684704
}
685705

706+
if (claims.kind === "github-attachment") {
707+
const attachmentResolver = yield* GitHubAttachmentResolver.GitHubAttachmentResolver;
708+
const location = yield* attachmentResolver.resolve(claims.url);
709+
return location ? ({ kind: "redirect", location } satisfies ResolvedAsset) : null;
710+
}
711+
686712
const decodedPath = decodeRelativePath(relativePath);
687713
if (decodedPath === null) return null;
688714
const path = yield* Path.Path;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { VcsProcessExitError } from "@t3tools/contracts";
3+
import { describe, expect, it } from "@effect/vitest";
4+
import * as Effect from "effect/Effect";
5+
import * as Layer from "effect/Layer";
6+
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
7+
import { ChildProcessSpawner } from "effect/unstable/process";
8+
9+
import * as ServerConfig from "../config.ts";
10+
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
11+
import * as VcsProcess from "../vcs/VcsProcess.ts";
12+
import * as GitHubAttachmentResolver from "./GitHubAttachmentResolver.ts";
13+
14+
const ATTACHMENT_URL =
15+
"https://github.com/user-attachments/assets/0b1f6f2e-3c4d-4e5f-8a9b-0c1d2e3f4a5b";
16+
const SIGNED_URL =
17+
"https://github-production-user-asset-6210df.s3.amazonaws.com/1/2.png?X-Amz-Expires=300&X-Amz-Signature=abc";
18+
19+
interface RecordedRequest {
20+
readonly url: string;
21+
readonly authorization: string | undefined;
22+
}
23+
24+
function makeResolverLayer(options: {
25+
readonly token: string | null;
26+
readonly respond: () => Response;
27+
}) {
28+
const ghCalls: Array<ReadonlyArray<string>> = [];
29+
const requests: RecordedRequest[] = [];
30+
const vcsProcessLayer = Layer.succeed(VcsProcess.VcsProcess, {
31+
run: (input) => {
32+
ghCalls.push(input.args);
33+
if (options.token === null) {
34+
return Effect.fail(
35+
new VcsProcessExitError({
36+
operation: input.operation,
37+
command: input.command,
38+
cwd: input.cwd,
39+
exitCode: 1,
40+
detail: "not logged in",
41+
failureKind: "authentication",
42+
}),
43+
);
44+
}
45+
return Effect.succeed({
46+
exitCode: ChildProcessSpawner.ExitCode(0),
47+
stdout: `${options.token}\n`,
48+
stderr: "",
49+
stdoutTruncated: false,
50+
stderrTruncated: false,
51+
});
52+
},
53+
});
54+
const httpClientLayer = Layer.succeed(
55+
HttpClient.HttpClient,
56+
HttpClient.make((request) =>
57+
Effect.sync(() => {
58+
requests.push({ url: request.url, authorization: request.headers.authorization });
59+
return HttpClientResponse.fromWeb(request, options.respond());
60+
}),
61+
),
62+
);
63+
const configLayer = ServerConfig.ServerConfig.layerTest(process.cwd(), {
64+
prefix: "t3-github-attachment-test-",
65+
});
66+
const layer = GitHubAttachmentResolver.layer.pipe(
67+
Layer.provide(GitHubCli.layer),
68+
Layer.provide(vcsProcessLayer),
69+
Layer.provide(httpClientLayer),
70+
Layer.provide(configLayer),
71+
Layer.provideMerge(NodeServices.layer),
72+
);
73+
return { layer, ghCalls, requests };
74+
}
75+
76+
const redirectTo = (location: string) => () =>
77+
new Response(null, { status: 302, headers: { location } });
78+
79+
describe("GitHubAttachmentResolver", () => {
80+
it.effect("sends the gh token and returns the signed redirect target", () => {
81+
const { layer, ghCalls, requests } = makeResolverLayer({
82+
token: "ghp_test",
83+
respond: redirectTo(SIGNED_URL),
84+
});
85+
return Effect.gen(function* () {
86+
const resolver = yield* GitHubAttachmentResolver.GitHubAttachmentResolver;
87+
expect(yield* resolver.resolve(ATTACHMENT_URL)).toBe(SIGNED_URL);
88+
expect(ghCalls).toEqual([["auth", "token", "--hostname", "github.com"]]);
89+
expect(requests).toEqual([{ url: ATTACHMENT_URL, authorization: "token ghp_test" }]);
90+
}).pipe(Effect.provide(layer));
91+
});
92+
93+
it.effect("reads the token once for a body full of attachments", () => {
94+
const { layer, ghCalls, requests } = makeResolverLayer({
95+
token: "ghp_test",
96+
respond: redirectTo(SIGNED_URL),
97+
});
98+
return Effect.gen(function* () {
99+
const resolver = yield* GitHubAttachmentResolver.GitHubAttachmentResolver;
100+
yield* resolver.resolve(ATTACHMENT_URL);
101+
yield* resolver.resolve(`${ATTACHMENT_URL}2`);
102+
expect(ghCalls).toHaveLength(1);
103+
expect(requests).toHaveLength(2);
104+
}).pipe(Effect.provide(layer));
105+
});
106+
107+
it.effect("asks anonymously when gh has no token", () => {
108+
const { layer, requests } = makeResolverLayer({ token: null, respond: redirectTo(SIGNED_URL) });
109+
return Effect.gen(function* () {
110+
const resolver = yield* GitHubAttachmentResolver.GitHubAttachmentResolver;
111+
expect(yield* resolver.resolve(ATTACHMENT_URL)).toBe(SIGNED_URL);
112+
expect(requests).toEqual([{ url: ATTACHMENT_URL, authorization: undefined }]);
113+
}).pipe(Effect.provide(layer));
114+
});
115+
116+
it.effect("yields nothing when GitHub answers with a page instead of a redirect", () => {
117+
const { layer } = makeResolverLayer({
118+
token: "ghp_test",
119+
respond: () => new Response("Not Found", { status: 404 }),
120+
});
121+
return Effect.gen(function* () {
122+
const resolver = yield* GitHubAttachmentResolver.GitHubAttachmentResolver;
123+
expect(yield* resolver.resolve(ATTACHMENT_URL)).toBeNull();
124+
}).pipe(Effect.provide(layer));
125+
});
126+
127+
it.effect("yields nothing for a redirect that is not https", () => {
128+
const { layer } = makeResolverLayer({
129+
token: "ghp_test",
130+
respond: redirectTo("http://example.com/asset.png"),
131+
});
132+
return Effect.gen(function* () {
133+
const resolver = yield* GitHubAttachmentResolver.GitHubAttachmentResolver;
134+
expect(yield* resolver.resolve(ATTACHMENT_URL)).toBeNull();
135+
}).pipe(Effect.provide(layer));
136+
});
137+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as Context from "effect/Context";
2+
import * as Duration from "effect/Duration";
3+
import * as Effect from "effect/Effect";
4+
import * as Layer from "effect/Layer";
5+
import * as Option from "effect/Option";
6+
import { FetchHttpClient, HttpClient } from "effect/unstable/http";
7+
8+
import * as ServerConfig from "../config.ts";
9+
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
10+
11+
const GITHUB_HOST = "github.com";
12+
const TOKEN_CACHE_TTL = Duration.minutes(5);
13+
const RESOLVE_TIMEOUT = Duration.seconds(15);
14+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
15+
16+
/**
17+
* Turns a GitHub upload link into the time-limited download GitHub answers a
18+
* signed-in reader with. Only the redirect target is read; the bytes go from
19+
* GitHub's storage straight to the client.
20+
*/
21+
export class GitHubAttachmentResolver extends Context.Service<
22+
GitHubAttachmentResolver,
23+
{
24+
/** The signed download URL, or `null` when GitHub refuses or answers with a page. */
25+
readonly resolve: (url: string) => Effect.Effect<string | null>;
26+
}
27+
>()("t3/assets/GitHubAttachmentResolver") {}
28+
29+
function isHttpsUrl(value: string): boolean {
30+
try {
31+
return new URL(value).protocol === "https:";
32+
} catch {
33+
return false;
34+
}
35+
}
36+
37+
/** @public Service construction is part of the canonical Effect module API. */
38+
export const make = Effect.gen(function* () {
39+
const config = yield* ServerConfig.ServerConfig;
40+
const github = yield* GitHubCli.GitHubCli;
41+
const httpClient = yield* HttpClient.HttpClient;
42+
43+
// `gh` owns the credential, so login state and GH_TOKEN keep working as they do for
44+
// every other GitHub call. Cached so a body full of screenshots does not spawn one
45+
// process per image. Without a token the request goes out anonymously, which is
46+
// what a public repository needs and what a private one already failed with.
47+
const token = yield* github
48+
.execute({ cwd: config.stateDir, args: ["auth", "token", "--hostname", GITHUB_HOST] })
49+
.pipe(
50+
Effect.map((output) => output.stdout.trim() || null),
51+
Effect.orElseSucceed(() => null),
52+
Effect.cachedWithTTL(TOKEN_CACHE_TTL),
53+
);
54+
55+
const resolve = Effect.fn("GitHubAttachmentResolver.resolve")(function* (url: string) {
56+
const authorization = yield* token;
57+
return yield* httpClient
58+
.get(url, {
59+
headers: authorization === null ? {} : { authorization: `token ${authorization}` },
60+
})
61+
.pipe(
62+
Effect.map((response) => {
63+
const location = response.headers.location?.trim();
64+
return REDIRECT_STATUSES.has(response.status) &&
65+
location !== undefined &&
66+
isHttpsUrl(location)
67+
? location
68+
: null;
69+
}),
70+
Effect.scoped,
71+
Effect.timeoutOption(RESOLVE_TIMEOUT),
72+
Effect.map(Option.getOrNull),
73+
Effect.tapError((cause) =>
74+
Effect.logWarning("Failed to resolve a GitHub attachment.", { url, cause }),
75+
),
76+
Effect.orElseSucceed(() => null),
77+
// The signed target is on another host and following it would download the file.
78+
Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" }),
79+
);
80+
});
81+
82+
return GitHubAttachmentResolver.of({ resolve });
83+
});
84+
85+
export const layer = Layer.effect(GitHubAttachmentResolver, make);

0 commit comments

Comments
 (0)