fix(tool): prevent path traversal bypass and normalize paths in code_search and code_comment - #1089
Conversation
…search and code_comment (alibaba#1088)
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s). |
|
Thanks for the PR. The hardening in That said — I ran a quick scan across 174 historical review sessions on this repo (1580 A few questions:
Not blocking — the code is correct and the tests are good. Just want to calibrate whether this addresses an observed issue or a hypothetical one. |
| func TestCodeSearchProvider_Execute_WithFilePatterns(t *testing.T) { | ||
| dir := setupTestRepo(t) | ||
| p := NewCodeSearch(&FileReader{RepoDir: dir, Mode: ModeWorkspace}) | ||
|
|
||
| got, err := p.Execute(context.Background(), map[string]any{ | ||
| "search_text": "Util", | ||
| "file_patterns": []any{"pkg/"}, | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !strings.Contains(got, "util.go") { | ||
| t.Errorf("expected util.go in result, got: %s", got) | ||
| } | ||
| } |
There was a problem hiding this comment.
You are missing coverage.
| func TestCodeSearchProvider_Execute_WithFilePatterns(t *testing.T) { | |
| dir := setupTestRepo(t) | |
| p := NewCodeSearch(&FileReader{RepoDir: dir, Mode: ModeWorkspace}) | |
| tests := []struct { | |
| name string | |
| pattern string | |
| }{ | |
| {name: "forward slash", pattern: "pkg/"}, | |
| {name: "backslash", pattern: "pkg\\"}, | |
| } | |
| for _, test := range tests { | |
| t.Run(test.name, func(t *testing.T) { | |
| got, err := p.Execute(context.Background(), map[string]any{ | |
| "search_text": "Util", | |
| "file_patterns": []any{test.pattern}, | |
| }) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| if !strings.Contains(got, "util.go") { | |
| t.Errorf("expected util.go for pattern %q, got: %s", test.pattern, got) | |
| } | |
| }) | |
| } | |
| } |
|
Thanks for the feedback and discussion @lizhengfeng101! To share what we actually observed testing locally in a native Windows environment: While the CLI doesn't crash thanks to the existing fallbacks, path discrepancies cause subtle runtime friction on Windows:
Also, I have incorporated @wu21-web's test coverage suggestion in the latest commit ( |
Description
This PR fixes two cross-platform path handling issues in
internal/tool:Fix path traversal bypass & normalize pathspecs in
code_search:hasTraversalPathComponentnow normalizes backslashes\to/before splitting on/, preventing traversal sequences like..\pkgorpkg\..\internalfrom bypassing the..safety check.file_patternsitems from\to/before passing them togit grepso pathspec matching works consistently on all platforms.Normalize comment paths in
code_comment:parseCommentsInnernow canonicalizes comment paths (\to/, stripping leading./, resolving., and trimming redundant slashes vianormalizeCommentPath)../correctly matchCommentCollector.CommentsForPath(d.NewPath)during review compilation instead of being silently dropped.Type of Change
How Has This Been Tested?
Added
TestParseComments_NormalizePathininternal/tool/code_comment_test.goto verify normalization of\,./, and redundant slashes.Added backslash test cases to
TestCodeSearchProvider_Execute_RejectsTraversalPatternininternal/tool/code_search_test.go.Verified targeted unit tests and full package tests pass cleanly (
go test -v ./internal/tool/...).make testpasses locallyUnit tests pass locally
Checklist
go fmt,go vet)Related Issues
Closes #1088