File size: 11,258 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | /**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
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,
);
});
});
describe('workspace path boundary validation in isKnownSafeCommand', () => {
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'gemini-cli-test-safety-'),
);
const workspaceDir = path.join(tempDir, 'workspace');
const outsideDir = path.join(tempDir, 'outside');
beforeEach(() => {
fs.mkdirSync(workspaceDir, { recursive: true });
fs.mkdirSync(outsideDir, { recursive: true });
fs.writeFileSync(
path.join(workspaceDir, 'in_workspace.txt'),
'in workspace',
);
fs.writeFileSync(path.join(outsideDir, 'outside.txt'), 'outside content');
});
it('should consider ls or cat safe with in-workspace arguments', () => {
expect(
isKnownSafeCommand(
['ls', 'in_workspace.txt'],
workspaceDir,
workspaceDir,
),
).toBe(true);
expect(
isKnownSafeCommand(
['cat', 'in_workspace.txt'],
workspaceDir,
workspaceDir,
),
).toBe(true);
});
it('should reject ls or cat when argument traverses outside workspace', () => {
expect(
isKnownSafeCommand(
['ls', '../outside/outside.txt'],
workspaceDir,
workspaceDir,
),
).toBe(false);
expect(
isKnownSafeCommand(
['cat', path.join(outsideDir, 'outside.txt')],
workspaceDir,
workspaceDir,
),
).toBe(false);
});
it('should reject safe commands accessing a symlink pointing outside workspace', () => {
const symlinkPath = path.join(workspaceDir, 'my_link');
if (!fs.existsSync(symlinkPath)) {
fs.symlinkSync(outsideDir, symlinkPath);
}
expect(
isKnownSafeCommand(['ls', 'my_link'], workspaceDir, workspaceDir),
).toBe(false);
expect(
isKnownSafeCommand(
['cat', 'my_link/outside.txt'],
workspaceDir,
workspaceDir,
),
).toBe(false);
});
it('should reject commands with unresolved shell variable expansions in paths', () => {
expect(
isKnownSafeCommand(['ls', '${d}'], workspaceDir, workspaceDir),
).toBe(false);
expect(
isKnownSafeCommand(['cat', '$SECRET_PATH'], workspaceDir, workspaceDir),
).toBe(false);
});
it('should reject commands with user-specific tilde expansions', () => {
expect(
isKnownSafeCommand(['ls', '~root/secret'], workspaceDir, workspaceDir),
).toBe(false);
});
it('should reject commands when effectiveCwd is outside workspace', () => {
expect(isKnownSafeCommand(['ls'], outsideDir, workspaceDir)).toBe(false);
expect(isKnownSafeCommand(['pwd'], outsideDir, workspaceDir)).toBe(false);
});
it('should reject grep or rg with file options pointing outside workspace', () => {
const outsideFile = path.join(outsideDir, 'outside.txt');
expect(
isKnownSafeCommand(
['grep', '--file', outsideFile, 'pattern'],
workspaceDir,
workspaceDir,
),
).toBe(false);
expect(
isKnownSafeCommand(
['grep', `-f${outsideFile}`, 'pattern'],
workspaceDir,
workspaceDir,
),
).toBe(false);
expect(
isKnownSafeCommand(
['/usr/bin/rg', '--file', outsideFile, 'pattern'],
workspaceDir,
workspaceDir,
),
).toBe(false);
});
it('should reject cd with switches escaping workspace and bare cd escaping', () => {
expect(
isKnownSafeCommand(
['cd', '-P', '../outside'],
workspaceDir,
workspaceDir,
),
).toBe(false);
expect(
isKnownSafeCommand(
['cd', '-L', '../outside'],
workspaceDir,
workspaceDir,
),
).toBe(false);
expect(isKnownSafeCommand(['cd'], workspaceDir, workspaceDir)).toBe(
false,
);
});
it('should reject find commands with paths escaping workspace even with options', () => {
expect(
isKnownSafeCommand(
['find', '-L', outsideDir],
workspaceDir,
workspaceDir,
),
).toBe(false);
expect(
isKnownSafeCommand(
['find', '.', '-type', 'f', outsideDir],
workspaceDir,
workspaceDir,
),
).toBe(false);
});
it('should reject file-reading commands with options containing relative paths escaping workspace', () => {
expect(
isKnownSafeCommand(
['cat', '--output=subdir/../../outside/file'],
workspaceDir,
workspaceDir,
),
).toBe(false);
});
it('should reject file-reading commands and grep/rg when an argument starting with hyphen is an existing file escaping workspace', () => {
const hyphenSymlinkName = '-escapedFile';
const hyphenSymlinkPath = path.join(workspaceDir, hyphenSymlinkName);
const outsideFile = path.join(outsideDir, 'outside.txt');
fs.symlinkSync(outsideFile, hyphenSymlinkPath);
try {
expect(
isKnownSafeCommand(
['cat', hyphenSymlinkName],
workspaceDir,
workspaceDir,
),
).toBe(false);
expect(
isKnownSafeCommand(
['grep', 'foo', hyphenSymlinkName],
workspaceDir,
workspaceDir,
),
).toBe(false);
expect(
isKnownSafeCommand(
['/usr/bin/rg', 'foo', hyphenSymlinkName],
workspaceDir,
workspaceDir,
),
).toBe(false);
} finally {
if (fs.existsSync(hyphenSymlinkPath)) {
fs.unlinkSync(hyphenSymlinkPath);
}
}
});
});
describe('ln command safety in isDangerousCommand', () => {
it('should flag ln with symbolic flags as dangerous', () => {
expect(isDangerousCommand(['ln', '-s', 'target', 'link'])).toBe(true);
expect(isDangerousCommand(['ln', '-sf', 'target', 'link'])).toBe(true);
expect(isDangerousCommand(['ln', '--symbolic', 'target', 'link'])).toBe(
true,
);
});
it('should not flag hard link ln without symbolic flag as dangerous', () => {
expect(isDangerousCommand(['ln', 'target', 'link'])).toBe(false);
});
});
});
|