-
Notifications
You must be signed in to change notification settings - Fork 413
Fix: Path Traversal in File Tools #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { createTwoFilesPatch } from "diff"; | ||
| import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"; | ||
| import { dirname, isAbsolute, resolve } from "path"; | ||
| import { existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "fs"; | ||
| import { dirname, isAbsolute, relative, resolve } from "path"; | ||
| import { summarizeDiagnostics, syncFileWithLsp } from "../lsp/runtime"; | ||
| import type { LspDiagnosticFile } from "../lsp/types"; | ||
|
|
||
|
|
@@ -19,7 +19,39 @@ export interface FileResult { | |
| lspDiagnostics?: LspDiagnosticFile[]; | ||
| } | ||
|
|
||
| function safeRealpath(p: string): string { | ||
| try { | ||
| return realpathSync(p); | ||
| } catch { | ||
| return resolve(p); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Ensure a user/agent-supplied path cannot escape the workspace, whether via | ||
| * lexical traversal (e.g. `../../.ssh/id_rsa`) or a symlink that crosses | ||
| * outside the workspace root. Mirrors the directory-prefix guards applied | ||
| * elsewhere in this repo (e.g. `assertInsideSchedulesDir`). Throws on escape | ||
| * so the calling tool surfaces a clean failure instead of touching the host. | ||
| */ | ||
| function assertInsideWorkspace(filePath: string, cwd: string): void { | ||
| const root = safeRealpath(cwd); | ||
| const full = isAbsolute(filePath) ? filePath : resolve(cwd, filePath); | ||
|
|
||
| const rel = relative(root, full); | ||
| if (rel.startsWith("..") || isAbsolute(rel)) { | ||
| throw new Error(`Path "${filePath}" resolves outside the workspace and was rejected.`); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cwd symlink false rejectionMedium Severity The workspace root is taken from Reviewed by Cursor Bugbot for commit 33e1057. Configure here. |
||
|
|
||
| const realTarget = safeRealpath(full); | ||
| const realRel = relative(root, realTarget); | ||
| if (realRel.startsWith("..") || isAbsolute(realRel)) { | ||
| throw new Error(`Path "${filePath}" points outside the workspace via a symlink and was rejected.`); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Symlink bypass on new pathsHigh Severity When the target path does not exist yet, Additional Locations (1)Reviewed by Cursor Bugbot for commit 33e1057. Configure here. |
||
| } | ||
|
|
||
| function resolvePath(filePath: string, cwd: string): string { | ||
| assertInsideWorkspace(filePath, cwd); | ||
| return isAbsolute(filePath) ? filePath : resolve(cwd, filePath); | ||
| } | ||
|
|
||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dot-dot filename false reject
Low Severity
Escape detection uses
rel.startsWith(".."), which also matches legitimate relative paths whose first segment is a filename starting with two dots (for example..foo), so those in-workspace files are rejected incorrectly.Additional Locations (1)
src/tools/file.ts#L46-L49Reviewed by Cursor Bugbot for commit 33e1057. Configure here.