MCPcopy Create free account
hub / github.com/claude-code-best/claude-code / validatePath

Function validatePath

src/utils/permissions/pathValidation.ts:382–494  ·  view source on GitHub ↗
(
  path: string,
  cwd: string,
  toolPermissionContext: ToolPermissionContext,
  operationType: FileOperationType,
)

Source from the content-addressed store, hash-verified

380 * Returns whether the path is allowed and the resolved path for error messages.
381 */
382export function validatePath(
383 path: string,
384 cwd: string,
385 toolPermissionContext: ToolPermissionContext,
386 operationType: FileOperationType,
387): ResolvedPathCheckResult {
388 // Remove surrounding quotes if present
389 const cleanPath = expandTilde(path.replace(/^['"]|['"]$/g, ''))
390
391 // SECURITY: Block UNC paths that could leak credentials
392 if (containsVulnerableUncPath(cleanPath)) {
393 return {
394 allowed: false,
395 resolvedPath: cleanPath,
396 decisionReason: {
397 type: 'other',
398 reason: 'UNC network paths require manual approval',
399 },
400 }
401 }
402
403 // SECURITY: Reject tilde variants (~user, ~+, ~-, ~N) that expandTilde doesn't handle.
404 // expandTilde resolves ~ and ~/ to $HOME, but ~root, ~+, ~- etc. are left as literal
405 // text and resolved as relative paths (e.g., /cwd/~root/.ssh/id_rsa).
406 // The shell expands these differently (~root → /var/root, ~+ → $PWD, ~- → $OLDPWD),
407 // creating a TOCTOU gap: we validate /cwd/~root/... but bash reads /var/root/...
408 // This check is safe from false positives because expandTilde already converted
409 // ~ and ~/ to absolute paths starting with /, so only unexpanded variants remain.
410 if (cleanPath.startsWith('~')) {
411 return {
412 allowed: false,
413 resolvedPath: cleanPath,
414 decisionReason: {
415 type: 'other',
416 reason:
417 'Tilde expansion variants (~user, ~+, ~-) in paths require manual approval',
418 },
419 }
420 }
421
422 // SECURITY: Reject paths containing ANY shell expansion syntax ($ or % characters,
423 // or paths starting with = which triggers Zsh equals expansion)
424 // - $VAR (Unix/Linux environment variables like $HOME, $PWD)
425 // - ${VAR} (brace expansion)
426 // - $(cmd) (command substitution)
427 // - %VAR% (Windows environment variables like %TEMP%, %USERPROFILE%)
428 // - Nested combinations like $(echo $HOME)
429 // - =cmd (Zsh equals expansion, e.g. =rg expands to /usr/bin/rg)
430 // All of these are preserved as literal strings during validation but expanded
431 // by the shell during execution, creating a TOCTOU vulnerability
432 if (
433 cleanPath.includes('$') ||
434 cleanPath.includes('%') ||
435 cleanPath.startsWith('=')
436 ) {
437 return {
438 allowed: false,
439 resolvedPath: cleanPath,

Callers

nothing calls this directly

Calls 7

validateGlobPatternFunction · 0.85
safeResolvePathFunction · 0.85
getFsImplementationFunction · 0.85
expandTildeFunction · 0.70
isPathAllowedFunction · 0.70
resolveFunction · 0.50

Tested by

no test coverage detected