Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix: GitLab merge request images and videos load in private projects #18793

ClosedRasalas(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/18793/headopened View on GitHub

Problem

Images and videos uploaded to a private GitLab project's merge request show as "Image unavailable" / "Video unavailable" in the pull request view. GitLab writes uploads as project-relative links (/uploads/<secret>/<name>), which the clients treated as local paths. Even made absolute, a private project only serves them to a signed-in request; without a session GitLab redirects to /users/sign_in.

Change

  • Web and mobile resolve relative /uploads/ links in GitLab bodies and comments against the project URL. Both clients then map the URL to a new gitlab-media asset, the same way github-media works (one shared pullRequestMediaResource helper in client-runtime).
  • The server fetches the upload from GitLab's upload API, GET /projects/:id/uploads/:secret/:filename. That route exists since GitLab 17.4 and needs only Guest access. The token is the one glab stores for the upload's own host. Ambient GITLAB_TOKEN-style variables are ignored, because the host comes from text someone wrote into the merge request. OAuth logins are refreshed before use, and a 401 drops the cached token.
  • Anything the API cannot serve (no glab login for the host, GitLab before 17.4) answers with a redirect to the upload URL itself, so public projects keep working as before.
  • The redirect-following, header forwarding and content-type checks of the existing GitHub media proxy are shared rather than copied (GitHubMediaFetch.ts → SourceControlMedia.ts). The token never leaves the GitLab origin, including across object-storage redirects.

GraphQL was considered and does not help here: GitLab's GraphQL API does not return file contents, so the REST upload route is the only API path to the bytes.

Scope and approval

This replaces #15270, which was closed during PR cleanup because it did not state the model and harness. It is the GitLab follow-up to #11374, whose closing comment asked for a fresh, focused patch, rebuilt on the architecture #11706 introduced for GitHub media. Related: #11412. Contract, server, web and mobile changes all serve the same fix: uploads in the existing merge request view. Desktop uses the web renderer.

Not covered: GitLab installs under a relative URL root (https://host/gitlab/...) fall back to the redirect.

Verification

Against a private, self-hosted GitLab 19.4 merge request with an uploaded PNG and a six-second MP4, in T3's Browser panel (web client):

Before (main)After
Uploads show as unavailableImage renders and video loads
  • After: both elements load from signed gitlab-media asset URLs. The image decodes at 180×180. The video plays (reached 1.6 s), seeks to 4.0 s, and reports no media error.
  • The upload API with the stored bearer token returned 200 for the image and 206 for a video range request. Without a token it returned 404, and the web URL without a session redirected to /users/sign_in.

Focused tests (79 passing): URL parsing and link resolution, glab credential lookup (stored token only, OAuth refresh, invalidation), the proxy (API URL and bearer token, token kept off a storage redirect, redirect fallback on no login / 404 / 401), signing and refusal of non-upload URLs, and mobile preview sources.

vp test run packages/shared/src/gitlabMedia.test.ts packages/source-control-gitlab/src/server/GitLabCredentials.test.ts apps/server/src/assets/AssetAccess.test.ts packages/client-runtime/src/pullRequestMarkdown.test.ts apps/mobile/src/features/pull-requests/pullRequestMediaSource.test.ts

Typecheck passes for server, web, mobile, contracts, shared, client-runtime and source-control-gitlab. Targeted lint has no errors. Not run on a native mobile device or the packaged desktop app.

Written by Claude Opus 5.5 (claude-opus-5-5) in Claude Code, run from T3 Code.

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    2. @@ -0,0 +51,21 @@
      51+/** The API route that serves the upload to any project member (GitLab 17.4+). */
      52+export function gitlabUploadApiUrl(upload: GitLabUpload): string {
      53+ return `${upload.origin}/api/v4/projects/${encodeURIComponent(upload.project)}/uploads/${upload.secret}/${upload.fileSegment}`;
      54+}
      55+
      56+export function gitlabMediaFileName(fetchUrl: string): string {
      57+ const segment = new URL(fetchUrl).pathname.split("/").pop() ?? "";
      58+ let decoded: string;
      59+ try {
      60+ decoded = decodeURIComponent(segment);
      61+ } catch {
      62+ decoded = segment;
      63+ }
      64+ const name = decoded.replace(/[\p{Cc}\\/]/gu, "");
      65+ return name.length > 0 ? name : "gitlab-upload";
      66+}
      67+
      68+/** Rewrites GitLab's project-relative upload links against the project's web URL. */
      69+export function resolveGitLabUploadLinks(markdown: string, projectUrl: string): string {
      70+ const base = projectUrl.replace(/\/+$/u, "");
      71+ return markdown.replace(
      macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      🟡 Medium src/gitlabMedia.ts:71

      resolveGitLabUploadLinks rewrites /uploads/... inside fenced, indented, and inline code, so displayed or copied examples no longer preserve the author's original text. The raw markdown.replace runs before splitPullRequestBody can guard code blocks; restrict rewriting to actual link and HTML destinations outside code.

      🤖 Copy this AI Prompt to have your agent fix this:
      In file @packages/shared/src/gitlabMedia.ts around line 71:
      
      `resolveGitLabUploadLinks` rewrites `/uploads/...` inside fenced, indented, and inline code, so displayed or copied examples no longer preserve the author's original text. The raw `markdown.replace` runs before `splitPullRequestBody` can guard code blocks; restrict rewriting to actual link and HTML destinations outside code.

      Fixed in 5595c42e11: fenced blocks and inline code spans are now left as written, with a test covering both. Indented lines are still resolved on purpose, since a four-space indent is just as often an image in a nested list item as a code block.

      Sorry, I'm unable to act on this request because you do not have permissions within this repository.

    3. Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This PR adds a substantial production path for fetching private GitLab media with stored host credentials, including new asset signing, redirect handling, and web/mobile rendering behavior. The authentication-sensitive scope and an unresolved Markdown-rewriting correctness finding require human review.

      Not approved because:

      • 1 blocking correctness issue found at or above your repo's Minimum Blocking Severity
      Adjust the Minimum Blocking Severity for this repo — including turning it Off — in [Settings](https://app.macroscope.com/pingdotgg/settings#Repos). You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. Note

      This comment is posted by Julius' dot

      Closing for missing prior maintainer triage under Establish the problem and scope first. This is a substantial bug fix: it adds GitLab credential discovery, caching and OAuth refresh, plus authenticated upload delivery through a new signed asset type. It therefore needs a maintainer-triaged issue establishing the failure and intended behavior.

      #11412 has no maintainer triage response. The closing comment on #11374 asks you to check the current direction before returning with a fresh patch; it does not supply that approval. #11706 covers GitHub media and explicitly excludes the GitLab work.

      The private-upload problem and supplied before/after evidence are useful, and the changes address one coherent problem. Please get maintainer triage of #11412 establishing the intended GitLab behavior, link that response here, and request reconsideration under the closure guidance.

    5. juliusmarminge closed this

Sign in to join this conversation.