Skip to content

Commit b5e067c

Browse files
fix(gitlab): load uploads through api_host logins and directly when unfetchable
A glab login stored under another name whose api_host is the upload's host (an SSH alias) now supplies the token. Media the server will not fetch, such as a public self-hosted upload on a host glab holds no login for, redirects the client to its URL instead of failing with 502, so it loads as it did before GitLab claimed upload URLs. AI-assisted; verified with the tests and tools listed above.
1 parent fe879a8 commit b5e067c

7 files changed

Lines changed: 104 additions & 22 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,37 @@ describe("AssetAccess", () => {
15721572
);
15731573
});
15741574

1575+
it.effect("sends the client to media its host's driver will not fetch", () => {
1576+
const url = `https://gitlab.example.test/team/app/uploads/${"0".repeat(32)}/demo.mp4`;
1577+
const media = sourceControlDefinitions.mediaUrl(url)!;
1578+
const layerDrivers = Layer.succeed(BuiltInDrivers.SourceControlDrivers, [
1579+
{
1580+
driver: { kind: media.definition.kind, definition: media.definition },
1581+
instance: fakeSourceControlDriverInstance(media.definition.kind, {
1582+
pullRequests: null,
1583+
fetchMedia: () => Effect.succeed(null),
1584+
}),
1585+
},
1586+
]);
1587+
return Effect.gen(function* () {
1588+
const response = yield* sourceControlMediaResponse(
1589+
{ url: media.fetchUrl, cwd: "/repo", repository: "team/app", expiresAt: 0 },
1590+
{},
1591+
);
1592+
expect(response.status).toBe(302);
1593+
expect(response.headers.location).toBe(url);
1594+
}).pipe(
1595+
Effect.provide(
1596+
Layer.mergeAll(
1597+
layerTest,
1598+
layerDrivers,
1599+
Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({}),
1600+
),
1601+
),
1602+
Effect.scoped,
1603+
);
1604+
});
1605+
15751606
it.effect("refuses media no host keeps behind a credential, without echoing the URL", () =>
15761607
Effect.gen(function* () {
15771608
const encodeAssetAccessError = Schema.encodeEffect(Schema.fromJsonString(AssetAccessError));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export const sourceControlMediaResponse = Effect.fn(
6666
headers: forwarded,
6767
...(repository === undefined ? {} : { repository }),
6868
});
69-
if (response === null) return HttpServerResponse.empty({ status: 502, headers });
69+
// Where the server holds no credential for it, the client loads the URL as it would unproxied.
70+
if (response === null) return HttpServerResponse.redirect(asset.url, { status: 302, headers });
7071
// It carries none of the upstream entity headers: a `content-length` with no body behind it
7172
// holds the connection open until the browser gives up on it.
7273
if (response.status >= 400) {

‎packages/source-control-core/src/server/driver.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export interface SourceControlDriverInstance {
5555
readonly pullRequests: PullRequestProviderApi | null;
5656
/**
5757
* Fetches media the definition's `mediaUrl` claimed, with the credential the host serves it
58-
* to. Null when the fetch went nowhere a response could come from, such as a redirect loop.
58+
* to. Null when the server cannot fetch it, such as without a credential for its host or on a
59+
* redirect loop; the client is then sent to the URL itself.
5960
*/
6061
readonly fetchMedia?: (
6162
request: SourceControlMediaRequest,

‎packages/source-control-gitlab/src/server/GitLabCredentials.test.ts‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ import type * as SourceControlHost from "@t3tools/source-control-core/server/Sou
88
import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
99
import * as GitLabCredentials from "./GitLabCredentials.ts";
1010

11-
/** A scripted glab: `config get <key> --host <host>` reads `config`, `api` refreshes. */
12-
function harness(config: Record<string, Record<string, string>>, refreshed?: string) {
11+
/**
12+
* A scripted glab: `config get <key> --host <host>` reads `config`, `api` refreshes, and
13+
* `auth status` prints `status`.
14+
*/
15+
function harness(config: Record<string, Record<string, string>>, refreshed?: string, status = "") {
1316
const runs: Array<SourceControlHost.SourceControlProcessInput> = [];
1417
const run: SourceControlHost.SourceControlHost["Service"]["process"]["run"] = (input) =>
1518
Effect.sync(() => {
1619
runs.push(input);
1720
const host = input.args[input.args.indexOf("--host") + 1] ?? "";
21+
if (input.args[0] === "auth")
22+
return TestSourceControlHost.processOutput("", { stderr: status });
1823
if (input.args[0] === "api") {
1924
if (refreshed !== undefined) config[input.args[2]!]!.token = refreshed;
2025
return TestSourceControlHost.processOutput("{}");
@@ -54,10 +59,24 @@ describe("GitLabCredentials", () => {
5459
});
5560

5661
it.effect("has nothing for a host glab holds no login for", () => {
57-
const { layer, runs } = harness({});
62+
const { layer } = harness(
63+
{ "gitlab.example.com": { token: "stored" } },
64+
undefined,
65+
"gitlab.example.com\n ✓ REST API Endpoint: https://gitlab.example.com/api/v4/\n",
66+
);
5867
return Effect.gen(function* () {
5968
expect(yield* token("attacker.example")).toEqual(Option.none());
60-
expect(runs).toHaveLength(1);
69+
}).pipe(Effect.provide(layer));
70+
});
71+
72+
it.effect("reads the login whose api_host is the host", () => {
73+
const { layer } = harness(
74+
{ "gitlab-ssh.example.com": { token: "aliased" } },
75+
undefined,
76+
"gitlab-ssh.example.com\n ✓ REST API Endpoint: https://gitlab.example.com/api/v4/\n",
77+
);
78+
return Effect.gen(function* () {
79+
expect(yield* token("gitlab.example.com")).toEqual(Option.some("aliased"));
6180
}).pipe(Effect.provide(layer));
6281
});
6382

‎packages/source-control-gitlab/src/server/GitLabCredentials.ts‎

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import * as Redacted from "effect/Redacted";
1010
import * as HostProcess from "@t3tools/shared/HostProcess";
1111
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
1212

13+
import { parseGitLabAuthStatusHosts } from "./gitLabAuthStatus.ts";
14+
1315
/** How long a token is reused before `glab` is asked again. */
1416
const TOKEN_TTL = Duration.minutes(5);
1517
/** A missing login is asked again soon, so a fresh `glab auth login` applies. */
@@ -43,22 +45,37 @@ export const make = Effect.gen(function* () {
4345
const { process } = yield* SourceControlHost.SourceControlHost;
4446
const workingDirectory = yield* HostProcess.WorkingDirectory;
4547

48+
const run = (args: ReadonlyArray<string>, timeoutMs = 10_000) =>
49+
process.run({
50+
operation: "GitLabCredentials.get",
51+
command: "glab",
52+
args,
53+
cwd: workingDirectory,
54+
env: STORED_ONLY_ENV,
55+
allowNonZeroExit: true,
56+
timeoutMs,
57+
maxOutputBytes: 64 * 1024,
58+
});
4659
const glab = (args: ReadonlyArray<string>) =>
47-
process
48-
.run({
49-
operation: "GitLabCredentials.get",
50-
command: "glab",
51-
args,
52-
cwd: workingDirectory,
53-
env: STORED_ONLY_ENV,
54-
timeoutMs: 10_000,
55-
maxOutputBytes: 64 * 1024,
56-
})
57-
.pipe(Effect.map((output) => output.stdout.trim()));
60+
run(args).pipe(Effect.map((output) => output.stdout.trim()));
5861

5962
const storedToken = (host: string) => glab(["config", "get", "token", "--host", host]);
6063

61-
const lookup = Effect.fn("GitLabCredentials.lookup")(function* (host: string) {
64+
/** The login whose `api_host` is `host`, as a login stored under an SSH alias has. */
65+
const aliasFor = Effect.fn("GitLabCredentials.aliasFor")(function* (host: string) {
66+
// glab 1.79+ lists every host only with `--all`; older glab rejects the flag.
67+
for (const args of [
68+
["auth", "status", "--all"],
69+
["auth", "status"],
70+
]) {
71+
const output = yield* run(args, 30_000);
72+
const hosts = parseGitLabAuthStatusHosts(`${output.stdout}\n${output.stderr}`);
73+
if (hosts.length > 0) return hosts.find((entry) => entry.apiHost === host)?.host;
74+
}
75+
return undefined;
76+
});
77+
78+
const login = Effect.fn("GitLabCredentials.login")(function* (host: string) {
6279
if ((yield* storedToken(host)) === "") return Option.none();
6380
// An OAuth login expires within hours and glab refreshes it only on an API call.
6481
if ((yield* glab(["config", "get", "is_oauth2", "--host", host])) === "true") {
@@ -68,6 +85,13 @@ export const make = Effect.gen(function* () {
6885
return token === "" ? Option.none() : Option.some(Redacted.make(token));
6986
});
7087

88+
const lookup = Effect.fn("GitLabCredentials.lookup")(function* (host: string) {
89+
const token = yield* login(host);
90+
if (Option.isSome(token)) return token;
91+
const alias = yield* aliasFor(host);
92+
return alias === undefined || alias === host ? Option.none() : yield* login(alias);
93+
});
94+
7195
const cache = yield* Cache.makeWith(
7296
(host: string) => lookup(host).pipe(Effect.orElseSucceed(() => Option.none())),
7397
{

‎packages/source-control-gitlab/src/server/GitLabSourceControlProvider.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,13 @@ it("parses authenticated GitLab auth status hosts with ports and single-label na
189189
assert.deepStrictEqual(
190190
parseGitLabAuthStatusHosts(`localhost:8080
191191
✓ Logged in to localhost:8080 as local-user
192+
✓ REST API Endpoint: https://gitlab.localtest/api/v4/
192193
selfhosted
193194
✓ Logged in to selfhosted as single-label-user
194195
`),
195196
[
196-
{ host: "localhost:8080", account: "local-user" },
197-
{ host: "selfhosted", account: "single-label-user" },
197+
{ host: "localhost:8080", account: "local-user", apiHost: "gitlab.localtest" },
198+
{ host: "selfhosted", account: "single-label-user", apiHost: null },
198199
],
199200
);
200201
});

‎packages/source-control-gitlab/src/server/gitLabAuthStatus.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const HOST_LINE_PATTERN = /^(?:[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?|\[[a-f0-9:.]+\])(?::\d+)?$/iu;
22
const LOGGED_IN_PATTERN = /Logged in to .+? as\s+([^\s(]+)/iu;
3+
const REST_ENDPOINT_PATTERN = /REST API Endpoint:\s*https?:\/\/([^/\s]+)\//iu;
34

45
export interface GitLabAuthStatusHost {
56
readonly host: string;
67
readonly account: string | null;
8+
/** The host its API calls go to, which glab's `api_host` can set apart from `host`. */
9+
readonly apiHost: string | null;
710
}
811

912
export function parseGitLabAuthStatusHosts(text: string): ReadonlyArray<GitLabAuthStatusHost> {
@@ -14,8 +17,10 @@ export function parseGitLabAuthStatusHosts(text: string): ReadonlyArray<GitLabAu
1417
const flush = () => {
1518
if (currentHost === null) return;
1619

17-
const account = LOGGED_IN_PATTERN.exec(currentLines.join("\n"))?.[1]?.trim() || null;
18-
hosts.push({ host: currentHost, account });
20+
const lines = currentLines.join("\n");
21+
const account = LOGGED_IN_PATTERN.exec(lines)?.[1]?.trim() || null;
22+
const apiHost = REST_ENDPOINT_PATTERN.exec(lines)?.[1]?.toLowerCase() ?? null;
23+
hosts.push({ host: currentHost, account, apiHost });
1924
currentHost = null;
2025
currentLines = [];
2126
};

0 commit comments

Comments
 (0)