Skip to content

Commit 37aa955

Browse files
committed
fix(gitlab): render private GitLab uploads in merge requests
GitLab writes uploads into merge request bodies and comments as project-relative `/uploads/<secret>/<name>` links, which the clients treated as local paths, and a private project serves them only to a signed-in request. The GitLab definition now resolves those links against the merge request's project (a new `resolveRelativeLinks` hook, applied by web and mobile before the body is split) and claims upload URLs through `mediaUrl`. The GitLab driver's `fetchMedia` reads them through the upload API (GitLab 17.4+) with the token `glab` stores for that host, and falls back to the upload itself without a login, which still serves a public project.
1 parent 6e79211 commit 37aa955

15 files changed

Lines changed: 638 additions & 9 deletions

File tree

‎apps/mobile/src/features/pull-requests/PullRequestMarkdown.tsx‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
} from "./PullRequestMedia";
1111
import { videoMimeType } from "@t3tools/shared/video";
1212
import { createContext, useContext, useCallback, useMemo, useState } from "react";
13-
import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";
13+
import {
14+
changeRequestLinkResolver,
15+
changeRequestReferenceAutolinker,
16+
} from "@t3tools/client-runtime/pull-request-markdown";
1417
import type { ReferenceAutolink } from "@t3tools/client-runtime/source-control-clients";
1518
import { linkReferenceAutolinks } from "./pullRequestAutolinks";
1619

@@ -31,6 +34,7 @@ import type { MarkdownImageRenderer } from "../../native/SelectableMarkdownText"
3134
const AutolinksContext = createContext<((text: string) => ReadonlyArray<ReferenceAutolink>) | null>(
3235
null,
3336
);
37+
const RelativeLinksContext = createContext<((markdown: string) => string) | null>(null);
3438

3539
export function PullRequestMarkdownRepository(props: {
3640
/** The change request the markdown belongs to, whose host decides what its text links. */
@@ -47,6 +51,13 @@ export function PullRequestMarkdownRepository(props: {
4751
: changeRequestReferenceAutolinker({ provider, url }),
4852
[provider, url],
4953
);
54+
const resolveLinks = useMemo(
55+
() =>
56+
provider === undefined || url === undefined
57+
? null
58+
: changeRequestLinkResolver({ provider, url }),
59+
[provider, url],
60+
);
5061
const target = pullRequestTarget(props.params);
5162
const project = useProject({
5263
environmentId: target.environmentId,
@@ -59,7 +70,9 @@ export function PullRequestMarkdownRepository(props: {
5970
);
6071
return (
6172
<AutolinksContext value={autolinks}>
62-
<PullRequestMediaContext value={media}>{props.children}</PullRequestMediaContext>
73+
<RelativeLinksContext value={resolveLinks}>
74+
<PullRequestMediaContext value={media}>{props.children}</PullRequestMediaContext>
75+
</RelativeLinksContext>
6376
</AutolinksContext>
6477
);
6578
}
@@ -95,15 +108,27 @@ export function PullRequestMarkdown(props: {
95108
readonly markdown: string;
96109
readonly imageGroupMarkdown?: string;
97110
}) {
98-
const segments = useMemo(() => splitPullRequestBody(props.markdown), [props.markdown]);
111+
const resolveLinks = useContext(RelativeLinksContext);
112+
const markdown = useMemo(
113+
() => resolveLinks?.(props.markdown) ?? props.markdown,
114+
[resolveLinks, props.markdown],
115+
);
116+
const imageGroupMarkdown = useMemo(
117+
() =>
118+
props.imageGroupMarkdown === undefined
119+
? markdown
120+
: (resolveLinks?.(props.imageGroupMarkdown) ?? props.imageGroupMarkdown),
121+
[resolveLinks, props.imageGroupMarkdown, markdown],
122+
);
123+
const segments = useMemo(() => splitPullRequestBody(markdown), [markdown]);
99124
return (
100125
<View style={{ gap: 12 }}>
101126
{segments.map((segment) =>
102127
segment.kind === "markdown" ? (
103128
<PullRequestMarkdownContent
104129
key={segment.id}
105130
markdown={segment.text}
106-
imageGroupMarkdown={props.imageGroupMarkdown ?? props.markdown}
131+
imageGroupMarkdown={imageGroupMarkdown}
107132
/>
108133
) : (
109134
<PullRequestMedia key={segment.id} url={segment.url} kind={segment.media} />

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { projectChangeRequestUrl } from "@t3tools/client-runtime/change-request-links";
2-
import { changeRequestReferenceAutolinker } from "@t3tools/client-runtime/pull-request-markdown";
2+
import {
3+
changeRequestLinkResolver,
4+
changeRequestReferenceAutolinker,
5+
} from "@t3tools/client-runtime/pull-request-markdown";
36
import { useAtomValue } from "@effect/atom-react";
47
import { usePullRequestStack } from "~/state/usePullRequestStack";
58
import {
@@ -239,7 +242,11 @@ function pullRequestMarkdownContext(
239242
detail: Pick<PullRequestDetail, "provider" | "url"> | null,
240243
threadRef: ScopedThreadRef | null,
241244
) {
242-
return { autolinks: changeRequestReferenceAutolinker(detail), threadRef };
245+
return {
246+
autolinks: changeRequestReferenceAutolinker(detail),
247+
resolveLinks: changeRequestLinkResolver(detail),
248+
threadRef,
249+
};
243250
}
244251

245252
function pullRequestStackReference(

‎apps/web/src/components/pullRequest/PullRequestMarkdown.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { remarkPullRequestAutolinks, splitPullRequestBody } from "./pullRequestM
2323

2424
export const PullRequestMarkdownContext = createContext<{
2525
autolinks: ((text: string) => ReadonlyArray<ReferenceAutolink>) | null;
26+
resolveLinks: ((markdown: string) => string) | null;
2627
threadRef: ScopedThreadRef | null;
2728
} | null>(null);
2829

@@ -67,8 +68,8 @@ export function PullRequestMarkdown({
6768
threadRef?: ScopedThreadRef | null;
6869
className?: string;
6970
}) {
70-
const segments = splitPullRequestBody(text);
7171
const context = useContext(PullRequestMarkdownContext);
72+
const segments = splitPullRequestBody(context?.resolveLinks?.(text) ?? text);
7273
const autolinks = context?.autolinks;
7374
const resolvedThreadRef = threadRef ?? context?.threadRef ?? undefined;
7475
const extraRemarkPlugins: NonNullable<ReactMarkdownOptions["remarkPlugins"]> = autolinks

‎docs/user/source-control.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Install [GitLab CLI](https://gitlab.com/gitlab-org/cli), then sign in:
6262
glab auth login
6363
```
6464

65+
Images and videos uploaded to a private project's merge requests load through this login, which
66+
needs GitLab 17.4 or later.
67+
6568
### Bitbucket
6669

6770
Open **Settings → Source Control**, expand **Bitbucket**, and enter one of:

‎packages/client-runtime/src/pullRequestMarkdown.test.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { changeRequestReferenceAutolinker, splitPullRequestBody } from "./pullRequestMarkdown.ts";
3+
import {
4+
changeRequestLinkResolver,
5+
changeRequestReferenceAutolinker,
6+
splitPullRequestBody,
7+
} from "./pullRequestMarkdown.ts";
48

59
describe("pull request body segmentation", () => {
610
// GitHub writes a dropped image into the body as an `<img>` tag, so a bare attachment link on
@@ -140,3 +144,23 @@ describe("change request reference autolinks", () => {
140144
expect(urls("forgejo", "https://codeberg.org/acme/web/pulls/7", "#5")).toBeNull();
141145
});
142146
});
147+
148+
describe("change request relative links", () => {
149+
const upload = "/uploads/66dbcd21ec5d24ed6ea225176098d52b/shot.png";
150+
151+
it("resolves a GitLab upload against the merge request's project", () => {
152+
const resolve = changeRequestLinkResolver({
153+
provider: "gitlab",
154+
url: "https://gitlab.example.com/group/Sub/app/-/merge_requests/7",
155+
});
156+
expect(resolve?.(`![shot](${upload})`)).toBe(
157+
`![shot](https://gitlab.example.com/group/Sub/app${upload})`,
158+
);
159+
});
160+
161+
it("leaves bodies alone on a host that writes no relative links", () => {
162+
expect(
163+
changeRequestLinkResolver({ provider: "github", url: "https://github.com/acme/web/pull/7" }),
164+
).toBeNull();
165+
});
166+
});

‎packages/client-runtime/src/pullRequestMarkdown.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,19 @@ export function changeRequestReferenceAutolinker(
147147
? null
148148
: (text) => find(text, repositoryUrl);
149149
}
150+
151+
/**
152+
* Makes the links the change request's host writes relative to its repository absolute, so they
153+
* load from the host. Null when the host writes none.
154+
*/
155+
export function changeRequestLinkResolver(
156+
changeRequest: { readonly provider: string; readonly url: string } | null | undefined,
157+
): ((markdown: string) => string) | null {
158+
if (!changeRequest) return null;
159+
const resolve = sourceControlClients.get(changeRequest.provider).resolveRelativeLinks;
160+
const repositoryUrl = sourceControlClients.parseChangeRequestUrl(changeRequest.url)?.link
161+
.repositoryUrl;
162+
return resolve === undefined || repositoryUrl === undefined
163+
? null
164+
: (markdown) => resolve(markdown, repositoryUrl);
165+
}

‎packages/source-control-core/src/shared/definition.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ export interface SourceControlDefinition {
244244
text: string,
245245
repositoryUrl: string,
246246
) => ReadonlyArray<ReferenceAutolink>;
247+
/**
248+
* `markdown` with the links this host writes relative to the repository made absolute against
249+
* `repositoryUrl`. Absent for a host whose bodies carry none.
250+
*/
251+
readonly resolveRelativeLinks?: (markdown: string, repositoryUrl: string) => string;
247252
/** Where the host lists a change request's checks. Absent means the change request's own page. */
248253
readonly changeRequestChecksUrl?: (changeRequestUrl: string) => string;
249254
/**
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 Option from "effect/Option";
5+
import * as Redacted from "effect/Redacted";
6+
7+
import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
8+
import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
9+
import * as GitLabCredentials from "./GitLabCredentials.ts";
10+
11+
/** A scripted glab: `config get <key> --host <host>` reads `config`, `api` refreshes. */
12+
function harness(config: Record<string, Record<string, string>>, refreshed?: string) {
13+
const runs: Array<SourceControlHost.SourceControlProcessInput> = [];
14+
const run: SourceControlHost.SourceControlHost["Service"]["process"]["run"] = (input) =>
15+
Effect.sync(() => {
16+
runs.push(input);
17+
const host = input.args[input.args.indexOf("--host") + 1] ?? "";
18+
if (input.args[0] === "api") {
19+
if (refreshed !== undefined) config[input.args[2]!]!.token = refreshed;
20+
return TestSourceControlHost.processOutput("{}");
21+
}
22+
return TestSourceControlHost.processOutput(`${config[host]?.[input.args[2]!] ?? ""}\n`);
23+
});
24+
const layer = GitLabCredentials.layer.pipe(
25+
Layer.provide(TestSourceControlHost.layer({ process: { run } })),
26+
);
27+
return { layer, runs };
28+
}
29+
30+
const token = (host: string) =>
31+
GitLabCredentials.GitLabCredentials.pipe(
32+
Effect.flatMap((credentials) => credentials.get(host)),
33+
Effect.map(Option.map(Redacted.value)),
34+
);
35+
36+
describe("GitLabCredentials", () => {
37+
it.effect("reads only the token glab stores for the host", () => {
38+
const { layer, runs } = harness({ "gitlab.example.com": { token: "stored" } });
39+
return Effect.gen(function* () {
40+
expect(yield* token("GitLab.Example.com")).toEqual(Option.some("stored"));
41+
expect(runs.map((input) => input.args)).toEqual([
42+
["config", "get", "token", "--host", "gitlab.example.com"],
43+
["config", "get", "is_oauth2", "--host", "gitlab.example.com"],
44+
["config", "get", "token", "--host", "gitlab.example.com"],
45+
]);
46+
// glab would otherwise answer every host with an ambient token.
47+
for (const input of runs) {
48+
expect(input.env).toMatchObject({ GITLAB_TOKEN: "", OAUTH_TOKEN: "", CI_JOB_TOKEN: "" });
49+
}
50+
// Cached: a second read asks glab nothing.
51+
yield* token("gitlab.example.com");
52+
expect(runs).toHaveLength(3);
53+
}).pipe(Effect.provide(layer));
54+
});
55+
56+
it.effect("has nothing for a host glab holds no login for", () => {
57+
const { layer, runs } = harness({});
58+
return Effect.gen(function* () {
59+
expect(yield* token("attacker.example")).toEqual(Option.none());
60+
expect(runs).toHaveLength(1);
61+
}).pipe(Effect.provide(layer));
62+
});
63+
64+
it.effect("refreshes an OAuth login through an API call before reading it", () => {
65+
const { layer, runs } = harness(
66+
{ "gitlab.com": { token: "expired", is_oauth2: "true" } },
67+
"refreshed",
68+
);
69+
return Effect.gen(function* () {
70+
expect(yield* token("gitlab.com")).toEqual(Option.some("refreshed"));
71+
expect(runs.map((input) => input.args)).toContainEqual([
72+
"api",
73+
"--hostname",
74+
"gitlab.com",
75+
"user",
76+
]);
77+
}).pipe(Effect.provide(layer));
78+
});
79+
80+
it.effect("asks glab again after a refused token is invalidated", () => {
81+
const { layer, runs } = harness({ "gitlab.com": { token: "stored" } });
82+
return Effect.gen(function* () {
83+
const credentials = yield* GitLabCredentials.GitLabCredentials;
84+
yield* credentials.get("gitlab.com");
85+
yield* credentials.invalidate("gitlab.com");
86+
yield* credentials.get("gitlab.com");
87+
expect(runs).toHaveLength(6);
88+
}).pipe(Effect.provide(layer));
89+
});
90+
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as Cache from "effect/Cache";
2+
import * as Context from "effect/Context";
3+
import * as Duration from "effect/Duration";
4+
import * as Effect from "effect/Effect";
5+
import * as Exit from "effect/Exit";
6+
import * as Layer from "effect/Layer";
7+
import * as Option from "effect/Option";
8+
import * as Redacted from "effect/Redacted";
9+
10+
import * as HostProcess from "@t3tools/shared/HostProcess";
11+
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
12+
13+
/** How long a token is reused before `glab` is asked again. */
14+
const TOKEN_TTL = Duration.minutes(5);
15+
/** A missing login is asked again soon, so a fresh `glab auth login` applies. */
16+
const MISSING_TTL = Duration.seconds(10);
17+
18+
/**
19+
* glab hands an environment token to whichever host it is asked about. The host here comes from a
20+
* URL someone wrote into a merge request, so only the token stored for that host may answer.
21+
*/
22+
const STORED_ONLY_ENV = {
23+
GITLAB_TOKEN: "",
24+
GITLAB_ACCESS_TOKEN: "",
25+
OAUTH_TOKEN: "",
26+
CI_JOB_TOKEN: "",
27+
GLAB_ENABLE_CI_AUTOLOGIN: "false",
28+
};
29+
30+
/** The token `glab` stores for a GitLab host, used to read what that host serves to the user. */
31+
export class GitLabCredentials extends Context.Service<
32+
GitLabCredentials,
33+
{
34+
/** None when `glab` holds no login for the host. */
35+
readonly get: (host: string) => Effect.Effect<Option.Option<Redacted.Redacted<string>>>;
36+
/** Drops the held token after GitLab refused it, so the next read asks `glab` again. */
37+
readonly invalidate: (host: string) => Effect.Effect<void>;
38+
}
39+
>()("@t3tools/source-control-gitlab/server/GitLabCredentials") {}
40+
41+
/** @public Service construction is part of the canonical Effect module API. */
42+
export const make = Effect.gen(function* () {
43+
const { process } = yield* SourceControlHost.SourceControlHost;
44+
const workingDirectory = yield* HostProcess.WorkingDirectory;
45+
46+
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()));
58+
59+
const storedToken = (host: string) => glab(["config", "get", "token", "--host", host]);
60+
61+
const lookup = Effect.fn("GitLabCredentials.lookup")(function* (host: string) {
62+
if ((yield* storedToken(host)) === "") return Option.none();
63+
// An OAuth login expires within hours and glab refreshes it only on an API call.
64+
if ((yield* glab(["config", "get", "is_oauth2", "--host", host])) === "true") {
65+
yield* glab(["api", "--hostname", host, "user"]).pipe(Effect.ignore);
66+
}
67+
const token = yield* storedToken(host);
68+
return token === "" ? Option.none() : Option.some(Redacted.make(token));
69+
});
70+
71+
const cache = yield* Cache.makeWith(
72+
(host: string) => lookup(host).pipe(Effect.orElseSucceed(() => Option.none())),
73+
{
74+
capacity: 32,
75+
timeToLive: (exit) =>
76+
Exit.isSuccess(exit) && Option.isSome(exit.value) ? TOKEN_TTL : MISSING_TTL,
77+
},
78+
);
79+
80+
return GitLabCredentials.of({
81+
get: (host) => Cache.get(cache, host.trim().toLowerCase()),
82+
invalidate: (host) => Cache.invalidate(cache, host.trim().toLowerCase()),
83+
});
84+
});
85+
86+
export const layer = Layer.effect(GitLabCredentials, make);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ import * as Layer from "effect/Layer";
88
import { definition } from "../shared/definition.ts";
99

1010
import * as GitLabCli from "./GitLabCli.ts";
11+
import * as GitLabCredentials from "./GitLabCredentials.ts";
12+
import * as GitLabMediaFetch from "./gitLabMediaFetch.ts";
1113
import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
1214
import * as GitLabPullRequestProvider from "./GitLabPullRequestProvider.ts";
1315
import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
1416

15-
const services = GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer));
17+
const services = Layer.merge(
18+
GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer)),
19+
GitLabCredentials.layer,
20+
);
1621

1722
const makeInstance = Effect.all({
1823
sourceControl: GitLabSourceControlProvider.make,
1924
discovery: Effect.succeed(GitLabSourceControlProvider.discovery),
2025
pullRequests: GitLabPullRequestProvider.make,
26+
fetchMedia: GitLabMediaFetch.make,
2127
});
2228

2329
export const driver = defineSourceControlDriver({

0 commit comments

Comments
 (0)