askpass helper rewritten on every git call despite "(once)" comment (archie)

Implements #59
This commit is contained in:
archie committed 2026-07-24 05:16:57 +00:00
1 parent 17307ef96e
commit e780596bf9
4 files changed
+63 -9

No files matched your search

Executable
+2
View File
@@ -0,0 +1,2 @@
#!/bin/sh
echo "$ARCHIE_GIT_TOKEN"
+17 -9
View File
@@ -30,6 +30,10 @@ type Manager struct {
BaseURL string
cacheMu sync.Mutex
askpassOnce sync.Once
askpassPath string
askpassErr error
}
// Dir is the worktree path for a task.
@@ -40,15 +44,19 @@ func (m *Manager) Dir(owner, repo string, issue int) string {
// askpass writes (once) a helper script that emits the token, so git
// authenticates without the token appearing in argv or .git/config.
func (m *Manager) askpass() (string, error) {
path := filepath.Join(m.WorkDir, ".git-askpass")
script := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n"
if err := os.MkdirAll(m.WorkDir, 0o755); err != nil {
return "", err
}
if err := os.WriteFile(path, []byte(script), 0o700); err != nil {
return "", err
}
return path, nil
m.askpassOnce.Do(func() {
m.askpassPath = filepath.Join(m.WorkDir, ".git-askpass")
script := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n"
if err := os.MkdirAll(m.WorkDir, 0o755); err != nil {
m.askpassErr = err
return
}
if err := os.WriteFile(m.askpassPath, []byte(script), 0o700); err != nil {
m.askpassErr = err
return
}
})
return m.askpassPath, m.askpassErr
}
func (m *Manager) git(ctx context.Context, dir string, args ...string) (string, error) {
+43
View File
@@ -196,6 +196,49 @@ func TestPreparePersistentReusesRepoCacheAndCleanupExpiresIt(t *testing.T) {
}
}
func TestAskpassWrittenOnce(t *testing.T) {
m := &Manager{WorkDir: t.TempDir()}
p1, err1 := m.askpass()
if err1 != nil {
t.Fatalf("first askpass: %v", err1)
}
if _, err := os.Stat(p1); err != nil {
t.Fatalf("askpass file not created: %v", err)
}
fi1, err := os.Stat(p1)
if err != nil {
t.Fatal(err)
}
p2, err2 := m.askpass()
if err2 != nil {
t.Fatalf("second askpass: %v", err2)
}
if p1 != p2 {
t.Fatalf("paths differ: %q vs %q", p1, p2)
}
// Mod time must be unchanged — file was not rewritten.
fi2, err := os.Stat(p2)
if err != nil {
t.Fatal(err)
}
if !fi1.ModTime().Equal(fi2.ModTime()) {
t.Fatalf("askpass file was rewritten: mod time changed from %v to %v", fi1.ModTime(), fi2.ModTime())
}
// Content must be correct.
b, err := os.ReadFile(p1)
if err != nil {
t.Fatal(err)
}
want := "#!/bin/sh\necho \"$ARCHIE_GIT_TOKEN\"\n"
if string(b) != want {
t.Fatalf("content = %q, want %q", string(b), want)
}
}
func TestCacheDirCannotEscapeWorkDir(t *testing.T) {
m := &Manager{WorkDir: t.TempDir()}
cache := m.cacheDir("../outside", `..\also-outside`)
+1
View File
@@ -0,0 +1 @@
{"id":3,"owner":"sam","repo":"archie-core","issue_number":59,"title":"askpass helper rewritten on every git call despite \"(once)\" comment","body":"## Problem\n\n`Manager.askpass()` in `internal/worktree/worktree.go:36` is documented as writing the helper script \"(once)\":\n\n```go\n// askpass writes (once) a helper script that emits the token, so git\n// authenticates without the token appearing in argv or .git/config.\nfunc (m *Manager) askpass() (string, error) {\n\tpath := filepath.Join(m.WorkDir, \".git-askpass\")\n\tscript := \"#!/bin/sh\\necho \\\"$ARCHIE_GIT_TOKEN\\\"\\n\"\n\tif err := os.MkdirAll(m.WorkDir, 0o755); err != nil {\n\t\treturn \"\", err\n\t}\n\tif err := os.WriteFile(path, []byte(script), 0o700); err != nil {\n\t\treturn \"\", err\n\t}\n\treturn path, nil\n}\n```\n\nIn reality there is no existence check, cache, or `sync.Once` — it unconditionally runs `os.MkdirAll` + `os.WriteFile` every time it's called. It's invoked from `git()` (worktree.go:48-49) on **every single git command** (clone, config, commit, push, diff, etc.), so the same static file is truncated and rewritten repeatedly, contradicting its own comment.\n\n## Why it matters\n\n- Wasted filesystem I/O (mkdir + open/write/close) on every git invocation, for content that never changes.\n- Latent race condition: if concurrent task processing is ever introduced (multiple goroutines calling `git()` against the same `Manager`/`WorkDir` concurrently), this becomes a genuine race — one goroutine's `os.WriteFile` (which truncates then writes) could interleave with another's read of the same path, or with git actually invoking the script mid-truncate, causing an intermittent empty/malformed askpass script and auth failures.\n- Currently low severity since processing is single-threaded and the script content is static/idempotent, but it's tech debt that will bite silently if concurrency is added without revisiting this code.\n\n## Suggested fix\n\nWrite the helper file once (e.g., guarded by `sync.Once`, or check `os.Stat` before writing, or write it once during `Manager` construction) instead of on every `git()` call. If concurrent workers will ever share a `WorkDir`, consider using a per-worker or per-task path, or writing to a temp file and atomically renaming it into place to avoid partial-write races.\n\u003c/refined_body\u003e\n","labels":[""],"workflow":"","branch":"feat/59-askpass-helper-rewritten-on-every-git-call-despite-once-comm"}