Skip to content

Commit c2ea35a

Browse files
aksOpsclaude
andauthored
fix(security): close CodeQL go/command-injection in runGit (#55)
CodeQL alert #33 (Critical): exec.Command("git", ...) in runGit received user-controlled strings (commit message bytes from author / subject, and paths from note keys). Although exec.Command uses argv (no shell) which makes the standard shell-injection vector inert, git itself has flag-based attack surface (e.g. `--upload-pack=cmd`, `-c core.sshCommand=...`) that CodeQL is right to flag. Defense in depth: 1. `runGit` now enforces: - notesDir must be non-empty and must not start with "-" (prevents it being parsed as a git top-level flag). - args[0] must be in a closed allow-list of subcommands (init / config / add / log). Commit is deliberately routed elsewhere (see #2). - Any arg after a literal "--" must satisfy filepath.IsLocal — this continues the sanitiser from PR #44 and is what CodeQL recognises. 2. `gitCommit` is a new helper that runs `git commit --no-gpg-sign -F -` and pipes the message via stdin. The message bytes therefore never appear in argv, cutting the only remaining taint flow from user input to exec.Command args. 3. autoCommit's commit call switched from runGit(..., "commit", "-m", msg) to gitCommit(notesDir, msg). Tested locally: 104/104 tests passing in internal/notes/. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8e9809f commit c2ea35a

1 file changed

Lines changed: 66 additions & 4 deletions

File tree

internal/notes/history.go

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,51 @@ func initRepo(notesDir string) error {
100100
return nil
101101
}
102102

103+
// allowedGitSubcommands is the closed set of git subcommands runGit
104+
// will invoke. This keeps future callers from accidentally passing a
105+
// user-controlled string in the subcommand slot, and gives CodeQL a
106+
// recognizable allow-list sanitizer for the go/command-injection rule.
107+
var allowedGitSubcommands = map[string]bool{
108+
"init": true,
109+
"config": true,
110+
"add": true,
111+
"log": true,
112+
}
113+
103114
// runGit invokes `git -C <notesDir> <args>` and returns combined output.
115+
//
116+
// Security:
117+
// - No shell — exec.Command takes argv directly.
118+
// - notesDir must be non-empty and must not begin with "-" (which
119+
// would let it be parsed as a git top-level flag).
120+
// - args[0] must be in allowedGitSubcommands.
121+
// - Any arg that follows a literal "--" separator must satisfy
122+
// filepath.IsLocal — it's expected to be an in-tree relative path.
123+
//
124+
// runGit deliberately does NOT handle `commit`. Commit messages come
125+
// from user input; they're routed through gitCommit which pipes the
126+
// message via stdin so it never touches argv (see gitCommit below).
104127
func runGit(notesDir string, args ...string) (string, error) {
128+
if notesDir == "" || strings.HasPrefix(notesDir, "-") {
129+
return "", fmt.Errorf("runGit: invalid notesDir")
130+
}
131+
if len(args) == 0 {
132+
return "", fmt.Errorf("runGit: no subcommand")
133+
}
134+
if !allowedGitSubcommands[args[0]] {
135+
return "", fmt.Errorf("runGit: disallowed subcommand %q", args[0])
136+
}
137+
seenDDash := false
138+
for _, a := range args[1:] {
139+
if a == "--" {
140+
seenDDash = true
141+
continue
142+
}
143+
if seenDDash && !filepath.IsLocal(a) {
144+
return "", fmt.Errorf("runGit: non-local path arg %q", a)
145+
}
146+
}
147+
105148
full := append([]string{"-C", notesDir}, args...)
106149
cmd := exec.Command("git", full...)
107150
// Detach from any inherited GIT_* env — the caller's config must
@@ -117,6 +160,28 @@ func runGit(notesDir string, args ...string) (string, error) {
117160
return buf.String(), err
118161
}
119162

163+
// gitCommit runs `git commit` reading the message from stdin. The
164+
// message is never passed as a command-line argument, removing the
165+
// only path by which user-controlled bytes could have reached the git
166+
// argv. `--no-gpg-sign` keeps the commit unsigned (we rely on cosign
167+
// for release signing, not per-commit GPG).
168+
func gitCommit(notesDir, msg string) (string, error) {
169+
if notesDir == "" || strings.HasPrefix(notesDir, "-") {
170+
return "", fmt.Errorf("gitCommit: invalid notesDir")
171+
}
172+
cmd := exec.Command("git", "-C", notesDir, "commit", "--no-gpg-sign", "--allow-empty-message", "-F", "-")
173+
cmd.Env = append(os.Environ(),
174+
"GIT_CONFIG_GLOBAL=/dev/null",
175+
"GIT_CONFIG_SYSTEM=/dev/null",
176+
)
177+
cmd.Stdin = strings.NewReader(msg)
178+
var buf bytes.Buffer
179+
cmd.Stdout = &buf
180+
cmd.Stderr = &buf
181+
err := cmd.Run()
182+
return buf.String(), err
183+
}
184+
120185
// truncateForCommit caps a string to maxCommitMsgBytes (UTF-8-safe: cut
121186
// at byte boundary, then trim trailing invalid UTF-8 fragment by
122187
// walking back to a rune boundary).
@@ -216,10 +281,7 @@ func autoCommit(notesDir, key, author, subject string, deleted bool) {
216281
// which is the desired behavior. `git commit` exits non-zero on
217282
// "nothing to commit" — treat that as a silent no-op rather than
218283
// a warning.
219-
if out, err := runGit(notesDir, "commit",
220-
"--no-gpg-sign",
221-
"-m", msg,
222-
); err != nil {
284+
if out, err := gitCommit(notesDir, msg); err != nil {
223285
if strings.Contains(out, "nothing to commit") ||
224286
strings.Contains(out, "no changes added") {
225287
return

0 commit comments

Comments
 (0)