@@ -2343,4 +2344,13 @@ export function PullRequestDetailPanel({
);
+ return (
+
+ {panel}
+
+ );
}
diff --git a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx
index ef163710614ba..df7756b316c3b 100644
--- a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx
+++ b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx
@@ -1,16 +1,20 @@
import { Spinner } from "~/components/ui/spinner";
-import type {
- PullRequestActor,
- PullRequestCheck,
- PullRequestCheckStatus,
- PullRequestChecksState,
- PullRequestLabel,
- PullRequestMergeability,
- PullRequestReviewDecision,
- PullRequestState,
- ScopedThreadRef,
+import {
+ SOURCE_CONTROL_MEDIA_ASSET,
+ type AssetResource,
+ type EnvironmentId,
+ type PullRequestActor,
+ type PullRequestCheck,
+ type PullRequestCheckStatus,
+ type PullRequestChecksState,
+ type PullRequestLabel,
+ type PullRequestMergeability,
+ type PullRequestReviewDecision,
+ type PullRequestState,
+ type ScopedThreadRef,
} from "@t3tools/contracts";
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
+import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
import {
CircleCheckIcon,
CircleDashedIcon,
@@ -23,8 +27,17 @@ import {
UserRoundXIcon,
} from "lucide-react";
import { msg, useGT, useMessages } from "gt-react";
-import { Children, type CSSProperties, isValidElement, type ReactNode, useState } from "react";
-
+import {
+ Children,
+ createContext,
+ type CSSProperties,
+ isValidElement,
+ type ReactNode,
+ useContext,
+ useState,
+} from "react";
+
+import { useAssetUrlState } from "~/assets/assetUrls";
import { m, type MessageResolver } from "~/i18n/messages";
import { useOpenLink } from "~/browser/useOpenLink";
import { cn } from "~/lib/utils";
@@ -460,6 +473,13 @@ export function PullRequestReviewOutcomeBadge({
);
}
+/** Where the server fetches an avatar the client cannot load from the host itself. */
+export const PullRequestAvatarSourceContext = createContext<{
+ readonly environmentId: EnvironmentId;
+ readonly cwd: string;
+ readonly repository: string;
+} | null>(null);
+
export function PullRequestActorAvatar({
actor,
className,
@@ -467,10 +487,62 @@ export function PullRequestActorAvatar({
actor: PullRequestActor | null;
className?: string;
}) {
- const login = actor?.login ?? "ghost";
+ const source = useContext(PullRequestAvatarSourceContext);
const avatarUrl = actor?.avatarUrl ?? null;
- const [failedAvatarUrl, setFailedAvatarUrl] = useState(null);
- return avatarUrl === null || failedAvatarUrl === avatarUrl ? (
+ const fetchUrl = avatarUrl === null ? null : sourceControlClients.mediaUrl(avatarUrl)?.fetchUrl;
+ const login = actor?.login ?? "ghost";
+ return source !== null && fetchUrl != null ? (
+
+ ) : (
+
+ );
+}
+
+function HostActorAvatar({
+ environmentId,
+ resource,
+ avatarUrl,
+ login,
+ className,
+}: {
+ environmentId: EnvironmentId;
+ resource: AssetResource;
+ avatarUrl: string | null;
+ login: string;
+ className: string | undefined;
+}) {
+ const asset = useAssetUrlState(environmentId, resource);
+ return (
+
+ );
+}
+
+function ActorAvatarImage({
+ src,
+ login,
+ className,
+}: {
+ src: string | null;
+ login: string;
+ className: string | undefined;
+}) {
+ const [failedSrc, setFailedSrc] = useState(null);
+ return src === null || failedSrc === src ? (
setFailedAvatarUrl(avatarUrl)}
+ onError={() => setFailedSrc(src)}
/>
);
}
diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts
index bca514d9478ce..1a83f43f4f936 100644
--- a/packages/source-control-core/src/shared/definition.ts
+++ b/packages/source-control-core/src/shared/definition.ts
@@ -123,7 +123,7 @@ export interface ChangeRequestCheckoutInput {
readonly repositoryUrl?: string | null | undefined;
}
-/** Media a host serves only to a request carrying a credential, as `mediaUrl` reads it. */
+/** Media a client may not be able to load itself, as `mediaUrl` reads it. */
export interface SourceControlMediaUrl {
/** The canonical URL to fetch, the same for every spelling of one file. */
readonly fetchUrl: string;
@@ -387,8 +387,8 @@ export interface SourceControlDefinition {
*/
readonly canonicalRepositoryKey?: (key: string) => string | null;
/**
- * Null when `url` is not media this host keeps behind a credential. A client cannot load such
- * media itself, so it asks the server, whose driver for this host fetches it.
+ * Null when `url` is not media this host keeps behind a credential or where a remote client may
+ * not reach it. A client asks the server for such media, whose driver for this host fetches it.
*/
readonly mediaUrl?: (url: URL) => SourceControlMediaUrl | null;
/**
diff --git a/packages/source-control-gitlab/src/server/driver.ts b/packages/source-control-gitlab/src/server/driver.ts
index 2c71e348547f5..a734c2769ef97 100644
--- a/packages/source-control-gitlab/src/server/driver.ts
+++ b/packages/source-control-gitlab/src/server/driver.ts
@@ -4,22 +4,30 @@ import {
} from "@t3tools/source-control-core/server/driver";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
+import { HttpClient } from "effect/http";
import { definition } from "../shared/definition.ts";
import * as GitLabCli from "./GitLabCli.ts";
import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
import * as GitLabIssueTracker from "./GitLabIssueTracker.ts";
+import * as GitLabMediaFetch from "./gitLabMediaFetch.ts";
import * as GitLabPullRequestProvider from "./GitLabPullRequestProvider.ts";
import * as GitLabSourceControlProvider from "./GitLabSourceControlProvider.ts";
const services = GitLabPullRequestCli.layer.pipe(Layer.provideMerge(GitLabCli.layer));
-const makeInstance = Effect.all({
- sourceControl: GitLabSourceControlProvider.make,
- discovery: Effect.succeed(GitLabSourceControlProvider.discovery),
- pullRequests: GitLabPullRequestProvider.make,
- issues: GitLabIssueTracker.make,
+const makeInstance = Effect.gen(function* () {
+ const mediaHttpClient = yield* HttpClient.HttpClient;
+ return yield* Effect.all({
+ sourceControl: GitLabSourceControlProvider.make,
+ discovery: Effect.succeed(GitLabSourceControlProvider.discovery),
+ pullRequests: GitLabPullRequestProvider.make,
+ issues: GitLabIssueTracker.make,
+ fetchMedia: GitLabMediaFetch.make.pipe(
+ Effect.provideService(HttpClient.HttpClient, mediaHttpClient),
+ ),
+ });
});
export const driver = defineSourceControlDriver({
diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts
new file mode 100644
index 0000000000000..c8507f1ca778d
--- /dev/null
+++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts
@@ -0,0 +1,97 @@
+import { expect, it } from "@effect/vitest";
+import * as Effect from "effect/Effect";
+import * as Layer from "effect/Layer";
+import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
+
+import * as GitLabMediaFetch from "./gitLabMediaFetch.ts";
+
+const AVATAR = "https://git.example.test/uploads/-/system/user/avatar/857/avatar.png";
+
+const httpLayer = (onRequest: (request: HttpClientRequest.HttpClientRequest) => Response) =>
+ Layer.succeed(
+ HttpClient.HttpClient,
+ HttpClient.make((request) =>
+ Effect.sync(() => HttpClientResponse.fromWeb(request, onRequest(request))),
+ ),
+ );
+
+it.effect("loads an uploaded avatar over HTTP and never sends a GitLab token", () => {
+ const png = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
+ const requests: Array<{ url: string; authorization?: string }> = [];
+ return Effect.gen(function* () {
+ const fetchMedia = yield* GitLabMediaFetch.make;
+ const url = `${AVATAR}?v=1`;
+ const response = yield* fetchMedia({ url, headers: {} });
+ expect(requests).toEqual([{ url }]);
+ expect(response?.status).toBe(200);
+ expect(new Uint8Array(yield* response!.arrayBuffer)).toEqual(png);
+ }).pipe(
+ Effect.provide(
+ httpLayer((request) => {
+ requests.push({
+ url: request.url,
+ ...(request.headers.authorization
+ ? { authorization: request.headers.authorization }
+ : {}),
+ });
+ return new Response(png, { headers: { "content-type": "image/png" } });
+ }),
+ ),
+ );
+});
+
+it.effect("returns nothing when the host answers with a sign-in page", () =>
+ Effect.gen(function* () {
+ const fetchMedia = yield* GitLabMediaFetch.make;
+ const response = yield* fetchMedia({ url: AVATAR, headers: {} });
+ expect(response).toBeNull();
+ }).pipe(
+ Effect.provide(
+ httpLayer(
+ () =>
+ new Response("Sign in", {
+ status: 200,
+ headers: { "content-type": "text/html; charset=utf-8" },
+ }),
+ ),
+ ),
+ ),
+);
+
+it.effect("does not fetch an avatar from a loopback or link-local address", () => {
+ const requests: string[] = [];
+ return Effect.gen(function* () {
+ const fetchMedia = yield* GitLabMediaFetch.make;
+ expect(
+ yield* fetchMedia({
+ url: "http://169.254.169.254/uploads/-/system/user/avatar/857/avatar.png",
+ headers: {},
+ }),
+ ).toBeNull();
+ expect(requests).toEqual([]);
+ }).pipe(
+ Effect.provide(
+ httpLayer((request) => {
+ requests.push(request.url);
+ return new Response(null, { status: 500 });
+ }),
+ ),
+ );
+});
+
+it.effect("does not follow an avatar redirect onto a loopback or link-local address", () => {
+ const requests: string[] = [];
+ const internal = "https://169.254.169.254/uploads/-/system/user/avatar/857/avatar.png";
+ return Effect.gen(function* () {
+ const fetchMedia = yield* GitLabMediaFetch.make;
+ expect(yield* fetchMedia({ url: AVATAR, headers: {} })).toBeNull();
+ expect(requests).toEqual([AVATAR]);
+ }).pipe(
+ Effect.provide(
+ httpLayer((request) => {
+ requests.push(request.url);
+ return new Response(null, { status: 302, headers: { location: internal } });
+ }),
+ ),
+ );
+});
diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts
new file mode 100644
index 0000000000000..4aa077fd08271
--- /dev/null
+++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts
@@ -0,0 +1,66 @@
+import type { SourceControlMediaRequest } from "@t3tools/source-control-core/server/driver";
+import * as Effect from "effect/Effect";
+import {
+ FetchHttpClient,
+ HttpClient,
+ HttpClientRequest,
+ type HttpClientResponse,
+} from "effect/http";
+
+import { gitLabMediaUrl, isBlockedGitLabMediaHostname } from "../shared/gitLabMedia.ts";
+
+const MAX_REDIRECTS = 3;
+const MANUAL_REDIRECT: RequestInit = { redirect: "manual" };
+const MEDIA_CONTENT_TYPE_PATTERN = /^(?:image|video|audio)\/[\w!#$&^.+-]+$/i;
+
+const contentTypeOf = (response: HttpClientResponse.HttpClientResponse) =>
+ response.headers["content-type"]?.split(";", 1)[0]?.trim().toLowerCase() ?? "";
+
+export const make = Effect.gen(function* () {
+ const httpClient = HttpClient.withScope(yield* HttpClient.HttpClient);
+
+ const fetchFollowingRedirects = Effect.fnUntraced(function* (
+ url: string,
+ headers: Readonly>,
+ ) {
+ let target = url;
+ for (let hop = 0; ; hop += 1) {
+ const response: HttpClientResponse.HttpClientResponse | null = yield* httpClient
+ .execute(
+ HttpClientRequest.get(target).pipe(
+ HttpClientRequest.setHeaders({
+ ...headers,
+ "accept-encoding": "identity",
+ }),
+ ),
+ )
+ .pipe(
+ Effect.provideService(FetchHttpClient.RequestInit, MANUAL_REDIRECT),
+ Effect.orElseSucceed(() => null),
+ );
+ if (response === null) return null;
+ if (response.status < 300 || response.status >= 400) return response;
+ const location = response.headers.location;
+ if (!location || hop >= MAX_REDIRECTS) return null;
+ const next = URL.parse(location, target);
+ if (
+ next === null ||
+ next.protocol !== "https:" ||
+ next.username ||
+ next.password ||
+ isBlockedGitLabMediaHostname(next.hostname)
+ ) {
+ return null;
+ }
+ target = next.toString();
+ }
+ });
+
+ return Effect.fn("GitLabMediaFetch.fetchMedia")(function* (request: SourceControlMediaRequest) {
+ const url = URL.parse(request.url);
+ if (url === null || gitLabMediaUrl(url) === null) return null;
+ const response = yield* fetchFollowingRedirects(request.url, request.headers);
+ if (response === null || (response.status !== 200 && response.status !== 206)) return null;
+ return MEDIA_CONTENT_TYPE_PATTERN.test(contentTypeOf(response)) ? response : null;
+ });
+});
diff --git a/packages/source-control-gitlab/src/shared/definition.test.ts b/packages/source-control-gitlab/src/shared/definition.test.ts
index b59ade7b62b00..1675db6931cda 100644
--- a/packages/source-control-gitlab/src/shared/definition.test.ts
+++ b/packages/source-control-gitlab/src/shared/definition.test.ts
@@ -139,3 +139,32 @@ describe("GitLab remotes", () => {
expect(recognize("https://notgitlab.example.com/group/repo.git")).toBeNull();
});
});
+
+describe("GitLab media URLs", () => {
+ const media = (url: string) => definition.mediaUrl?.(new URL(url)) ?? null;
+
+ it("proxies a user's uploaded avatar and leaves every other image alone", () => {
+ expect(
+ media("https://gitlab.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1795510382"),
+ ).toEqual({
+ fetchUrl:
+ "https://gitlab.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1795510382",
+ fileName: "avatar.png",
+ });
+ expect(media("https://git.example.test/uploads/-/system/user/avatar/76/avatar.png")).toEqual({
+ fetchUrl: "https://git.example.test/uploads/-/system/user/avatar/76/avatar.png",
+ fileName: "avatar.png",
+ });
+ for (const url of [
+ "https://secure.gravatar.com/avatar/c4b495b9?s=80&d=identicon",
+ "https://gitlab.example.test/group/project/uploads/0123456789abcdef/shot.png",
+ "https://gitlab.example.test/uploads/-/system/project/avatar/12/logo.png",
+ "https://user:secret@gitlab.example.test/uploads/-/system/user/avatar/857/avatar.png",
+ "http://127.0.0.1:8080/uploads/-/system/user/avatar/857/avatar.png",
+ "http://169.254.169.254/uploads/-/system/user/avatar/857/avatar.png",
+ "http://localhost/uploads/-/system/user/avatar/857/avatar.png",
+ ]) {
+ expect(media(url), url).toBeNull();
+ }
+ });
+});
diff --git a/packages/source-control-gitlab/src/shared/definition.ts b/packages/source-control-gitlab/src/shared/definition.ts
index 31fea2dc296fb..2452748ccc22a 100644
--- a/packages/source-control-gitlab/src/shared/definition.ts
+++ b/packages/source-control-gitlab/src/shared/definition.ts
@@ -12,6 +12,7 @@ import {
projectChangeRequestHost,
remoteWebOrigin,
} from "@t3tools/source-control-core/shared/definition";
+import { gitLabMediaUrl, isGitLabHostname } from "./gitLabMedia.ts";
import { icon } from "./icon.ts";
import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts";
@@ -86,7 +87,7 @@ export const definition = defineSourceControl({
checkoutChangeRequestHost: () => null,
changeRequestHeadRef: (number) => `refs/merge-requests/${number}/head`,
recognizeRemote: ({ host, hostname }) =>
- hostname === "gitlab.com" || hostname.split(".").includes("gitlab")
+ isGitLabHostname(hostname)
? {
kind: KIND,
name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
@@ -97,4 +98,5 @@ export const definition = defineSourceControl({
isChangeRequestInProjectRepository(projectChangeRequestHost(identity, KIND), identity, link),
canReadChangeRequestOnHost: (identity, link) =>
isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link),
+ mediaUrl: gitLabMediaUrl,
});
diff --git a/packages/source-control-gitlab/src/shared/gitLabMedia.ts b/packages/source-control-gitlab/src/shared/gitLabMedia.ts
new file mode 100644
index 0000000000000..a6a511342aad3
--- /dev/null
+++ b/packages/source-control-gitlab/src/shared/gitLabMedia.ts
@@ -0,0 +1,25 @@
+import type { SourceControlMediaUrl } from "@t3tools/source-control-core/shared/definition";
+
+// A phone or a browser off the GitLab network cannot load this URL; the T3 server can.
+const USER_AVATAR_PATH = /^\/uploads\/-\/system\/user\/avatar\/\d+\/([^/]+)$/u;
+
+export const isGitLabHostname = (hostname: string) =>
+ hostname === "gitlab.com" || hostname.split(".").includes("gitlab");
+
+export const isBlockedGitLabMediaHostname = (hostname: string) => {
+ const host = hostname.toLowerCase();
+ if (host === "localhost" || host.endsWith(".localhost")) return true;
+ if (/^(?:\d{1,3}\.){3}\d{1,3}$/u.test(host)) return true;
+ return host.includes(":");
+};
+
+export function gitLabMediaUrl(url: URL): SourceControlMediaUrl | null {
+ if (url.protocol !== "https:" && url.protocol !== "http:") return null;
+ if (url.username || url.password) return null;
+ // Self-managed GitLab is often not named "gitlab", and its SSH host is not its web origin.
+ if (isBlockedGitLabMediaHostname(url.hostname)) return null;
+ const fileName = USER_AVATAR_PATH.exec(url.pathname)?.[1];
+ return fileName === undefined
+ ? null
+ : { fetchUrl: `${url.protocol}//${url.host}${url.pathname}${url.search}`, fileName };
+}