Harden AI search tool path filtering#960
Conversation
📝 WalkthroughWalkthroughAdds a ChangesSearch tool path safety
Estimated code review effort: 2 (Simple) | ~15 minutes Related PRs: None identified from the provided context. Suggested labels: security, tests Suggested reviewers: None identified from the provided context.
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/modules/ai/tools/search.ts (1)
95-107: 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy liftApply the result cap after safety filtering.
maxResultsis passed to native search before unsafe hits are filtered. If sensitive matches fill the native cap, this can return zero safe hits while later safe matches exist. Consider pushing the readability filter into native search, or over-fetching internally and slicing after filtering so the user-requested cap applies to safe hits.As per path instructions, watch for blast radius and silent behavior changes beyond the local fix.
Also applies to: 140-141
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/modules/ai/tools/search.ts` around lines 95 - 107, The result cap is being applied before safety filtering in the search flow, so unsafe hits can consume the native limit and hide later readable matches. Update the search path in search.ts around the res.hits filtering and result mapping so that maxResults applies to the post-filtered safe hits, either by moving the isReadableSearchHit check earlier into the native search path or by over-fetching and slicing after filtering. Keep the change localized to the search result processing and the analogous logic referenced in the other affected section, and avoid altering unrelated behavior such as truncation or file-scanned reporting.Source: Path instructions
🧹 Nitpick comments (1)
src/modules/ai/tools/search.test.ts (1)
52-96: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueGrep test only exercises absolute hit paths.
The glob test covers both a relative hit (
service-account-prod.json) and an absolute one (/etc/passwd) exercisingpathForSafety's branch logic, but the grep test's fixtures are all absolute paths. SincepathForSafetyhas a distinct relative-path branch (joining withroot), adding one relative-path grep fixture would close a small coverage gap for the shared helper.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/modules/ai/tools/search.test.ts` around lines 52 - 96, The grep test in search.test.ts only uses absolute hit paths, so it misses the relative-path branch in pathForSafety. Update the fixture in buildSearchTools/makeContext grep coverage to include at least one relative hit path alongside the existing absolute paths, and assert it is filtered or resolved the same way so both branches of the shared safety helper are exercised.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/modules/ai/tools/search.ts`:
- Around line 95-107: The result cap is being applied before safety filtering in
the search flow, so unsafe hits can consume the native limit and hide later
readable matches. Update the search path in search.ts around the res.hits
filtering and result mapping so that maxResults applies to the post-filtered
safe hits, either by moving the isReadableSearchHit check earlier into the
native search path or by over-fetching and slicing after filtering. Keep the
change localized to the search result processing and the analogous logic
referenced in the other affected section, and avoid altering unrelated behavior
such as truncation or file-scanned reporting.
---
Nitpick comments:
In `@src/modules/ai/tools/search.test.ts`:
- Around line 52-96: The grep test in search.test.ts only uses absolute hit
paths, so it misses the relative-path branch in pathForSafety. Update the
fixture in buildSearchTools/makeContext grep coverage to include at least one
relative hit path alongside the existing absolute paths, and assert it is
filtered or resolved the same way so both branches of the shared safety helper
are exercised.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: cf8eb458-a0e0-4345-a15a-656be58e9623
📒 Files selected for processing (2)
src/modules/ai/tools/search.test.tssrc/modules/ai/tools/search.ts
Summary
Verification
Note: full repo formatter checks currently report unrelated pre-existing formatting drift outside this PR.
Summary by CodeRabbit