Skip to content

Commit af21f1a

Browse files
committed
fix(server): only prefer HTTPS clones when GitLab's web_url is encrypted
An HTTPS preference selects the project's web_url, so a plaintext http:// instance (or glab's `http` setting) no longer auto-selects an unencrypted clone.
1 parent 31eff81 commit af21f1a

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,31 @@ layer("GitLabCli.layer", (it) => {
192192
}),
193193
);
194194

195+
it.effect("does not prefer HTTP transport when the project URL is plaintext", () =>
196+
Effect.gen(function* () {
197+
const plaintextProject = JSON.stringify({
198+
path_with_namespace: "group/project",
199+
web_url: "http://gitlab.internal/group/project",
200+
http_url_to_repo: "http://gitlab.internal/group/project.git",
201+
ssh_url_to_repo: "[email protected]:group/project.git",
202+
});
203+
const glab = yield* GitLabCli.GitLabCli;
204+
205+
for (const configured of ["https\n", "http\n"]) {
206+
mockedRun
207+
.mockReturnValueOnce(Effect.succeed(processOutput(plaintextProject)))
208+
.mockReturnValueOnce(Effect.succeed(processOutput(configured)));
209+
210+
const result = yield* glab.getRepositoryCloneUrls({
211+
cwd: "/repo",
212+
repository: "group/project",
213+
});
214+
215+
assert.strictEqual(result.preferredCloneProtocol, undefined);
216+
}
217+
}),
218+
);
219+
195220
it.effect("leaves the clone protocol unset when glab config cannot be read", () =>
196221
Effect.gen(function* () {
197222
mockedRun.mockReturnValueOnce(Effect.succeed(processOutput(projectJson))).mockReturnValueOnce(

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,27 +353,32 @@ function normalizeRepositoryCloneUrls(
353353
};
354354
}
355355

356-
/** Maps `glab config get git_protocol` output; glab accepts `ssh`, `https`, and `http`. */
357-
function parseGitProtocol(stdout: string): SourceControlPreferredCloneProtocol | undefined {
356+
function parseUrl(url: string): URL | null {
357+
try {
358+
return new URL(url);
359+
} catch {
360+
return null;
361+
}
362+
}
363+
364+
/**
365+
* Maps `glab config get git_protocol` output. An HTTPS preference selects the project's
366+
* `web_url`, so it only counts when that URL is encrypted; glab's `http` never does.
367+
*/
368+
function parseGitProtocol(
369+
stdout: string,
370+
webUrl: URL | null,
371+
): SourceControlPreferredCloneProtocol | undefined {
358372
switch (stdout.trim()) {
359373
case "ssh":
360374
return "ssh";
361375
case "https":
362-
case "http":
363-
return "https";
376+
return webUrl?.protocol === "https:" ? "https" : undefined;
364377
default:
365378
return undefined;
366379
}
367380
}
368381

369-
function hostOf(url: string): string | null {
370-
try {
371-
return new URL(url).host || null;
372-
} catch {
373-
return null;
374-
}
375-
}
376-
377382
function stateArgs(state: "open" | "closed" | "merged" | "all"): ReadonlyArray<string> {
378383
switch (state) {
379384
case "open":
@@ -479,12 +484,13 @@ export const make = Effect.gen(function* () {
479484
// `--host` falls back to the global setting, matching the protocol glab itself
480485
// would clone with. A missing or unreadable setting leaves the caller's default.
481486
const readGitProtocol = (input: { readonly cwd: string; readonly url: string }) => {
482-
const host = hostOf(input.url);
487+
const webUrl = parseUrl(input.url);
488+
const host = webUrl?.host || null;
483489
return execute({
484490
cwd: input.cwd,
485491
args: ["config", "get", "git_protocol", ...(host === null ? [] : ["--host", host])],
486492
}).pipe(
487-
Effect.map((result) => parseGitProtocol(result.stdout)),
493+
Effect.map((result) => parseGitProtocol(result.stdout, webUrl)),
488494
Effect.orElseSucceed(() => undefined),
489495
);
490496
};

0 commit comments

Comments
 (0)