fix(ast-grep-mcp): allow absolute paths whose realpath is inside the workspace
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -15,7 +15,7 @@ function resolveWorkspacePath(rawPath: string, workspaceDirectory: string): stri
|
||||
if (rawPath.length === 0) throw new Error("paths entries must be non-empty strings");
|
||||
if (rawPath.startsWith("-")) throw new Error(`paths entries must not start with '-': ${rawPath}`);
|
||||
if (rawPath.includes("\0")) throw new Error("paths entries must not contain null bytes");
|
||||
if (isAbsolute(rawPath)) throw new Error(`paths entries must be relative to the workspace: ${rawPath}`);
|
||||
if (isAbsolute(rawPath)) return resolveAbsoluteWorkspacePath(rawPath, workspaceDirectory);
|
||||
|
||||
const absolutePath = resolve(workspaceDirectory, rawPath);
|
||||
assertInsideWorkspace(absolutePath, workspaceDirectory, rawPath);
|
||||
@@ -29,6 +29,20 @@ function resolveWorkspacePath(rawPath: string, workspaceDirectory: string): stri
|
||||
return normalizedPath === "" ? "." : normalizedPath;
|
||||
}
|
||||
|
||||
function resolveAbsoluteWorkspacePath(rawPath: string, workspaceDirectory: string): string {
|
||||
let realPath: string;
|
||||
try {
|
||||
realPath = realpathSync(rawPath);
|
||||
} catch {
|
||||
throw new Error(`absolute path entry does not exist: ${rawPath}`);
|
||||
}
|
||||
|
||||
assertInsideWorkspace(realPath, workspaceDirectory, rawPath);
|
||||
|
||||
const normalizedPath = relative(workspaceDirectory, realPath);
|
||||
return normalizedPath === "" ? "." : normalizedPath;
|
||||
}
|
||||
|
||||
function assertInsideWorkspace(candidatePath: string, workspaceDirectory: string, rawPath: string): void {
|
||||
const workspaceRelativePath = relative(workspaceDirectory, candidatePath);
|
||||
if (workspaceRelativePath === "" || (!workspaceRelativePath.startsWith("..") && !isAbsolute(workspaceRelativePath))) return;
|
||||
|
||||
Reference in New Issue
Block a user