nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Setup wizards create agents.config.json with API keys as world-readable (0644) and write it non-atomically

Open akramcodez opened this issue 4 weeks ago • 2 comments

Description

The provider and MCP setup wizards write the config file (with plaintext API keys and bearer tokens) using a plain writeFileSync. A newly created file gets the default mode, usually 0644, so other users on the machine can read the keys, and a crash mid-write truncates the whole config.

Environment

  • OS: macOS 27.2 (umask 022); also applies to Linux
  • Node version: v22.23.4
  • Nanocoder version: 1.32.0 (main at daa0ad9b)
  • Provider: any provider added through /setup-config
  • Model: not model specific

Steps to Reproduce

  1. Make sure ~/.config/nanocoder/agents.config.json does not exist (or use a fresh NANOCODER_CONFIG_DIR).
  2. Run the provider wizard, enter an API key, save.
  3. ls -l the file.

In source/wizards/base-config-wizard.tsx the save is:

writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');

The same default-mode write is used when the empty config is first created in source/config/paths.ts. I confirmed that this call with a default umask produces mode 644 for a file holding an apiKey.

Expected Behavior

A file with credentials is created 0600, like the other places that store private data: the OAuth credential store (json-credential-store.ts), session files (session-manager.ts) and the memory store all use 0o600. The write should also go through the existing atomicWriteFile helper so an interrupted save cannot leave half a file.

Actual Behavior

agents.config.json (and the MCP config the MCP wizard writes) is created world-readable and is rewritten in place. If the file already exists, its existing mode is kept, so only fresh installs are affected, but that is also when most people run the wizard.

Logs/Screenshots

mode 644

(from fs.writeFileSync(f, JSON.stringify({nanocoder:{providers:[{name:'x',apiKey:'sk-test'}]}})) in a temp dir)

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder

atomicWriteFile already accepts a mode option, and prompt-history.ts shows how to keep the mode of an existing file, so a fix can write 0o600 when the file is new and preserve the current mode otherwise. If the wizard saves to the project directory the file lives in the repo, so a note in the wizard about not committing keys (or preferring $VAR references) would help too.

akramcodez avatar Nov 06 '26 09:11 akramcodez

Hey @akramcodez, if no one is already working on this, I'd be happy to pick it up.

@addyCooks @akramcodez, could one of you assign this to me?

Vishesh-Verma-07 avatar Nov 06 '26 13:11 Vishesh-Verma-07

Go ahead @Vishesh-Verma-07 !

addyCooks avatar Nov 07 '26 08:11 addyCooks