[Bug] Setup wizards create agents.config.json with API keys as world-readable (0644) and write it non-atomically
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
- Make sure
~/.config/nanocoder/agents.config.jsondoes not exist (or use a freshNANOCODER_CONFIG_DIR). - Run the provider wizard, enter an API key, save.
ls -lthe 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.
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?
Go ahead @Vishesh-Verma-07 !