Skip to content

Commit a9c9167

Browse files
author
SparkLabScout
committed
fix: path traversal in listDirectory
Fixes security vulnerability #463 The original check using startsWith(projectPath) could be bypassed with sibling directories that share a prefix with the project path. Example: projectPath=/home/user/project, directoryPath=../project-evil resolves to /home/user/project-evil which passes startsWith('/home/user/project') because 'project-evil' starts with 'project'. This fix adds path.sep to ensure we're checking for proper directory boundary, and also checks for exact match to allow listing the project root itself. This matches the pattern already used in code-search.ts.
1 parent 822a299 commit a9c9167

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

sdk/src/tools/list-directory.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ export async function listDirectory(params: {
1313
try {
1414
const resolvedPath = path.resolve(projectPath, directoryPath)
1515

16-
if (!resolvedPath.startsWith(projectPath)) {
16+
if (
17+
!resolvedPath.startsWith(projectPath + path.sep) &&
18+
resolvedPath !== projectPath
19+
) {
1720
return [
1821
{
1922
type: 'json',

0 commit comments

Comments
 (0)