fix(sandbox-container): prevent path traversal in file read/write/delete - #478
Open
NAVEENKUMARKR777 wants to merge 1 commit into
Open
NAVEENKUMARKR777 wants to merge 1 commit into
NAVEENKUMARKR777 wants to merge 1 commit into
Conversation
container_file_write, container_file_read, and container_file_delete took the client-supplied path and passed it straight into fs.writeFile (no cwd join at all) or joined it onto the working directory with path.join, which does not stop `..` segments or a leading `/` from resolving outside of it. A path like `../../etc/passwd` or an absolute path such as `/etc/cron.d/x` therefore escaped the sandbox's working directory entirely, giving arbitrary file read/write/delete on the container filesystem through an MCP tool call. Add resolve_in_workdir(), a single guard that resolves the requested path against process.cwd() and rejects anything that resolves outside of it, and route all three handlers through it instead of the ad hoc path.join calls. Fixes cloudflare#401
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
container_file_write,container_file_read, andcontainer_file_delete— the MCP tools backing the sandbox's file endpoints — took the client-suppliedpath(a free-form string, no format constraint inFileWrite/FilePathParam) and used it with no containment check:POST /files/contents(write) calledfs.writeFile(reqPath, file.text)directly, with nocwdjoin at all.GET/DELETE /files/contents/*joined it onto the working directory withpath.join(process.cwd(), reqPath), which normalizes..segments but does not stop them from climbing outside the base directory, and does not stop an absolute path from being concatenated straight onto the prefix.So a write with
path: "../../etc/cron.d/x"(or an absolute path) resolved outside the container'sworkdir, and reads/deletes were exploitable the same way — arbitrary file read/write/delete on the container filesystem via a directly-exposed MCP tool call. Confirmed by tracing the full call path from thecontainer_file_*tool registrations inapps/sandbox-container/server/container-tools.tsthroughuserContainer.tsintoapps/sandbox-container/container/sandbox.container.app.ts; none of the intermediate layers (stripProtocolFromFilePath,get_file_name_from_path) do any traversal or absolute-path check.Fix
Added
resolve_in_workdir()infileUtils.ts: resolves the requested path againstprocess.cwd()and throwsPathTraversalErrorif the resolved path isn't contained in it. All three handlers (GET/POST/DELETEon/files/contents) andlist_files_in_directorynow go through this single guard instead of the ad hocpath.join/raw-path calls. A rejected path returns 400 instead of touching the filesystem.Ordinary usage is unaffected — a leading
/in the request path (the normal shape, since these routes are matched off/files/contents/*) still resolves relative to the working directory, matching the priorpath.joinbehavior for non-malicious input.Test plan
resolve_in_workdirunit tests covering: relative path, leading-slash path (existing shape from the route),../traversal, absolute path treated as relative to the working dir, traversal that only escapes after internal..segments resolve, and the bare..segment.list_files_in_directorytest asserting a traversal path throwsPathTraversalError.pnpm testinapps/sandbox-container(both vitest configs) — all passing.pnpm check:typesandpnpm check:lintinapps/sandbox-container— clean.Fixes #401