Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't I use unix commands from php?

Tags:

bash

php

unix

Why would you prefer to keep from using bash commands via exec() in php?

I do not consider portability issue (I definitely will not port it to run on Windows). That's just a matter of a good way of writing scripts.

On the one hand:

  1. I need to write much more lines in php then in bash to accomplish the same task. For example, when I need to filter some lines in a file, I just can't imaging using something instead of cat file | grep string > new_file. This would take much more time and effort to do in php.
  2. I do not want to analyze all situations when something might go wrong. I will just show bash command output to the user, so he would know what exactly happened.
  3. I do not need to write another wrapper around filesystem functions and use it. It is much more efficient to leverage the OS for file searching, manipulation etc.

On the other hand:

  1. Calling unix command with exec() might be inefficient in most cases. It is quite expensive to spawn a separate process. Not talking about scripts running under apache, which is even much less efficient than spawning from command line scripts.
  2. Sometimes it turns out to be 'black magic-like' and perl-like scripting. Though it can be avoided via detailed comments.
  3. Maybe I'm just trying to use two different tools together when they are not supposed to. Each tool has its own application and should not be mixed together.
  4. Even though I'm sure users will not try to run script will malicious purposes, using exec() is a potential security threat. In most cases user data can be escaped with escapeshellarg(), but it is still an issue to take into account.
like image 414
altern Avatar asked Feb 22 '10 15:02

altern


3 Answers

another reason to avoid this is that it's much easier to create security holes like this. for example, if a user manage to sneak

`rm -rf /`

(With backticks) into the input, your bash code might actually nuke the server (or nuke something at least).

this is mostly a religious thing, most developers try to write code that always works. relying on external commands is a sure way to get your code to fail on some systems (even on the same OS).

like image 186
Omry Yadan Avatar answered Oct 26 '22 15:10

Omry Yadan


What are you trying to achieve? PHP has regex-based functions to find what you need from a file. Yes, you would probably need about 5 lines of code to do it, but it would probably be no more or less efficient.

The main reason against using exec() in PHP is for security. If you're trusting your user to give you a command to exec() in bash, they could easily run malicious commands, such as installing and starting backdoor trojans, removing files, and the like.

As long as you're careful though (use the shell escaping commands to clean user input, restrict the Apache user permissions etc) it shouldn't be a problem. I'm just working on a complete platform at the moment, which relies on the front-end executing shell processes simply because C++ is much faster than PHP, so I've written a lot of the backend logic as a shell application and keep PHP for the front-end logic.

like image 30
Andy Shellam Avatar answered Oct 26 '22 14:10

Andy Shellam


Even though you say portability isn't an issue, you never know for certain what the future holds, so I'd encourage you to reconsider that position. For example, I was once asked to port an editor that was written (by someone else) from Unix to DOS. The original program wasn't expected to be ported and was written with Unix specific calls deeply embedded in the code. After reviewing the amount of work required, we abandoned the task as too time consuming.

I have used exec calls in PHP; however, I had no other way to accomplish what I needed (I had to call another program written in another language with no other bridge between the languages). However, IMO, exec calls which aren't necessary are ugly. As others have said, they can also create security risks and slow your program down.

As you said yourself, you need to document the exec calls well to be sure they'll be understood by programmers. Why create the extra work? Not just now but in the future, when any changes to the exec call will also need to be documented.

Finally, I suggest you learn PHP and its functions a bit better. I'm not that good with PHP, but in just a matter of minutes with Google and php.net, I think I accomplished the same thing you gave as an example with:

$search_results = preg_grep($search_string, file($file_name));
foreach ($search_results as $result) {
    echo $result . "\n";
}

Yes, it's a bit more code, but not that much, and you can put it in a function if appropriate ... and I wouldn't be surprised if a PHP guru could shorten it.

like image 22
GreenMatt Avatar answered Oct 26 '22 13:10

GreenMatt