Skip to content

Commit 35d121e

Browse files
committed
fix(gitlab): only fetch uploads from hosts glab is logged in to
Older glab returns GITLAB_TOKEN from 'config get token' for any host, so an upload link on an unconfigured GitLab-looking host received the token. Check 'glab auth status --hostname' lists the host before reading it.
1 parent 0fae793 commit 35d121e

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ import * as GitLabMediaFetch from "./gitLabMediaFetch.ts";
99

1010
const UPLOAD = "/api/v4/projects/acme%2Fweb/uploads/871cca6041e4d445daf391a8a17d8fb2/clip.mp4";
1111

12-
/** `glab` with the given tokens per host, each read handing out the next one. */
13-
const glab = (tokens: Record<string, Array<string>>, calls: Array<string>) =>
12+
/**
13+
* `glab` with the given tokens per host, each read handing out the next one, logged in to
14+
* `loggedIn`. Older `glab` reads GITLAB_TOKEN out for any host, logged in or not.
15+
*/
16+
const glab = (
17+
tokens: Record<string, Array<string>>,
18+
calls: Array<string>,
19+
loggedIn = Object.keys(tokens),
20+
) =>
1421
Layer.mock(GitLabCli.GitLabCli)({
1522
execute: (input) =>
1623
Effect.sync(() => {
@@ -19,7 +26,10 @@ const glab = (tokens: Record<string, Array<string>>, calls: Array<string>) =>
1926
return {
2027
exitCode: ChildProcessSpawner.ExitCode(0),
2128
stdout: input.args[0] === "config" ? (tokens[host]?.shift() ?? "") : "{}",
22-
stderr: "",
29+
stderr:
30+
input.args[0] === "auth" && loggedIn.includes(host)
31+
? `${host}\n ✓ Logged in to ${host} as someone\n`
32+
: "",
2333
stdoutTruncated: false,
2434
stderrTruncated: false,
2535
};
@@ -48,7 +58,7 @@ describe("GitLabMediaFetch", () => {
4858
).toBeNull();
4959
expect(requests).toEqual([]);
5060
}).pipe(
51-
Effect.provide(glab({}, [])),
61+
Effect.provide(glab({ "gitlab.evil.test": ["env-token"] }, [], [])),
5262
Effect.provideService(
5363
HttpClient.HttpClient,
5464
respondWith(({ url }) => {
@@ -69,8 +79,10 @@ describe("GitLabMediaFetch", () => {
6979
expect(response?.status).toBe(200);
7080
expect(authorizations).toEqual(["Bearer expired", "Bearer fresh"]);
7181
expect(calls).toEqual([
82+
"auth status --hostname gitlab.com",
7283
"config get token --host gitlab.com",
7384
"api user",
85+
"auth status --hostname gitlab.com",
7486
"config get token --host gitlab.com",
7587
]);
7688
}).pipe(

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import * as HostProcess from "@t3tools/shared/HostProcess";
1414

1515
import * as GitLabCli from "./GitLabCli.ts";
16+
import { parseGitLabAuthStatusHosts } from "./gitLabAuthStatus.ts";
1617

1718
const TOKEN_TTL = Duration.minutes(5);
1819
const MISSING_TOKEN_TTL = Duration.seconds(10);
@@ -28,12 +29,19 @@ export const make = Effect.gen(function* () {
2829
const cwd = yield* HostProcess.WorkingDirectory;
2930
const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient);
3031

31-
// `glab` resolves GITLAB_TOKEN and its own config here, and prints nothing for a host it has
32-
// no account on.
32+
// Older `glab` hands GITLAB_TOKEN out for any host, so the host must be one it has a login for.
3333
const tokens = yield* Cache.makeWith(
3434
(host: string) =>
35-
cli.execute({ cwd, args: ["config", "get", "token", "--host", host] }).pipe(
36-
Effect.map((output) => output.stdout.trim() || null),
35+
cli.execute({ cwd, args: ["auth", "status", "--hostname", host] }).pipe(
36+
Effect.flatMap((status) =>
37+
parseGitLabAuthStatusHosts(`${status.stdout}\n${status.stderr}`).some(
38+
(entry) => entry.host === host.toLowerCase(),
39+
)
40+
? cli
41+
.execute({ cwd, args: ["config", "get", "token", "--host", host] })
42+
.pipe(Effect.map((output) => output.stdout.trim() || null))
43+
: Effect.succeed(null),
44+
),
3745
Effect.orElseSucceed(() => null),
3846
),
3947
{

0 commit comments

Comments
 (0)