-
Notifications
You must be signed in to change notification settings - Fork 1
WEB-4871: forward Grep/Glob/LS reads from the claude-code hook #161
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
Open
vigneshsubbiah16
wants to merge
3
commits into
staging
Choose a base branch
from
web-4871-forward-grep-glob-ls
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b310472
WEB-4871: forward Grep/Glob/LS reads from the claude-code hook
MohamedAklamaash ca1bb12
WEB-4871: only fall back to pattern for Glob, not Grep (review)
MohamedAklamaash 020d67e
fix(hook): close Grep/Glob/LS path-policy gaps (W1/W2/W3) + add tests
vigneshsubbiah16 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """ | ||
| Tests for read-equivalent tool (Grep/Glob/LS) path forwarding in | ||
| claude-code/hooks/unbound.py (WEB-4871). | ||
|
|
||
| Covers: | ||
| - _derive_read_equivalent_path (file_path the gateway evaluates) | ||
| - extract_command_for_pretool (LS command / approval-key, regression on others) | ||
|
|
||
| These pin the W1/W2/W3 review fixes: the no-`path` (whole-cwd-scan) case must | ||
| forward the cwd rather than nothing, LS must be path-specific, and Grep's regex | ||
| pattern must never be forwarded as a path. | ||
| """ | ||
|
|
||
| import unittest | ||
|
|
||
| import unbound | ||
|
|
||
|
|
||
| def _event(tool_name, tool_input, cwd=None): | ||
| e = {"tool_name": tool_name, "tool_input": tool_input} | ||
| if cwd is not None: | ||
| e["cwd"] = cwd | ||
| return e | ||
|
|
||
|
|
||
| class TestDeriveReadEquivalentPath(unittest.TestCase): | ||
| def d(self, tool_name, tool_input, cwd=None): | ||
| return unbound._derive_read_equivalent_path( | ||
| tool_name, tool_input, {"cwd": cwd} if cwd is not None else {} | ||
| ) | ||
|
|
||
| # Explicit path always wins | ||
| def test_grep_explicit_path(self): | ||
| self.assertEqual(self.d("Grep", {"pattern": "X", "path": "/a/b"}), "/a/b") | ||
|
|
||
| def test_ls_explicit_path(self): | ||
| self.assertEqual(self.d("LS", {"path": "/etc"}), "/etc") | ||
|
|
||
| def test_glob_explicit_path_beats_pattern(self): | ||
| self.assertEqual(self.d("Glob", {"pattern": "**/*.env", "path": "/p"}), "/p") | ||
|
|
||
| # W2: no path -> fall back to cwd (whole-tree scan must not be path-less) | ||
| def test_grep_no_path_falls_back_to_cwd(self): | ||
| self.assertEqual(self.d("Grep", {"pattern": "AWS_SECRET"}, cwd="/repo"), "/repo") | ||
|
|
||
| def test_ls_no_path_falls_back_to_cwd(self): | ||
| self.assertEqual(self.d("LS", {}, cwd="/repo"), "/repo") | ||
|
|
||
| # W3: Glob keeps the path-like pattern as a fallback (filename/glob policies) | ||
| def test_glob_pattern_used_when_no_path(self): | ||
| self.assertEqual(self.d("Glob", {"pattern": "**/*.pem"}, cwd="/repo"), "**/*.pem") | ||
|
|
||
| def test_glob_no_path_no_pattern_falls_back_to_cwd(self): | ||
| self.assertEqual(self.d("Glob", {}, cwd="/repo"), "/repo") | ||
|
|
||
| # Grep's regex pattern is NEVER forwarded as a path | ||
| def test_grep_pattern_never_used_as_path(self): | ||
| self.assertEqual(self.d("Grep", {"pattern": "SECRET_KEY.*="}, cwd="/repo"), "/repo") | ||
|
|
||
| # Non-breaking edge: nothing available -> None (caller forwards no file_path) | ||
| def test_no_path_no_cwd_returns_none(self): | ||
| self.assertIsNone(self.d("Grep", {"pattern": "X"})) | ||
| self.assertIsNone(self.d("LS", {})) | ||
| self.assertIsNone(self.d("Glob", {})) | ||
|
|
||
|
|
||
| class TestExtractCommandForPretool(unittest.TestCase): | ||
| # W1: LS command is path-specific (no "LS:LS" approval-key collapse) | ||
| def test_ls_with_path(self): | ||
| self.assertEqual( | ||
| unbound.extract_command_for_pretool(_event("LS", {"path": "/srv"})), "/srv" | ||
| ) | ||
|
|
||
| def test_ls_no_path_uses_cwd(self): | ||
| self.assertEqual( | ||
| unbound.extract_command_for_pretool(_event("LS", {}, cwd="/repo")), "/repo" | ||
| ) | ||
|
|
||
| def test_ls_no_path_no_cwd_falls_back_to_tool_name(self): | ||
| self.assertEqual(unbound.extract_command_for_pretool(_event("LS", {})), "LS") | ||
|
|
||
| # Regression: unchanged behavior for the other tools | ||
| def test_grep_returns_pattern(self): | ||
| self.assertEqual( | ||
| unbound.extract_command_for_pretool(_event("Grep", {"pattern": "X"})), "X" | ||
| ) | ||
|
|
||
| def test_glob_returns_pattern(self): | ||
| self.assertEqual( | ||
| unbound.extract_command_for_pretool(_event("Glob", {"pattern": "**/*.py"})), | ||
| "**/*.py", | ||
| ) | ||
|
|
||
| def test_read_returns_file_path(self): | ||
| self.assertEqual( | ||
| unbound.extract_command_for_pretool( | ||
| _event("Read", {"file_path": "/x/y"}) | ||
| ), | ||
| "/x/y", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
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.