Small example, reduced from a REST API node.js app:
const { exec } = require('child_process');
var userInput = 'untrusted source';
var cmd = `/bin/echo "${userInput}"`;
exec(cmd, function(err, stdout, stderr) {
console.log('echo: ' + stdout);
});
Assuming the userInput
is from an untrusted source, what needs to be done avoid any vulnerability? For example, the quoted "${userInput}"
parameter for echo
avoids input 'evil spirit; rm -rf /'
from causing damage. What else needs to be done to stay safe?
Update: The objective is to make a few existing shell scripts/commands in the file system available via a REST API on the intranet.
'untrusted \`date\`'
– Peter Thoeny May 11 '20 at 21:26