I'm writing a CLI using node and I've arrived at the part where I take user input and append it to a string that is the command for the child_process.exec
function.
const CURL_CHILD = exec('npm view --json ' + process.argv[2] + ...
I am trying to figure out what I need to do to process.argv[2]
before I pass it to the exec function. I've surfed around for a while and haven't found any questions or answers that address this specific case.
What is the best way to sanitize this user input for this particular use case? What is actually needed here?
Update I'm still surfing around trying to learn and answer my own question and found this link which suggests I use js-string-escape (a node package). I'd really like to use something native/vanilla to do this. Does node have any tools for this?
Update 2
I finally stumbled upon the buzzwords "command injection" and found a slew of articles recommending the use of child_process.execFile
or child_process.spawn
. I'm still curious if there is a native way to sanitize the input, while still securing the full shell process created by child_process.exec
. I am leaving this open in hopes someone can answer it.