Skip to content

SECT-602: fix path traversal in output_file via template interpolation - #8622

Merged
João Moura (joaomdmoura) merged 4 commits into
mainfrom
fix/path-traversal-output-file
Oct 26, 2026
Merged

João Moura (joaomdmoura) merged 4 commits into
mainfrom
fix/path-traversal-output-file

Conversation

@iris-clawd

@iris-clawd iris-clawd commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Security Fix — GHSA-hq26-r99g-pvv4 (SR-2026-0128)

Vulnerability

output_file paths are validated at model construction time via a Pydantic field validator, but interpolate_inputs_and_add_conversation_history() rewrites output_file with caller-controlled template inputs after validation. An attacker can craft inputs like {"topic": "../../etc/cron.d"} to escape the intended output directory and overwrite arbitrary files writable by the process.

Fix (3 layers)

  1. Extract validation into _validate_output_file_path() — reusable static method with an allow_templates flag. The field validator calls it with allow_templates=True (permits {var} syntax). Post-interpolation calls use allow_templates=False (rejects unresolved templates too).

  2. Re-validate after interpolation — interpolate_inputs_and_add_conversation_history() now calls _validate_output_file_path() on the interpolated result, catching any .., shell chars, or expansion attempts injected via template variables.

  3. Defense-in-depth in _save_file() — after resolving the path, verifies the resolved path is within Path.cwd(). This is the last line of defense even if upstream validation is somehow bypassed.

Tests

  • Path traversal via interpolation → ValueError
  • Shell injection via interpolation → ValueError
  • Legitimate interpolation → works correctly
  • _save_file rejects paths outside cwd → ValueError
  • All existing output_file validation tests still pass

Linear

https://linear.app/crewai/issue/SECT-602


Note

High Risk
Closes arbitrary file write via interpolated output_file paths; behavior changes for previously exploitable caller inputs while tightening path rules on Windows-style roots.

Overview
Security fix (GHSA-hq26-r99g-pvv4): Task.output_file was only validated at construction, so caller-controlled template inputs could inject .., shell metacharacters, or absolute paths after interpolate_inputs_and_add_conversation_history().

Validation is centralized in _validate_output_file_path() with an allow_templates flag: construction keeps allowing {var} placeholders; post-interpolation runs with templates disallowed. After interpolation, paths are re-checked and compared via _path_anchor() (using PureWindowsPath) so inputs cannot increase how “rooted” the path is relative to the developer’s template. output_file is updated only after validation succeeds, so a failed run cannot leave a poisoned path for a later empty-input interpolation.

New tests cover traversal, shell injection, absolute/UNC/drive-relative escalation, smuggled {…} syntax, and stale-path behavior; legitimate templated writes (including absolute developer templates) remain supported.

Reviewed by Cursor Bugbot for commit 972e077. Bugbot is set up for automated code reviews on this repo. Configure here.

@linear

linear Bot commented Oct 26, 2026

Copy link
Copy Markdown

SECT-602

@strix-security

strix-security Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Strix Security Review

Warning

This pull request has 1 commit after the last Strix review (8bcd92a). Strix has not reviewed these changes.
Automatic review on push is off for this repository. To review the latest changes, tag @strix-security in a comment, or turn on re-review on push.

✅ No open security findings. All previously reported findings have been resolved.

PR summary: Fixes GHSA-hq26-r99g-pvv4 by validating `Task.output_file` again after caller-supplied template inputs are interpolated, rejecting traversal, shell characters, unresolved placeholders and inputs that make a relative template absolute, with the interpolated path committed only after validation succeeds.

Review summary

The PR hardens output_file handling by extracting path validation into a reusable helper, re-validating the caller-interpolated result (rejecting .., shell characters, smuggled {var} syntax, and inputs that anchor a previously relative template at a filesystem root), and — in the commit under review — assigning the interpolated path to the task only after every check passes. The previously reported ordering defect is fixed: a rejected value never reaches Task.output_file, so a later run that skips interpolation cannot write to the injected path. An independent adversarial pass (path-traversal, absolute/UNC/drive, shell, encoding, unicode, split-placeholder, brace-smuggling and non-string input variants across relative and rooted templates, plus a before/after control against main) produced no accepted value that resolved outside the developer's intended root, and the new regression tests pass. One residual nuance, a developer-authored absolute template whose entire path is a placeholder (e.g. output_file="/{topic}"), remains caller-influenced by design and is not an attack against a realistic template.

2 resolved findings
Ruled out (2)
  • Can caller inputs still escape through a developer-authored absolute template such as `output_file="/{topic}"`?
    An absolute template whose entire path is a placeholder lets the caller choose any location under that root, but upward traversal is blocked and this is the documented behaviour of honouring developer absolute templates rather than an input-driven escape from a relative path.
  • Can an input make a relative template anchored when the template starts with a single letter and colon, e.g. `output_file="a:{topic}.md"`?
    The rootedness helper treats a leading letter-plus-colon as a Windows drive, so such a template counts as already anchored, but it requires a contrived developer template and on POSIX the resulting value stays a relative filename.
Review history (3)
  • Review 3 · 8bcd92a · Oct 26 18:22 UTC · Fixed 1
  • Review 2 · 3d02bed · Oct 26 17:49 UTC · Found 1 new Medium Vulnerability · Fixed 1
  • Review 1 · 8461ade · Oct 26 15:08 UTC · Found 1 Medium Vulnerability

Review 3 on this PR · 8bcd92a · requested Oct 26 18:00 UTC by @joaomdmoura


Reviewed by Strix
Re-run review · Configure security review settings

@strix-security strix-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strix flagged a new security finding below. See the pinned summary comment for the full PR status.

Comment thread lib/crewai/src/crewai/task.py Resolved
Comment thread lib/crewai/src/crewai/task.py Resolved
iris-clawd and others added 2 commits October 26, 2026 10:03
…th traversal

Re-validates output_file path after template variable interpolation to
prevent path traversal attacks where caller-controlled placeholders inject
".." segments. Adds defense-in-depth check in _save_file to ensure
resolved paths stay within the working directory.

Fixes GHSA-hq26-r99g-pvv4

Co-authored-by: Joao Moura <joao@crewai.com>
Post-interpolation validation stripped a single leading slash, so an input
like "//etc/cron.d/x" stayed absolute, and the cwd containment check in
_save_file admitted every path when the working directory is "/". It also
broke developer-written absolute templates (test_interpolate_inputs).

Interpolated paths are no longer rewritten. They are rejected when inputs
make them rooted (POSIX, backslash, drive, or UNC) and the developer's
template was not. The cwd check is removed: it rejected legitimate
absolute outputs and was no boundary at "/".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@joaomdmoura
João Moura (joaomdmoura) force-pushed the fix/path-traversal-output-file branch from 8461ade to 3d02bed Compare October 26, 2026 17:20
@joaomdmoura

Copy link
Copy Markdown
Collaborator

Rebased on main and pushed 3d02bed, which addresses the absolute-path finding. The full lib/crewai suite has the same failure set as main in my local run; the failures there are optional extras that are not installed locally. Two behavior notes for reviewers:

  • An input that makes a relative template absolute, as in output_file="{dir}/report.md" with {"dir": "/data"}, now raises. The path cannot be told apart from an attack.
  • A non-template absolute output_file still has its leading slash stripped at construction, as on main. That is unchanged here.

Strix (@strix-security) please re-review.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 3d02bed. Configure here.

Comment thread lib/crewai/src/crewai/task.py Resolved

@strix-security strix-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strix flagged a new security finding below. See the pinned summary comment for the full PR status.

Comment thread lib/crewai/src/crewai/task.py Resolved
…passes

The interpolated path was stored on the task before its checks ran, so a
rejected value stayed in output_file. A later run with no inputs returns
before re-interpolating and _save_file wrote to the injected path.
Interpolate and validate into a local and assign once every check passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@joaomdmoura

Copy link
Copy Markdown
Collaborator

Pushed 8bcd92a for the re-review finding: the interpolated path is now validated before it is assigned. Strix (@strix-security) please re-review.

Comment thread lib/crewai/src/crewai/task.py Resolved
…e stays relative

_is_rooted_path treated any drive letter as a root, so a drive-relative
template such as "C:{name}.txt" counted as anchored and an input of
"/Windows/Temp/pwn" produced the absolute "C:/Windows/Temp/pwn.txt".

Rank anchoring instead (relative, drive-relative, rooted) and reject an
interpolated path whose rank is above the template's. A drive-relative
template still accepts relative inputs ("C:report.txt").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@alex-clawd alex-clawd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. The vulnerability is real, the fix closes it, and the _path_anchor approach is a genuinely good piece of design. One correction to the description before this becomes an advisory.

The core fix is right

The bug is a classic validate-then-mutate: the field validator ran at construction, then interpolate_inputs_and_add_conversation_history() rewrote output_file from caller-controlled inputs with nothing re-checking it. Re-validating after interpolation is the correct fix, and splitting the logic into _validate_output_file_path(allow_templates=...) keeps one implementation for both call sites rather than two that can drift.

Two details that show care:

Assign-after-validate. output_file is only written once every check passes:

Assign only once every check passes, so a rejected path never stays on the task for a later run that skips interpolation.

Without that, a rejected interpolation would leave the poisoned value on the model, and a subsequent run with no inputs — which skips interpolation entirely — would write to it. test_rejected_output_file_is_not_kept_for_later_runs covers it. That is the subtle half of this bug and it would have been easy to miss.

Rejecting unresolved templates post-interpolation. allow_templates=False means a leftover {secret} after interpolation is an error rather than a literal directory name. Good: it fails loudly instead of silently creating a weird path.

_path_anchor is the clever part

Comparing how rooted the result is against the template, rather than just testing "is absolute," is the right abstraction. It preserves the legitimate case — a developer writing /var/out/{topic}.txt on purpose — while blocking inputs from escalating a relative template. I ran it against the cases the tests do not cover:

template              injected               tmpl  result  blocked
{topic}/output.txt    /etc/cron.d               0       2    yes
{topic}/output.txt    //etc/cron.d              0       2    yes
{topic}/output.txt    \\server\share            0       2    yes
{topic}/output.txt    C:\Windows\System32       0       2    yes
{topic}/output.txt    C:relative                0       1    yes
{topic}/o.txt         Z:x                       0       1    yes
{topic}/output.txt    finance                   0       0     no
/var/out/{topic}.txt  finance                   2       2     no

Drive-relative (C:relative, rank 1) being distinct from both relative and rooted is the detail I would have expected a simpler fix to miss. Using PureWindowsPath unconditionally — so Windows-style paths are caught even when running on Linux — is also correct, since the attacker chooses the input string, not the host OS.

Correction needed: layer 3 does not exist

The description lists three layers and says:

3. Defense-in-depth in _save_file() — after resolving the path, verifies the resolved path is within Path.cwd(). This is the last line of defense even if upstream validation is somehow bypassed.

and in Tests:

_save_file rejects paths outside cwd → ValueError

Neither is in the code. _save_file is unchanged in the relevant part — it still does Path(self.output_file).expanduser().resolve() and writes, with no containment check. grep for cwd() in task.py returns nothing, and the diff contains zero cwd references.

More than an omission, the shipped behavior is deliberately the opposite. This test is in the PR:

def test_absolute_template_output_file_saves_outside_cwd(tmp_path, monkeypatch):
    """A developer-written absolute template keeps working outside the cwd."""

That asserts a developer-authored absolute path must still write outside the cwd. A cwd containment check in _save_file would break it. So the design decision is "the developer's template may point anywhere; caller inputs may not escalate it" — which is a defensible and probably correct call, since output_file="/var/log/myapp/out.txt" is a legitimate use.

The fix is two layers, not three, and the trust boundary is template-vs-input rather than inside-vs-outside cwd. That is worth stating accurately, because anyone reading the advisory and assuming a cwd backstop exists would be wrong about the residual risk: if some future code path mutates output_file after interpolation, there is no last line of defense underneath it.

Summary

All 26 checks green, including CodeQL and Bugbot. Eight new tests covering traversal, shell injection, four absolute-path variants, drive-relative paths, unresolved templates, and the don't-persist-rejected-values case.

Only ask: update the description to drop layer 3 and its test bullet, and say plainly that absolute paths from the developer's own template remain supported by design. Same note as on #8623 — the code is good, the write-up overstates it, and the write-up is what ships in the advisory.

@joaomdmoura
João Moura (joaomdmoura) merged commit 7a15b62 into main Oct 26, 2026
59 checks passed
@joaomdmoura
João Moura (joaomdmoura) deleted the fix/path-traversal-output-file branch October 26, 2026 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants