Skip to content

feat(tools): opt-in workspace containment for file tools - #5971

Open
lalith0407 wants to merge 1 commit into
OpenHands:mainfrom
lalith0407:fix/opt-in-workspace-containment
Open

lalith0407 wants to merge 1 commit into
OpenHands:mainfrom
lalith0407:fix/opt-in-workspace-containment

Conversation

@lalith0407

@lalith0407 lalith0407 commented Oct 14, 2026 •

Copy link
Copy Markdown

HUMAN:

I was trying out the SDK and noticed during a live run that the agent read and referenced a file outside its workspace. That surprised me because apply_patch already blocks this, so I checked the other file tools and found they don't. I kept the fix opt-in so nothing changes for existing setups. Not sure if you'd prefer the setting on the workspace instead of per tool, happy to change it.


AGENT:

Why

Proposed fix for #5970. File tools other than apply_patch can read and write anywhere on the host, even when the user expects them to stay in working_dir. This adds an opt-in, default-off guard rail, so default behavior is unchanged. Happy to adjust the shape (for example a workspace-level setting instead of tool params) to maintainer preference.

Summary

  • New openhands.tools.utils.path_containment with PathContainment and is_within_roots. It resolves the path (normalizes .., follows symlinks) and compares whole path components (/x/ws2 is not inside /x/ws) using os.path.normcase.
  • New tool params restrict_to_workspace and allowed_roots, e.g. Tool(name="file_editor", params={"restrict_to_workspace": True}), for file_editor (all commands), planning_file_editor (the PLAN.md dir stays allowed), grep and glob (the search root is checked, and results that resolve outside are dropped), and the Gemini-style read_file, write_file, edit and list_directory. Blocked calls return an error observation naming the allowed roots.
  • apply_patch now uses the shared is_within_roots; its behavior and message are unchanged. The LocalWorkspace docstring now says working_dir is not a security boundary, the terminal is never confined, and to use DockerWorkspace for isolation.

Issue Number

Fixes #5970

How to Test

uv sync --dev
uv run pytest tests/tools/test_path_containment.py   # 36 tests
uv run pytest tests/tools                            # full tools suite
uv run pre-commit run --files <changed files>

Evidence:

  • tests/tools: 1074 passed, 15 skipped.
  • pre-commit (ruff, pycodestyle, pyright, import rules, tool registration) passes.
  • No-LLM repro over all file tools: 15/15 reads and writes escape the workspace by default, and 15/15 are blocked with the flag on.
  • Live Agent/Conversation run (gemini-3.5-flash-lite, flag on): asked to view a file outside the workspace, the agent got "Access to '…/outside/secret.txt' was denied: the path is outside the workspace. This tool is restricted to: …/ws", fell back to the in-workspace file, and finished. The canary never reached the LLM. With the flag off, the same prompt returned the canary.

Type

  • Feature

Notes

  • Not a sandbox: the terminal can still reach any path, and the docs say so.
  • Not covered by this PR: browser_use, and shell-based reads through task/delegate.
  • Possible overlap with open PR fix(file-editor): preserve symlinks when editing existing files #5097 (file_editor symlink-preserving writes). It changes _atomic_write, not path validation; I expect at most a trivial rebase.
  • Possible follow-ups: a workspace- or conversation-level setting that propagates to all file tools, and an allowed_roots UI in Agent Canvas.

Jev-Fast-Audit

⚡ Jev fast audit · estimates · 0.40s · commit 8e9b092
Strongest signal: No primary concern selected.
Evidence: No primary concern to locate.
Coverage: ⚠️ reduced context — partial coverage; 33/45 hunks, 15/20 files (context budget: 33, file budget: 5, hunk budget: 12).

All estimates and evidence
Estimate Likelihood / value Direct evidence
SQL injection 3.0% No direct hunk selected
Command injection 13.0% No direct hunk selected
Weakened authentication 5.0% No direct hunk selected
Weakened authorization 17.0% No direct hunk selected
Contract regression 16.0% No direct hunk selected
Data loss 5.0% No direct hunk selected
Sensitive data disclosure 4.0% No direct hunk selected
Unexpected data transfer 3.0% No direct hunk selected
Credential misuse 4.0% No direct hunk selected
Untrusted instruction authority 3.0% No direct hunk selected
Package source redirection 4.0% No direct hunk selected
Unverified remote execution 2.0% No direct hunk selected
Privileged environment access 7.0% No direct hunk selected
Security assessment bypass 7.0% No direct hunk selected
Prohibited workload 2.0% No direct hunk selected
Primary concern None selected; confidence 83.0% No primary concern to locate

Add a shared PathContainment helper (openhands.tools.utils.path_containment)
that resolves a path (normalizing `..` and following symlinks) and checks it
is inside the workspace root or explicit extra roots, comparing whole path
components so `/x/ws2` is not treated as inside `/x/ws`.

File tools accept new opt-in, default-off tool params
`restrict_to_workspace` and `allowed_roots` (e.g.
`Tool(name="file_editor", params={"restrict_to_workspace": True})`):
file_editor (all commands), planning_file_editor (PLAN.md dir stays
allowed), grep and glob (search root checked, results resolving outside
dropped), and the Gemini-style read_file/write_file/edit/list_directory.
Rejected paths return an error observation naming the allowed roots.

apply_patch now uses the shared `is_within_roots` check; its behavior and
error message are unchanged. With the flag off, behavior is identical.
LocalWorkspace docs clarify that working_dir is not a security boundary
and that the terminal is never confined (use DockerWorkspace for isolation).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant