-
Notifications
You must be signed in to change notification settings - Fork 14.5k
fix: add system PATH fallback for ripgrep resolution (#26777) #26868
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
13 commits
Select commit
Hold shift + click to select a range
7126690
fix(core): secure and minimal ripgrep fallback resolution
cocosheng-g 35a4664
fix(core): resolve ripgrep globalSetup exports and windows path test …
cocosheng-g e56819c
fix(core): secure path resolution for ripgrep and commandSafety
cocosheng-g ce9678f
test(core): add unit tests for isTrustedSystemPath and commandSafety …
cocosheng-g d2d66a4
refactor(core): extract isRipgrep helper in commandSafety
cocosheng-g a45d708
fix(security): resolve Search Path Interruption vulnerability by deco…
cocosheng-g 811ae89
fix(security): strictly validate the real path of system ripgrep to p…
cocosheng-g ca41e1b
fix(security): securely handle root CWD in isTrustedSystemPath
cocosheng-g 5bcc400
fix(security): securely handle root CWD using isSubpath in isTrustedS…
cocosheng-g f271fcd
test(core): fix windows test failure by mocking process.platform in r…
cocosheng-g a9d4764
test(core): explicitly test ripgrep fallback logic on both POSIX and …
cocosheng-g 4673ff5
test(integration): mock getRipgrepPath in ripgrep-real E2E tests
cocosheng-g 772e0c5
Merge remote-tracking branch 'origin/main' into fix/issue-26777
cocosheng-g 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
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,129 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
| import { | ||
| isStrictlyApproved, | ||
| isKnownSafeCommand, | ||
| isDangerousCommand, | ||
| } from './commandSafety.js'; | ||
| import * as paths from '../../utils/paths.js'; | ||
|
|
||
| vi.mock('../../utils/paths.js', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import('../../utils/paths.js')>(); | ||
| return { | ||
| ...actual, | ||
| resolveToRealPath: vi.fn((p: string) => p), | ||
| isTrustedSystemPath: vi.fn(() => false), | ||
| }; | ||
| }); | ||
|
|
||
| describe('commandSafety', () => { | ||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| describe('rg specific logic', () => { | ||
| it('should consider rg safe without unsafe args if path is trusted', () => { | ||
| vi.mocked(paths.resolveToRealPath).mockReturnValue('/usr/bin/rg'); | ||
| vi.mocked(paths.isTrustedSystemPath).mockReturnValue(true); | ||
|
|
||
| // Using isKnownSafeCommand which calls isSafeToCallWithExec under the hood | ||
| expect(isKnownSafeCommand(['/usr/bin/rg', 'pattern', 'file.txt'])).toBe( | ||
| true, | ||
| ); | ||
| expect(paths.resolveToRealPath).toHaveBeenCalledWith('/usr/bin/rg'); | ||
| expect(paths.isTrustedSystemPath).toHaveBeenCalledWith('/usr/bin/rg'); | ||
| }); | ||
|
|
||
| it('should not consider bare rg safe (Search Path Interruption prevention)', () => { | ||
| // Bare 'rg' is not an absolute path, so it fails `isTrustedCommandPath` | ||
| expect(isKnownSafeCommand(['rg', 'pattern', 'file.txt'])).toBe(false); | ||
| }); | ||
|
|
||
| it('should not consider rg safe with unsafe args even if path is trusted', () => { | ||
| vi.mocked(paths.resolveToRealPath).mockReturnValue('/usr/bin/rg'); | ||
| vi.mocked(paths.isTrustedSystemPath).mockReturnValue(true); | ||
|
|
||
| expect( | ||
| isKnownSafeCommand(['/usr/bin/rg', '--search-zip', 'pattern']), | ||
| ).toBe(false); | ||
| expect(isKnownSafeCommand(['/usr/bin/rg', '-z', 'pattern'])).toBe(false); | ||
| expect(isKnownSafeCommand(['/usr/bin/rg', '--pre=cat', 'pattern'])).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
|
|
||
| it('should consider rg dangerous with unsafe args', () => { | ||
| vi.mocked(paths.resolveToRealPath).mockReturnValue('/usr/bin/rg'); | ||
| vi.mocked(paths.isTrustedSystemPath).mockReturnValue(true); | ||
|
|
||
| expect( | ||
| isDangerousCommand(['/usr/bin/rg', '--search-zip', 'pattern']), | ||
| ).toBe(true); | ||
| expect(isDangerousCommand(['/usr/bin/rg', '--pre=cat', 'pattern'])).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('should not consider rg safe if path is untrusted', () => { | ||
| vi.mocked(paths.resolveToRealPath).mockReturnValue('/tmp/malicious/rg'); | ||
| vi.mocked(paths.isTrustedSystemPath).mockReturnValue(false); | ||
|
|
||
| expect(isKnownSafeCommand(['/tmp/malicious/rg', 'pattern'])).toBe(false); | ||
| expect(paths.resolveToRealPath).toHaveBeenCalledWith('/tmp/malicious/rg'); | ||
| }); | ||
|
|
||
| it('should not consider rg safe if path resolution throws', () => { | ||
| vi.mocked(paths.resolveToRealPath).mockImplementation(() => { | ||
| throw new Error('Resolution failed'); | ||
| }); | ||
| vi.mocked(paths.isTrustedSystemPath).mockReturnValue(true); | ||
|
|
||
| expect(isKnownSafeCommand(['/some/path/rg', 'pattern'])).toBe(false); | ||
| }); | ||
|
|
||
| it('should flag untrusted rg as dangerous if it has unsafe args (Paranoid validation)', () => { | ||
| vi.mocked(paths.resolveToRealPath).mockReturnValue('/tmp/malicious/rg'); | ||
| vi.mocked(paths.isTrustedSystemPath).mockReturnValue(false); | ||
|
|
||
| // isDangerousCommand relies on isRipgrepCommand, which strictly identifies intent (name) | ||
| // and doesn't care about path safety. So even an untrusted rg will be flagged if it has unsafe args. | ||
| expect(isDangerousCommand(['/tmp/malicious/rg', '--search-zip'])).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isStrictlyApproved', () => { | ||
| it('should approve rg if explicitly in approved tools regardless of path', async () => { | ||
| // In this case, isStrictlyApproved relies on `tools.includes(command)` | ||
| expect( | ||
| await isStrictlyApproved( | ||
| '/tmp/malicious/rg', | ||
| ['pattern'], | ||
| ['/tmp/malicious/rg'], | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('should approve rg if path is trusted', async () => { | ||
| vi.mocked(paths.resolveToRealPath).mockReturnValue('/usr/bin/rg'); | ||
| vi.mocked(paths.isTrustedSystemPath).mockReturnValue(true); | ||
|
|
||
| expect(await isStrictlyApproved('/usr/bin/rg', ['pattern'])).toBe(true); | ||
| }); | ||
|
|
||
| it('should reject rg if path is untrusted and not explicitly approved', async () => { | ||
| vi.mocked(paths.resolveToRealPath).mockReturnValue('/tmp/malicious/rg'); | ||
| vi.mocked(paths.isTrustedSystemPath).mockReturnValue(false); | ||
|
|
||
| expect(await isStrictlyApproved('/tmp/malicious/rg', ['pattern'])).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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.