fix(agents): restrict inline-code sandbox to a json-only __import__ #118
No reviewers
Labels
No labels
auto/blocked-by-deps
auto/ci-timeout
auto/claimed-implementer
auto/claimed-merge
auto/claimed-reviewer
auto/driver-down
auto/invariant-violation
auto/last-attempt-tier-0
auto/last-attempt-tier-1
auto/last-attempt-tier-2
auto/last-attempt-tier-min
Automation Tracking
auto/needs-conflict-resolution
auto/needs-implementer
auto/postmortem
auto/ready-to-merge
auto/restart-throttled
auto/revert
auto/sentinel
auto/stale-inactivity
auto/unstable
Blocked
Bounty
$100
Bounty
$1000
Bounty
$10000
Bounty
$20
Bounty
$2000
Bounty
$250
Bounty
$50
Bounty
$500
Bounty
$5000
Bounty
$750
MoSCoW
Could have
MoSCoW
Must have
MoSCoW
Should have
Needs Feedback
Points
1
Points
13
Points
2
Points
21
Points
3
Points
34
Points
5
Points
55
Points
8
Points
88
Priority
Backlog
Priority
CI Blocker
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Signed-off: Owner
Signed-off: Scrum Master
Signed-off: Tech Lead
Spike
State
Completed
State
Duplicate
State
In Progress
State
In Review
State
Paused
State
Unverified
State
Verified
State
Wont Do
Type
Automation
Type
Bug
Type
Discussion
Type
Documentation
Type
Epic
Type
Feature
Type
Legendary
Type
Refactor
Type
Support
Type
Task
Type
Testing
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Blocks
#107 Inline-code sandbox exposes __import__, letting code bypass the Restricted Built-ins set entirely (e.g. import os)
cleveragents/cleveractors-core
Reference
cleveragents/cleveractors-core!118
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "bugfix/m1-inline-sandbox-import-restriction"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
ToolAgent._execute_python_codebound the inline-code sandbox's__builtins__["__import__"]directly to the real, unrestricted__import__builtin, so inline code (atype: toolagent'scode:body, or theexec_python-gatedpython_exectool) couldimport os/subprocess/socketand reach exactly the filesystem/network/process facilities docs/index.md §13.2.1's exhaustive Restricted Built-ins table and §13.2.3's prohibited-capabilities list were written to keep out.Fixes it by replacing the raw reference with a new restricted shim,
_sandboxed_import, that permits onlyjsonand rejects every other module name via the sameNameError→ExecutionErrortranslation_execute_python_codealready uses for other prohibited-name access.Collateral fixes required to keep the suite green (both depended on the exact gap being closed):
features/stderr_suppression.feature/steps: it exercised inline code doingimport systo reach real stderr, which is no longer reachable by design.import re) text extraction in severaltests/fixtures/email_graph/*.yamlagent configs to use only sandbox-permittedstrmethods, preserving the same output keys.Test plan
nox -s unit_tests— 2962 scenarios passed, including new positive (import json) and TDD regression (@tdd_issue_107,@tdd_expected_failremoved) scenarios infeatures/inline_sandbox_import_restriction.feature.nox -s integration_tests— 345 Robot tests passed, including newrobot/inline_sandbox_import_restriction.robot.nox -s coverage_report— COVERAGE OK: 96.6% (threshold 96.5%).nox -s lint,nox -s format -- --check,nox -s typecheck,nox -s security_scan,nox -s dead_code— all green.import json/from json import dumpssucceed andimport os/subprocessraiseExecutionErrorvia the sandbox.Closes #107
PR Review: !118 (Ticket #107)
Verdict: Approve
The change correctly closes the security gap described in #107: the inline-code sandbox now routes every
importstatement through_sandboxed_import, which rejects any module other thanjsonand surfaces the failure through the existingNameError→ExecutionErrorpath. Both entry points (type: toolinlinecode:bodies and theexec_python-gatedpython_execbuilt-in tool) are covered, the expression sandbox is untouched, existingimport jsonfixtures continue to work, and the collateral fixture updates remove the now-prohibitedimport reusage without introducing new imports.Critical Issues
None
Major Issues
None
Minor Issues
Hardening:
_sandboxed_importdoes not reject non-zerolevelarguments.File:
src/cleveractors/agents/tool.py, lines 35–53The shim correctly blocks every module name except
json, but it still passes a non-zerolevelthrough to the real__import__. For defense-in-depth, consider rejectinglevel != 0with the sameNameError, since the spec only intends the top-leveljsonmodule to be reachable and relative imports are not part of the allowed surface.The regression assertion only checks the error category, not the failure cause.
File:
features/steps/inline_sandbox_import_restriction_steps.py, lines 21–35The step asserts
isinstance(context.error, ExecutionError), which is good, but it does not verify that theExecutionErrorwas actually triggered by the forbidden import (e.g., by checking that the module name appears in the message or that the post-import statement did not run). Consider tightening the assertion so a future regression that accidentally allows the import but fails later cannot masquerade as a passing test.Nits
Duplicated string-parsing logic in email fixtures.
Files:
tests/fixtures/email_graph/*/{business_email,component_orders}.yamlThe regex replacement logic for component-order part numbers and quantities is copy-pasted across six fixture files. If these fixtures need further semantic tweaks, a shared helper or template would reduce drift.
Robot library couples to a private method.
File:
robot/InlineSandboxImportRestrictionLib.py, lines 46–48Run Inline Codecallsagent._python_exec_tooldirectly. This is fine for an integration test, but it creates a brittle coupling to a private API; consider documenting it or using the publicprocess_messagepath.Summary
This is a focused, well-scoped security fix. The shim is minimal, the regression coverage exercises both documented entry points and multiple dynamic-import forms, and the collateral cleanup (removing
import refrom fixtures and retiring thestderr_suppressionfeature that depended onimport sys) is clearly justified. No critical or major issues were found; the minor items above are optional hardening/quality improvements.64cbb46f35e99eb26579Thanks for the review, @hurui200320! Addressed the minor issues; the two nits were left as-is with reasoning below.
Applied:
_sandboxed_importnow also rejects a non-zerolevel(relative import forms such asfrom .json import dumps) — §13.2.1 lists only the absolutejsonmodule, so relative forms aren't part of the allowed surface. Added a new scenario (python_exec tool rejects a relative import of json) covering this path.inline_sandbox_import_restriction_steps.pynow also checks that the rejected module name appears in theExecutionErrormessage and thatresultstays unset (the statement after the rejected import never ran), so it can no longer pass on an unrelated error.Left as-is (nits, not spec violations):
email_graphfixture YAMLs: genuine deduplication isn't feasible here without either introducing a shared import the sandbox is specifically designed to forbid, or an unrelated fixture-templating refactor — disproportionate scope for this PR. Happy to open a follow-up issue if you'd like it tracked separately.robot/InlineSandboxImportRestrictionLib.pycallingagent._python_exec_tooldirectly: test-only code, not a maintainability concern for the production API surface.All nox sessions green (lint, format, typecheck, security_scan, dead_code, unit_tests, coverage delta on the touched code, integration_tests, e2e_tests). Amended the existing commit (still unmerged, single-commit branch) rather than adding a new one, per project convention.
e99eb26579c5154701aaFollow-up: went ahead and deduplicated the fixture extraction logic too (code quality/maintainability took priority over leaving it as a nit).
Since the sandboxed
code:bodies still can't import a shared helper module (that's the exact restriction this PR adds), the two duplicated snippets (business_email meeting/urgency detection, component_orders part/quantity/BOM detection) now live once each as constants inrobot/EmailGraphLib.py, expanded from a placeholder token back into each fixture's config right afteryaml.safe_load(and after resolving a namespaced package viaLocalPackageStore) — before the config ever reachescreate_executor. Verified equivalence by executing the old and new snippets against representative inputs and diffing the results, then re-ran the full Robot email_graph suite end-to-end (345/345 passed).The private-method-coupling nit (
InlineSandboxImportRestrictionLib.pycallingagent._python_exec_tool) is still left as-is — test-only code, not a maintainability concern for the production API.All nox sessions re-verified green after this change. Force-pushed the amended commit again.