Skip to content

fix: path traversal vulnerability in listDirectory (#463)#469

Open
Jah-yee wants to merge 1 commit intoCodebuffAI:mainfrom
Jah-yee:fix/list-directory-path-traversal
Open

fix: path traversal vulnerability in listDirectory (#463)#469
Jah-yee wants to merge 1 commit intoCodebuffAI:mainfrom
Jah-yee:fix/list-directory-path-traversal

Conversation

@Jah-yee
Copy link
Copy Markdown

@Jah-yee Jah-yee commented Mar 10, 2026

Summary

Root Cause

The original check !resolvedPath.startsWith(projectPath) fails when:

  • projectPath = /home/user/project
  • directoryPath = ../project-evil
  • resolvedPath = /home/user/project-evil
  • /home/user/project-evil.startsWith(/home/user/project) = true (incorrectly allows)

Fix

if (
  !resolvedPath.startsWith(projectPath + path.sep) &&
  resolvedPath !== projectPath
) {

This matches the pattern already used in code-search.ts (lines 52-53).

Testing

The fix ensures:

  1. Direct child directories are accessible (e.g., src, ../sibling)
  2. Parent directories are blocked (e.g., .., ../../etc)
  3. Sibling directories with shared prefix are blocked (e.g., ../project-evil)
  4. Project root itself can be listed

Fixes security vulnerability CodebuffAI#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.
@hiSandog
Copy link
Copy Markdown
Contributor

hiSandog commented Apr 9, 2026

I think this fixes the shared-prefix case, but sdk/src/tools/list-directory.ts still looks traversable through symlinks inside the repo.

path.resolve(projectPath, directoryPath) only normalizes the lexical path. If the workspace contains something like project/link -> /etc, then directoryPath="link" resolves to /repo/link, passes the new startsWith(projectPath + path.sep) guard, and fs.readdir(resolvedPath) will still enumerate /etc.

So the old prefix bug is closed, but an attacker can still escape the project root through a checked-in or generated symlink. I would either compare realpath(projectPath) vs realpath(resolvedPath) before reading, or explicitly lstat and reject symlinked directory targets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants