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

Function validatePath

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

Source from the content-addressed store, hash-verified

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