Sandbox execution
Sandbox execution gives AgentShield a controlled way to run configured hook commands and inspect their behavior before those hooks take part in a normal agent workflow. It limits the command's working context, replaces sensitive values with canaries, records observable risk signals, and turns those signals into findings for the security report.
Analysis boundary
Hook execution
The hook execution system reads hook entries from the target settings and runs each command in its own temporary work directory. The run captures exit status, standard output, standard error, duration, timeout state, and observations so later analysis can assess behavior without using the host workflow's real working directory or secret values.
The executor accepts both the legacy flat hook shape and the standard nested command-hook shape. It supports these hook stages:
PreToolUsePostToolUseUserPromptSubmitNotificationSessionStartSessionEndStopSubagentStopPreCompact
Malformed entries and non-command nested hooks are ignored. Each recognized command produces one execution record. A settings file with no recognized hook commands produces no execution records, while a non-empty but unrecognized hooks block can be identified separately so it is not silently treated as an empty configuration. See Adversarial security validation for how this phase fits with prompt and data-flow checks.
References: src/…/executor.ts• L40-56 src/…/executor.ts• L82-166 src/…/executor.ts• L318-330
Host workflow separation
The analysis run separates the hook from the workflow by giving it a temporary current directory and a small controlled environment. The environment contains fake values for common credentials. If a hook prints or stores one of these canaries, the run shows that the hook tried to access sensitive environment data without revealing a real credential.
This separation is a detection aid rather than a complete operating-system sandbox. The command still runs as a child process on the analysis host. Network activity is recognized from command and output patterns, while the executor does not promise to block every network connection or every host-side action.
References: src/…/executor.ts• L60-78 src/…/executor.ts• L174-223 src/…/executor.ts• L265-313 src/…/executor.ts• L363-416
Execution lifecycle
Run stages
The execution lifecycle moves from configuration to isolated behavior evidence and then to cleanup. Each stage has a distinct responsibility, which keeps command execution separate from risk interpretation.
This diagram shows how a configured hook becomes evidence and then a security result.
flowchart LR
A[Settings content] --> B[Parse valid hooks]
B --> C[Create temporary work context]
C --> D[Run one hook command]
D --> E[Capture output and exit state]
E --> F[Record behavior observations]
F --> G[Analyze findings and verdict]
G --> H[Report sandbox result]
H --> I[Remove temporary work context]
The command receives a time limit. A long-running command is stopped and remains marked as timed out so the analyzer can treat the event as evidence rather than as a successful clean run. Multiple hooks are processed in settings order, and their records remain available for per-hook review.
References: src/…/executor.ts• L174-313 src/…/executor.ts• L318-330 src/…/executor.ts• L333-342
Cleanup responsibility
Cleanup removes the temporary work directory after analysis. The cleanup step limits leftover test data and canary values, but it is best effort. A failed cleanup therefore needs operating-system review instead of being treated as proof that no temporary data remains.
References: src/…/executor.ts• L333-342 tests/…/sandbox.test.ts• L620-639
Behavioral evidence
Observation categories
The executor records signals that show what a hook attempted or produced. These signals describe behavior; they do not by themselves prove that the command completed the suspected action.
| Signal | Security question |
|---|---|
| Environment access | Did the hook expose a fake sensitive value? |
| Network request | Did the command or output indicate an outbound connection? |
| DNS lookup | Did the hook attempt name resolution that could support data transfer? |
| File read | Did output mention access to a sensitive host path? |
| File write | Did the hook create files or directories in its work area? |
| Process spawn | Did the hook try to keep a process detached or persistent? |
| Suspicious output | Did output contain URLs, non-local IPs, or encoded data? |
The observation record includes a detail, a severity, and a timestamp. File monitoring inspects the temporary work area for created entries. It also checks command output for selected sensitive paths and suspicious patterns.
References: src/…/executor.ts• L15-38 src/…/executor.ts• L265-286 src/…/executor.ts• L346-361 src/…/executor.ts• L418-633
Risk verdicts
The behavioral analyzer converts observations into findings that retain a title, description, severity, and evidence. It sums finding weights into a score capped at 100, then assigns safe, suspicious, or malicious as the run verdict.
The decision model gives critical findings immediate malicious status. High total risk also produces a malicious result. Lower risk or a high-severity finding produces a suspicious result. A run with no findings is safe within the limits of the observations that the executor can see.
References: src/…/analyzer.ts• L5-20 src/…/analyzer.ts• L38-83 src/…/analyzer.ts• L286-320 tests/…/sandbox.test.ts• L488-618
Session sandbox boundary
Session isolation
MiniClaw uses a separate session sandbox to define where an agent's file tools may operate during an active session. A session receives a private directory under the configured sandbox root, owner-only directory access, a session duration, file-size limits, allowed file types, and a network policy.
The default session posture denies network access and limits the session to a short lifetime and bounded file size. These controls reduce the impact of a compromised agent, but they are separate from the hook-analysis executor described above.
References: src/…/types.ts• L158-210 src/…/types.ts• L276-304 src/…/sandbox.ts• L154-194
Tool path boundary
MiniClaw protects file tools by first scoping path arguments to the session directory and then validating them before file access. The validation rejects traversal outside the root and checks existing paths for symlink escapes. This gives the live agent session a path boundary that is distinct from the behavior evidence collected during hook analysis.
The sandbox manager also rejects disallowed extensions and oversized files. Security events record blocked path, tool, and sandbox violations with a session identifier so the live-session audit trail can be reviewed separately from sandbox execution findings.
References: src/…/tools.ts• L223-295 src/…/tools.ts• L297-339 src/…/sandbox.ts• L22-114 src/…/sandbox.ts• L116-152 src/…/types.ts• L212-244
Validation integration
Active scan phase
The command-line scan enables sandbox analysis as one active phase beside taint analysis and injection testing. When enabled, it discovers the target settings file, executes its hooks, analyzes every execution, and maps the results into shared sandbox behaviors and report findings.
The report keeps the hook count, per-hook behavior, and risk findings together. It labels sandbox findings as configuration misconfiguration findings, which lets downstream reports show runtime evidence without mixing it with static findings or prompt-resistance results.
References: src/index.ts• L64-110 src/index.ts• L527-573 src/types.ts• L330-347 src/types.ts• L419-431
Related validation methods
Sandbox execution answers what configured hooks try to do at runtime. It does not answer whether an agent follows hostile prompts or whether a static configuration contains an unsafe source-to-sink path. Use Injection and taint testing for those complementary checks.
Keeping these methods separate matters because a hook can look harmless under a prompt test while still leaking a canary at runtime. In the same way, a taint flow can expose a risk path that a single execution does not reach.
References: src/types.ts• L314-370 src/types.ts• L419-431 src/index.ts• L527-573 src/…/terminal.ts• L480-509
Change checklist
When these files or sections are changed, remember to consider these:
- When hook parsing changes, keep both supported settings shapes, all supported hook stages, and the per-hook execution count aligned with the settings contract.
- When the controlled environment or canary set changes, review secret exposure checks and make sure real host credentials are not passed into the child process.
- When an observation type or severity changes, update the behavioral finding mapping, verdict thresholds, shared report shape, and terminal rendering together.
- When timeout or cleanup behavior changes, re-check that a timed-out run is not reported as clean and that temporary work directories do not remain without notice.
- When MiniClaw path rules or session defaults change, review both proactive path scoping and reactive validation because they form separate parts of the live-session boundary.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A settings file has no recognized hooks | Sandbox analysis returns no result or executes zero hooks. | Distinguish “no hook to run” from “a hooks block was present but unsupported.” |
| A hook uses a network path that is not visible in command or output text | The run may miss the network action because the executor observes patterns rather than enforcing a network block. | Treat sandbox evidence as partial runtime evidence and use a stronger isolation boundary for untrusted commands. |
| A hook reads host paths through behavior not represented in captured output | File access can escape the intended analysis context without producing a finding. | Do not treat the temporary work directory as a full host boundary. Review high-impact hooks with stronger controls. |
| A hook exceeds its time limit | The child is stopped and the run is not a normal completion. | Preserve the timeout finding and treat it as suspicious behavior. |
| Cleanup cannot remove the work directory | Hook output or canary data can remain on disk. | Check cleanup in the host environment and remove residual directories under controlled ownership. |
| A critical observation is mapped without its severity | A secret leak or equivalent behavior can receive the wrong verdict. | Keep observation severity, behavioral finding severity, and verdict rules synchronized. |
Key files
| File | Why to read it |
|---|---|
| src/…/executor.ts | Defines hook selection across supported settings shapes, isolated command runs, canary values, timeout handling, observations, and cleanup. |
| src/…/analyzer.ts | Defines how runtime observations become findings, risk scores, and behavior verdicts. |
| src/…/index.ts | Shows the public sandbox analysis surface and the shared types exposed to the scan. |
| src/index.ts | Shows how settings discovery, execution, analysis, and report mapping connect in the active scan. |
| src/types.ts | Defines shared sandbox result and deep-scan contracts. |
| src/…/sandbox.ts | Defines the live session path boundary and session sandbox lifecycle. |
| src/…/tools.ts | Shows how file-tool paths are scoped and re-validated during a MiniClaw session. |
| src/…/types.ts | Defines session sandbox policy, limits, defaults, and security events. |