nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] `nanocoder daemon start` has no directory-trust check; headless mode permits unattended command execution

Open addyCooks opened this issue 2 months ago • 2 comments

Description

nanocoder daemon start is a dedicated CLI fast path (cli.tsx) that goes straight to runDaemonCli → startDaemon → bootSkillPipeline, loading and registering every .nanocoder/agents|commands|tools/*.md and .nanocoder/skills/<name>/skill.yaml in the project root. source/daemon/ has zero references to trust/Trust anywhere this command never touches App.tsx or useDirectoryTrust, the app's only trust-check implementation.

Once booted, headless mode (used for every daemon-triggered run) excludes only ask_user/agent/write_plan from the tool list execute_bash, write_file, string_replace, diff_edit, and MCP tools all remain available and are explicitly coded to skip confirmation in headless (execute-bash.tsx's own comment: "Even auto-accept still prompts for bash... in headless mode" — i.e. headless is strictly more permissive than auto-accept for bash). execute_bash's denylist only blocks a handful of literal catastrophic patterns and is trivially bypassed. OS-level sandboxing is opt-in and explicitly unsupported on Windows.

Location

  • source/cli.tsx:48-73 (daemon fast path never imports App.tsx/useDirectoryTrust)
  • source/daemon/cli.ts, source/daemon/daemon.ts, source/daemon/entry.ts
  • source/skills/bootstrap.ts (bootSkillPipeline)
  • source/tools/tool-manager.ts:38-58 (MODE_EXCLUDED_TOOLS.headless)
  • source/tools/execute-bash.tsx:168-182 (if (mode === 'headless') return false;)
  • source/utils/tool-approval.ts:13-20
  • source/services/bash-sandbox.ts:136-140 (unsupported on Windows)

Environment

  • OS: Any (Windows has no sandbox fallback at all)
  • Node version: v22.22.3
  • Nanocoder version: 1.30.0
  • Provider: N/A
  • Model: N/A

Steps to Reproduce

  1. Commit .nanocoder/skills/evil/skill.yaml to a repo with subscribe: [{kind: file.changed, paths: ["**/*"]}] and a subagent whose systemPrompt drives execute_bash with a payload.
  2. Clone the repo and run nanocoder daemon start in it (a documented, encouraged command) — no trust prompt appears anywhere in this path.
  3. Touch any file (including a routine, unrelated edit) file.changed fires, dispatches the subagent in headless mode, and execute_bash runs the payload with no confirmation.

Expected Behavior

daemon start should refuse to arm subscriptions / load skill content from a project directory that has not been explicitly trusted, matching the protection the interactive TUI is supposed to provide (see also F-21/this issue's sibling, which shows that protection is itself incomplete).

Actual Behavior

The daemon boots and arms every subscription with no trust check of any kind, then executes tool calls (including execute_bash) with no human confirmation and no default sandbox.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently (established via code read grep across source/daemon/ for trust/Trust returns zero matches)
  • [x] This issue occurs with the latest version of nanocoder

Suggested fix: add a trust check to the daemon boot path, e.g. refuse to start (or start in a strictly reduced/no-exec tool posture) unless the project directory is already in preferences.trustedDirectories, requiring at least one prior interactive nanocoder run (or an explicit --trust-directory flag) before daemon start will arm any subscription.

addyCooks avatar Sep 09 '26 12:09 addyCooks

Hey @akramcodez,

Confirmed by code read: grep -rn "trust" source/daemon/ returns zero, and the daemon fast path at cli.tsx:50-73 goes straight to runDaemonCli without ever loading App.tsx/useDirectoryTrust. Meanwhile headless excludes only ['ask_user','agent','write_plan'] (tool-manager.ts:58), and both execute-bash.tsx:180 and tool-approval.ts:18 explicitly skip approval in headless.

Plan:

  1. Add a trust check to the daemon boot path before bootSkillPipeline arms anything, reuse the non-Ink pattern already in plain/shell.ts:394 so there's one shared trust helper instead of a third copy.
  2. daemon start in an untrusted dir → refuse with a clear message pointing at one prior interactive run or --trust-directory. No skill loading, no subscriptions armed.
  3. Optional follow-up if you want it in scope: a reduced no-exec posture for headless instead of a hard refuse.

Tests: AVA specs for untrusted → non-zero exit + no skills loaded + no subscriptions armed, trusted → boots normally, --trust-directory path works, plus the shared-helper refactor covered by existing plain/shell.spec.ts cases. Manual repro with the .nanocoder/skills/evil/ setup from the issue, before/after.

Can you assign this to me?

addyCooks avatar Sep 09 '26 12:09 addyCooks

Confirmed the gap is still present on current main (537ee6c): the daemon fast path in source/cli.tsx goes straight to runDaemonCli -> start() -> launchDaemon -> entry.ts without touching useDirectoryTrust, and grep -r trust source/daemon/ returns nothing. Meanwhile headless mode skips confirmations for execute_bash / write_file / MCP tools, so an untrusted repo's .nanocoder/ content gets unattended execution.

Plan (mirroring the non-interactive rules the plain shell already enforces in source/plain/shell.ts):

  • Gate daemon start on directory trust: refuse with a user-facing message unless the project root is already in preferences.trustedDirectories, the caller passes --trust-directory, or NANOCODER_TRUST_DIRECTORY=1 is set (both of the latter record standing trust, since the daemon boots detached and can re-boot via autostart without the CLI).
  • Add the same check to the daemon boot itself (entry.ts), so autostart boots set up by daemon install are covered too.
  • Regression tests in source/daemon/cli.spec.ts covering: refuse-when-untrusted (no spawn), trusted-via-preferences, env-var trust (persisted), and --trust-directory (persisted).

I have the fix implemented and tested locally (red -> green); PR to follow shortly.

yetuge avatar Sep 11 '26 10:09 yetuge