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

Function validatePath

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

Source from the content-addressed store, hash-verified

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