From 254c4445d76d5d3f32c0601704808b82bb8f394e Mon Sep 17 00:00:00 2001 From: ernest0n Date: Tue, 24 Nov 2026 21:28:00 +0400 Subject: [PATCH 1/7] fix(gitlab): All Activity avatars load through the T3 server Mobile and remote clients load GitLab user avatars as a raw image URL. A phone off the GitLab network cannot reach that host, so the image fails and the UI falls back to initials. Fetch the public avatar bytes on the server with a plain HTTP GET (no token) and serve them as the existing source-control media asset. --- .../pull-requests/PullRequestPresentation.tsx | 60 +++++++++- .../pullRequest/PullRequestDetailPanel.tsx | 12 +- .../pullRequest/pullRequestPresentation.tsx | 104 +++++++++++++++--- .../src/server/driver.ts | 18 ++- .../src/server/gitLabMediaFetch.test.ts | 67 +++++++++++ .../src/server/gitLabMediaFetch.ts | 32 ++++++ .../src/shared/definition.test.ts | 21 ++++ .../src/shared/definition.ts | 2 + .../src/shared/gitLabMedia.ts | 13 +++ 9 files changed, 302 insertions(+), 27 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..62644d680da16 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,56 @@ 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/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx index 9bc214851c74e..11482bbe78b57 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, @@ -1573,7 +1574,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..fb60993a21c53 100644 --- a/apps/web/src/components/pullRequest/pullRequestPresentation.tsx +++ b/apps/web/src/components/pullRequest/pullRequestPresentation.tsx @@ -1,15 +1,19 @@ 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 { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; import { CircleCheckIcon, CircleDashedIcon, @@ -22,8 +26,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 { cn } from "~/lib/utils"; @@ -463,6 +476,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, @@ -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-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..f232d6355f217 --- /dev/null +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts @@ -0,0 +1,67 @@ +import { expect, it } from "@effect/vitest"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; +import { HttpClient, HttpClientResponse } from "effect/http"; + +import * as GitLabMediaFetch from "./gitLabMediaFetch.ts"; + +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 = "https://git.example.test/uploads/-/system/user/avatar/857/avatar.png?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( + Layer.succeed( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.sync(() => { + requests.push({ + url: request.url, + ...(request.headers.authorization + ? { authorization: request.headers.authorization } + : {}), + }); + return HttpClientResponse.fromWeb( + request, + 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: "https://git.example.test/uploads/-/system/user/avatar/857/avatar.png", + headers: {}, + }); + expect(response).toBeNull(); + }).pipe( + Effect.provide( + Layer.succeed( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.sync(() => + HttpClientResponse.fromWeb( + request, + new Response("Sign in", { + status: 200, + headers: { "content-type": "text/html; charset=utf-8" }, + }), + ), + ), + ), + ), + ), + ), +); 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..06268f99fd7a9 --- /dev/null +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts @@ -0,0 +1,32 @@ +import type { SourceControlMediaRequest } from "@t3tools/source-control-core/server/driver"; +import * as Effect from "effect/Effect"; +import { HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/http"; + +import { gitLabMediaUrl } from "../shared/gitLabMedia.ts"; + +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); + + return Effect.fn("GitLabMediaFetch.fetchMedia")(function* (request: SourceControlMediaRequest) { + const url = URL.parse(request.url); + if (url === null || gitLabMediaUrl(url) === null) return null; + // No token: UploadsController ignores PAT, and a Private-Token would ride a redirect to object storage. + const response = yield* httpClient + .execute( + HttpClientRequest.get(request.url).pipe( + HttpClientRequest.setHeaders({ + ...request.headers, + "accept-encoding": "identity", + }), + ), + ) + .pipe(Effect.orElseSucceed(() => null)); + 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..18bb22e42a6dd 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("proxies a user's uploaded avatar 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..d6a8bc1e80d13 --- /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 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 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 e55fdff3fb5e5a1606d6eec82b923a4bd0be9981 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 24 Nov 2026 17:36:30 +0000 Subject: [PATCH 2/7] fix(mobile): GitLab avatar falls back to its own URL when the server cannot load it Co-Authored-By: Claude Opus 5.5 --- .../features/pull-requests/PullRequestPresentation.tsx | 10 ++++++++-- packages/source-control-core/src/shared/definition.ts | 6 +++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx b/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx index 62644d680da16..bffb195178785 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx @@ -83,7 +83,13 @@ function HostAvatar(props: { const asset = useAssetUrlState(props.environmentId, props.resource); return ( 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; /** From bc7fb56e2cf2cff6dbb32a43c51ab471a69e531f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 24 Nov 2026 17:42:59 +0000 Subject: [PATCH 3/7] fix(gitlab): only proxy avatars from hosts T3 recognises as GitLab The server fetches any URL mediaUrl claims, so a markdown image on an arbitrary host with an avatar-shaped path made the server request it. Co-Authored-By: Claude Opus 5.5 --- .../src/server/gitLabMediaFetch.test.ts | 4 ++-- .../src/shared/definition.test.ts | 13 ++++++++----- .../source-control-gitlab/src/shared/definition.ts | 4 ++-- .../source-control-gitlab/src/shared/gitLabMedia.ts | 5 +++++ 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts index f232d6355f217..09748b069ef1f 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts @@ -10,7 +10,7 @@ it.effect("loads an uploaded avatar over HTTP and never sends a GitLab token", ( const requests: Array<{ url: string; authorization?: string }> = []; return Effect.gen(function* () { const fetchMedia = yield* GitLabMediaFetch.make; - const url = "https://git.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1"; + const url = "https://gitlab.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1"; const response = yield* fetchMedia({ url, headers: {} }); expect(requests).toEqual([{ url }]); expect(response?.status).toBe(200); @@ -42,7 +42,7 @@ 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: "https://git.example.test/uploads/-/system/user/avatar/857/avatar.png", + url: "https://gitlab.example.test/uploads/-/system/user/avatar/857/avatar.png", headers: {}, }); expect(response).toBeNull(); diff --git a/packages/source-control-gitlab/src/shared/definition.test.ts b/packages/source-control-gitlab/src/shared/definition.test.ts index 18bb22e42a6dd..dd9deb608d476 100644 --- a/packages/source-control-gitlab/src/shared/definition.test.ts +++ b/packages/source-control-gitlab/src/shared/definition.test.ts @@ -145,16 +145,19 @@ describe("GitLab media URLs", () => { it("proxies a user's uploaded avatar and leaves every other image alone", () => { expect( - media("https://git.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1795510382"), + media("https://gitlab.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", + fetchUrl: + "https://gitlab.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", + "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://169.254.169.254/uploads/-/system/user/avatar/857/avatar.png", + "https://internal.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 d8b50473c999d..86f8d4c69a227 100644 --- a/packages/source-control-gitlab/src/shared/definition.ts +++ b/packages/source-control-gitlab/src/shared/definition.ts @@ -12,7 +12,7 @@ import { projectChangeRequestHost, remoteWebOrigin, } from "@t3tools/source-control-core/shared/definition"; -import { gitLabMediaUrl } from "./gitLabMedia.ts"; +import { gitLabMediaUrl, isGitLabHostname } from "./gitLabMedia.ts"; import { icon } from "./icon.ts"; import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts"; @@ -85,7 +85,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", diff --git a/packages/source-control-gitlab/src/shared/gitLabMedia.ts b/packages/source-control-gitlab/src/shared/gitLabMedia.ts index d6a8bc1e80d13..06e206a28c728 100644 --- a/packages/source-control-gitlab/src/shared/gitLabMedia.ts +++ b/packages/source-control-gitlab/src/shared/gitLabMedia.ts @@ -3,9 +3,14 @@ import type { SourceControlMediaUrl } from "@t3tools/source-control-core/shared/ // 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 function gitLabMediaUrl(url: URL): SourceControlMediaUrl | null { if (url.protocol !== "https:" && url.protocol !== "http:") return null; if (url.username || url.password) return null; + // The server fetches what this claims, so only hosts T3 already treats as GitLab. + if (!isGitLabHostname(url.hostname.toLowerCase())) return null; const fileName = USER_AVATAR_PATH.exec(url.pathname)?.[1]; return fileName === undefined ? null From ab43f18d27fe2d46aca0437a9a9e6d5dd19b10ca Mon Sep 17 00:00:00 2001 From: ernest0n Date: Tue, 24 Nov 2026 21:47:06 +0400 Subject: [PATCH 4/7] fix(gitlab): fetch avatars only from the checkout's GitLab origin Hosts T3 recognises as GitLab miss self-managed names such as git.fix-price.ru. Claim the avatar path on any host, then refuse the GET unless the URL host is the checkout's git remote. --- .../src/assets/sourceControlMediaResponse.ts | 18 ++-- .../source-control-core/src/server/driver.ts | 2 + .../src/server/gitLabMediaFetch.test.ts | 90 +++++++++++-------- .../src/server/gitLabMediaFetch.ts | 4 +- .../src/shared/definition.test.ts | 6 +- .../src/shared/gitLabMedia.ts | 5 +- 6 files changed, 76 insertions(+), 49 deletions(-) diff --git a/apps/server/src/assets/sourceControlMediaResponse.ts b/apps/server/src/assets/sourceControlMediaResponse.ts index 0cc653bc06189..826b4ce0cbd83 100644 --- a/apps/server/src/assets/sourceControlMediaResponse.ts +++ b/apps/server/src/assets/sourceControlMediaResponse.ts @@ -3,6 +3,7 @@ import * as Clock from "effect/Clock"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import { HttpServerResponse } from "effect/http"; +import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definition"; import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts"; import * as BuiltInDrivers from "../sourceControl/builtInDrivers.ts"; @@ -54,17 +55,22 @@ export const sourceControlMediaResponse = Effect.fn( } // Clients that predate `repository` fall back to the repository checked out at `cwd`. let repository = asset.repository; - if (repository === undefined) { - const identities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver; - const identity = yield* identities.resolve(asset.cwd); - if (identity?.provider === media.definition.kind && identity.owner && identity.name) { - repository = `${identity.owner}/${identity.name}`; - } + const identities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver; + const identity = yield* identities.resolve(asset.cwd); + if ( + repository === undefined && + identity?.provider === media.definition.kind && + identity.owner && + identity.name + ) { + repository = `${identity.owner}/${identity.name}`; } + const host = identity ? parseGitRemoteHost(identity.locator.remoteUrl)?.host : undefined; const response = yield* fetchMedia({ url: asset.url, headers: forwarded, ...(repository === undefined ? {} : { repository }), + ...(host === undefined ? {} : { host }), }); if (response === null) return HttpServerResponse.empty({ status: 502, headers }); // It carries none of the upstream entity headers: a `content-length` with no body behind it diff --git a/packages/source-control-core/src/server/driver.ts b/packages/source-control-core/src/server/driver.ts index fcf737fe2181d..409853a6e0f0b 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; + /** Git remote `host` of the checkout, so a fetch can refuse any other origin. */ + readonly host?: string; } /** The server side of a definition's `credentials.appSignIn`. */ diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts index 09748b069ef1f..8de76cc948c1a 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts @@ -1,39 +1,42 @@ import { expect, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; -import { HttpClient, HttpClientResponse } from "effect/http"; +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 HOST = "git.example.test"; + +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 = "https://gitlab.example.test/uploads/-/system/user/avatar/857/avatar.png?v=1"; - const response = yield* fetchMedia({ url, headers: {} }); + const url = `${AVATAR}?v=1`; + const response = yield* fetchMedia({ url, headers: {}, host: HOST }); expect(requests).toEqual([{ url }]); expect(response?.status).toBe(200); expect(new Uint8Array(yield* response!.arrayBuffer)).toEqual(png); }).pipe( Effect.provide( - Layer.succeed( - HttpClient.HttpClient, - HttpClient.make((request) => - Effect.sync(() => { - requests.push({ - url: request.url, - ...(request.headers.authorization - ? { authorization: request.headers.authorization } - : {}), - }); - return HttpClientResponse.fromWeb( - request, - new Response(png, { headers: { "content-type": "image/png" } }), - ); - }), - ), - ), + httpLayer((request) => { + requests.push({ + url: request.url, + ...(request.headers.authorization + ? { authorization: request.headers.authorization } + : {}), + }); + return new Response(png, { headers: { "content-type": "image/png" } }); + }), ), ); }); @@ -41,27 +44,40 @@ it.effect("loads an uploaded avatar over HTTP and never sends a GitLab token", ( 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: "https://gitlab.example.test/uploads/-/system/user/avatar/857/avatar.png", - headers: {}, - }); + const response = yield* fetchMedia({ url: AVATAR, headers: {}, host: HOST }); expect(response).toBeNull(); }).pipe( Effect.provide( - Layer.succeed( - HttpClient.HttpClient, - HttpClient.make((request) => - Effect.sync(() => - HttpClientResponse.fromWeb( - request, - new Response("Sign in", { - status: 200, - headers: { "content-type": "text/html; charset=utf-8" }, - }), - ), - ), - ), + httpLayer( + () => + new Response("Sign in", { + status: 200, + headers: { "content-type": "text/html; charset=utf-8" }, + }), ), ), ), ); + +it.effect("does not fetch an avatar on any origin other than the repository's GitLab host", () => { + 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: {}, + host: HOST, + }), + ).toBeNull(); + expect(yield* fetchMedia({ url: AVATAR, headers: {} })).toBeNull(); + expect(requests).toEqual([]); + }).pipe( + Effect.provide( + httpLayer((request) => { + requests.push(request.url); + return new Response(null, { status: 500 }); + }), + ), + ); +}); diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts index 06268f99fd7a9..562ddadb9b206 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts @@ -14,7 +14,9 @@ export const make = Effect.gen(function* () { return Effect.fn("GitLabMediaFetch.fetchMedia")(function* (request: SourceControlMediaRequest) { const url = URL.parse(request.url); - if (url === null || gitLabMediaUrl(url) === null) return null; + if (request.host === undefined || url === null || gitLabMediaUrl(url, request.host) === null) { + return null; + } // No token: UploadsController ignores PAT, and a Private-Token would ride a redirect to object storage. const response = yield* httpClient .execute( diff --git a/packages/source-control-gitlab/src/shared/definition.test.ts b/packages/source-control-gitlab/src/shared/definition.test.ts index dd9deb608d476..096899a19ea43 100644 --- a/packages/source-control-gitlab/src/shared/definition.test.ts +++ b/packages/source-control-gitlab/src/shared/definition.test.ts @@ -151,13 +151,15 @@ describe("GitLab media URLs", () => { "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://169.254.169.254/uploads/-/system/user/avatar/857/avatar.png", - "https://internal.example.test/uploads/-/system/user/avatar/857/avatar.png", ]) { expect(media(url), url).toBeNull(); } diff --git a/packages/source-control-gitlab/src/shared/gitLabMedia.ts b/packages/source-control-gitlab/src/shared/gitLabMedia.ts index 06e206a28c728..8759026199c35 100644 --- a/packages/source-control-gitlab/src/shared/gitLabMedia.ts +++ b/packages/source-control-gitlab/src/shared/gitLabMedia.ts @@ -6,11 +6,10 @@ const USER_AVATAR_PATH = /^\/uploads\/-\/system\/user\/avatar\/\d+\/([^/]+)$/u; export const isGitLabHostname = (hostname: string) => hostname === "gitlab.com" || hostname.split(".").includes("gitlab"); -export function gitLabMediaUrl(url: URL): SourceControlMediaUrl | null { +export function gitLabMediaUrl(url: URL, host?: string): SourceControlMediaUrl | null { if (url.protocol !== "https:" && url.protocol !== "http:") return null; if (url.username || url.password) return null; - // The server fetches what this claims, so only hosts T3 already treats as GitLab. - if (!isGitLabHostname(url.hostname.toLowerCase())) return null; + if (host !== undefined && url.host.toLowerCase() !== host.toLowerCase()) return null; const fileName = USER_AVATAR_PATH.exec(url.pathname)?.[1]; return fileName === undefined ? null From 438bf89e3266bed6c26868aef749de833fcc099f Mon Sep 17 00:00:00 2001 From: ernest0n Date: Tue, 24 Nov 2026 21:52:55 +0400 Subject: [PATCH 5/7] fix(gitlab): do not bind avatar fetch to the git remote host A self-managed GitLab often serves avatars on a web host that is not the SSH clone host, so requiring the checkout remote origin blocked the reported URL. Refuse loopback and IP literals instead. A failed mobile image now falls back to initials. --- .../pull-requests/PullRequestPresentation.tsx | 6 ++++-- .../src/assets/sourceControlMediaResponse.ts | 18 ++++++------------ .../source-control-core/src/server/driver.ts | 2 -- .../src/server/gitLabMediaFetch.test.ts | 9 +++------ .../src/server/gitLabMediaFetch.ts | 4 +--- .../src/shared/definition.test.ts | 3 +++ .../src/shared/gitLabMedia.ts | 12 ++++++++++-- 7 files changed, 27 insertions(+), 27 deletions(-) diff --git a/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx b/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx index bffb195178785..0dab7205ec34e 100644 --- a/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx +++ b/apps/mobile/src/features/pull-requests/PullRequestPresentation.tsx @@ -4,7 +4,7 @@ import type { AssetResource, EnvironmentId, PullRequestActor } from "@t3tools/co import IconAlertTriangle from "@tabler/icons-react-native/IconAlertTriangle"; import { Image } from "expo-image"; import { useGT } from "gt-react-native"; -import { useContext } from "react"; +import { useContext, useState } from "react"; import { View } from "react-native"; import { useResolveClassNames } from "uniwind"; @@ -108,13 +108,15 @@ function AvatarImage(props: { readonly size: number; }) { const { size } = props; - return props.uri ? ( + const [failedUri, setFailedUri] = useState(null); + return props.uri && failedUri !== props.uri ? ( setFailedUri(props.uri)} /> ) : ( >; /** `owner/name` of the change request the media is in, else of the checkout it was shown in. */ readonly repository?: string; - /** Git remote `host` of the checkout, so a fetch can refuse any other origin. */ - readonly host?: string; } /** The server side of a definition's `credentials.appSignIn`. */ diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts index 8de76cc948c1a..f08c43eb68602 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts @@ -6,7 +6,6 @@ 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 HOST = "git.example.test"; const httpLayer = (onRequest: (request: HttpClientRequest.HttpClientRequest) => Response) => Layer.succeed( @@ -22,7 +21,7 @@ it.effect("loads an uploaded avatar over HTTP and never sends a GitLab token", ( return Effect.gen(function* () { const fetchMedia = yield* GitLabMediaFetch.make; const url = `${AVATAR}?v=1`; - const response = yield* fetchMedia({ url, headers: {}, host: HOST }); + const response = yield* fetchMedia({ url, headers: {} }); expect(requests).toEqual([{ url }]); expect(response?.status).toBe(200); expect(new Uint8Array(yield* response!.arrayBuffer)).toEqual(png); @@ -44,7 +43,7 @@ it.effect("loads an uploaded avatar over HTTP and never sends a GitLab token", ( 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: {}, host: HOST }); + const response = yield* fetchMedia({ url: AVATAR, headers: {} }); expect(response).toBeNull(); }).pipe( Effect.provide( @@ -59,7 +58,7 @@ it.effect("returns nothing when the host answers with a sign-in page", () => ), ); -it.effect("does not fetch an avatar on any origin other than the repository's GitLab host", () => { +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; @@ -67,10 +66,8 @@ it.effect("does not fetch an avatar on any origin other than the repository's Gi yield* fetchMedia({ url: "http://169.254.169.254/uploads/-/system/user/avatar/857/avatar.png", headers: {}, - host: HOST, }), ).toBeNull(); - expect(yield* fetchMedia({ url: AVATAR, headers: {} })).toBeNull(); expect(requests).toEqual([]); }).pipe( Effect.provide( diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts index 562ddadb9b206..06268f99fd7a9 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts @@ -14,9 +14,7 @@ export const make = Effect.gen(function* () { return Effect.fn("GitLabMediaFetch.fetchMedia")(function* (request: SourceControlMediaRequest) { const url = URL.parse(request.url); - if (request.host === undefined || url === null || gitLabMediaUrl(url, request.host) === null) { - return null; - } + if (url === null || gitLabMediaUrl(url) === null) return null; // No token: UploadsController ignores PAT, and a Private-Token would ride a redirect to object storage. const response = yield* httpClient .execute( diff --git a/packages/source-control-gitlab/src/shared/definition.test.ts b/packages/source-control-gitlab/src/shared/definition.test.ts index 096899a19ea43..1675db6931cda 100644 --- a/packages/source-control-gitlab/src/shared/definition.test.ts +++ b/packages/source-control-gitlab/src/shared/definition.test.ts @@ -160,6 +160,9 @@ describe("GitLab media URLs", () => { "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/gitLabMedia.ts b/packages/source-control-gitlab/src/shared/gitLabMedia.ts index 8759026199c35..ae6644588718e 100644 --- a/packages/source-control-gitlab/src/shared/gitLabMedia.ts +++ b/packages/source-control-gitlab/src/shared/gitLabMedia.ts @@ -6,10 +6,18 @@ const USER_AVATAR_PATH = /^\/uploads\/-\/system\/user\/avatar\/\d+\/([^/]+)$/u; export const isGitLabHostname = (hostname: string) => hostname === "gitlab.com" || hostname.split(".").includes("gitlab"); -export function gitLabMediaUrl(url: URL, host?: string): SourceControlMediaUrl | null { +const isBlockedHostname = (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; - if (host !== undefined && url.host.toLowerCase() !== host.toLowerCase()) return null; + // Self-managed GitLab is often not named "gitlab", and its SSH host is not its web origin. + if (isBlockedHostname(url.hostname)) return null; const fileName = USER_AVATAR_PATH.exec(url.pathname)?.[1]; return fileName === undefined ? null From 0a37ac48820267e6603a9f5b6100257394688eee Mon Sep 17 00:00:00 2001 From: ernest0n Date: Tue, 24 Nov 2026 21:56:17 +0400 Subject: [PATCH 6/7] fix(gitlab): do not follow avatar redirects to blocked hosts FetchHttpClient follows 302s by default, so a GitLab Location of a loopback or link-local address would still be requested. Follow redirects by hand, the same way GitHub media does, and refuse hops that are not https, carry userinfo, or hit a blocked host. --- .../src/server/gitLabMediaFetch.test.ts | 17 ++++++ .../src/server/gitLabMediaFetch.ts | 60 +++++++++++++++---- .../src/shared/gitLabMedia.ts | 4 +- 3 files changed, 66 insertions(+), 15 deletions(-) diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts index f08c43eb68602..90ea4e28681e7 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts @@ -78,3 +78,20 @@ it.effect("does not fetch an avatar from a loopback or link-local address", () = ), ); }); + +it.effect("does not follow an avatar redirect onto a loopback or link-local address", () => { + const requests: string[] = []; + const internal = "http://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 index 06268f99fd7a9..4aa077fd08271 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.ts @@ -1,9 +1,16 @@ import type { SourceControlMediaRequest } from "@t3tools/source-control-core/server/driver"; import * as Effect from "effect/Effect"; -import { HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/http"; +import { + FetchHttpClient, + HttpClient, + HttpClientRequest, + type HttpClientResponse, +} from "effect/http"; -import { gitLabMediaUrl } from "../shared/gitLabMedia.ts"; +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) => @@ -12,20 +19,47 @@ const contentTypeOf = (response: HttpClientResponse.HttpClientResponse) => 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; - // No token: UploadsController ignores PAT, and a Private-Token would ride a redirect to object storage. - const response = yield* httpClient - .execute( - HttpClientRequest.get(request.url).pipe( - HttpClientRequest.setHeaders({ - ...request.headers, - "accept-encoding": "identity", - }), - ), - ) - .pipe(Effect.orElseSucceed(() => 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/gitLabMedia.ts b/packages/source-control-gitlab/src/shared/gitLabMedia.ts index ae6644588718e..a6a511342aad3 100644 --- a/packages/source-control-gitlab/src/shared/gitLabMedia.ts +++ b/packages/source-control-gitlab/src/shared/gitLabMedia.ts @@ -6,7 +6,7 @@ const USER_AVATAR_PATH = /^\/uploads\/-\/system\/user\/avatar\/\d+\/([^/]+)$/u; export const isGitLabHostname = (hostname: string) => hostname === "gitlab.com" || hostname.split(".").includes("gitlab"); -const isBlockedHostname = (hostname: string) => { +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; @@ -17,7 +17,7 @@ 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 (isBlockedHostname(url.hostname)) return null; + if (isBlockedGitLabMediaHostname(url.hostname)) return null; const fileName = USER_AVATAR_PATH.exec(url.pathname)?.[1]; return fileName === undefined ? null From 2932993422f73bb033401ffe88c6756a94d8c434 Mon Sep 17 00:00:00 2001 From: ernest0n Date: Tue, 24 Nov 2026 22:09:15 +0400 Subject: [PATCH 7/7] fix(gitlab): exercise avatar redirect hostname checks over https The redirect fixture used http://169.254.169.254, so the hop was rejected on scheme before the blocked-hostname check ran. --- .../source-control-gitlab/src/server/gitLabMediaFetch.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts index 90ea4e28681e7..c8507f1ca778d 100644 --- a/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts +++ b/packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts @@ -81,7 +81,7 @@ it.effect("does not fetch an avatar from a loopback or link-local address", () = it.effect("does not follow an avatar redirect onto a loopback or link-local address", () => { const requests: string[] = []; - const internal = "http://169.254.169.254/uploads/-/system/user/avatar/857/avatar.png"; + 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();