Repository navigation
feat(tools): opt-in workspace containment for file tools - #5971
Open
lalith0407 wants to merge 1 commit into
Open
lalith0407 wants to merge 1 commit into
lalith0407 wants to merge 1 commit into
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_patchcan read and write anywhere on the host, even when the user expects them to stay inworking_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
openhands.tools.utils.path_containmentwithPathContainmentandis_within_roots. It resolves the path (normalizes.., follows symlinks) and compares whole path components (/x/ws2is not inside/x/ws) usingos.path.normcase.restrict_to_workspaceandallowed_roots, e.g.Tool(name="file_editor", params={"restrict_to_workspace": True}), forfile_editor(all commands),planning_file_editor(the PLAN.md dir stays allowed),grepandglob(the search root is checked, and results that resolve outside are dropped), and the Gemini-styleread_file,write_file,editandlist_directory. Blocked calls return an error observation naming the allowed roots.apply_patchnow uses the sharedis_within_roots; its behavior and message are unchanged. TheLocalWorkspacedocstring now saysworking_diris not a security boundary, the terminal is never confined, and to useDockerWorkspacefor isolation.Issue Number
Fixes #5970
How to Test
Evidence:
tests/tools: 1074 passed, 15 skipped.Agent/Conversationrun (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
Notes
browser_use, and shell-based reads throughtask/delegate._atomic_write, not path validation; I expect at most a trivial rebase.allowed_rootsUI in Agent Canvas.Jev-Fast-Audit
⚡ Jev fast audit · estimates · 0.40s · commit 8e9b092⚠️ reduced context — partial coverage; 33/45 hunks, 15/20 files (context budget: 33, file budget: 5, hunk budget: 12).
Strongest signal: No primary concern selected.
Evidence: No primary concern to locate.
Coverage:
All estimates and evidence