Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: hardened Git reads fetch nothing in a partial clone on older Git either (GST-2331)
garrytan38eccaac5e75
diff --git a/src/core/hardened-git.ts b/src/core/hardened-git.tsindex b6bd1557c2..a046abe7e8 100644--- a/src/core/hardened-git.ts+++ b/src/core/hardened-git.ts@@ -36,6 +36,17 @@ export const HARDENED_GIT_ARGS: readonly string[] = ['--no-pager', '--no-optiona* would otherwise beat the args' `protocol.allow=never`), so the read fails and nothing is fetched.*/export const NO_LAZY_FETCH_ENV: NodeJS.ProcessEnv = { GIT_NO_LAZY_FETCH: '1', GIT_ALLOW_PROTOCOL: '' };+let lazyFetchOff = true;++/** Test seam: `false` leaves `GIT_NO_LAZY_FETCH` (never the empty `GIT_ALLOW_PROTOCOL`) out of every hardened and managed-sync read, as a git older than 2.45 ignores it; returns the restore. */+export function __setNoLazyFetchForTests(value: boolean): () => void {+ const previous = lazyFetchOff;+ lazyFetchOff = value;+ return () => { lazyFetchOff = previous; };+}++/** `NO_LAZY_FETCH_ENV` as the test seam above leaves it, for a read under `gitChildEnv`. */+export const noLazyFetchEnv = (): NodeJS.ProcessEnv => lazyFetchOff ? NO_LAZY_FETCH_ENV : { ...NO_LAZY_FETCH_ENV, GIT_NO_LAZY_FETCH: undefined };const OPERATOR_CONFIG_TTL_MS = 30_000;let operatorConfig: { key: string; at: number; values: string[] } | null = null;@@ -80,12 +91,14 @@ export function __resetOperatorGitConfigForTests(): void { operatorConfig = null/*** System and global config stay off and every `GIT_*` variable is dropped, except the operator's `safe.directory` entries* (system, global, then environment, as Git orders them), carried as environment config (#7584). They relax only Git's- * ownership check for a repository owned by another user, which the operator allowed for their own `git` too.+ * ownership check for a repository owned by another user, which the operator allowed for their own `git` too. GST-2331: an+ * empty `GIT_ALLOW_PROTOCOL` allows no transport whatever the repository's `protocol.<name>.allow` says, so a read in a+ * partial clone fetches nothing even on a Git that ignores `GIT_NO_LAZY_FETCH`.*/export function hardenedGitEnvironment(operatorSafeDirectories: string[] = operatorConfigSafeDirectories()): NodeJS.ProcessEnv {const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_')));return { ...env, GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: '/dev/null', GIT_OPTIONAL_LOCKS: '0',- GIT_NO_LAZY_FETCH: '1', GIT_NO_REPLACE_OBJECTS: '1', GIT_TERMINAL_PROMPT: '0', GIT_LFS_SKIP_SMUDGE: '1', LC_ALL: 'C',+ ...(lazyFetchOff ? { GIT_NO_LAZY_FETCH: '1' } : {}), GIT_ALLOW_PROTOCOL: '', GIT_NO_REPLACE_OBJECTS: '1', GIT_TERMINAL_PROMPT: '0', GIT_LFS_SKIP_SMUDGE: '1', LC_ALL: 'C',...safeDirectoryEnv([...operatorSafeDirectories, ...envSafeDirectories()]) };}diff --git a/src/core/persistence/sync-blobs.ts b/src/core/persistence/sync-blobs.tsindex 26835f32ce..961efcac35 100644--- a/src/core/persistence/sync-blobs.ts+++ b/src/core/persistence/sync-blobs.ts@@ -13,7 +13,7 @@ import { mkdtempSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { gitChildEnv } from '../git-env.ts';-import { HARDENED_GIT_ARGS, NO_LAZY_FETCH_ENV } from '../hardened-git.ts';+import { HARDENED_GIT_ARGS, noLazyFetchEnv } from '../hardened-git.ts';import { PAGE_MAX_BYTES } from '../page-size-policy.ts';/** The sync read bound (`readSyncFile`): bigger files are held without being read. The page ceiling (page-size-policy.ts), which also covers MAX_IMAGE_BYTES. */@@ -23,7 +23,7 @@ export interface TreeBlob { oid: string; size: number }function git(gitRoot: string, args: string[], input?: string, maxBuffer = 32 * 1024 ** 2, env: NodeJS.ProcessEnv = {}): Buffer {return execFileSync('git', ['-c', 'core.quotepath=false', ...HARDENED_GIT_ARGS, '--literal-pathspecs', '-C', gitRoot, ...args],- { env: gitChildEnv({ ...NO_LAZY_FETCH_ENV, ...(noLazyFetch ? {} : { GIT_NO_LAZY_FETCH: undefined }), ...env }), timeout: 60_000, maxBuffer, stdio: ['pipe', 'pipe', 'pipe'], ...(input !== undefined ? { input } : {}) });+ { env: gitChildEnv({ ...noLazyFetchEnv(), ...env }), timeout: 60_000, maxBuffer, stdio: ['pipe', 'pipe', 'pipe'], ...(input !== undefined ? { input } : {}) });}/** At this many paths `readTreeBlobs` lists the whole tree once instead of path chunks (#6712: the prescreen of a tree walk). */@@ -74,14 +74,7 @@ export function readBlobBytes(gitRoot: string, blobs: TreeBlob[]): Map<string, B}let checkAttrSource: boolean | undefined;-let noLazyFetch = true;--/** Test seam: `false` leaves `GIT_NO_LAZY_FETCH` (not the empty `GIT_ALLOW_PROTOCOL`) out of every read here, as a git older than 2.45 ignores it; returns the restore. */-export function __setNoLazyFetchForTests(value: boolean): () => void {- const previous = noLazyFetch;- noLazyFetch = value;- return () => { noLazyFetch = previous; };-}+export { __setNoLazyFetchForTests } from '../hardened-git.ts';/** Test seam: `false` takes the path of a git older than 2.40; returns the restore. */export function __setCheckAttrSourceForTests(value: boolean | undefined): () => void {diff --git a/test/sync-partial-clone-2331.test.ts b/test/sync-partial-clone-2331.test.tsindex 685db79759..bec07b7247 100644--- a/test/sync-partial-clone-2331.test.ts+++ b/test/sync-partial-clone-2331.test.ts@@ -1,6 +1,7 @@import { afterAll, beforeAll, describe, expect, test } from 'bun:test';import { execFileSync } from 'node:child_process';-import { chmodSync, existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';+import { chmodSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';import { join } from 'node:path';import type { BrainEngine } from '../src/core/engine.ts';import { __setNoLazyFetchForTests, __setPinnedWindowForTests } from '../src/core/persistence/sync-blobs.ts';@@ -8,6 +9,7 @@ import { readGitSourceHolds } from '../src/core/persistence/sync-holds.ts';import { __resetHistoryIndexesForTest, readOriginEvidence } from '../src/core/persistence/sync-origin-evidence.ts';import { performManagedSync } from '../src/core/persistence/sync-run.ts';import { OperationError } from '../src/core/ops/contract.ts';+import { hardenedGitSync } from '../src/core/hardened-git.ts';import { managedBrain } from './helpers/managed-brain.ts';import { persistencePostgresTemplate } from './helpers/persistence-postgres.ts';import { testBackends } from './helpers/test-backends.ts';@@ -330,3 +332,20 @@ for (const backend of testBackends()) describe('[' + backend + '] managed sync i}, root => { remote = bloblessClone(root, ['seed', 'a'], false, 'tree:0'); });}, 240_000);});++test('a hardened read in a treeless clone fetches nothing on a Git that ignores GIT_NO_LAZY_FETCH, even where the repository allows the file transport', () => {+ const dir = mkdtempSync(join(tmpdir(), 'gst2331-hardened-')), root = join(dir, 'clone');+ try {+ const remote = bloblessClone(root, ['seed', 'a'], true, 'tree:0');+ const upstream = join(root, '..', 'upstream');+ writeFileSync(join(upstream, 'notes/b.md'), note('b')); commit(upstream, 'b'); git(upstream, 'push', '-q', remote.url, 'HEAD:main');+ git(root, 'fetch', '-q', 'origin');+ git(root, 'config', 'protocol.file.allow', 'always');+ rmSync(remote.marker, { force: true });+ const restore = __setNoLazyFetchForTests(false);+ let listed;+ try { listed = hardenedGitSync(root, ['ls-tree', '-z', '--full-tree', 'origin/main', '--', 'notes/b.md'], { timeoutMs: 30_000, maxBytes: 1024 * 1024 }); } finally { restore(); }+ expect(existsSync(remote.marker)).toBe(false);+ expect(listed.ok).toBe(false);+ } finally { rmSync(dir, { recursive: true, force: true }); }+}, 60_000);