4

I'm adding arguments to child_process.exec command using string concatenation and they are ignored

var exec = require( "child_process" ).exec;

var cmd = exec( "grunt build --project="+application, {
        cwd: application
    },
    function( error, stdout, stderr ){});

cmd.stdout.pipe( process.stdout );
cmd.stderr.pipe( process.stderr );

Why is string concatenation a problem and how to avoid it?

2 Answers 2

4

Your code is vulnerable to command injection. It depends where's application coming from and you need to make sure it's not customizable by user.

Malicious code in your example would be

var application = '; rm -rf .'

but it wouldn't work since you're also trying to change the current directory via pwd.

The general recommendation is to be careful with child_process.exec and use child_process.execFile or child_process.spawn instead.

Sign up to request clarification or add additional context in comments.

Comments

0

Check your grunt build task to see if there is anything wrong. There is nothing wrong in your code with string concatenation in child_process.exec

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.