MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / resolveSafePath

Function resolveSafePath

packages/agent-core/src/services/fs/fsPathSafety.ts:38–74  ·  view source on GitHub ↗
(
  cwd: string,
  inputPath: string,
)

Source from the content-addressed store, hash-verified

36}
37
38export async function resolveSafePath(
39 cwd: string,
40 inputPath: string,
41): Promise<PathSafetyResult> {
42
43 if (inputPath === '' || inputPath === '/') {
44 throw new FsPathEscapesError(inputPath, 'empty');
45 }
46
47 if (path.isAbsolute(inputPath)) {
48 throw new FsPathEscapesError(inputPath, 'absolute');
49 }
50
51 const segments = inputPath.split(/[/\\]+/);
52 if (segments.some((s) => s === '..')) {
53 throw new FsPathEscapesError(inputPath, 'dotdot_segment');
54 }
55
56 const realCwd = await fs.realpath(cwd);
57
58 const candidate = path.resolve(realCwd, inputPath);
59
60 const resolved = await realpathLongestExistingPrefix(candidate);
61
62 if (!isInsideOrEqual(resolved, realCwd)) {
63
64 const reason: FsPathEscapesError['reason'] = isInsideOrEqual(candidate, realCwd)
65 ? 'symlink_outside_cwd'
66 : 'resolved_outside_cwd';
67 throw new FsPathEscapesError(inputPath, reason, resolved);
68 }
69
70 return {
71 absolute: resolved,
72 relative: toPosixRelative(realCwd, resolved),
73 };
74}
75
76function isInsideOrEqual(child: string, parent: string): boolean {
77 const rel = path.relative(parent, child);

Callers 13

createMethod · 0.90
listMethod · 0.90
readMethod · 0.90
statMethod · 0.90
statManyMethod · 0.90
mkdirMethod · 0.90
resolveDownloadMethod · 0.90
resolvePathMethod · 0.90
statusMethod · 0.90
diffMethod · 0.90
addFunction · 0.90

Calls 4

isInsideOrEqualFunction · 0.85
toPosixRelativeFunction · 0.70
resolveMethod · 0.65

Tested by

no test coverage detected