Skip to content

fix(platform): single-quote paths in QuotePath to neutralize NVM_DIR injection#78

Merged
dipto0321 merged 1 commit into
mainfrom
fix/platform/nvm-dir-command-injection
Jul 3, 2026
Merged

fix(platform): single-quote paths in QuotePath to neutralize NVM_DIR injection#78
dipto0321 merged 1 commit into
mainfrom
fix/platform/nvm-dir-command-injection

Conversation

@dipto0321

Copy link
Copy Markdown
Owner

Summary

platform.QuotePath wrapped POSIX paths in double quotes and only escaped " and \\, leaving $, $(...), and backticks exposed to bash expansion. NVM.Version() builds a source <path> && nvm --version script using whatever NVM_DIR (or ~/.nvm) provides — so an attacker who can set NVM_DIR (CI runner env, direnv .envrc, devcontainer, compromised build step) gets arbitrary command execution the moment nvm is detected or queried.

POSIX QuotePath now uses single-quote wrapping with '\'' to escape embedded '. Inside single quotes bash performs NO expansion — $, $(...), backticks, and \\ all become literal. The Windows branch (cmd.exe) is unchanged. New regression tests at platform + detector levels.

Closes #43.

Linked issues

Type of change

  • fix — bug fix (PATCH bump; this is a security fix, see "Severity" below)

Checklist

  • Title follows Conventional Commits (fix(scope): subject)
  • I ran make ci locally and it passes (fmt, vet, test pass locally; make lint fails on the v2→v1 schema mismatch tracked in build(lint): .golangci.yml v1 schema incompatible with golangci-lint v2 (brew installs v2, make lint fails to even parse config) #62 — pre-existing on main, independent of this fix; CI runs golangci-lint v1.64.8 per .github/workflows/ci.yml and will parse the config fine)
  • I added or updated tests for the change
  • I updated relevant docs (README, docs/, inline godoc)
  • No new linter warnings
  • If breaking: I documented the migration path in the PR body and updated CHANGELOG.md (N/A — security fix)

Severity

Critical / Security. NVM_DIR is read from the environment without validation. The injection fires on every command path that triggers NVM.Version() or any of the nvm mutation methods (Install, Use, Uninstall, SetDefault). Recommended for fast-track review and merge.

Scope notes

Out of scope (separate issues):

Screenshots / output

The user-visible behaviour change is: no behaviour change for benign inputs. The fix is a defence: a path or NVM_DIR value containing $USER, $(...), or whoami is now carried verbatim into bash's command stream and never interpreted. Verification path: set NVM_DIR='/tmp/$(touch /tmp/PWNED)', run nodeup check/tmp/PWNED is never created (before the fix, it would be).

…injection

`platform.QuotePath` wrapped POSIX paths in double quotes and only
escaped `"` and `\`, leaving `$`, `$(...)`, and backticks exposed to
bash expansion. `NVM.Version()` builds a `source <path> && nvm
--version` script using whatever `NVM_DIR` (or `~/.nvm`) provides,
so an attacker who can set `NVM_DIR` (CI runner env, direnv
`.envrc`, devcontainer, compromised build step) gets arbitrary
command execution the moment nvm is detected or queried.

Switch QuotePath's POSIX branch to single-quote wrapping with
`'\''` to escape embedded `'`. Inside single quotes bash performs
no expansion — `$`, `$(...)`, backticks, and `\\` all become
literal. The Windows (cmd.exe) branch is unchanged: it still wraps
paths-with-spaces in double quotes; full injection-safety on
Windows would need a different approach but nvm-windows is its own
detector with its own quoting story.

The existing test table's expectations are updated to the new
contract; a new `TestQuotePathNeutralizesShellInjection` exercises
a payload path with `$(touch /tmp/PWND_…)` through `printf '%s'` and
asserts both that the round-trip is byte-identical AND that the
sentinel file is never created. A new detector-level
`TestNVM_Version_NVMDirShellInjectionIsNeutralized` points `NVM_DIR`
at a payload string and asserts no unquoted `$(touch` occurrence
in the emitted script.

Refs #43

Co-Authored-By: puku-ai-2.8 <noreply@puku.sh>
@cocogitto-bot

cocogitto-bot Bot commented Jul 3, 2026

Copy link
Copy Markdown

✔️ 2b0b2f9 - Conventional commits check succeeded.

@dipto0321 dipto0321 merged commit e214195 into main Jul 3, 2026
10 checks passed
@dipto0321 dipto0321 deleted the fix/platform/nvm-dir-command-injection branch July 3, 2026 01:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(detector): command injection via NVM_DIR env var in nvm detection

1 participant