crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] output_file and FilePath reject filenames that contain `..`, like `summary..v2.md`

Open bballhaus opened this issue 5 days ago • 2 comments

Description

The path traversal checks for Task.output_file and crewai_files.FilePath test for the substring .. anywhere in the path, not for a .. path segment. So ordinary filenames like summary..v2.md or notes...txt are rejected as "path traversal", even though they can't leave the directory.

It also breaks kickoff() for a templated output_file. output_file="reports/{topic}.md" passes validation, but if an input ends in an ellipsis ({"topic": "What comes next..."}), the interpolated path is re-checked and rejected. The docs suggest {placeholder} values in output_file (docs/edge/en/concepts/tasks.mdx), so a normal input can make a run fail.

The same check guards artifact.file_path (Task._validate_output_file_path). FilePath._validate_file_exists in lib/crewai-files/src/crewai_files/core/sources.py has the same substring test, so an existing input file named report..v2.pdf is rejected too.

Steps to Reproduce

  1. Create a Task with output_file="reports/summary..v2.md" (snippet below)
  2. Create a Task with output_file="reports/{topic}.md" and interpolate {"topic": "What comes next..."}
  3. Create FilePath(path="report..v2.pdf") for a file that exists in the working directory

Expected behavior

Filenames that only contain .. inside a name are accepted. Paths with a real .. segment (../x, a/../x, ..\x) are still rejected, as they are today.

Screenshots/Code snippets

from crewai import Agent, Task

agent = Agent(role="Writer", goal="Write", backstory="Writes reports")

for path in ["reports/summary..v2.md", "notes...txt"]:
    try:
        Task(description="d", expected_output="e", agent=agent, output_file=path)
        print(path, "accepted")
    except ValueError as e:
        print(path, "rejected:", "Path traversal" in str(e))

task = Task(description="Write about {topic}", expected_output="e", agent=agent,
            output_file="reports/{topic}.md")
try:
    task.interpolate_inputs_and_add_conversation_history({"topic": "What comes next..."})
    print("interpolated:", task.output_file)
except ValueError as e:
    print("interpolation rejected:", e)

Operating System

Other (specify in additional context)

Python Version

3.12

crewAI Version

1.15.47

crewAI Tools Version

1.15.47

Virtual Environment

Venv

Evidence

reports/summary..v2.md rejected: True
notes...txt rejected: True
interpolation rejected: Interpolated output_file path failed validation: Path traversal attempts are not allowed in output_file paths

FilePath(path="report..v2.pdf") with the file present raises Path traversal not allowed: report..v2.pdf.

Possible Solution

Check path segments instead of the substring, e.g. ".." in PureWindowsPath(value).parts in Task._validate_output_file_path (task.py already imports PureWindowsPath, and it splits on both / and \), and the same in FilePath. That still rejects ../x, a/../b, ..\x, C:..\x and \\srv\share\..\x, including traversal added through template inputs (#8622). Since Windows trims trailing dots and spaces from names, it would be safer to also reject a segment made only of dots and spaces (..., .. ), while still allowing summary..v2.md and notes...txt.

I'd like to open a PR for this, with regression tests for both the accepted filenames and the traversal forms that must stay rejected.

Additional context

macOS 27 (not in the dropdown). Found while reading the output_file validation; I used Claude Code to help find and reproduce it.

bballhaus avatar Nov 30 '26 22:11 bballhaus

Hi @lorenzejay, I'd like to take this one. Could you assign it to me? My plan is the segment check from the Possible Solution, in both Task._validate_output_file_path and FilePath, with regression tests for the filenames that should pass and the traversal forms that must stay rejected.

I also can't add labels from my account. Could you add bug and llm-generated? I used Claude Code to help find and reproduce this.

bballhaus avatar Nov 30 '26 22:11 bballhaus

cc @Vidit-Ostwal since you reviewed the related flow fix in #9499. Happy to have either of you take a look once the PR is up.

bballhaus avatar Nov 30 '26 22:11 bballhaus