Reputation: 45
I have this code :
$path = "/somedirectory/$user";
echo exec('du -s '.escapeshellarg($path));
This code is used to check how many space the user use in his directory. The $user value can be any alphanumeric value and ".-"
Is it safe to use it ?
Upvotes: 1
Views: 386
Reputation: 655229
escapeshellarg
ensures that the value is interpreted as a single, plain shell argument without further shell expansion (e. g., no `…`
, $(…)
, ${…}
, etc.). That means that any value in $path
is passed as is as a single argument to du
.
However, as already mentioned, it does not prevent from passing arguments that may be malicious when interpreted by the executable.
Upvotes: 2
Reputation: 98
Safe if only your exec does not rely on any $user related commands.
For example, $user . "/du -s " is absolutely unsafe.
Upvotes: 0
Reputation: 3328
This is safe in the sense that it won't run arbitrary commands, but depending on how $user
gets set, ..
could sneak in there, which would let whoever is looking at this to see the size of the /
directory, which may or may not be of concern to you.
Upvotes: 1