fix: robust ripgrep path resolution and 1p hermetic execution support (#27253)

This commit is contained in:
Coco Sheng
2026-05-20 12:29:00 -04:00
committed by GitHub
parent 2c85f57402
commit 29481a1562
4 changed files with 132 additions and 4 deletions
+63
View File
@@ -811,6 +811,24 @@ describe('normalizePath', () => {
expect(isTrustedSystemPath(cwd)).toBe(false);
});
it('should not reject paths if the current working directory is the root directory', () => {
mockPlatform('linux');
const originalCwd = process.cwd;
process.cwd = vi.fn().mockReturnValue('/');
expect(isTrustedSystemPath('/usr/bin/rg')).toBe(true);
process.cwd = originalCwd;
});
it('should not reject paths if the current working directory is a Windows root directory', () => {
mockPlatform('win32');
vi.stubEnv('SystemRoot', 'C:\\Windows');
const originalCwd = process.cwd;
process.cwd = vi.fn().mockReturnValue('C:\\');
expect(isTrustedSystemPath('C:\\Windows\\System32\\rg.exe')).toBe(true);
process.cwd = originalCwd;
vi.unstubAllEnvs();
});
it('should allow trusted paths on Windows', () => {
mockPlatform('win32');
vi.stubEnv('SystemRoot', 'C:\\Windows');
@@ -854,5 +872,50 @@ describe('normalizePath', () => {
expect(isTrustedSystemPath('/tmp/rg')).toBe(false);
expect(isTrustedSystemPath('/Library/rg')).toBe(false);
});
it('should allow 1P internal hermetic execution paths', () => {
mockPlatform('linux');
expect(isTrustedSystemPath('/google/bin/rg')).toBe(true);
expect(
isTrustedSystemPath(
'/google/src/cloud/user/workspace/bazel-out/k8-fastbuild/bin/rg',
),
).toBe(true);
expect(
isTrustedSystemPath(
'/google/src/cloud/user/workspace/blaze-out/k8-opt/bin/rg',
),
).toBe(true);
});
describe('in secure hermetic environments', () => {
const originalCwd = process.cwd;
const cwd = '/sandbox';
beforeEach(() => {
mockPlatform('linux');
process.cwd = vi.fn().mockReturnValue(cwd);
});
afterEach(() => {
process.cwd = originalCwd;
vi.unstubAllEnvs();
});
it('should reject paths in the CWD by default', () => {
expect(isTrustedSystemPath(path.join(cwd, 'bin/rg'))).toBe(false);
});
it.each([
['TEST_SRCDIR', '/mock/runfiles'],
['BAZEL_TEST', '1'],
['TEST_WORKSPACE', 'my_workspace'],
['RUNFILES_DIR', '/mock/runfiles'],
])('should bypass CWD rejection when %s is set', (envVar, value) => {
vi.stubEnv(envVar, value);
expect(isTrustedSystemPath(path.join(cwd, 'bin/rg'))).toBe(true);
});
});
});
});
+11 -1
View File
@@ -527,10 +527,17 @@ export function isTrustedSystemPath(filePath: string): boolean {
// 1. Explicitly reject paths in current working directory to prevent RCE
// Exclude root directories to avoid inadvertently rejecting all system paths.
// Bypass this restriction in secure, hermetic environments (e.g., Bazel/Blaze).
const isHermeticEnv =
!!process.env['TEST_SRCDIR'] ||
!!process.env['TEST_WORKSPACE'] ||
!!process.env['BAZEL_TEST'] ||
!!process.env['RUNFILES_DIR'];
const normCwd = normalizePath(process.cwd());
const isRoot = normCwd === '/' || /^[a-zA-Z]:[\\/]?$/.test(normCwd);
if (!isRoot && isSubpath(normCwd, normPath)) {
return false;
return isHermeticEnv;
}
// 2. Allow standard system directories
@@ -555,6 +562,9 @@ export function isTrustedSystemPath(filePath: string): boolean {
'/usr/local/Cellar',
'/usr/sbin',
'/sbin',
// 1P internal hermetic execution paths
'/google/bin',
'/google/src/cloud',
].map((p) => normalizePath(p));
return trustedPrefixes.some(