Skip to content

Commit 47551d1

Browse files
cursoragentBen Schellenberger
andcommitted
sanitize_fixtures: dedupe file grep keys and harden token validation
Stop prepending the commit hash to git grep lines, which already use commit:path:line:content. Reject blocked strings that start with a hyphen so they cannot be misparsed as git grep flags, and pass patterns with -e for correct argument handling. Co-authored-by: Ben Schellenberger <TBRX103@users.noreply.github.com>
1 parent 7ba5e52 commit 47551d1

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

sanitize_fixtures.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ func validateBlockedRefPathToken(token string) error {
263263
if !blockedRefPathTokenPattern.MatchString(token) {
264264
return fmt.Errorf("must contain only letters, numbers, dot, underscore, or dash")
265265
}
266+
if strings.HasPrefix(token, "-") {
267+
return fmt.Errorf("cannot start with hyphen")
268+
}
266269
if strings.HasPrefix(token, ".") || strings.HasSuffix(token, ".") {
267270
return fmt.Errorf("cannot start or end with dot")
268271
}
@@ -302,14 +305,14 @@ func collectBlockedFileContentMatches(repoPath string, commits []string, blocked
302305
return nil, err
303306
}
304307
for _, line := range lines {
305-
matchSet[commit+":"+line] = struct{}{}
308+
matchSet[line] = struct{}{}
306309
}
307310
}
308311
return sortedKeys(matchSet), nil
309312
}
310313

311314
func runGitGrepForCommit(repoPath, commit, blocked string) ([]string, error) {
312-
cmd := exec.Command("git", "grep", "-n", "-I", "-F", blocked, commit, "--")
315+
cmd := exec.Command("git", "grep", "-n", "-I", "-F", "-e", blocked, commit, "--")
313316
cmd.Dir = repoPath
314317

315318
var stderr bytes.Buffer
@@ -322,7 +325,7 @@ func runGitGrepForCommit(repoPath, commit, blocked string) ([]string, error) {
322325
}
323326
return nil, fmt.Errorf(
324327
"git command failed: git %v\nStdout: %s\nStderr: %s\nError: %w",
325-
[]string{"grep", "-n", "-I", "-F", blocked, commit, "--"},
328+
[]string{"grep", "-n", "-I", "-F", "-e", blocked, commit, "--"},
326329
strings.TrimSpace(string(output)),
327330
strings.TrimSpace(stderr.String()),
328331
err,

0 commit comments

Comments
 (0)