Skip to content

Commit f5e0f3c

Browse files
committed
fix(source-control): opt in to repository-scoped GitHub credentials
Reuse existing global Git HTTPS helpers for github.com repository requests, including multiple organisation-scoped PATs belonging to the same login. Keep existing host authentication precedence unchanged by default. Carry validated repository scope through the shared request policy, typed REST and GraphQL transports, native operations, resolver batches, server caches, and media redirects. Preserve original remote spelling for helper matching while retaining canonical identities, and keep fallback credential-cache provenance separate from operation scope. Add real Git, routing, mutation, media, identity and rotation regressions, plus opt-in documentation. Verified 586 focused tests with one explicit pre-existing Bun VM-GC exclusion, scoped typechecks and lint/format checks, fresh independent review, and eight native checks against a clean-built candidate with case-sensitive helper rules.
1 parent 2131698 commit f5e0f3c

21 files changed

Lines changed: 2739 additions & 203 deletions
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import * as Layer from "effect/Layer";
4+
import * as Redacted from "effect/Redacted";
5+
import * as HostProcess from "@t3tools/shared/HostProcess";
6+
import { HttpClient, HttpClientResponse } from "effect/http";
7+
import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials";
8+
import { githubMediaResponse } from "./GitHubMediaFetch.ts";
9+
10+
const asset = (url: string) => ({
11+
url,
12+
cwd: "/untrusted-checkout",
13+
expiresAt: Number.MAX_SAFE_INTEGER,
14+
});
15+
16+
describe("repository GitHub media credentials", () => {
17+
it.effect.each([true, false])(
18+
"keeps legacy fallback redirect behavior only with opt-in disabled: %s",
19+
(enabled) => {
20+
const authorizations: Array<string | undefined> = [];
21+
return Effect.gen(function* () {
22+
yield* githubMediaResponse(
23+
asset("https://raw.githubusercontent.com/OwnerA/Repo/main/image.png"),
24+
{},
25+
);
26+
expect(authorizations).toEqual([
27+
"Bearer fake-fallback",
28+
enabled ? undefined : "Bearer fake-fallback",
29+
]);
30+
}).pipe(
31+
Effect.provide(
32+
Layer.mock(GitHubCredentials.GitHubCredentials)({
33+
get: (host, repository) => {
34+
expect(repository).toBe("OwnerA/Repo");
35+
// A repository helper miss returns the legacy credential without helper provenance.
36+
return Effect.succeed({
37+
host,
38+
source: "settings",
39+
fingerprint: "default",
40+
token: Redacted.make("fake-fallback"),
41+
});
42+
},
43+
}),
44+
),
45+
Effect.provideService(
46+
HttpClient.HttpClient,
47+
HttpClient.make((request) => {
48+
authorizations.push(request.headers.authorization);
49+
return Effect.succeed(
50+
HttpClientResponse.fromWeb(
51+
request,
52+
new Response(
53+
null,
54+
authorizations.length === 1
55+
? {
56+
status: 302,
57+
headers: {
58+
location: "https://raw.githubusercontent.com/OwnerB/Repo/main/image.png",
59+
},
60+
}
61+
: { status: 200, headers: { "content-type": "image/png" } },
62+
),
63+
),
64+
);
65+
}),
66+
),
67+
Effect.provideService(HostProcess.Environment, {
68+
T3CODE_GITHUB_USE_GIT_CREDENTIALS: enabled ? "1" : "0",
69+
}),
70+
Effect.scoped,
71+
);
72+
},
73+
);
74+
75+
it.effect.each([401, 403])(
76+
"invalidates the host cache after opted-in fallback HTTP %s",
77+
(status) => {
78+
const invalidations: Array<readonly [string, string | undefined]> = [];
79+
let requests = 0;
80+
return Effect.gen(function* () {
81+
expect(
82+
(yield* githubMediaResponse(
83+
asset("https://raw.githubusercontent.com/OwnerA/Repo/main/image.png"),
84+
{},
85+
)).status,
86+
).toBe(status);
87+
expect(invalidations).toEqual([["github.com", undefined]]);
88+
expect(requests).toBe(1);
89+
}).pipe(
90+
Effect.provide(
91+
Layer.mock(GitHubCredentials.GitHubCredentials)({
92+
get: (host) =>
93+
Effect.succeed({
94+
host,
95+
source: "gh",
96+
fingerprint: "default",
97+
token: Redacted.make("fake-fallback"),
98+
}),
99+
invalidate: (host, repository) =>
100+
Effect.sync(() => {
101+
invalidations.push([host, repository]);
102+
}),
103+
}),
104+
),
105+
Effect.provideService(
106+
HttpClient.HttpClient,
107+
HttpClient.make((request) => {
108+
requests++;
109+
return Effect.succeed(
110+
HttpClientResponse.fromWeb(request, new Response(null, { status })),
111+
);
112+
}),
113+
),
114+
Effect.provideService(HostProcess.Environment, { T3CODE_GITHUB_USE_GIT_CREDENTIALS: "1" }),
115+
Effect.scoped,
116+
);
117+
},
118+
);
119+
120+
it.effect(
121+
"uses raw URL repository context and strips a scoped token across repository redirects",
122+
() => {
123+
const scopes: Array<string | undefined> = [];
124+
const authorizations: Array<string | undefined> = [];
125+
return Effect.gen(function* () {
126+
const response = yield* githubMediaResponse(
127+
asset("https://raw.githubusercontent.com/OwnerA/Repo/main/image.png"),
128+
{},
129+
);
130+
expect(response.status).toBe(200);
131+
expect(scopes).toEqual(["OwnerA/Repo"]);
132+
expect(authorizations).toEqual(["Bearer fake-owner-a", undefined]);
133+
}).pipe(
134+
Effect.provide(
135+
Layer.mock(GitHubCredentials.GitHubCredentials)({
136+
get: (host, repository) => {
137+
scopes.push(repository);
138+
return Effect.succeed({
139+
host,
140+
repository: "ownera/repo",
141+
source: "git",
142+
fingerprint: "fake-a",
143+
token: Redacted.make("fake-owner-a"),
144+
});
145+
},
146+
}),
147+
),
148+
Effect.provideService(
149+
HttpClient.HttpClient,
150+
HttpClient.make((request) => {
151+
authorizations.push(request.headers.authorization);
152+
return Effect.succeed(
153+
HttpClientResponse.fromWeb(
154+
request,
155+
new Response(
156+
null,
157+
authorizations.length === 1
158+
? {
159+
status: 302,
160+
headers: {
161+
location: "https://raw.githubusercontent.com/OwnerB/Repo/main/image.png",
162+
},
163+
}
164+
: { status: 200, headers: { "content-type": "image/png" } },
165+
),
166+
),
167+
);
168+
}),
169+
),
170+
Effect.scoped,
171+
);
172+
},
173+
);
174+
175+
it.effect("keeps attachments without repository context on the host default", () => {
176+
const scopes: Array<string | undefined> = [];
177+
return Effect.gen(function* () {
178+
yield* githubMediaResponse(asset("https://github.com/user-attachments/assets/fake-id"), {});
179+
expect(scopes).toEqual([undefined]);
180+
}).pipe(
181+
Effect.provide(
182+
Layer.mock(GitHubCredentials.GitHubCredentials)({
183+
get: (host, repository) => {
184+
scopes.push(repository);
185+
return Effect.succeed({
186+
host,
187+
source: "settings",
188+
fingerprint: "default",
189+
token: Redacted.make("fake-default"),
190+
});
191+
},
192+
}),
193+
),
194+
Effect.provideService(
195+
HttpClient.HttpClient,
196+
HttpClient.make((request) =>
197+
Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status: 404 }))),
198+
),
199+
),
200+
Effect.scoped,
201+
);
202+
});
203+
204+
it.effect("reports helper failures without a host fallback or HTTP request", () =>
205+
Effect.gen(function* () {
206+
const error = yield* githubMediaResponse(
207+
asset("https://raw.githubusercontent.com/OwnerA/Repo/main/image.png"),
208+
{},
209+
).pipe(Effect.flip);
210+
expect(error._tag).toBe("GitHubGitCredentialError");
211+
}).pipe(
212+
Effect.provide(
213+
Layer.mock(GitHubCredentials.GitHubCredentials)({
214+
get: () =>
215+
Effect.fail(
216+
new GitHubCredentials.GitHubGitCredentialError({
217+
host: "github.com",
218+
reason: "process",
219+
}),
220+
),
221+
}),
222+
),
223+
Effect.provideService(
224+
HttpClient.HttpClient,
225+
HttpClient.make(() => Effect.die("Must not fetch after helper failure")),
226+
),
227+
Effect.scoped,
228+
),
229+
);
230+
});

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

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as Clock from "effect/Clock";
44
import * as Effect from "effect/Effect";
55
import * as Option from "effect/Option";
66
import * as Redacted from "effect/Redacted";
7+
import * as HostProcess from "@t3tools/shared/HostProcess";
78
import {
89
FetchHttpClient,
910
HttpClient,
@@ -50,22 +51,66 @@ const SVG_CONTENT_TYPE = "image/svg+xml";
5051
// body cannot run script against this origin.
5152
const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
5253

53-
const githubToken = GitHubCredentials.GitHubCredentials.pipe(
54-
Effect.flatMap((credentials) => credentials.get("github.com")),
55-
Effect.map((credential) => credential.token),
56-
Effect.orElseSucceed(() => null),
57-
);
54+
const repositoryOf = (source: string) => {
55+
const url = new URL(source);
56+
const parts = url.pathname.split("/");
57+
const repository = `${parts[1]}/${parts[2]}`;
58+
return isCredentialedHost(source) &&
59+
parts.length > 3 &&
60+
parts[1] !== "user-attachments" &&
61+
GitHubCredentials.normalizeCredentialRepository(repository) !== null
62+
? repository
63+
: null;
64+
};
65+
type MediaCredential = GitHubCredentials.GitHubCredential & {
66+
readonly operationRepository?: string;
67+
};
68+
const githubToken = (source: string) =>
69+
!isCredentialedHost(source)
70+
? Effect.succeed(null)
71+
: GitHubCredentials.GitHubCredentials.pipe(
72+
Effect.flatMap((credentials) =>
73+
Effect.gen(function* () {
74+
// Raw/LFS URLs and legacy attachments carry an explicit repository. Modern
75+
// user-attachments URLs do not, so they keep the host's default credential.
76+
const repository = repositoryOf(source);
77+
const credential = yield* credentials.get("github.com", repository ?? undefined);
78+
const enabled =
79+
(yield* HostProcess.Environment).T3CODE_GITHUB_USE_GIT_CREDENTIALS === "1";
80+
// Redirect scope follows the requested repository even on a helper miss. Keep
81+
// credential.repository as the actual cache provenance for invalidation.
82+
const operationRepository =
83+
enabled && repository !== null
84+
? GitHubCredentials.normalizeCredentialRepository(repository)!
85+
: credential.repository;
86+
return {
87+
...credential,
88+
...(operationRepository === undefined ? {} : { operationRepository }),
89+
} satisfies MediaCredential;
90+
}),
91+
),
92+
Effect.catchIf(
93+
(error) => error._tag !== "GitHubGitCredentialError",
94+
() => Effect.succeed(null),
95+
),
96+
);
5897

5998
const fetchFollowingRedirects = Effect.fn("GitHubMediaFetch.fetchFollowingRedirects")(function* (
6099
url: string,
61100
headers: Record<string, string>,
62-
token: Redacted.Redacted | null,
101+
credential: MediaCredential | null,
63102
) {
64103
const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient);
65104
let target = url;
66105
for (let hop = 0; ; hop += 1) {
67106
const authorization =
68-
token !== null && isCredentialedHost(target) ? `Bearer ${Redacted.value(token)}` : null;
107+
credential !== null &&
108+
isCredentialedHost(target) &&
109+
(credential.operationRepository === undefined ||
110+
GitHubCredentials.normalizeCredentialRepository(repositoryOf(target) ?? "") ===
111+
credential.operationRepository)
112+
? `Bearer ${Redacted.value(credential.token)}`
113+
: null;
69114
const response: HttpClientResponse.HttpClientResponse = yield* httpClient
70115
.execute(
71116
HttpClientRequest.get(target).pipe(
@@ -92,13 +137,20 @@ export const githubMediaResponse = Effect.fn("GitHubMediaFetch.githubMediaRespon
92137
requestHeaders: Record<string, string | undefined>,
93138
) {
94139
// Both media hosts are served by github.com's account, which is the host `gh` stores it under.
95-
const token = yield* githubToken;
140+
const credential = yield* githubToken(asset.url);
96141
const forwarded: Record<string, string> = {};
97142
for (const name of FORWARDED_REQUEST_HEADERS) {
98143
const value = requestHeaders[name];
99144
if (value !== undefined) forwarded[name] = value;
100145
}
101-
const response = yield* fetchFollowingRedirects(asset.url, forwarded, token);
146+
const response = yield* fetchFollowingRedirects(asset.url, forwarded, credential);
147+
if (
148+
credential?.operationRepository !== undefined &&
149+
(response?.status === 401 || response?.status === 403)
150+
) {
151+
const credentials = yield* GitHubCredentials.GitHubCredentials;
152+
yield* credentials.invalidate("github.com", credential.repository);
153+
}
102154
const remainingSeconds = Math.floor((asset.expiresAt - (yield* Clock.currentTimeMillis)) / 1000);
103155
const headers: Record<string, string> = {
104156
"cache-control":

0 commit comments

Comments
 (0)