All files / src/entities/pipelines artifact.ts

100% Statements 73/73
100% Branches 44/44
100% Functions 7/7
100% Lines 68/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 17527x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x                     27x   32x 32x   1x   31x             6x   25x       27x   6x 6x   1x   5x 1x   4x 4x 4x 4x         40x 40x 40x   40x 64x                           63x 27x   27x 27x 26x 25x 25x   1x       27x 41x 41x 41x 40x 40x 40x 36x 36x 31x             12x 1x   11x 5x   5x   4x 4x   4x       6x   36x         41x       36x 5x 1x               4x     4x                           5x                            
import { enhancedFetch, GitLabTimeoutError } from '../../utils/fetch';
import { gitlab, GitLabApiError } from '../../utils/gitlab-api';
import { getGitLabBaseUrl } from '../../utils/gitlab-base-url';
import { normalizeProjectId } from '../../utils/projectIdentifier';
import { encodeApiSegment } from '../../utils/api-path';
import { parseRetryAfterSeconds } from '../../utils/retry-after';
import { BODY_TIMEOUT_MS } from '../../config';
import { StructuredToolError, type ArtifactReadRefusal } from '../../utils/error-handler';
import { currentMcpAbortSignal } from '../../mcp-server-context';
import { encodeArtifactPath, ARTIFACT_MAX_BYTES, ARTIFACT_MAX_OFFSET } from './artifact-path';
import { readArtifactWindow, ArtifactReadError } from './artifact-window';
 
interface ArtifactInput {
  project_id: string;
  job_id: string;
  artifact_path: string;
  byte_offset: number;
  byte_limit: number;
}
 
/** Validate a provider redirect before connecting, without retaining invalid signed URL data. */
export function resolveArtifactRedirect(target: URL, location: string): URL {
  let next: URL;
  try {
    next = new URL(location, target);
  } catch {
    throw new Error('Artifact server returned an invalid redirect URL');
  }
  if (
    !['http:', 'https:'].includes(next.protocol) ||
    next.username ||
    next.password ||
    next.hash ||
    (target.protocol === 'https:' && next.protocol !== 'https:')
  ) {
    throw new Error('Artifact server returned an unsafe redirect');
  }
  return next;
}
 
/** A browser link is derived from GitLab's job URL, not from a signed storage URL. */
export function artifactViewUrl(jobUrl: string, encodedPath: string): string {
  let view: URL;
  try {
    view = new URL(jobUrl);
  } catch {
    throw new Error('GitLab returned an invalid artifact-view URL');
  }
  if (!['http:', 'https:'].includes(view.protocol) || view.username || view.password) {
    throw new Error('GitLab returned an invalid artifact-view URL');
  }
  view.pathname += `/artifacts/file/${encodedPath}`;
  view.search = '';
  view.hash = '';
  return view.toString();
}
 
/** The configured GitLab supplies download locations; a caller never supplies an origin. */
async function artifactResponse(url: string, offset: number, limit: number) {
  let target = new URL(url);
  const origin = target.origin;
  let external = false;
  // Fetch's redirect limit: https://fetch.spec.whatwg.org/#http-redirect-fetch
  for (let redirects = 0; redirects <= 20; redirects++) {
    const response = await enhancedFetch(target.toString(), {
      rawBody: true,
      redirect: 'manual',
      skipAuth: external,
      redactQuery: true,
      signal: currentMcpAbortSignal(),
      rateLimitBaseUrl: external ? target.origin : getGitLabBaseUrl(),
      rateLimit: !external,
      headers: {
        Accept: '*/*',
        'Accept-Encoding': 'identity',
        Range: `bytes=${offset}-${offset + limit + 3}`,
      },
    });
    if (![301, 302, 303, 307, 308].includes(response.status)) return { response, external };
    const location = response.headers.get('location');
    // Each redirect depends on this response; finish cancellation before following its Location.
    await response.body?.cancel();
    if (!location) throw new Error('Artifact redirect has no Location');
    const next = resolveArtifactRedirect(target, location);
    external ||= next.origin !== origin;
    target = next;
  }
  throw new Error('Artifact server exceeded the redirect limit');
}
 
/** Read only the requested artifact file, retaining bytes for one UTF-8 window. */
export async function readArtifactFile(input: ArtifactInput) {
  const { project_id, job_id, artifact_path, byte_offset, byte_limit } = input;
  const encodedPath = encodeArtifactPath(artifact_path);
  const project = normalizeProjectId(project_id);
  const job = encodeApiSegment(job_id);
  const url = `${getGitLabBaseUrl()}/api/v4/projects/${project}/jobs/${job}/artifacts/${encodedPath}`;
  const { response, external } = await artifactResponse(url, byte_offset, byte_limit);
  try {
    assertDownloadStatus(response, external, input);
    return {
      project_id,
      job_id,
      artifact_path,
      ...(await readArtifactWindow(response, byte_offset, byte_limit)),
    };
  } catch (error) {
    if (error instanceof Error && 'code' in error && error.code === 'UND_ERR_BODY_TIMEOUT') {
      throw new GitLabTimeoutError('body', BODY_TIMEOUT_MS, error);
    }
    if (error instanceof ArtifactReadError) {
      await discardUnreadBody(response);
      let viewUrl: string | undefined;
      if (error.reason === 'not_text') {
        // Resolve a canonical browser link only for the refusal, not for every successful window.
        const metadata = await gitlab.get<{ web_url: string }>(`projects/${project}/jobs/${job}`);
        viewUrl = artifactViewUrl(metadata.web_url, encodedPath);
      }
      throw refusal(input, error.reason, error.message, {
        ...(viewUrl !== undefined && { artifact_view_url: viewUrl }),
      });
    }
    throw error;
  } finally {
    await discardUnreadBody(response);
  }
}
 
async function discardUnreadBody(response: Response): Promise<void> {
  if (!response.bodyUsed && !response.body?.locked) await response.body?.cancel();
}
 
function assertDownloadStatus(response: Response, external: boolean, input: ArtifactInput): void {
  if (response.ok || response.status === 416) return;
  if (external) {
    throw refusal(
      input,
      'storage_http_error',
      `Artifact storage returned HTTP ${response.status}`,
      { http_status: response.status },
    );
  }
  const wait =
    response.status === 429
      ? parseRetryAfterSeconds(response.headers.get('retry-after'), Date.now())
      : null;
  throw new GitLabApiError(
    response.status,
    '',
    `GitLab API error: ${response.status} ${response.statusText}`,
    wait === null ? undefined : Math.ceil(wait),
  );
}
 
function refusal(
  input: ArtifactInput,
  reason: string,
  message: string,
  details: Pick<ArtifactReadRefusal, 'artifact_view_url' | 'http_status'>,
): StructuredToolError {
  return new StructuredToolError({
    error_code: 'ARTIFACT_READ_REFUSED',
    reason,
    message,
    tool: 'browse_pipelines',
    action: 'artifact_file',
    project_id: input.project_id,
    job_id: input.job_id,
    artifact_path: input.artifact_path,
    max_window_bytes: ARTIFACT_MAX_BYTES,
    max_prefix_bytes: ARTIFACT_MAX_OFFSET,
    ...details,
  });
}