diff --git a/packages/opencode/src/tool/glob.ts b/packages/opencode/src/tool/glob.ts index a2611246c66..1dbb5cfee79 100644 --- a/packages/opencode/src/tool/glob.ts +++ b/packages/opencode/src/tool/glob.ts @@ -31,6 +31,13 @@ export const GlobTool = Tool.define("glob", { let search = params.path ?? Instance.directory search = path.isAbsolute(search) ? search : path.resolve(Instance.directory, search) + + // Prevent searching root directory + const parsed = path.parse(search) + if (search === parsed.root) { + throw new Error(`Searching root directory is not allowed: ${search}`) + } + await assertExternalDirectory(ctx, search, { kind: "directory" }) const limit = 100 diff --git a/packages/opencode/src/tool/grep.ts b/packages/opencode/src/tool/grep.ts index 82e7ac1667e..e818dbfe15c 100644 --- a/packages/opencode/src/tool/grep.ts +++ b/packages/opencode/src/tool/grep.ts @@ -37,6 +37,13 @@ export const GrepTool = Tool.define("grep", { let searchPath = params.path ?? Instance.directory searchPath = path.isAbsolute(searchPath) ? searchPath : path.resolve(Instance.directory, searchPath) + + // Prevent searching root directory + const parsed = path.parse(searchPath) + if (searchPath === parsed.root) { + throw new Error(`Searching root directory is not allowed: ${searchPath}`) + } + await assertExternalDirectory(ctx, searchPath, { kind: "directory" }) const rgPath = await Ripgrep.filepath()