From 981680474b407bc4e77f92cb504d3485491efd7e Mon Sep 17 00:00:00 2001 From: Vishnu Jayavel Date: Sun, 11 Oct 2026 19:34:35 -0700 Subject: [PATCH] fix(sync): bound advisory Git remote probe waits Disable inherited askpass helpers and limit retained output-pipe waits to two seconds. Preserve UNKNOWN for incomplete remote output. Agent-Signature: codex-gpt-6.1-sol-high on behalf of Vishnu Jayavel --- cmd/bd/sync_git.go | 6 +++- cmd/bd/sync_git_test.go | 69 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/cmd/bd/sync_git.go b/cmd/bd/sync_git.go index f660b595a..c08cf9a82 100644 --- a/cmd/bd/sync_git.go +++ b/cmd/bd/sync_git.go @@ -128,7 +128,11 @@ func gitRemoteHasDoltDataRefStatus(remote string) (bool, error) { // config discovery through env-derived paths (see testSSHConnectivity in // dolt.go for the same taint path). cmd := exec.CommandContext(ctx, "git", "ls-remote", gitRemoteURLForLsRemote(remote), "refs/dolt/data") - cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0") + // Terminal prompts and inherited askpass helpers can block an advisory probe. + cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0", "GIT_ASKPASS=", "SSH_ASKPASS=") + // Descendants may retain stdout/stderr after Git exits or the context expires. + // Bound that pipe wait separately from the 10-second network deadline. + cmd.WaitDelay = 2 * time.Second output, err := cmd.Output() if err != nil { return false, gitLsRemoteProbeError(ctx, err) diff --git a/cmd/bd/sync_git_test.go b/cmd/bd/sync_git_test.go index a0efdea5f..7675560d2 100644 --- a/cmd/bd/sync_git_test.go +++ b/cmd/bd/sync_git_test.go @@ -1,8 +1,12 @@ package main import ( + "errors" "os" + "os/exec" "path/filepath" + "runtime" + "strconv" "strings" "testing" ) @@ -221,3 +225,68 @@ func TestGitRemoteHasDoltDataRefStatusSurfacesGitStderr(t *testing.T) { t.Errorf("error %q does not look like it carries git's stderr", msg) } } + +// The probe is advisory and must not invoke inherited credential prompts. +func TestGitRemoteHasDoltDataRefStatusDisablesAskpass(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("fixture requires a POSIX shell") + } + dir := t.TempDir() + script := `#!/bin/sh +if [ "$GIT_TERMINAL_PROMPT" != 0 ] || [ -n "$GIT_ASKPASS" ] || [ -n "$SSH_ASKPASS" ]; then + echo 'fatal: inherited credential prompt is enabled' >&2 + exit 128 +fi +printf '0123456789012345678901234567890123456789\trefs/dolt/data\n' +` + if err := os.WriteFile(filepath.Join(dir, "git"), []byte(script), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + t.Setenv("GIT_TERMINAL_PROMPT", "1") + t.Setenv("GIT_ASKPASS", "/inherited/git-askpass") + t.Setenv("SSH_ASKPASS", "/inherited/ssh-askpass") + + hasData, err := gitRemoteHasDoltDataRefStatus("origin") + if err != nil || !hasData { + t.Fatalf("noninteractive probe = (%v, %v), want (true, nil)", hasData, err) + } +} + +// A successful git process can leave a descendant holding its output pipes. +// Incomplete output must remain UNKNOWN, even when stdout contains the ref. +func TestGitRemoteHasDoltDataRefStatusRetainedPipes(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("fixture requires a POSIX shell") + } + dir := t.TempDir() + pidPath := filepath.Join(dir, "child.pid") + script := `#!/bin/sh +/bin/sleep 6 & +printf '%s\n' "$!" > "$PROBE_CHILD_PID" +printf '0123456789012345678901234567890123456789\trefs/dolt/data\n' +exit 0 +` + if err := os.WriteFile(filepath.Join(dir, "git"), []byte(script), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + t.Setenv("PROBE_CHILD_PID", pidPath) + t.Cleanup(func() { + data, err := os.ReadFile(pidPath) + if err != nil { + return + } + pid, err := strconv.Atoi(strings.TrimSpace(string(data))) + if err == nil { + if child, err := os.FindProcess(pid); err == nil { + _ = child.Kill() + } + } + }) + + hasData, err := gitRemoteHasDoltDataRefStatus("origin") + if hasData || !errors.Is(err, exec.ErrWaitDelay) { + t.Fatalf("probe with retained output pipes = (%v, %v), want UNKNOWN with ErrWaitDelay", hasData, err) + } +}