Repository navigation
fix(tools): make apply_patch tolerant and re-expose edit_file #956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e9698ee
fix(tools): make apply_patch tolerant and re-expose edit_file
kevincodex1 ff9b0de
fix(tools): keep CRLF line endings when relativizing patch headers
kevincodex1 9483000
fix(sandbox): share the structured-patch classifier with apply_patch
kevincodex1 29e5fdf
test(sandbox): compare slash-normalised patch paths so the check pass…
kevincodex1 5a1d966
test(sandbox): validate the parsed patch path at the scope boundary
kevincodex1 e320477
fix(tools): apply unified diffs in-process through the workspace root
kevincodex1 2366201
fix(tools): harden unified-diff parsing and cover the copy operation
kevincodex1 08100a3
fix(tools): require an @@ line before treating ---/+++ inside a hunk …
kevincodex1 3f65447
refactor(tools): share one line-count implementation between reader a…
kevincodex1 3e01c21
fix(tools): verify a unified deletion's expected content before remov…
kevincodex1 d0f1394
test(tools): a move with a missing source is refused before publishing
kevincodex1 03e2da1
fix(tools): byte-exact unified deletions and git's header-only empty-…
kevincodex1 5dd78ad
test(tools): remove the move source after planning via a deterministi…
kevincodex1 3454f1a
test(tui): read_file fixtures use the compact N→ prefix
kevincodex1 4f01651
fix(tools): name the committed files when a patch is partially applied
kevincodex1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package sandbox | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestApplyPatchPathBlockOnlyRejectsRelativeTraversal(t *testing.T) { | ||
| root := t.TempDir() | ||
| inside := filepath.Join(root, "main.js") | ||
| structured := func(path string) string { | ||
| return strings.Join([]string{"*** Begin Patch", "*** Update File: " + path, "@@", "-a", "+b", "*** End Patch"}, "\n") | ||
| } | ||
| for name, patch := range map[string]string{ | ||
| "absolute inside workspace": structured(inside), | ||
| "relative": structured("main.js"), | ||
| "decorated markers": "*** Begin Patch ***\n*** Update File: main.js\n@@\n-a\n+b\n*** End Patch ***", | ||
| "no-space marker": "***Begin Patch\n*** Update File: main.js\n@@\n-a\n+b\n***End Patch", | ||
| } { | ||
| request := Request{ToolName: "apply_patch", WorkspaceRoot: root, SideEffect: SideEffectWrite, Args: map[string]any{"patch": patch}} | ||
| if block := applyPatchPathBlock(request); block != nil { | ||
| t.Fatalf("%s: unexpected block %+v", name, block) | ||
| } | ||
| } | ||
| for _, path := range []string{"../escape.js", ".."} { | ||
| for name, patch := range map[string]string{"canonical": structured(path), "no-space": "***Begin Patch\n*** Update File: " + path + "\n@@\n-a\n+b\n***End Patch"} { | ||
| request := Request{ToolName: "apply_patch", WorkspaceRoot: root, SideEffect: SideEffectWrite, Args: map[string]any{"patch": patch}} | ||
| block := applyPatchPathBlock(request) | ||
| if block == nil || block.Code != BlockOutsideWorkspace { | ||
| t.Fatalf("%s %q must be blocked as traversal, got %+v", name, path, block) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Every marker spelling the tool applies must be classified as structured at | ||
| // the sandbox boundary too; otherwise the boundary scans the patch as a unified | ||
| // diff, extracts no targets, and validates nothing (fail-open). | ||
| func TestStructuredPatchClassifierMatchesToolSpellings(t *testing.T) { | ||
| for _, header := range []string{"*** Begin Patch", "*** Begin Patch ***", "***Begin Patch", " *** Begin Patch ", "\ufeff*** Begin Patch"} { | ||
| patch := header + "\n*** Update File: main.js\n@@\n-a\n+b\n*** End Patch" | ||
| if !IsStructuredPatch(patch) { | ||
| t.Fatalf("%q must classify as a structured patch", header) | ||
| } | ||
| if paths := applyPatchPaths(patch); len(paths) != 1 || paths[0] != "main.js" { | ||
| t.Fatalf("%q: sandbox must extract the structured target, got %v", header, paths) | ||
| } | ||
| } | ||
| for _, header := range []string{"--- a/x", "Begin Patch", "*** Begin Patchwork", "*** Update File: x"} { | ||
| if IsStructuredPatch(header + "\n-a\n+b") { | ||
| t.Fatalf("%q must not classify as a structured patch", header) | ||
| } | ||
| } | ||
| if StructuredPatchMarker("*** End Patch ***") != "end" || StructuredPatchMarker("***End Patch") != "end" { | ||
| t.Fatal("decorated end markers must classify as end") | ||
| } | ||
| } | ||
|
|
||
| func TestApplyPatchRequestPathsCarryAbsolutePathsToScopeValidation(t *testing.T) { | ||
| root := t.TempDir() | ||
| // NewScope also grants the system temp dir, so a sibling t.TempDir() is | ||
| // legitimately in scope; pick a path under the filesystem root instead. | ||
| outside, err := filepath.Abs(filepath.Join(string(filepath.Separator), "zero-outside-workspace-test", "escape.js")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| scope, err := NewScope(root, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| inside := filepath.Join(root, "main.js") | ||
| structured := func(header, footer, path string) string { | ||
| return strings.Join([]string{header, "*** Update File: " + path, "@@", "-a", "+b", footer}, "\n") | ||
| } | ||
| for name, spelling := range map[string][2]string{"canonical": {"*** Begin Patch", "*** End Patch"}, "no-space": {"***Begin Patch", "***End Patch"}} { | ||
| // Failure path: the exact path the boundary parsed must be denied by the | ||
| // scope. structuredPatchHeaderPaths normalises separators to "/", so the | ||
| // parsed form is compared in slash form and then validated as-is. | ||
| paths := applyPatchRequestPaths(map[string]any{"patch": structured(spelling[0], spelling[1], outside)}) | ||
| if len(paths) != 1 || paths[0] != filepath.ToSlash(outside) { | ||
| t.Fatalf("%s: absolute patch path must reach scope validation unchanged, got %v", name, paths) | ||
| } | ||
| if block := scope.validate(paths[0]); block == nil || block.Code != BlockOutsideWorkspace { | ||
| t.Fatalf("%s: scope must deny the parsed outside path %q, got %+v", name, paths[0], block) | ||
| } | ||
| // Success path: the parsed inside path must be accepted by the scope. | ||
| paths = applyPatchRequestPaths(map[string]any{"patch": structured(spelling[0], spelling[1], inside)}) | ||
| if len(paths) != 1 || paths[0] != filepath.ToSlash(inside) { | ||
| t.Fatalf("%s: inside patch path must reach scope validation unchanged, got %v", name, paths) | ||
| } | ||
| if block := scope.validate(paths[0]); block != nil { | ||
| t.Fatalf("%s: scope must accept the parsed inside path %q, got %+v", name, paths[0], block) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.