fix(cli): add config-io module for safe config file I/O and preset validation#1370
fix(cli): add config-io module for safe config file I/O and preset validation#1370
Conversation
…lidation Add src/lib/config-io.ts with atomic JSON read/write (temp + rename), EACCES error handling with user-facing remediation hints, and directory permission enforcement. - Refactor credentials.js to use readConfigFile/writeConfigFile - Refactor registry.js to use readConfigFile/writeConfigFile - Add validatePreset() to policies.js (warns on missing binaries section) - ConfigPermissionError with actionable remediation (sudo chown / rm) - Co-located tests for config-io module Fixes #692, #606. Supersedes the config-io and preset validation parts of #782 (without the runner.js redaction, which landed separately in #1246). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughAdded a new permission-aware config I/O module with atomic write semantics and a permission error type; refactored registry and credentials to use it; added preset validation in policies; added tests and a CommonJS shim for the compiled config-io output. Changes
Sequence Diagram(s)sequenceDiagram
participant App as Application
participant CIO as config-io
participant FS as Filesystem
App->>CIO: writeConfigFile(targetPath, data)
CIO->>CIO: ensureConfigDir(parentDir)
CIO->>FS: mkdir -p parentDir (mode 0o700)
FS-->>CIO: success / EACCES
alt EACCES
CIO->>App: throw ConfigPermissionError (with remediation)
else Success
CIO->>FS: write temp file (0o600, PID-suffix)
FS-->>CIO: write success / error
CIO->>FS: rename(temp, target)
FS-->>CIO: rename success / EACCES
alt Rename EACCES
CIO->>FS: unlink(temp) [best-effort]
CIO->>App: throw ConfigPermissionError
else Success
CIO->>App: return
end
end
sequenceDiagram
participant App as Application
participant CIO as config-io
participant FS as Filesystem
App->>CIO: readConfigFile(path, default)
CIO->>FS: stat/access path
FS-->>CIO: exists / missing / EACCES
alt EACCES
CIO->>App: throw ConfigPermissionError
else Missing
CIO->>App: return default
else Exists
CIO->>FS: readFile(path)
FS-->>CIO: contents
CIO->>CIO: JSON.parse(contents)
alt Parse error
CIO->>App: return default
else Success
CIO->>App: return parsed data
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@bin/lib/policies.js`:
- Around line 65-73: The current check in validatePreset using
presetContent.includes("binaries:") can be fooled by comments or scalars;
instead parse the YAML and detect a real top-level key. Replace the substring
check in validatePreset with a YAML parse (e.g., yaml.parse / js-yaml.safeLoad)
of presetContent and then check for Object.prototype.hasOwnProperty.call(parsed,
"binaries") (and that parsed.binaries is not undefined/null); if parsing fails,
log a warning and treat the preset as missing the binaries section so the
original warning is emitted for presetName.
In `@src/lib/config-io.ts`:
- Around line 29-41: The current buildRemediation() message assumes sudo is
available; update it to include non-sudo fallback actions so environments
without sudo get actionable guidance. In the buildRemediation() function
(reference: nemoclawDir and HOME usage) add alternative suggestions such as
recreating the config under the current user's home (e.g., remove or move the
directory if writable), instructing to remove the directory without sudo when
the user owns it (e.g., rm -rf $HOME/.nemoclaw), and advising to relocate or
initialize config in a user-writable path (for example creating a new config
under $HOME or specifying an alternative CONFIG_HOME), so the error message
covers both sudo and non-sudo environments. Ensure the text clearly
distinguishes when sudo is required vs when the non-sudo command applies.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: b236f71c-5208-4de3-9a9b-e2b0d59af3a1
📒 Files selected for processing (7)
bin/lib/config-io.jsbin/lib/credentials.jsbin/lib/policies.jsbin/lib/registry.jssrc/lib/config-io.test.tssrc/lib/config-io.tstest/registry.test.js
| function validatePreset(presetContent, presetName) { | ||
| if (!presetContent.includes("binaries:")) { | ||
| console.warn( | ||
| ` Warning: preset '${presetName}' has no binaries section — ` + | ||
| `this will cause 403 errors in the sandbox (ref: #676)`, | ||
| ); | ||
| return false; | ||
| } | ||
| return true; |
There was a problem hiding this comment.
Detect the actual YAML key, not a raw substring.
presetContent.includes("binaries:") also matches comments, descriptions, or other scalar text, so a preset can still lack the real binaries section and skip the warning. Please parse the preset or at least anchor the check to a YAML key.
Suggested fix
function validatePreset(presetContent, presetName) {
- if (!presetContent.includes("binaries:")) {
+ let parsed = null;
+ try {
+ parsed = YAML.parse(presetContent);
+ } catch {
+ parsed = null;
+ }
+ if (
+ !parsed ||
+ typeof parsed !== "object" ||
+ !Object.prototype.hasOwnProperty.call(parsed, "binaries")
+ ) {
console.warn(
` Warning: preset '${presetName}' has no binaries section — ` +
`this will cause 403 errors in the sandbox (ref: `#676`)`,
);
return false;
}
return true;
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@bin/lib/policies.js` around lines 65 - 73, The current check in
validatePreset using presetContent.includes("binaries:") can be fooled by
comments or scalars; instead parse the YAML and detect a real top-level key.
Replace the substring check in validatePreset with a YAML parse (e.g.,
yaml.parse / js-yaml.safeLoad) of presetContent and then check for
Object.prototype.hasOwnProperty.call(parsed, "binaries") (and that
parsed.binaries is not undefined/null); if parsing fails, log a warning and
treat the preset as missing the binaries section so the original warning is
emitted for presetName.
| const { readConfigFile, writeConfigFile } = require("./config-io"); | ||
|
|
There was a problem hiding this comment.
Lock acquisition still bypasses the new config permission handling.
save() now wraps the final file write, but every mutating path reaches acquireLock() first. Permission failures creating ~/.nemoclaw or ${REGISTRY_FILE}.lock still escape as plain fs errors, so the new remediation never reaches the main registerSandbox / updateSandbox / removeSandbox / setDefault flows that issue #692 targets.
Also applies to: 124-129
| function buildRemediation(): string { | ||
| const home = process.env.HOME || os.homedir(); | ||
| const nemoclawDir = path.join(home, ".nemoclaw"); | ||
| return [ | ||
| " To fix, run one of:", | ||
| "", | ||
| ` sudo chown -R $(whoami) ${nemoclawDir}`, | ||
| ` # or, if the directory was created by another user:`, | ||
| ` sudo rm -rf ${nemoclawDir} && nemoclaw onboard`, | ||
| "", | ||
| " This usually happens when NemoClaw was first run with sudo", | ||
| " or the config directory was created by a different user.", | ||
| ].join("\n"); |
There was a problem hiding this comment.
Remediation still assumes sudo exists.
Issue #692 explicitly includes environments where sudo is unavailable, but both suggested fixes here still require it. In those installs the new ConfigPermissionError is still not actionable. Please add a non-sudo fallback, e.g. recreating config under a user-writable HOME or config directory.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/lib/config-io.ts` around lines 29 - 41, The current buildRemediation()
message assumes sudo is available; update it to include non-sudo fallback
actions so environments without sudo get actionable guidance. In the
buildRemediation() function (reference: nemoclawDir and HOME usage) add
alternative suggestions such as recreating the config under the current user's
home (e.g., remove or move the directory if writable), instructing to remove the
directory without sudo when the user owns it (e.g., rm -rf $HOME/.nemoclaw), and
advising to relocate or initialize config in a user-writable path (for example
creating a new config under $HOME or specifying an alternative CONFIG_HOME), so
the error message covers both sudo and non-sudo environments. Ensure the text
clearly distinguishes when sudo is required vs when the non-sudo command
applies.
Convert the last 3 blocked-by-#782 CJS modules to TypeScript: - credentials.js → src/lib/credentials.ts - registry.js → src/lib/registry.ts - policies.js → src/lib/policies.ts 716 CLI tests pass. Coverage ratchet passes. Depends on #1370 (config-io module). Relates to #924. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ericksoa
left a comment
There was a problem hiding this comment.
Nice consolidation — the deduplication of atomic-write logic from credentials.js and registry.js into a shared module is clean, and the ConfigPermissionError with remediation hints is a real UX win for #692.
A few items worth considering before merge (inline comments below).
- ensureConfigDir: chmod 0o700 on pre-existing dirs with weaker modes (preserves old credentials.js hardening behavior) - readConfigFile: remove TOCTOU existsSync, catch ENOENT directly - acquireLock: use ensureConfigDir for consistent permission errors - applyPreset: bail early when validatePreset returns false Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
All four items addressed in a63e957:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@bin/lib/policies.js`:
- Around line 253-256: The calls to applyPreset (used in the resume flow,
interactive flow, and single selection flow) are ignoring its boolean return and
thus silently continuing on failure; update each call site that currently just
calls applyPreset (notably the resume, interactive and single-selection code
paths) to check the return value and handle failure the same way as the other
sites: either throw an error when applyPreset returns false or wrap the call in
the same try/catch + retry logic used at the other locations (the handlers
around applyPreset that perform retries at lines noted in the review), ensuring
failures are logged and cause the flow to abort or retry rather than silently
proceeding.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: c59564c8-12c4-4072-badd-41205052bfa1
📒 Files selected for processing (3)
bin/lib/policies.jsbin/lib/registry.jssrc/lib/config-io.ts
✅ Files skipped from review due to trivial changes (1)
- src/lib/config-io.ts
| if (!validatePreset(presetContent, presetName)) { | ||
| return false; | ||
| } | ||
|
|
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Find all call sites of applyPreset and check if return value is used
echo "=== Call sites of applyPreset ==="
rg -n --type js '\bapplyPreset\s*\(' -A2 -B2
echo ""
echo "=== Specifically checking if return value is captured ==="
# Look for patterns where applyPreset is called without assignment or conditional
rg -n --type js '^\s*(policies\.)?applyPreset\s*\('Repository: NVIDIA/NemoClaw
Length of output: 3708
🏁 Script executed:
#!/bin/bash
# Check the full applyPreset function implementation
echo "=== Full applyPreset function in policies.js ==="
sed -n '236,290p' bin/lib/policies.js
echo ""
echo "=== Context around line 3193 in onboard.js ==="
sed -n '3185,3200p' bin/lib/onboard.jsRepository: NVIDIA/NemoClaw
Length of output: 2452
🏁 Script executed:
#!/bin/bash
# Check the broader context of the resume workflow to understand error handling intent
echo "=== Function context for line 3193 ==="
sed -n '3170,3210p' bin/lib/onboard.js
echo ""
echo "=== Check other unchecked call sites ==="
echo "Line 3307 context:"
sed -n '3300,3315p' bin/lib/onboard.js
echo ""
echo "Line 1119 context:"
sed -n '1110,1125p' bin/nemoclaw.jsRepository: NVIDIA/NemoClaw
Length of output: 2796
Unchecked return values at multiple call sites may leave presets silently unapplied.
The resume workflow at line 3193 calls applyPreset without checking its return value. When preset loading or validation fails, applyPreset returns false with a logged error, but the loop continues silently and the function returns chosen[] as if all presets were applied.
This pattern exists at three unchecked call sites:
- Line 3193 (resume flow)
- Line 3307 (interactive flow)
- Line 1119 (single selection flow)
In contrast, other call sites (lines 3082, 3131, 3145, 3240) wrap applyPreset in try-catch blocks with retry logic. The inconsistency means some failures trigger retries while others are silently ignored, potentially leaving sandboxes in misconfigured states.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@bin/lib/policies.js` around lines 253 - 256, The calls to applyPreset (used
in the resume flow, interactive flow, and single selection flow) are ignoring
its boolean return and thus silently continuing on failure; update each call
site that currently just calls applyPreset (notably the resume, interactive and
single-selection code paths) to check the return value and handle failure the
same way as the other sites: either throw an error when applyPreset returns
false or wrap the call in the same try/catch + retry logic used at the other
locations (the handlers around applyPreset that perform retries at lines noted
in the review), ensuring failures are logged and cause the flow to abort or
retry rather than silently proceeding.
The selectFromList function was added to policies.js on main (via #1370) after our TS migration branched. Add the typed implementation to keep the TS module in sync. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
src/lib/config-io.ts: atomic JSON read/write (temp + rename), EACCES error handling withConfigPermissionErrorand user-facing remediation hintscredentials.jsto usereadConfigFile/writeConfigFile(removes manual fs + chmod)registry.jsto usereadConfigFile/writeConfigFile(removes manual atomic write)validatePreset()topolicies.js— warns when a preset lacksbinaries:section (All policy presets and default telegram/discord policies return 403 due to missingbinariessection #676)Supersedes the config-io and preset validation parts of #782 (without the runner.js redaction, which landed separately in #1246). Fixes #692, #606.
Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests
Refactor
Bug Fixes