Skip to content

Commit 256013f

Browse files
committed
fix(server): isolate GitLab upload credentials from ambient tokens
1 parent 5f40004 commit 256013f

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

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

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
1212
import type { GitLabUploadReference } from "@t3tools/contracts";
1313
import * as GitLabUploadMedia from "./GitLabUploadMedia.ts";
1414
import * as GitLabCli from "../sourceControl/GitLabCli.ts";
15+
import * as VcsProcess from "../vcs/VcsProcess.ts";
1516
import * as ServerConfig from "../config.ts";
1617

1718
const reference: GitLabUploadReference = {
@@ -33,6 +34,7 @@ function fixture(
3334
protocol?: string;
3435
endpoint?: string;
3536
token?: () => string;
37+
environment?: NodeJS.ProcessEnv;
3638
response?: (
3739
request: { url: string; method: string; headers: Readonly<Record<string, string>> },
3840
index: number,
@@ -59,18 +61,30 @@ function fixture(
5961
});
6062
const layer = GitLabUploadMedia.layer.pipe(
6163
Layer.provide(
62-
Layer.mock(GitLabCli.GitLabCli)({
63-
execute: ({ args }) => {
64-
commands.push(args);
65-
return Effect.succeed(
66-
output(
67-
args[0] === "config"
68-
? (options.protocol ?? "https")
69-
: `REST API Endpoint: ${options.endpoint ?? "https://api.example/api/v4/"}\nToken found in config file: ${options.token?.() ?? "private-credential"}`,
70-
),
71-
);
72-
},
73-
}),
64+
GitLabCli.layer.pipe(
65+
Layer.provide(
66+
Layer.mock(VcsProcess.VcsProcess)({
67+
run: ({ args, env }) => {
68+
commands.push(args);
69+
const environment = { ...options.environment, ...env };
70+
// glab gives ambient access tokens precedence over the selected host's stored token.
71+
const token =
72+
environment.GITLAB_TOKEN ||
73+
environment.GITLAB_ACCESS_TOKEN ||
74+
environment.OAUTH_TOKEN ||
75+
(options.token?.() ?? "private-credential") ||
76+
(environment.GLAB_ENABLE_CI_AUTOLOGIN === "true" ? environment.CI_JOB_TOKEN : "");
77+
return Effect.succeed(
78+
output(
79+
args[0] === "config"
80+
? (options.protocol ?? "https")
81+
: `REST API Endpoint: ${options.endpoint ?? "https://api.example/api/v4/"}\nToken found in config file: ${token}`,
82+
),
83+
);
84+
},
85+
}),
86+
),
87+
),
7488
),
7589
Layer.provide(Layer.succeed(HttpClient.HttpClient, client)),
7690
Layer.provide(
@@ -82,6 +96,27 @@ function fixture(
8296
}
8397

8498
describe("GitLabUploadMedia", () => {
99+
it.effect.each(["GITLAB_TOKEN", "GITLAB_ACCESS_TOKEN", "OAUTH_TOKEN", "CI_JOB_TOKEN"])(
100+
"does not use ambient %s for an upload-selected host",
101+
(key) => {
102+
const environment = { [key]: "ambient-secret", GLAB_ENABLE_CI_AUTOLOGIN: "true" };
103+
const configured = fixture({ environment });
104+
const unconfigured = fixture({ environment, token: () => "" });
105+
return Effect.gen(function* () {
106+
yield* Effect.gen(function* () {
107+
const service = yield* GitLabUploadMedia.GitLabUploadMedia;
108+
expect((yield* service.respond(reference, {}, "GET")).status).toBe(200);
109+
expect(configured.requests[0]?.headers["private-token"]).toBe("private-credential");
110+
}).pipe(Effect.provide(configured.layer));
111+
yield* Effect.gen(function* () {
112+
const service = yield* GitLabUploadMedia.GitLabUploadMedia;
113+
expect((yield* service.respond(reference, {}, "GET")).status).toBe(502);
114+
expect(unconfigured.requests).toHaveLength(0);
115+
}).pipe(Effect.provide(unconfigured.layer));
116+
}).pipe(Effect.scoped);
117+
},
118+
);
119+
85120
it.effect("streams decoded upload names through the HTTPS API and reuses the credential", () => {
86121
const f = fixture();
87122
return Effect.gen(function* () {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ const make = Effect.gen(function* () {
7070
args,
7171
timeoutMs: 10_000,
7272
maxOutputBytes: 16 * 1024,
73+
// Upload authors choose the host. Only its stored credentials may be used;
74+
// glab otherwise sends a higher-priority ambient token during auth status.
75+
env: {
76+
GITLAB_TOKEN: "",
77+
GITLAB_ACCESS_TOKEN: "",
78+
OAUTH_TOKEN: "",
79+
CI_JOB_TOKEN: "",
80+
GLAB_ENABLE_CI_AUTOLOGIN: "false",
81+
},
7382
});
7483
// auth status validates the token over the network. Check the effective protocol first.
7584
const protocol = yield* execute(["config", "get", "api_protocol", "--host", host]);

‎apps/server/src/sourceControl/GitLabCli.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export class GitLabCli extends Context.Service<
267267
readonly cwd: string;
268268
readonly args: ReadonlyArray<string>;
269269
readonly timeoutMs?: number;
270+
readonly env?: NodeJS.ProcessEnv;
270271
/** Piped to the child's stdin, for payloads that must never appear in argv. */
271272
readonly stdin?: string;
272273
readonly maxOutputBytes?: number;
@@ -423,6 +424,7 @@ export const make = Effect.gen(function* () {
423424
args: input.args,
424425
cwd: input.cwd,
425426
timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
427+
...(input.env === undefined ? {} : { env: input.env }),
426428
...(input.stdin === undefined ? {} : { stdin: input.stdin }),
427429
...(input.maxOutputBytes === undefined ? {} : { maxOutputBytes: input.maxOutputBytes }),
428430
})

0 commit comments

Comments
 (0)