From edaadf7bd31185de32f95cc03ce04be181e78345 Mon Sep 17 00:00:00 2001 From: ernest0n Date: Tue, 24 Nov 2026 16:46:18 +0400 Subject: [PATCH 1/2] fix(gitlab): All Activity avatars load with the GitLab login A GitLab user's uploaded avatar is served from /uploads/-/system/user/avatar// on the instance. Web and mobile loaded it straight from the browser with no credential, so a host that requires sign-in answered with its login page and the merge request timeline fell back to initials. GitLab now claims those avatar URLs through the definition's mediaUrl, and its driver fetches them with `glab api `, which sends the host's token. Inside a merge request, web PullRequestActorAvatar and mobile PullRequestAvatar load a claimed avatar through the source control media asset, the same way merge request images already load. Gravatar and other public avatars still load directly. Made-with: claudeAgent/claude-opus-5-5 --- .../pull-requests/PullRequestPresentation.tsx | 66 +++++++++++- apps/server/src/assets/AssetAccess.test.ts | 7 +- .../src/assets/sourceControlMediaResponse.ts | 1 + .../pullRequest/PullRequestDetailPanel.tsx | 12 ++- .../pullRequest/pullRequestPresentation.tsx | 102 +++++++++++++++--- .../source-control-core/src/server/driver.ts | 2 + .../src/server/GitLabCli.ts | 2 + .../src/server/driver.ts | 2 + .../src/server/gitLabMediaFetch.test.ts | 55 ++++++++++ .../src/server/gitLabMediaFetch.ts | 33 ++++++ .../src/shared/definition.test.ts | 21 ++++ .../src/shared/definition.ts | 2 + .../src/shared/gitLabMedia.ts | 13 +++ 13 files changed, 296 insertions(+), 22 deletions(-) create mode 100644 packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts create mode 100644 packages/source-control-gitlab/src/server/gitLabMediaFetch.ts create mode 100644 packages/source-control-gitlab/src/shared/gitLabMedia.ts diff --git a/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx b/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx index f9d15a8abc4d9..c4cdb480dedf4 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx @@ -1,9 +1,10 @@ import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients"; -import type { PullRequestActor } from "@t3tools/contracts"; +import type { AssetResource, EnvironmentId, PullRequestActor } from "@t3tools/contracts"; import IconAlertTriangle from "@tabler/icons-react-native/IconAlertTriangle"; import { Image } from "expo-image"; import { useGT } from "gt-react-native"; +import { useContext } from "react"; import { View } from "react-native"; import { useResolveClassNames } from "uniwind"; @@ -11,6 +12,10 @@ import { AppText as Text } from "../../components/ui/AppText"; import { cn } from "../../lib/cn"; import { changeRequestTermKind } from "../../lib/changeRequestTerm"; import { EmptyState } from "../../components/ui/EmptyState"; +import { assetImageCacheKey } from "../../lib/assetImageSource"; +import { useAssetUrlState } from "../../state/assets"; +import { PullRequestMediaContext } from "./PullRequestMedia"; +import { pullRequestMediaSource } from "./pullRequestMediaSource"; export function PullRequestReadStatus(props: { readonly pending: boolean; @@ -52,11 +57,62 @@ export function PullRequestAvatar(props: { readonly size?: number; }) { const size = props.size ?? 24; - return props.actor?.avatarUrl ? ( + const media = useContext(PullRequestMediaContext); + const avatarUrl = props.actor?.avatarUrl; + const source = avatarUrl ? pullRequestMediaSource(avatarUrl, media) : null; + const resource = source && "resource" in source ? (source.resource ?? null) : null; + if (media !== null && resource !== null) { + return ( + + ); + } + return ; +} + +function HostAvatar(props: { + readonly environmentId: EnvironmentId; + readonly resource: AssetResource; + readonly actor: PullRequestActor | null; + readonly size: number; +}) { + const asset = useAssetUrlState(props.environmentId, props.resource); + return ( + + ); +} + +function AvatarImage(props: { + readonly uri: string | null; + readonly cacheKey?: string | undefined; + readonly actor: PullRequestActor | null; + readonly size: number; +}) { + const size = props.size; + return props.uri ? ( diff --git a/apps/server/src/assets/AssetAccess.test.ts b/apps/server/src/assets/AssetAccess.test.ts index e70be7cb9f1fc..10d44493a3b0d 100644 --- a/apps/server/src/assets/AssetAccess.test.ts +++ b/apps/server/src/assets/AssetAccess.test.ts @@ -1957,7 +1957,12 @@ describe("AssetAccess", () => { cookie: "session", }); expect(fetched).toEqual([ - { url: media.fetchUrl, headers: { range: "bytes=0-4" }, repository: "owner/repo" }, + { + url: media.fetchUrl, + headers: { range: "bytes=0-4" }, + cwd: "/repo", + repository: "owner/repo", + }, ]); expect(response.status).toBe(206); // The upstream sent no type, so it comes from the file name the host gave the media. diff --git a/apps/server/src/assets/sourceControlMediaResponse.ts b/apps/server/src/assets/sourceControlMediaResponse.ts index 0cc653bc06189..9b40a888e380e 100644 --- a/apps/server/src/assets/sourceControlMediaResponse.ts +++ b/apps/server/src/assets/sourceControlMediaResponse.ts @@ -64,6 +64,7 @@ export const sourceControlMediaResponse = Effect.fn( const response = yield* fetchMedia({ url: asset.url, headers: forwarded, + cwd: asset.cwd, ...(repository === undefined ? {} : { repository }), }); if (response === null) return HttpServerResponse.empty({ status: 502, headers }); diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx index 729c6b4157876..63293acab6801 100644 --- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx +++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx @@ -160,6 +160,7 @@ import { resolvePickableEnvironments } from "./pullRequestProjectAssignment.logi import { PullRequestChecksPopover } from "./PullRequestChecksPopover"; import { PullRequestActorLabel, + PullRequestAvatarSourceContext, PullRequestMetaLine, PullRequestReviewOutcomeIcon, pullRequestChecksState, @@ -1569,7 +1570,7 @@ export function PullRequestDetailPanel({ ); } - return ( + const panel = (
{threadPickerOpen && detail ? ( ); + return ( + + {panel} + + ); } diff --git a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx index 32f40d02d5797..91b3573483162 100644 --- a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx +++ b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx @@ -1,13 +1,16 @@ import { Spinner } from "~/components/ui/spinner"; -import type { - PullRequestActor, - PullRequestCheck, - PullRequestCheckStatus, - PullRequestChecksState, - PullRequestLabel, - PullRequestMergeability, - PullRequestReviewDecision, - PullRequestState, +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, } from "@t3tools/contracts"; import { formatDiffCount } from "@t3tools/client-runtime/diff-count"; import { @@ -22,7 +25,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 { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; +import { useAssetUrlState } from "~/assets/assetUrls"; import { m, type MessageResolver } from "~/i18n/messages"; import { cn } from "~/lib/utils"; @@ -463,6 +476,13 @@ export function PullRequestReviewOutcomeBadge({ ); } +/** Where the server fetches an avatar the host serves only to a signed-in request. */ +export const PullRequestAvatarSourceContext = createContext<{ + readonly environmentId: EnvironmentId; + readonly cwd: string; + readonly repository: string; +} | null>(null); + export function PullRequestActorAvatar({ actor, className, @@ -470,10 +490,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/server/driver.ts b/packages/source-control-core/src/server/driver.ts index fcf737fe2181d..cd4cc83d3e674 100644 --- a/packages/source-control-core/src/server/driver.ts +++ b/packages/source-control-core/src/server/driver.ts @@ -29,6 +29,8 @@ export interface SourceControlMediaRequest { readonly headers: Readonly>; /** `owner/name` of the change request the media is in, else of the checkout it was shown in. */ readonly repository?: string; + /** The checkout the media was shown in, for a host that fetches through its CLI. */ + readonly cwd?: string; } /** The server side of a definition's `credentials.appSignIn`. */ diff --git a/packages/source-control-gitlab/src/server/GitLabCli.ts b/packages/source-control-gitlab/src/server/GitLabCli.ts index 76bd56b699d42..1948737632cea 100644 --- a/packages/source-control-gitlab/src/server/GitLabCli.ts +++ b/packages/source-control-gitlab/src/server/GitLabCli.ts @@ -313,6 +313,7 @@ export class GitLabCli extends Context.Service< /** Keeps sensitive payloads out of argv. */ readonly stdin?: string; readonly maxOutputBytes?: number; + readonly onStdoutChunk?: (chunk: Uint8Array) => void; }) => Effect.Effect; readonly listMergeRequests: (input: { @@ -571,6 +572,7 @@ export const make = Effect.gen(function* () { timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS, ...(input.stdin === undefined ? {} : { stdin: input.stdin }), ...(input.maxOutputBytes === undefined ? {} : { maxOutputBytes: input.maxOutputBytes }), + ...(input.onStdoutChunk === undefined ? {} : { onStdoutChunk: input.onStdoutChunk }), }) .pipe(Effect.mapError(mapError)); }); diff --git a/packages/source-control-gitlab/src/server/driver.ts b/packages/source-control-gitlab/src/server/driver.ts index 2c71e348547f5..df90c10703ae8 100644 --- a/packages/source-control-gitlab/src/server/driver.ts +++ b/packages/source-control-gitlab/src/server/driver.ts @@ -10,6 +10,7 @@ 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"; @@ -20,6 +21,7 @@ const makeInstance = Effect.all({ discovery: Effect.succeed(GitLabSourceControlProvider.discovery), pullRequests: GitLabPullRequestProvider.make, issues: GitLabIssueTracker.make, + fetchMedia: GitLabMediaFetch.make, }); 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..4429dae15928e --- /dev/null +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts @@ -0,0 +1,55 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { assert, it, afterEach, vi } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import { ChildProcessSpawner } from "effect/process"; + +import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; +import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; +import * as GitLabCli from "./GitLabCli.ts"; +import * as GitLabMediaFetch from "./gitLabMediaFetch.ts"; + +const mockedRun = vi.fn(); +const layer = it.layer( + GitLabCli.layer.pipe( + Layer.provide(TestSourceControlHost.layer({ process: { run: mockedRun } })), + Layer.provideMerge(NodeServices.layer), + ), +); + +afterEach(() => { + mockedRun.mockReset(); +}); + +layer("GitLabMediaFetch", (it) => { + it.effect("reads an uploaded avatar's bytes through glab, signed in to its host", () => + Effect.gen(function* () { + const png = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0xff]); + mockedRun.mockImplementationOnce((input) => + Effect.sync(() => { + input.onStdoutChunk?.(png); + return { + exitCode: ChildProcessSpawner.ExitCode(0), + stdout: new TextDecoder().decode(png), + stderr: "", + stdoutTruncated: false, + stderrTruncated: false, + }; + }), + ); + const url = "https://git.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1"; + const fetchMedia = yield* GitLabMediaFetch.make; + const response = yield* fetchMedia({ url, headers: {}, cwd: "/repo" }); + + assert.deepStrictEqual(mockedRun.mock.calls[0]?.[0].args, [ + "api", + "--hostname", + "git.example.test", + url, + ]); + assert.strictEqual(response?.status, 200); + if (response === null) return; + assert.deepStrictEqual(new Uint8Array(yield* response.arrayBuffer), png); + }), + ); +}); 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..77fee7b424d92 --- /dev/null +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts @@ -0,0 +1,33 @@ +import type { SourceControlMediaRequest } from "@t3tools/source-control-core/server/driver"; +import * as Effect from "effect/Effect"; +import { HttpClientRequest, HttpClientResponse } from "effect/http"; + +import { gitLabMediaUrl } from "../shared/gitLabMedia.ts"; +import * as GitLabCli from "./GitLabCli.ts"; + +const MAX_MEDIA_BYTES = 8 * 1024 * 1024; + +export const make = Effect.gen(function* () { + const cli = yield* GitLabCli.GitLabCli; + + return Effect.fn("GitLabMediaFetch.fetchMedia")(function* (request: SourceControlMediaRequest) { + const url = URL.parse(request.url); + if (url === null || gitLabMediaUrl(url) === null || request.cwd === undefined) return null; + const chunks: Array = []; + const result = yield* cli + .execute({ + cwd: request.cwd, + callerNamedHost: url.host, + // glab sends the host's token to a full URL on that host, not only to `/api/v4` paths. + args: ["api", "--hostname", url.host, request.url], + maxOutputBytes: MAX_MEDIA_BYTES, + onStdoutChunk: (chunk) => chunks.push(chunk), + }) + .pipe(Effect.orElseSucceed(() => null)); + if (result === null || result.stdoutTruncated) return null; + return HttpClientResponse.fromWeb( + HttpClientRequest.get(request.url), + new Response(Buffer.concat(chunks), { status: 200 }), + ); + }); +}); diff --git a/packages/source-control-gitlab/src/shared/definition.test.ts b/packages/source-control-gitlab/src/shared/definition.test.ts index b59ade7b62b00..8a459c8f0193c 100644 --- a/packages/source-control-gitlab/src/shared/definition.test.ts +++ b/packages/source-control-gitlab/src/shared/definition.test.ts @@ -139,3 +139,24 @@ 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("fetches a user's uploaded avatar through the host and leaves every other image alone", () => { + expect( + media("https://git.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1795510382"), + ).toEqual({ + fetchUrl: "https://git.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1795510382", + fileName: "avatar.png", + }); + for (const url of [ + "https://secure.gravatar.com/avatar/c4b495b9?s=80&d=identicon", + "https://git.example.test/group/project/uploads/0123456789abcdef/shot.png", + "https://git.example.test/uploads/-/system/project/avatar/12/logo.png", + "https://user:secret@git.example.test/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 f879329b3f53f..d8b50473c999d 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 } from "./gitLabMedia.ts"; import { icon } from "./icon.ts"; import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts"; @@ -95,4 +96,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..9778ee1a88def --- /dev/null +++ b/packages/source-control-gitlab/src/shared/gitLabMedia.ts @@ -0,0 +1,13 @@ +import type { SourceControlMediaUrl } from "@t3tools/source-control-core/shared/definition"; + +// A host that requires sign-in serves a user's uploaded avatar only to a signed-in request. +const USER_AVATAR_PATH = /^\/uploads\/-\/system\/user\/avatar\/\d+\/([^/]+)$/u; + +export function gitLabMediaUrl(url: URL): SourceControlMediaUrl | null { + if (url.protocol !== "https:" && url.protocol !== "http:") return null; + if (url.username || url.password) 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 }; +} From 09d56e8f009986754fd26351f8a48e73fd4f00f5 Mon Sep 17 00:00:00 2001 From: ernest0n Date: Tue, 24 Nov 2026 17:01:50 +0400 Subject: [PATCH 2/2] fix(gitlab): stop buffering oversized avatar bytes from glab onStdoutChunk receives every stdout byte, including those past maxOutputBytes. Stop retaining chunks once the media cap is exceeded; the existing truncation check still rejects the response. --- .../source-control-gitlab/src/server/gitLabMediaFetch.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts index 77fee7b424d92..11f472439776b 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts @@ -14,6 +14,7 @@ export const make = Effect.gen(function* () { const url = URL.parse(request.url); if (url === null || gitLabMediaUrl(url) === null || request.cwd === undefined) return null; const chunks: Array = []; + let retainedBytes = 0; const result = yield* cli .execute({ cwd: request.cwd, @@ -21,7 +22,11 @@ export const make = Effect.gen(function* () { // glab sends the host's token to a full URL on that host, not only to `/api/v4` paths. args: ["api", "--hostname", url.host, request.url], maxOutputBytes: MAX_MEDIA_BYTES, - onStdoutChunk: (chunk) => chunks.push(chunk), + onStdoutChunk: (chunk) => { + if (retainedBytes + chunk.byteLength > MAX_MEDIA_BYTES) return; + retainedBytes += chunk.byteLength; + chunks.push(chunk); + }, }) .pipe(Effect.orElseSucceed(() => null)); if (result === null || result.stdoutTruncated) return null;