From 99270bbac10ba2e288cbb55977625e294368ba5f Mon Sep 17 00:00:00 2001 From: KRATOS <84986124+gnanam1990@users.noreply.github.com> Date: Sat, 14 Nov 2026 08:59:12 +0530 Subject: [PATCH 1/3] fix(memory): inspect local privacy with a fixed cached-index query --- internal/gitsafe/gitsafe.go | 4 +- internal/gitsafe/index.go | 35 +++++++++ internal/gitsafe/index_test.go | 77 ++++++++++++++++++++ internal/memory/escape_test.go | 24 +++--- internal/memory/memory.go | 12 +-- internal/memory/privacy_index_review_test.go | 71 ++++++++++++++++++ 6 files changed, 197 insertions(+), 26 deletions(-) create mode 100644 internal/gitsafe/index.go create mode 100644 internal/gitsafe/index_test.go create mode 100644 internal/memory/privacy_index_review_test.go diff --git a/internal/gitsafe/gitsafe.go b/internal/gitsafe/gitsafe.go index b21509f00..97d7ba9d6 100644 --- a/internal/gitsafe/gitsafe.go +++ b/internal/gitsafe/gitsafe.go @@ -14,7 +14,9 @@ // // The single-key cases are switched off on the command line. Filter and diff // driver commands are named per driver, so they cannot be switched off without -// knowing the names; a repository whose own config defines any is skipped instead. +// knowing the names; Command and Check skip repositories defining any instead. +// IndexEntriesCommand has fixed index-only arguments that never read file content, +// so that narrower query does not need to refuse driver definitions. // Commands defined in the user's own global config, such as git-lfs, are the // user's choice and are not affected. package gitsafe diff --git a/internal/gitsafe/index.go b/internal/gitsafe/index.go new file mode 100644 index 000000000..4adc77182 --- /dev/null +++ b/internal/gitsafe/index.go @@ -0,0 +1,35 @@ +package gitsafe + +import ( + "context" + "os/exec" + "runtime" + "strings" +) + +// IndexEntriesCommand lists cached index paths under one literal path. Its argv +// is fixed: it cannot inspect worktree contents, run filters/textconv, recurse +// into submodules, or accept additional options. Repository driver definitions +// therefore do not require the refusal used by Command for status/diff. +// +// Repository redirects, hooks, fsmonitor and optional index writes retain the +// same protections as Command. The caller must run it and propagate Git errors; +// a failed query says nothing about which files are tracked. Literal pathspecs +// require Git 1.9, the existing floor of memory's privacy query. +func IndexEntriesCommand(ctx context.Context, dir, path string) *exec.Cmd { + env := environment() + filtered := env[:0] + for _, entry := range env { + name, _, _ := strings.Cut(entry, "=") + if runtime.GOOS == "windows" { + name = strings.ToUpper(name) + } + switch name { + case "GIT_LITERAL_PATHSPECS", "GIT_GLOB_PATHSPECS", "GIT_NOGLOB_PATHSPECS", "GIT_ICASE_PATHSPECS": + continue + } + filtered = append(filtered, entry) + } + checked := &Checked{dir: dir, env: filtered} + return checked.Command(ctx, "--literal-pathspecs", "ls-files", "--cached", "-z", "--", path) +} diff --git a/internal/gitsafe/index_test.go b/internal/gitsafe/index_test.go new file mode 100644 index 000000000..2bdd9166f --- /dev/null +++ b/internal/gitsafe/index_test.go @@ -0,0 +1,77 @@ +package gitsafe + +import ( + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +func TestIndexEntriesAreLiteralCachedAndNeverRunRepositoryPrograms(t *testing.T) { + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git unavailable") + } + if !gitAtLeast(t, 1, 9) { + t.Skip("literal pathspecs require Git 1.9") + } + t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull) + t.Setenv("GIT_CONFIG_NOSYSTEM", "1") + root := t.TempDir() + git := func(args ...string) { + t.Helper() + cmd := exec.Command("git", append([]string{"-C", root}, args...)...) + cmd.Env = environment() + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v: %s", args, err, out) + } + } + git("init", "-q") + for _, dir := range []string{"notes[1]", "notes1", "--others"} { + if err := os.MkdirAll(filepath.Join(root, dir), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(root, dir, "tracked.md"), []byte("tracked"), 0600); err != nil { + t.Fatal(err) + } + } + git("add", ".") + if err := os.WriteFile(filepath.Join(root, "untracked.md"), []byte("untracked"), 0600); err != nil { + t.Fatal(err) + } + marker := filepath.Join(root, "program-ran") + program := "echo invoked >> '" + strings.ReplaceAll(filepath.ToSlash(marker), "'", "'\\''") + "'" + for _, key := range []string{"filter.tripwire.clean", "filter.tripwire.smudge", "filter.tripwire.process", "diff.tripwire.command", "diff.tripwire.textconv", "core.fsmonitor"} { + git("config", key, program) + } + if err := os.WriteFile(filepath.Join(root, ".gitattributes"), []byte("* filter=tripwire diff=tripwire\n"), 0600); err != nil { + t.Fatal(err) + } + t.Setenv("GIT_DIR", filepath.Join(t.TempDir(), ".git")) + t.Setenv("GIT_INDEX_FILE", filepath.Join(t.TempDir(), "empty-index")) + // Conflicting inherited pathspec modes must not turn a literal query into + // an empty or wildcard answer about a different set of tracked paths. + for _, key := range []string{"GIT_LITERAL_PATHSPECS", "GIT_GLOB_PATHSPECS", "GIT_NOGLOB_PATHSPECS", "GIT_ICASE_PATHSPECS"} { + t.Setenv(key, "1") + } + for _, dir := range []string{"notes[1]", "--others"} { + out, err := IndexEntriesCommand(context.Background(), root, dir).CombinedOutput() + if err != nil || string(out) != dir+"/tracked.md\x00" { + t.Fatalf("literal index query %q = %q, %v", dir, out, err) + } + } + if _, err := Command(context.Background(), root, "status", "--porcelain"); !errors.Is(err, ErrRepositoryDefinesCommands) { + t.Fatalf("status/diff policy was weakened: %v", err) + } + if _, err := os.Stat(marker); !os.IsNotExist(err) { + t.Fatalf("repository program executed: %v", err) + } + if err := os.WriteFile(filepath.Join(root, ".git", "index"), []byte("corrupt index"), 0600); err != nil { + t.Fatal(err) + } + if out, err := IndexEntriesCommand(context.Background(), root, ".").CombinedOutput(); err == nil { + t.Fatalf("corrupt index appeared to be a valid empty index: %q", out) + } +} diff --git a/internal/memory/escape_test.go b/internal/memory/escape_test.go index 3b12e8b35..1ca742e19 100644 --- a/internal/memory/escape_test.go +++ b/internal/memory/escape_test.go @@ -1,6 +1,7 @@ package memory import ( + "context" "errors" "fmt" "os" @@ -980,28 +981,23 @@ func TestACorruptRepositoryStillRefusesLocalWrites(t *testing.T) { } } -// A REPOSITORY THAT DEFINES FILTER OR DIFF COMMANDS IS NOT ASKED, so the local -// write stays refused — but the refusal has to say that, not that git failed: -// git was never run, and "git could not say" sends the reader looking for a -// broken repository instead of at the config that made Zero decline to ask. -func TestALocalWriteInAFilterRepositoryNamesTheConfigRefusal(t *testing.T) { +// The fixed cached-index query can establish privacy without reading worktree +// contents. General status/diff commands still refuse repository-defined drivers. +func TestLocalPrivacyQueryDoesNotWeakenGeneralFilterRefusal(t *testing.T) { root := t.TempDir() paths := DefaultPaths(root) seedStoreRepo(t, root) runStoreGit(t, root, "config", "filter.crypt.clean", "cat") - _, err := Write(paths, ScopeLocal, "private", "d", "machine-local secret") - if !errors.Is(err, ErrNotPrivate) { - t.Fatalf("Write in a repository defining a filter command = %v, want ErrNotPrivate", err) - } + _, err := gitsafe.Command(context.Background(), root, "status", "--porcelain") if !errors.Is(err, gitsafe.ErrRepositoryDefinesCommands) { - t.Errorf("the refusal does not carry the config cause: %v", err) + t.Fatalf("general Git commands no longer refuse filter definitions: %v", err) } - if strings.Contains(err.Error(), "git could not say") { - t.Errorf("the refusal blames git, which was never run: %v", err) + if _, err := Write(paths, ScopeLocal, "private", "d", "machine-local secret"); err != nil { + t.Fatalf("fixed index-only privacy query refused the local write: %v", err) } - if _, statErr := os.Lstat(filepath.Join(paths.LocalDir, "private.md")); !os.IsNotExist(statErr) { - t.Errorf("the refused note was written anyway: %v", statErr) + if _, err := os.Lstat(filepath.Join(paths.LocalDir, "private.md")); err != nil { + t.Fatalf("private note was not saved: %v", err) } } diff --git a/internal/memory/memory.go b/internal/memory/memory.go index bf21fb0e3..82757df3a 100644 --- a/internal/memory/memory.go +++ b/internal/memory/memory.go @@ -773,12 +773,6 @@ func refuseTrackedLocalStore(handle *os.Root, relative, dir string) error { } for _, repo := range repos { tracked, err := trackedStoreEntries(repo, resolved) - if errors.Is(err, gitsafe.ErrRepositoryDefinesCommands) { - // Still a refusal, but git was never run, so it must not read as - // git failing to answer. - return fmt.Errorf("%w: %s is inside the git repository at %s, and %w; without asking git, Zero cannot confirm that nothing there is tracked", - ErrNotPrivate, dir, repo, err) - } if err != nil { return fmt.Errorf("%w: %s is inside the git repository at %s, and git could not say which files there are tracked: %v", ErrNotPrivate, dir, repo, err) @@ -902,14 +896,10 @@ func trackedStoreEntries(repo, dir string) ([]string, error) { // Query from this repository's root, not the store, where Git could select // a nested repository. Literal pathspecs prevent glob characters in a // directory name from selecting a different set of indexed paths. - cmd, err := gitsafe.Command(ctx, repo, "ls-files", "-z", "--", filepath.ToSlash(relative)) - if err != nil { - return nil, err - } + cmd := gitsafe.IndexEntriesCommand(ctx, repo, filepath.ToSlash(relative)) // Cancelling the context does not by itself return control here: WaitDelay is // what stops a git holding its pipes open from outliving the deadline. cmd.WaitDelay = gitIndexTimeout - cmd.Env = append(gitDiscoveryEnv(cmd.Env), "GIT_LITERAL_PATHSPECS=1") var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr diff --git a/internal/memory/privacy_index_review_test.go b/internal/memory/privacy_index_review_test.go new file mode 100644 index 000000000..79cc7d36b --- /dev/null +++ b/internal/memory/privacy_index_review_test.go @@ -0,0 +1,71 @@ +package memory + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/Twigpine/zero/internal/gitsafe" +) + +func TestLocalPrivacyIndexReadDoesNotExecuteConfiguredGitPrograms(t *testing.T) { + for _, tracked := range []bool{false, true} { + t.Run(map[bool]string{false: "untracked", true: "tracked"}[tracked], func(t *testing.T) { + root := t.TempDir() + paths := DefaultPaths(root) + seedStoreRepo(t, root) + note := writeStoreFile(t, paths, "private.md", "---\nname: private\n---\n\noriginal\n") + writeStoreFile(t, paths, gitignoreName, localIgnoreContent) + if tracked { + runStoreGit(t, root, "add", "-f", ".zero/memory/local/private.md") + } + marker := filepath.Join(root, "repository-program-ran") + program := "echo invoked >> '" + strings.ReplaceAll(filepath.ToSlash(marker), "'", "'\\''") + "'" + for _, key := range []string{"filter.tripwire.clean", "filter.tripwire.smudge", "filter.tripwire.process", "diff.tripwire.command", "diff.tripwire.textconv", "core.fsmonitor"} { + runStoreGit(t, root, "config", key, program) + } + if err := os.WriteFile(filepath.Join(root, ".gitattributes"), []byte("* filter=tripwire diff=tripwire\n"), 0600); err != nil { + t.Fatal(err) + } + if _, err := gitsafe.Command(context.Background(), root, "status", "--porcelain"); !errors.Is(err, gitsafe.ErrRepositoryDefinesCommands) { + t.Fatalf("general Git safety gate changed: %v", err) + } + _, err := Write(paths, ScopeLocal, "private", "d", "replacement") + if tracked { + if !errors.Is(err, ErrNotPrivate) { + t.Errorf("tracked private note accepted: %v", err) + } + body, readErr := os.ReadFile(note) + if readErr != nil || !strings.Contains(string(body), "original") { + t.Errorf("tracked note changed: %q %v", body, readErr) + } + } else if err != nil { + t.Errorf("index-only privacy query refused a harmless filter definition: %v", err) + } + if _, err := os.Stat(marker); !os.IsNotExist(err) { + t.Fatalf("repository-defined program ran: %v", err) + } + }) + } +} + +func TestLocalPrivacyIndexErrorStaysClosed(t *testing.T) { + root := t.TempDir() + paths := DefaultPaths(root) + seedStoreRepo(t, root) + path := writeStoreFile(t, paths, "private.md", "---\nname: private\n---\n\noriginal\n") + writeStoreFile(t, paths, gitignoreName, localIgnoreContent) + if err := os.WriteFile(filepath.Join(root, ".git", "index"), []byte("corrupt index"), 0600); err != nil { + t.Fatal(err) + } + if _, err := Write(paths, ScopeLocal, "private", "d", "replacement"); !errors.Is(err, ErrNotPrivate) { + t.Fatalf("failed index query admitted a local write: %v", err) + } + body, err := os.ReadFile(path) + if err != nil || !strings.Contains(string(body), "original") { + t.Fatalf("failed privacy query replaced the note: %q %v", body, err) + } +} From 9f3b3fb600fd758fd1c9c573f8de2ec9b616827f Mon Sep 17 00:00:00 2001 From: KRATOS <84986124+gnanam1990@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:12:26 +0530 Subject: [PATCH 2/3] fix(memory): prevent sparse privacy queries from fetching objects Suppress lazy fetching and deny all transports for the fixed cached-index query. The transport allow-list predates partial clones, so the existing Git version floor remains unchanged even on clients that ignore the newer no-lazy-fetch setting. Inherited values cannot re-enable either mechanism. Reject index-query diagnostics even on a zero exit: Git can report a missing sparse tree and return success with empty stdout. Suppress harmless sparse expansion advice so complete sparse queries remain usable. Add a real sparse/promisor regression with a benign upload-pack positive control, an independent transport-backstop case, a healthy sparse query and the real local-write boundary preserving an existing note. Native Git reproduction confirms program execution before the guards and no execution with either protection. Local Go execution is blocked by Windows Application Control; at the user's direction, Go validation is delegated to this draft PR's full GitHub CI. --- internal/gitsafe/index.go | 20 ++- internal/memory/memory.go | 7 + .../memory/privacy_promisor_review_test.go | 137 ++++++++++++++++++ 3 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 internal/memory/privacy_promisor_review_test.go diff --git a/internal/gitsafe/index.go b/internal/gitsafe/index.go index 4adc77182..921520e6c 100644 --- a/internal/gitsafe/index.go +++ b/internal/gitsafe/index.go @@ -13,9 +13,17 @@ import ( // therefore do not require the refusal used by Command for status/diff. // // Repository redirects, hooks, fsmonitor and optional index writes retain the -// same protections as Command. The caller must run it and propagate Git errors; -// a failed query says nothing about which files are tracked. Literal pathspecs -// require Git 1.9, the existing floor of memory's privacy query. +// same protections as Command. The caller must propagate Git errors and reject +// stderr diagnostics even on a zero exit: Git can report a missing sparse tree +// while returning an empty result successfully. Literal pathspecs require Git +// 1.9, the existing floor of memory's privacy query. +// +// Sparse-index expansion can need missing promisor tree objects. Suppress lazy +// fetching where supported and deny every transport as a backstop: otherwise a +// fetch can run the repository's upload-pack or a remote helper. The protocol +// allow-list already exists in Git 2.6.7, before partial clones; older Git ignores +// these environment variables but cannot perform a promisor fetch. No newer +// command-line flag or Git version requirement is introduced. func IndexEntriesCommand(ctx context.Context, dir, path string) *exec.Cmd { env := environment() filtered := env[:0] @@ -25,11 +33,13 @@ func IndexEntriesCommand(ctx context.Context, dir, path string) *exec.Cmd { name = strings.ToUpper(name) } switch name { - case "GIT_LITERAL_PATHSPECS", "GIT_GLOB_PATHSPECS", "GIT_NOGLOB_PATHSPECS", "GIT_ICASE_PATHSPECS": + case "GIT_LITERAL_PATHSPECS", "GIT_GLOB_PATHSPECS", "GIT_NOGLOB_PATHSPECS", "GIT_ICASE_PATHSPECS", + "GIT_ALLOW_PROTOCOL", "GIT_NO_LAZY_FETCH": continue } filtered = append(filtered, entry) } + filtered = append(filtered, "GIT_ALLOW_PROTOCOL=", "GIT_NO_LAZY_FETCH=1") checked := &Checked{dir: dir, env: filtered} - return checked.Command(ctx, "--literal-pathspecs", "ls-files", "--cached", "-z", "--", path) + return checked.Command(ctx, "-c", "advice.sparseIndexExpanded=false", "--literal-pathspecs", "ls-files", "--cached", "-z", "--", path) } diff --git a/internal/memory/memory.go b/internal/memory/memory.go index 82757df3a..4f4d7f604 100644 --- a/internal/memory/memory.go +++ b/internal/memory/memory.go @@ -909,6 +909,13 @@ func trackedStoreEntries(repo, dir string) ([]string, error) { } return nil, err } + // Some Git versions report an unreadable sparse tree on stderr but return + // success with no entries. That is not proof of privacy. Harmless sparse + // expansion advice is suppressed by the fixed query; other diagnostics fail + // closed just like a nonzero exit. + if detail := singleLine(stderr.String()); detail != "" { + return nil, fmt.Errorf("git index query emitted diagnostics: %s", detail) + } entries := make([]string, 0, 4) prefix := filepath.ToSlash(relative) if prefix == "." { diff --git a/internal/memory/privacy_promisor_review_test.go b/internal/memory/privacy_promisor_review_test.go new file mode 100644 index 000000000..be6fbb894 --- /dev/null +++ b/internal/memory/privacy_promisor_review_test.go @@ -0,0 +1,137 @@ +package memory + +import ( + "bytes" + "context" + "encoding/hex" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/Twigpine/zero/internal/gitsafe" +) + +func TestLocalPrivacyNeverFetchesMissingSparseTrees(t *testing.T) { + t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull) + t.Setenv("GIT_CONFIG_NOSYSTEM", "1") + t.Setenv("GIT_DEFAULT_HASH", "sha1") + root := t.TempDir() + git := func(args ...string) string { + t.Helper() + return strings.TrimSpace(runStoreGit(t, root, args...)) + } + var major, minor int + if _, err := fmt.Sscanf(git("--version"), "git version %d.%d", &major, &minor); err != nil { + t.Fatal(err) + } + // Only the fixture needs sparse-checkout init --sparse-index (Git 2.32). + // The production query retains its existing Git 1.9 floor. + if major < 2 || (major == 2 && minor < 32) { + t.Skip("sparse-index fixture requires Git 2.32") + } + git("init", "-q") + paths := DefaultPaths(root) + const original = "---\nname: private\n---\n\noriginal\n" + writeStoreFile(t, paths, "private.md", original) + if err := os.Mkdir(filepath.Join(root, "keep"), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(root, "keep", "note.md"), []byte("kept"), 0600); err != nil { + t.Fatal(err) + } + git("add", ".") + git("-c", "core.hooksPath="+os.DevNull, "-c", "commit.gpgSign=false", "commit", "-qm", "fixture") + tree := git("rev-parse", "HEAD:.zero") + git("sparse-checkout", "init", "--cone", "--sparse-index") + git("sparse-checkout", "set", "keep") + index, err := os.ReadFile(filepath.Join(root, ".git", "index")) + if err != nil { + t.Fatal(err) + } + oid, err := hex.DecodeString(tree) + if err != nil || len(oid) != 20 || !bytes.Contains(index, []byte("sdir")) || !bytes.Contains(index, oid) { + t.Fatalf("fixture did not retain the excluded tree in a sparse index: %v", err) + } + // A healthy sparse query still finds tracked notes, without advice being + // mistaken for a broken query. Restore a working note for the write test. + entries, err := trackedStoreEntries(root, paths.LocalDir) + if err != nil || strings.Join(entries, ",") != "private.md" { + t.Fatalf("healthy sparse query = %q, %v", entries, err) + } + note := writeStoreFile(t, paths, "private.md", original) + writeStoreFile(t, paths, gitignoreName, localIgnoreContent) + marker := filepath.Join(root, "upload-pack-ran") + program := "echo invoked > '" + strings.ReplaceAll(filepath.ToSlash(marker), "'", "'\\''") + "'; exit 1 #" + git("config", "core.repositoryformatversion", "1") + git("config", "extensions.partialClone", "origin") + git("config", "remote.origin.url", filepath.ToSlash(t.TempDir())) + git("config", "remote.origin.promisor", "true") + git("config", "remote.origin.partialclonefilter", "blob:none") + git("config", "remote.origin.uploadpack", program) + git("config", "protocol.file.allow", "always") + if err := os.Remove(filepath.Join(root, ".git", "objects", tree[:2], tree[2:])); err != nil { + t.Fatal(err) + } + t.Setenv("GIT_ALLOW_PROTOCOL", "file") + t.Setenv("GIT_NO_LAZY_FETCH", "0") + relative, err := filepath.Rel(root, paths.LocalDir) + if err != nil { + t.Fatal(err) + } + ctx := context.Background() + // Positive control: the same query with the fetch guards removed really + // reaches the benign marker-writing upload-pack. No network is involved. + control := gitsafe.IndexEntriesCommand(ctx, root, filepath.ToSlash(relative)) + control.Env = append(withoutPrivacyFetchGuards(control.Env), "GIT_ALLOW_PROTOCOL=file", "GIT_NO_LAZY_FETCH=0") + out, _ := control.CombinedOutput() + if _, err := os.Stat(marker); err != nil { + t.Fatalf("positive control did not reach upload-pack: %v; %s", err, out) + } + if err := os.Remove(marker); err != nil { + t.Fatal(err) + } + for _, fallback := range []bool{false, true} { + t.Run(map[bool]string{false: "no-lazy-fetch", true: "transport-backstop"}[fallback], func(t *testing.T) { + cmd := gitsafe.IndexEntriesCommand(ctx, root, filepath.ToSlash(relative)) + if fallback { + // Emulate Git that ignores the newer no-lazy-fetch setting: + // the protocol allow-list must stop transport independently. + cmd.Env = append(cmd.Env, "GIT_NO_LAZY_FETCH=0") + } + var diagnostics bytes.Buffer + cmd.Stderr = &diagnostics + if err := cmd.Run(); err == nil && diagnostics.Len() == 0 { + t.Error("missing tree was silently accepted as a complete query") + } + if _, err := os.Stat(marker); !os.IsNotExist(err) { + t.Fatalf("privacy query ran repository upload-pack: %v", err) + } + }) + } + // Git can return zero with an empty stdout even after a missing-tree error. + // The real write boundary must refuse that result and retain the old note. + if _, err := Write(paths, ScopeLocal, "private", "d", "replacement"); !errors.Is(err, ErrNotPrivate) { + t.Errorf("missing sparse tree admitted a local write: %v", err) + } + if body, err := os.ReadFile(note); err != nil || string(body) != original { + t.Errorf("failed privacy query replaced the old note: %q, %v", body, err) + } + if _, err := os.Stat(marker); !os.IsNotExist(err) { + t.Fatalf("local write ran repository upload-pack: %v", err) + } +} + +func withoutPrivacyFetchGuards(env []string) []string { + var filtered []string + for _, entry := range env { + name, _, _ := strings.Cut(entry, "=") + if strings.EqualFold(name, "GIT_ALLOW_PROTOCOL") || strings.EqualFold(name, "GIT_NO_LAZY_FETCH") { + continue + } + filtered = append(filtered, entry) + } + return filtered +} From 359c1592481f1372de56e816bbb280d782755f29 Mon Sep 17 00:00:00 2001 From: KRATOS <84986124+gnanam1990@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:03:11 +0530 Subject: [PATCH 3/3] test(memory): make privacy fetch guards explicit across platforms --- internal/gitsafe/index_test.go | 17 ++++++++++++++++- internal/memory/privacy_promisor_review_test.go | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/internal/gitsafe/index_test.go b/internal/gitsafe/index_test.go index 2bdd9166f..7fa42a5ff 100644 --- a/internal/gitsafe/index_test.go +++ b/internal/gitsafe/index_test.go @@ -51,13 +51,28 @@ func TestIndexEntriesAreLiteralCachedAndNeverRunRepositoryPrograms(t *testing.T) } t.Setenv("GIT_DIR", filepath.Join(t.TempDir(), ".git")) t.Setenv("GIT_INDEX_FILE", filepath.Join(t.TempDir(), "empty-index")) + t.Setenv("GIT_ALLOW_PROTOCOL", "file") + t.Setenv("GIT_NO_LAZY_FETCH", "0") // Conflicting inherited pathspec modes must not turn a literal query into // an empty or wildcard answer about a different set of tracked paths. for _, key := range []string{"GIT_LITERAL_PATHSPECS", "GIT_GLOB_PATHSPECS", "GIT_NOGLOB_PATHSPECS", "GIT_ICASE_PATHSPECS"} { t.Setenv(key, "1") } for _, dir := range []string{"notes[1]", "--others"} { - out, err := IndexEntriesCommand(context.Background(), root, dir).CombinedOutput() + cmd := IndexEntriesCommand(context.Background(), root, dir) + for key, want := range map[string]string{"GIT_ALLOW_PROTOCOL": "", "GIT_NO_LAZY_FETCH": "1"} { + var values []string + for _, entry := range cmd.Env { + name, value, ok := strings.Cut(entry, "=") + if ok && strings.EqualFold(name, key) { + values = append(values, value) + } + } + if len(values) != 1 || values[0] != want { + t.Fatalf("privacy query %s = %q, want one value %q", key, values, want) + } + } + out, err := cmd.CombinedOutput() if err != nil || string(out) != dir+"/tracked.md\x00" { t.Fatalf("literal index query %q = %q, %v", dir, out, err) } diff --git a/internal/memory/privacy_promisor_review_test.go b/internal/memory/privacy_promisor_review_test.go index be6fbb894..b1926d078 100644 --- a/internal/memory/privacy_promisor_review_test.go +++ b/internal/memory/privacy_promisor_review_test.go @@ -99,7 +99,7 @@ func TestLocalPrivacyNeverFetchesMissingSparseTrees(t *testing.T) { if fallback { // Emulate Git that ignores the newer no-lazy-fetch setting: // the protocol allow-list must stop transport independently. - cmd.Env = append(cmd.Env, "GIT_NO_LAZY_FETCH=0") + cmd.Env = append(withoutPrivacyFetchGuards(cmd.Env), "GIT_ALLOW_PROTOCOL=", "GIT_NO_LAZY_FETCH=0") } var diagnostics bytes.Buffer cmd.Stderr = &diagnostics