import { Option } from "effect"; import { GitRemote } from "../GitRemote.js"; /** One `git config --list --show-scope -z` record as printed: its scope token is not yet checked. */ interface ScopedEntry { readonly scope: string; readonly key: string; readonly value: string; } /** The scopes a repository's own files (and what they include) report. */ const REPOSITORY_SCOPES: ReadonlySet = new Set(["local", "worktree"]); /** * The keys untrusted mode lets a repository's own config (local and worktree scope) hold, each with why: what git * writes for an ordinary checkout, a few settings a person or a tool commonly puts on one, and keys whose program the * mode pins regardless. Matched on git's listed key, case-insensitively, with any subsection (`.+` under the `s` flag * crosses a newline). Every other key fails the member before it spawns, so a key nobody has thought of fails closed. * To extend it, add a key only when its value can name no program, path git executes or helper transport, or add a * `REFUSED_VALUES` row that refuses the values that can. */ const ALLOWED_KEYS: ReadonlyArray = [ // git init writes these on every platform (symlinks where the filesystem lacks them). /^core\.(repositoryformatversion|filemode|bare|logallrefupdates|ignorecase|precomposeunicode|symlinks)$/i, // git writes core.worktree into a submodule's gitdir; untrusted mode's work-tree check vets where it points. /^core\.worktree$/i, // clone --sparse and sparse-checkout write these (the last into config.worktree). /^core\.(sparsecheckout|sparsecheckoutcone)$/i, // Line-ending conversion and its checks: values git interprets (true, false, input, lf, crlf, warn), never a program. /^core\.(autocrlf|eol|safecrlf)$/i, // sparse-checkout disable writes index.sparse=false into config.worktree; index.version is a format number. /^index\.(sparse|version)$/i, // A branch name for the next init, a fast-forward policy and signing switches: data only. The signing program // itself is pinned to the user's. /^init\.defaultbranch$/i, /^pull\.ff$/i, /^(commit|tag)\.gpgsign$/i, // Untrusted mode pins core.hooksPath to /dev/null on every spawn, so the repository's value never applies. /^core\.hookspath$/i, // init --object-format, init --ref-format and sparse-checkout write these. Never partialclone: lazy fetches. /^extensions\.(objectformat|refstorage|worktreeconfig)$/i, // clone and remote add write url and fetch; pushurl is the same address for pushes. Values vetted below. /^remote\..+\.(url|pushurl|fetch)$/is, // clone --no-tags writes tagopt; prune and prunetags are booleans fetch reads. tagOpt is vetted below. /^remote\..+\.(tagopt|prune|prunetags)$/is, // clone and push --set-upstream write remote and merge; rebase is a person's choice. Values vetted below. /^branch\..+\.(remote|merge|rebase)$/is, // VS Code records the branch a feature branch started from, and its GitHub Pull Requests extension the pull // request a branch tracks: a ref name and an owner#repo#number, data only. /^branch\..+\.(vscode-merge-base|github-pr-owner-number)$/is, // A person's choice of how a pull integrates; the kit's pull spells its own flag anyway. Vetted below. /^pull\.rebase$/i, // clone --recurse-submodules, submodule add and init write these. Values vetted below. /^submodule\.active$/i, /^submodule\..+\.(url|active|update|branch)$/is, // git lfs install --local writes its format version, and its filter with git-lfs's standard commands (vetted // below to those exact values); untrusted mode empties a repository-defined filter anyway. /^lfs\.repositoryformatversion$/i, /^filter\.lfs\.(clean|smudge|process|required)$/i, // Data only. /^user\.(name|email)$/i, ]; /** git-lfs's own filter entries, as `git lfs install` writes them (git-lfs-install(1), git-lfs-config(5)). */ const LFS_FILTER: ReadonlyMap = new Map([ ["filter.lfs.clean", "git-lfs clean -- %f"], ["filter.lfs.smudge", "git-lfs smudge -- %f"], ["filter.lfs.process", "git-lfs filter-process"], ["filter.lfs.required", "true"], ]); /** The transports a repository's own remote address may name: network ones, with no remote helper git would run. */ const NETWORK_SCHEMES: ReadonlySet = new Set(["https", "http", "ssh", "git", "git+ssh", "ssh+git"]); /** * Whether a repository's own remote address is a network URL or an scp-like address. A local path and a `file://` * URL are refused, and so is an address with a DOS drive prefix: a push to one runs that repository's hooks under * its own config. An address a caller passes to a * member is the caller's and is never held to this. */ const isNetworkRemoteAddress = (value: string): boolean => !hasDosDrivePrefix(value) && Option.match(GitRemote.parse(value), { onNone: () => false, // git dispatches a scheme case-sensitively: `SSH://` names a remote helper, not ssh. onSome: (form) => form._tag === "GitRemoteScp" || (form._tag === "GitRemoteUrl" && NETWORK_SCHEMES.has(form.scheme)), }); /** * Whether an address starts with a DOS drive prefix (`C:`), which Git for Windows reads as a local path * (`has_dos_drive_prefix` in `url_is_local_not_ssh`) although it parses as an scp-like address with a one-letter * host. Refused on every platform: the same config can be read on Windows. * * @internal */ export const hasDosDrivePrefix = (value: string): boolean => /^[A-Za-z]:/.test(value); /** Allowed keys whose value can still name a program, a local repository or a refused transport. */ const REFUSED_VALUES: ReadonlyArray boolean]> = [ [/^(remote\..+\.(url|pushurl)|submodule\..+\.url)$/is, (_, value) => !isNetworkRemoteAddress(value)], [/^submodule\..+\.update$/is, (_, value) => value.trimStart().startsWith("!")], // An interactive rebase opens the repository's editor. [/^(branch\..+\.rebase|pull\.rebase)$/is, (_, value) => /^\s*(i|interactive)\s*$/i.test(value)], // git reads only these two; anything else is not a tag option. [/^remote\..+\.tagopt$/is, (_, value) => value !== "--no-tags" && value !== "--tags"], [/^filter\.lfs\./i, (key, value) => LFS_FILTER.get(key.toLowerCase()) !== value], ]; /** * The repository-scope keys of a scoped listing outside untrusted mode's allow-list, or with a refused value: each * once, sorted, never a value. * * @internal */ export const refusedRepositoryKeys = (config: ReadonlyArray): ReadonlyArray => { const refused = new Set(); for (const entry of config) { if (!REPOSITORY_SCOPES.has(entry.scope)) continue; const allowed = ALLOWED_KEYS.some((pattern) => pattern.test(entry.key)) && !REFUSED_VALUES.some(([pattern, refuses]) => pattern.test(entry.key) && refuses(entry.key, entry.value)); if (!allowed) refused.add(entry.key); } return [...refused].sort(); }; /** * The repository-scope keys `repositoryConfig: "pin"` still refuses, the ones the second layer cannot neutralise: an * include directive, which the listing reads in one context and git may resolve in another (an `includeIf` on * `gitdir:` or `onbranch:` that matches only in a new worktree or after a branch switch, its programs never seen by * the overrides computed here), and `core.sshCommand`, which the service itself reads to build its ssh pin. */ const PIN_REFUSED_KEYS: ReadonlyArray = [/^include\./i, /^includeif\./i, /^core\.sshcommand$/i]; /** * The repository-scope keys of a scoped listing that untrusted mode refuses under `repositoryConfig: "pin"`: each * once, sorted, never a value. * * @internal */ export const pinRefusedRepositoryKeys = (config: ReadonlyArray): ReadonlyArray => { const refused = new Set(); for (const entry of config) { if (REPOSITORY_SCOPES.has(entry.scope) && PIN_REFUSED_KEYS.some((pattern) => pattern.test(entry.key))) { refused.add(entry.key); } } return [...refused].sort(); };