// Offline object reads use the system Git, never caller-selected helpers or // inherited repository/configuration variables. Git 2.39+ is the supported floor. import { execFile } from 'node:child_process'; import { promisify } from 'node:util'; import { realpath, readdir } from 'node:fs/promises'; import { join } from 'node:path'; const execute = promisify(execFile); const environment = Object.freeze({ PATH: '/usr/bin:/bin', LANG: 'C', LC_ALL: 'C', GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_SYSTEM: '/dev/null', GIT_CONFIG_GLOBAL: '/dev/null', GIT_NO_REPLACE_OBJECTS: '1', GIT_OPTIONAL_LOCKS: '0', GIT_TERMINAL_PROMPT: '0', // Empty allowlist denies every transport, including file and external helpers, // even if repository config sets protocol..allow=always. GIT_ALLOW_PROTOCOL: '', GIT_PROTOCOL_FROM_USER: '0', // Defense in depth on newer Git only; older versions are protected by the // promisor preflight and supported transport allowlist above. GIT_NO_LAZY_FETCH: '1', }); export async function openLocalGit(repo) { async function run(maxBuffer, args, missingConfig = false) { try { const { stdout } = await execute('/usr/bin/git', [ '-c', 'protocol.allow=never', '-c', 'core.hooksPath=/dev/null', '-c', 'maintenance.auto=false', '-c', 'gc.auto=0', '-C', repo, ...args, ], { encoding: 'buffer', maxBuffer, timeout: 30000, env: environment }); return stdout; } catch (error) { if (missingConfig && error.code === 1) return Buffer.alloc(0); // Do not disclose local configuration, endpoints or inherited secrets. throw new Error('Offline Git read failed; require complete local objects and a valid explicit repository'); } } const version = (await run(1024, ['--version'])).toString('ascii'); const match = /^git version (\d+)\.(\d+)\./.exec(version); if (!match || Number(match[1]) < 2 || (Number(match[1]) === 2 && Number(match[2]) < 39)) throw new Error('Offline capture requires system Git >=2.39'); const top = (await run(64 * 1024, ['rev-parse', '--show-toplevel'])).toString('utf8').trim(); if (await realpath(top) !== repo) throw new Error('explicit repository must be its worktree root'); // Config inspection cannot request objects. Include repository/worktree includes // but not system/global config (disabled above). Reject even declared false // promisor entries conservatively instead of guessing partial-clone completeness. const promisor = await run(64 * 1024, ['config', '--includes', '--null', '--get-regexp', '^(extensions[.]partialclone|remote[.].*[.]promisor)$'], true); if (promisor.length) throw new Error('partial/promisor repositories are not supported for offline capture'); const objects = (await run(64 * 1024, ['rev-parse', '--path-format=absolute', '--git-path', 'objects'])).toString('utf8').trim(); const packs = await readdir(join(objects, 'pack')).catch(error => { if (error.code === 'ENOENT') return []; throw error; }); if (packs.some(name => name.endsWith('.promisor'))) throw new Error('promised object packs are not supported for offline capture'); return (maxBuffer, ...args) => run(maxBuffer, args); }