Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

DeepSeek Harness Sandbox and Approval: Making Agent Dangerous Actions Controllable

DeepSeek Harness Sandbox and Approval: Making Agent Dangerous Actions Controllable

The previous chapter covered the tool execution pipeline, of which two stages are of greatest concern: how the sandbox constrains commands, and how approval decides whether an action can be done.

This chapter will delve into the two services ctx.sandbox and ctx.approval, clarifying their respective roles and fail-closed principles.

In short: the sandbox governs what boundaries commands run within, approval governs whether this specific operation is allowed, and both default to fail-closed.


First, the big picture: sandbox decisions + approval flow

The following diagram looks at both services together: on the left is how the sandbox wraps argv, on the right is how approval makes a one-time decision.

sandbox decision and approval flow diagram

Together they answer the same question: when an Agent wants to do something risky, how to constrain it.

The sandbox circles which files a process can touch, while approval leaves whether to allow this one operation to the answerer to decide.


ctx.sandbox: wrapping argv according to policy

The process sandbox model is: the consumer hands over the exact argv, and the backend wraps it according to the file-effects policy.

The key point is exact argv rather than a shell string--a shell-shaped consumer must pass ['bash', '-c', command]。

ctx.sandbox.confine(argv, policy) returns a ConfinedArgv, namely the replaced argv plus the backend's enforcement facts.

SandboxMode(sandbox mode) only controls file system effects, not network or process visibility.

read-only: only allows necessary data receivers (such as /dev/null), denies writes.

workspace-write: also allows writes under the workspace root and the backend-committed temporary area.

danger-full-access: bypasses isolation; the consumer directly spawns the original argv without calling ctx.sandbox.

Only the first two modes are sent to the provider,danger-full-access which never enter the sandbox at all.

This guarantees a security property: restricted execution necessarily reaches ctx.sandbox, and silent non-isolated pass-through is never legal.


Enforcement degree and fallback rules

The backend reports the enforcement completeness it actually achieved: enforcement: 'full' | 'partial'.

full means the backend controlled all file effects promised by the mode;partial means only a subset was controlled.

Current partial-enforcement cases include older Landlock ABIs, and the Everyone and hard-link boundaries of the Windows ACL runner.

Consumers requiring absolute boundaries must treat partial as insufficient, rejecting it or exposing this distinction upward.

Policy resolution has a clear fallback order, which the official documentation summarizes in three levels:

Precedence Source Description
Highest Approved explicit mode passed in during a one-time privilege-escalation retry mode, overriding the session policy
Second The last session sandbox/mode Events persisted with the session log, replay-reconstructible
Go back Deployment default mode Agent-less calls and sessions without cwd use the configured root directory

Normal tool calls derive from the calling session's immutable cwd workspaceRoot。

root is first normalized by file system semantics, then by lexical normalization, so a cwd containing symlink/.. identifies the directory where the process actually runs.

Fail-closed: when no backend is available,ctx.sandbox.confine will throw SandboxUnavailableError, error code SANDBOX_UNAVAILABLE。

Under a restricted policy, silent non-isolated pass-through is never legal.


Hands-on example: running bash once in restricted mode

Let's string together the call path of sandboxed bash with code.

Example

// File path: my-plugins/sandbox-demo/src/index.ts

// Demonstrates a sandboxed consumer: first resolve the policy, then let ctx.sandbox wrap argv.

import

type

{

Context

}

from

'@deepseek-ai/cordis'

import

{

defineTool

}

from

'@deepseek-ai/dsh-tools'

export

const

name

=

'sandbox-demo'

export

const

inject

=

[

'tools'

,

'sandbox'

,

'sandboxPolicy'

]

export

function

apply

(

ctx

:

Context

)

{

  ctx.

tools

.

register

(

defineTool

(

{

    name

:

'sandboxed_echo'

,

    description

:

'Run echo inside the sandbox. The runoob demo command.'

,

    parameters

:

{

      text

:

{

type

:

'string'

,

required

:

true

,

description

:

'Text to echo'

}

,

   

}

,

    output

:

{

schema

:

{

type

:

'string'

}

}

,

    async execute

(

args

,

exec

)

{

     

// 1. Resolve the complete policy for this call: the session cwd is the workspace boundary.

     

const

policy

=

ctx.

sandboxPolicy

.

resolve

(

{

session

:

exec.

agent

?

.

session

}

)

     

// 2. The consumer hands over the exact argv (program plus arguments), not a shell string.

     

const

argv

=

[

'bash'

,

'-c'

,

`echo $

{

JSON.

stringify

(

args.

text

)

}

`

]

     

// 3. danger-full-access spawns directly; everything else is handed to the sandbox for wrapping.

     

if

(

policy.

mode

===

'danger-full-access'

)

{

       

const

{

spawn

}

=

await

import

(

'node:child_process'

)

       

// ... spawn(argv) and collect output

       

return

`echo $

{

args.

text

}

`

// Demo: normal return

     

}

     

// 4. Restricted mode: confine returns the replaced argv, throwing SANDBOX_UNAVAILABLE if no backend.

     

const

confined

=

ctx.

sandbox

.

confine

(

argv

,

{

mode

:

policy.

mode

,

workspaceRoot

:

policy.

workspaceRoot

}

)

     

// 5. The consumer then spawns confined.argv, and decides whether to require full based on confined.enforcement.

     

if

(

confined.

enforcement

===

'partial'

&&

policy.

mode

!==

'read-only'

)

{

       

return

{

isError

:

true

,

error

:

{

message

:

'partial enforcement is not acceptable for runoob demo'

}

}

     

}

     

return

`confined echo $

{

args.

text

}

(

enforcement

:

$

{

confined.

enforcement

}

)

`

   

}

,

 

}

)

)

}

Note: the above code is a demonstrative reference, omitting the details of spawn and output collection.

The production-grade consumer is dsh-bash-sandbox, which is responsible for both spawn and result attribution.

The real difficulty lies in result classification: distinguishing sandbox runner failure from sandbox working normally but denying.

The sandbox also brings back two orthogonal stderr classifiers:

denialSignatures: identifies cases where a restricted command is blocked while the sandbox works normally (bwrap's EROFS text, Landlock's EACCES, Seatbelt's EPERM).

runnerFailureRules: identifies cases where the runner denies or fails before executing the command (a Landlock failure requires exit code 125 plus a line of fatal diagnostic).

The consumer should first report runner failures as sandbox infrastructure failures, not ordinary task failures.


ctx.approval: one-time permission decisions

The approval service answers a very narrow question: can this specific operation continue?

It passes the question through approval/request a waterfall to distribute the question to answerers.

An answerer returns a result when it is responsible for handling the request; otherwise it calls next() for delegation; the first answerer occupies the single decision slot.

The result type ApprovalOutcome is closed:

Result Meaning Caller behavior
allowed-once One-time allow The only allow result, authorizing only the one operation asked about
rejected Explicit denial rejected
cancelled Request withdrawn (e.g., aborted by signal) rejected
unavailable No answerer, answerer throws an exception, or returns a non-compliant value Fail-closed, deny in all cases

Key security property: missing, non-responsible, exception-throwing, or non-compliant answerers all produce unavailable, not an allow.

The caller treats rejected、cancelled and unavailable as deny in all cases.


Per-session approval policy: ask and never

ApprovalPolicy decides what happens before the interactive answerer runs.

Strategy Behavior Typical scenario
ask(default) Delegates to the composed answerer chain; falls back to when no answerer is present: unavailable Interactive UI, requiring a human decision
never Deterministic return of rejected, without dispatching any answerer A strict headless posture for CI and unattended runs

The effective value is the last approval/policy event in the session log, falling back to the service configuration.

setApprovalPolicy(session, policy) is the only write path, so replay can reconstruct the override.

never Enforced inside the service, before waterfall distribution, so even registering an answerer later with prepend cannot bypass it.

The distinction between audit and model visibility: approval audit events for (approval/asked The difference between approval/decided) are log-only and do not enter the model transcript.

Model-visible behavior is the caller-derived tool result and a snapshot of the current runtime context.


Permission presets: bundling two knobs into named presets

Sandbox mode (sandbox/mode) and approval policy (approval/policy) are two mutually independent knobs.

The permission presets layer ctx.permissionPresets bundles them into named presets for clients to offer as a single permission selector.

Default Sandbox mode Approval policy Meaning
workspace-write workspace-write ask Writable workspace, ask the user for sensitive operations
danger-full-access danger-full-access never Bypass sandbox, no prompts, only runs in disposable environments

The preset table is configuration-driven; the default table includes the two above; the name custom is reserved for derived non-preset states.

current(events) Fold the actually effective preset from the two knobs, not just look at the preset's own events.

When two presets share the same knob combination,permission/preset events let current() still preserve which one the user actually chose.

Important: a permission preset only describes the capabilities it actually governs.

Official incident review 0002 warned: combined file system access cannot safely follow a runtime bash-only preset.

In the runoob production environment,danger-full-access should only be assigned to one-time, disposable sandbox environments.


Summary and self-test

Sandbox and approval separate capability boundaries from decision authorization: the sandbox wraps argv with file-effects policies, approval uses waterfall answerers for one-time allowing.

Self-test questions:

Problem Reference
A consumer wants to run danger-full-access mode--will it call ctx.sandbox? No, it directly spawns the original argv without entering the sandbox
If the approval chain has no answerer, what is the result? unavailable, fail-closed, the caller denies
In CI, to never ask and deterministically deny all approvals, which policy should be used? approval/policy: never