fix(platform): single-quote paths in QuotePath to neutralize NVM_DIR injection#78
Merged
Merged
Conversation
…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>
|
✔️ 2b0b2f9 - Conventional commits check succeeded. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
platform.QuotePathwrapped POSIX paths in double quotes and only escaped"and\\, leaving$,$(...), and backticks exposed to bash expansion.NVM.Version()builds asource <path> && nvm --versionscript using whateverNVM_DIR(or~/.nvm) provides — so an attacker who can setNVM_DIR(CI runner env, direnv.envrc, devcontainer, compromised build step) gets arbitrary command execution the moment nvm is detected or queried.POSIX
QuotePathnow 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
fix(scope): subject)make cilocally and it passes (fmt, vet, test pass locally;make lintfails 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 onmain, independent of this fix; CI runs golangci-lint v1.64.8 per.github/workflows/ci.ymland will parse the config fine)docs/, inline godoc)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):
nvmDir()layer — a defense-in-depth follow-up. The current fix at theQuotePathlayer is sufficient on POSIX (single quotes neutralize everything), but a whitelist would also help Windows users where cmd.exe has a different unquote story.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,$(...), orwhoamiis now carried verbatim into bash's command stream and never interpreted. Verification path: setNVM_DIR='/tmp/$(touch /tmp/PWNED)', runnodeup check—/tmp/PWNEDis never created (before the fix, it would be).