Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsafe use of exec #60

Closed
cristianstaicu opened this issue Jul 21, 2016 · 9 comments
Closed

Unsafe use of exec #60

cristianstaicu opened this issue Jul 21, 2016 · 9 comments

Comments

@cristianstaicu
Copy link

cristianstaicu commented Jul 21, 2016

The module does not sanitize the input before passing it to exec. Therefore, the following code snippet may produce unexpected results for some of the users of the library:

var growl = require("growl");
growl("test`ls`");

Use a sanitization npm module like shell-quote or replace exec with spawn!

@keymandll
Copy link
Contributor

Something like the below might work.

  // To mitigate code-injection vulnerability
  var command = args.join(' ');
  command = command.replace(/[\\`$]/g, function(s) {
    return '\\' + s;
  });
  exec(command, fn);

@cristianstaicu
Copy link
Author

cristianstaicu commented Sep 5, 2016

It is a bit more complicated than that! Your solution does not protect against stuff like this:

growl("my message $(ls)");

A more complete solution here: https://github.com/substack/node-shell-quote/blob/master/index.js

@keymandll
Copy link
Contributor

Did you actually test my code? Could you please provide an example that bypasses it? (other than what you have provided as that one does not) No offense, I'm just curious.

@cristianstaicu
Copy link
Author

cristianstaicu commented Sep 5, 2016

Oops, saw the dollar in the regex later. :( I did not check the code to be honest, but I know for a fact that just replacing one or two characters is usually not enough, you want to maybe quote the string as well. Take a look at the php description of the same thing:
http://php.net/manual/en/function.escapeshellarg.php

@cristianstaicu
Copy link
Author

cristianstaicu commented Sep 5, 2016

Even something like this might bypass it:

growl("my message; touch a-file")

@keymandll
Copy link
Contributor

Well, I have just checked your example by updating my code to print out the value of the resulting command variable:

x@pw:/home/x# cat ./test.js
var growl = require('growl')
growl("my message; echo 'test' > /tmp/this_should_not_work")

x@pw:/home/x# node ./test.js
notify-send "my message; echo 'test' > /tmp/this_should_not_work"
x@pw:/home/x# ls -l /tmp/
total 4
srwxrwxrwx 1 mongodb nogroup    0 Sep  5 13:44 mongodb-27017.sock
drwxr-xr-x 3 root    root    4096 Sep  5 13:45 npm-1326-f77d6571

So there are double quotes added for any string. (not by my code). Now you may say that let's break out by adding a double-quote. It will not work. :)

@cristianstaicu
Copy link
Author

Oops, sorry I did not see the quote part in growl's source code. So, yes probably it works for most of the cases, but I was just trying to convince you to use a more standard solution, rather than a self-baked one. One reason to do so, is that the standard solutions are extensively tested by the community and new patches are added now and then:
https://nodesecurity.io/advisories/117

@keymandll
Copy link
Contributor

So, I'm not the developer of Growl but I'm happy to play around with things a bit to help improve stuff.
Yes, I agree with you in general. Standard, well tested things are the way to go.

I have checked the shellescape you have linked earlier and it solves the problem by wrapping all the stuff in single quotes:

x@pw:/home/x# node ./test.js
notify-send '"my message $(/usr/bin/id > /tmp/this_should_not_work)"'

The only side-effect is that you will have double quotes in all parts of the notification.
So, so far as I see my solution requires 3 extra lines added in terms of changes and for now it seems to addresses the original issue. Versus using shellescape which means one additional dependency, a little bit slower code (not that it really matters), and additional changes (removing all quote() 's ) needed in the Growl source.

Ok, let's conclude that the optimal for long term would be to eliminate the use of the quote()'s and use shellescape. Deal? :)

@keymandll
Copy link
Contributor

Actually, using spawn() of child_process seems to be a much better solution. And then there's no need for the extra dependency.

deiga pushed a commit that referenced this issue Jul 13, 2017
* fix(lib): fixed command injection vulnerability according to Issue #60

* Removed unnecessary dependency by using child_process spawn() method
@deiga deiga closed this as completed Jul 14, 2017
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

No branches or pull requests

3 participants