file_write tool accepts a path containing embedded markup/control characters and fails late with a raw OS error instead of rejecting it up front #175

Closed
opened 2026-11-19 16:16:59 +00:00 by CoreRasurae · 1 comment
Member

Metadata

Commit Message: fix(agents): reject malformed file_write path arguments before touching the filesystem
Branch: bugfix/m1-file-write-path-validation

Background and context

ToolAgent._file_write_tool (src/cleveractors/agents/tool.py) only validates the file argument for emptiness (_validate_file_write_args) and for traversal / absolute-path / home-directory escapes (_validate_file_path_safety). It has no check that the string is actually a well-formed relative path containing only path-legal characters. When an LLM emits a file_write tool call whose file argument is corrupted (e.g. contaminated with stray markup the model hallucinated or copy-leaked from elsewhere in its context), that garbage string passes both validators untouched and is handed straight to FileAccessCore.write(), which fails deep inside the OS call with an opaque, hard-to-diagnose error.

Current behavior

Observed directly in a production run of the example CLI (python test_app.py test --llm-protocol http --llm-ip <host> --llm-port 8000 --local-store packages --unsafe --prompt "..." --graph 80487-fpu-actor-tools-graph.yaml). The model emitted a file_write tool call with:

{"tool": "file_write", "args": {"file": "file\" <span class=\"editor-tab\" data-tab=\"file\">ao486_MiSTer/rtl/ao486/fpu/fpu_stackfile.v", "content": "..."}}

The file value has an HTML-like fragment (file" <span class="editor-tab" data-tab="file">) prepended to what was clearly meant to be ao486_MiSTer/rtl/ao486/fpu/fpu_stackfile.v. This string is non-empty, contains no .., is not absolute, and does not start with ~ -- so it sails through both _validate_file_write_args and _validate_file_path_safety unmodified. The write then fails inside FileAccessCore.write() with:

File write failed for file" <span class="editor-tab" data-tab="file">ao486_MiSTer/rtl/ao486/fpu/fpu_stackfile.v: [Errno 2] No such file or directory: 'file" <span class="editor-tab" data-tab="file">ao486_MiSTer'

which _file_write_tool wraps as ExecutionError(f"File write failed: {e}") and surfaces to the agent as Tool agent ... execution failed: File write failed: [Errno 2] No such file or directory: .... The agent's only recovery signal is a generic [Errno 2] OS error with no indication that the path itself is malformed; in the captured run, the agent responded with "Let me retry with the correct path" and the graph run ended without producing the intended file.

Expected behavior

_validate_file_path_safety (or a new, dedicated check called before it) rejects a file argument that is not a syntactically valid relative file path -- in particular, paths containing characters that can never legally appear in a filename on the target filesystem (e.g. control characters, <, >, ", and other shell/markup metacharacters not legitimately part of a path) or containing embedded whitespace runs that indicate a corrupted/concatenated string -- and raises a clear ExecutionError describing exactly what about the path is invalid, before any filesystem call is attempted. The error message must be specific enough that the calling agent can self-correct (e.g. "file path contains disallowed characters: '<', '"'" rather than a raw OSError/[Errno 2]).

Acceptance criteria

  • A file_write call whose file argument contains HTML/markup-like fragments (e.g. embedded <...> tags) or other characters illegal in a filename is rejected by _validate_file_path_safety (or an equivalent new validation step) with an ExecutionError raised before FileAccessCore.write() or any other OS call is reached.
  • The raised ExecutionError message names the specific invalid character(s)/pattern found, not just the eventual OS error.
  • Legitimate relative paths (including ones with spaces, hyphens, underscores, dots within a single path segment, and nested subdirectories) continue to be accepted unchanged -- no regression on valid inputs.
  • Existing traversal / absolute-path / home-directory-escape checks in _validate_file_path_safety continue to pass unchanged.

Supporting information

  • Affected method: ToolAgent._file_write_tool / ToolAgent._validate_file_path_safety in src/cleveractors/agents/tool.py.
  • Captured evidence: a local log excerpt (llm_requests_prematurely_eneded_the_execution.log) from a CleverActors CLI run. The same run also surfaced an unrelated logging-duplication defect, filed separately as #173/#174 -- not part of this ticket.
  • Related but distinct: #126 (hardening file_read directory listing against symlink TOCTOU) and #105 (symlink-safe open) address security hardening of path resolution; this ticket addresses input validation of the literal path string itself, which is a different concern and does not overlap with either.

Subtasks

  • Add a path-character/well-formedness check to _validate_file_path_safety (or a new _validate_path_characters helper called from it) that rejects control characters and markup/shell metacharacters illegal in filenames, with a descriptive ExecutionError message.
  • Apply the same check consistently wherever a tool-provided path string is accepted (file_write, and any sibling tools such as file_read that take a file/path argument) so the fix is not file_write-specific.
  • Tests (Behave): add scenarios in features/ covering a corrupted path with embedded markup, a path with illegal control characters, and confirm valid paths (including ones with spaces/hyphens) are unaffected.
  • Verify coverage >=97% via nox -s coverage_report.
  • Run nox (all default sessions), fix any errors.

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line matches the Commit Message in Metadata exactly.
  • The commit is pushed to the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a PR to master, reviewed, and merged.
  • A companion Type/Testing TDD issue exists, the TDD scenario (tagged @tdd_expected_fail) is merged first, and this issue's Forgejo dependency correctly shows it depends on that TDD issue.

Note for triage

This fits Epic #77 ("LLM Agent Runtime Stabilization") as a tool-dispatch input-validation hardening item, alongside its existing children #39 and #63 -- recommend linking it there, but leaving the final call to maintainer triage.

## Metadata Commit Message: `fix(agents): reject malformed file_write path arguments before touching the filesystem` Branch: bugfix/m1-file-write-path-validation ## Background and context `ToolAgent._file_write_tool` (`src/cleveractors/agents/tool.py`) only validates the `file` argument for emptiness (`_validate_file_write_args`) and for traversal / absolute-path / home-directory escapes (`_validate_file_path_safety`). It has no check that the string is actually a well-formed relative path containing only path-legal characters. When an LLM emits a `file_write` tool call whose `file` argument is corrupted (e.g. contaminated with stray markup the model hallucinated or copy-leaked from elsewhere in its context), that garbage string passes both validators untouched and is handed straight to `FileAccessCore.write()`, which fails deep inside the OS call with an opaque, hard-to-diagnose error. ## Current behavior Observed directly in a production run of the example CLI (`python test_app.py test --llm-protocol http --llm-ip <host> --llm-port 8000 --local-store packages --unsafe --prompt "..." --graph 80487-fpu-actor-tools-graph.yaml`). The model emitted a `file_write` tool call with: ```json {"tool": "file_write", "args": {"file": "file\" <span class=\"editor-tab\" data-tab=\"file\">ao486_MiSTer/rtl/ao486/fpu/fpu_stackfile.v", "content": "..."}} ``` The `file` value has an HTML-like fragment (`file" <span class="editor-tab" data-tab="file">`) prepended to what was clearly meant to be `ao486_MiSTer/rtl/ao486/fpu/fpu_stackfile.v`. This string is non-empty, contains no `..`, is not absolute, and does not start with `~` -- so it sails through both `_validate_file_write_args` and `_validate_file_path_safety` unmodified. The write then fails inside `FileAccessCore.write()` with: ``` File write failed for file" <span class="editor-tab" data-tab="file">ao486_MiSTer/rtl/ao486/fpu/fpu_stackfile.v: [Errno 2] No such file or directory: 'file" <span class="editor-tab" data-tab="file">ao486_MiSTer' ``` which `_file_write_tool` wraps as `ExecutionError(f"File write failed: {e}")` and surfaces to the agent as `Tool agent ... execution failed: File write failed: [Errno 2] No such file or directory: ...`. The agent's only recovery signal is a generic `[Errno 2]` OS error with no indication that the path itself is malformed; in the captured run, the agent responded with "Let me retry with the correct path" and the graph run ended without producing the intended file. ## Expected behavior `_validate_file_path_safety` (or a new, dedicated check called before it) rejects a `file` argument that is not a syntactically valid relative file path -- in particular, paths containing characters that can never legally appear in a filename on the target filesystem (e.g. control characters, `<`, `>`, `"`, and other shell/markup metacharacters not legitimately part of a path) or containing embedded whitespace runs that indicate a corrupted/concatenated string -- and raises a clear `ExecutionError` describing exactly what about the path is invalid, before any filesystem call is attempted. The error message must be specific enough that the calling agent can self-correct (e.g. "file path contains disallowed characters: '<', '\"'" rather than a raw `OSError`/`[Errno 2]`). ## Acceptance criteria - A `file_write` call whose `file` argument contains HTML/markup-like fragments (e.g. embedded `<...>` tags) or other characters illegal in a filename is rejected by `_validate_file_path_safety` (or an equivalent new validation step) with an `ExecutionError` raised **before** `FileAccessCore.write()` or any other OS call is reached. - The raised `ExecutionError` message names the specific invalid character(s)/pattern found, not just the eventual OS error. - Legitimate relative paths (including ones with spaces, hyphens, underscores, dots within a single path segment, and nested subdirectories) continue to be accepted unchanged -- no regression on valid inputs. - Existing traversal / absolute-path / home-directory-escape checks in `_validate_file_path_safety` continue to pass unchanged. ## Supporting information - Affected method: `ToolAgent._file_write_tool` / `ToolAgent._validate_file_path_safety` in `src/cleveractors/agents/tool.py`. - Captured evidence: a local log excerpt (`llm_requests_prematurely_eneded_the_execution.log`) from a CleverActors CLI run. The same run also surfaced an unrelated logging-duplication defect, filed separately as #173/#174 -- not part of this ticket. - Related but distinct: #126 (hardening `file_read` directory listing against symlink TOCTOU) and #105 (symlink-safe open) address *security* hardening of path resolution; this ticket addresses *input validation* of the literal path string itself, which is a different concern and does not overlap with either. ## Subtasks - [ ] Add a path-character/well-formedness check to `_validate_file_path_safety` (or a new `_validate_path_characters` helper called from it) that rejects control characters and markup/shell metacharacters illegal in filenames, with a descriptive `ExecutionError` message. - [ ] Apply the same check consistently wherever a tool-provided path string is accepted (`file_write`, and any sibling tools such as `file_read` that take a `file`/path argument) so the fix is not file_write-specific. - [ ] Tests (Behave): add scenarios in `features/` covering a corrupted path with embedded markup, a path with illegal control characters, and confirm valid paths (including ones with spaces/hyphens) are unaffected. - [ ] Verify coverage >=97% via `nox -s coverage_report`. - [ ] Run `nox` (all default sessions), fix any errors. ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the first line matches the Commit Message in Metadata exactly. - The commit is pushed to the branch matching the Branch in Metadata exactly. - The commit is submitted as a PR to master, reviewed, and merged. - A companion `Type/Testing` TDD issue exists, the TDD scenario (tagged `@tdd_expected_fail`) is merged first, and this issue's Forgejo dependency correctly shows it depends on that TDD issue. ## Note for triage This fits Epic #77 ("LLM Agent Runtime Stabilization") as a tool-dispatch input-validation hardening item, alongside its existing children #39 and #63 -- recommend linking it there, but leaving the final call to maintainer triage.
Author
Member

Blocked by #176 (TDD issue-capture test must be written and merged first, per the mandatory Bug Fix TDD Workflow).

Blocked by #176 (TDD issue-capture test must be written and merged first, per the mandatory Bug Fix TDD Workflow).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveractors-core#175
No description provided.