452 lines · 18.7 KB
import { jobTokenAccess, jobTokenRepoPermission, markJobTokenPush } from "../ci/job-token";
import { enforceRateLimit, requestIp } from "../limits";
import { HttpError } from "../errors";
import { hasPermission, loadRepo } from "../repos";
import { scopedRepoPermission, tokenCredential } from "../token-scope";
import {
  directUpdateViolation,
  forcePushViolation,
  loadProtections,
  protectionFor,
  tagProtectionFor,
} from "../protection";
import { findForcePushes } from "./force-push";
import type { BranchProtection } from "../protection";
import { triggerPush } from "../events/push-trigger";
import { isHiddenRef } from "./hidden-refs";
import {
  filterAdvertisement,
  filterLsRefs,
  inspectUploadRequest,
  MalformedUploadRequestError,
} from "./advertise-filter";
import { scanPushBody } from "../security/push-protection";
import { basicAuthHeader, repoTokenSecret } from "./artifacts";
import { concatBytes, packetText, parseAllPackets, pktLine, readUntilFlush, reportStatusResponse } from "./pktline";
import { invalidateRefs } from "./refs";
import { ZERO_SHA } from "./store";
import { scratchGitPermission, scratchPushPolicy } from "../sites/scratch-git";
import type { Permission, RepoRow, Viewer } from "~/lib/types";
import { randomId } from "../crypto";
import { countingBody, haveDetector, meterGitOp } from "../metering/git";

const GIT_PATH = /^\/([^/]+)\/([^/]+?)(?:\.git)?\/(info\/refs|git-upload-pack|git-receive-pack)$/;
const MAX_COMMAND_BYTES = 1024 * 1024;
const MAX_PUSH_COMMANDS = 1000;
const MAX_TRIGGERED_REFS = 100;
const FORWARDED_REQUEST_HEADERS = ["content-type", "accept", "content-encoding", "git-protocol", "user-agent"];
const FORWARDED_RESPONSE_HEADERS = ["content-type", "cache-control", "expires", "pragma"];
const SIDEBAND_DATA = 1;
const SHA_PATTERN = /^[0-9a-f]{40}$/;