Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Bug: list-directory path traversal — missing path.sep in startsWith check #463

Copy link
Copy link
@dsurchisactive

Description

@dsurchisactive
Issue body actions

Description

The path traversal check in listDirectory() can be bypassed using sibling directory names that share a prefix with the project path.

Root Cause

In sdk/src/tools/list-directory.ts (line 16):

const resolvedPath = path.resolve(projectPath, directoryPath)
if (!resolvedPath.startsWith(projectPath)) { ... }

If projectPath is /home/user/project and directoryPath is ../project-evil, the resolved path /home/user/project-evil passes the startsWith('/home/user/project') check because it's a string prefix match.

The same codebase gets this right in code-search.ts (lines 52-53):

if (
  !searchCwd.startsWith(projectRoot + path.sep) &&
  searchCwd !== projectRoot
) { ... }

Impact

A sandboxed agent or user-controlled directoryPath parameter can list directories outside the declared project root, as long as the sibling directory name starts with the project directory name.

Suggested Fix

if (
  !resolvedPath.startsWith(projectPath + path.sep) &&
  resolvedPath !== projectPath
) {
  return [{ type: 'json', value: { errorMessage: `Invalid path...` } }]
}

This matches the pattern already used in code-search.ts.

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.