r/PHPhelp • u/noNudesPrettyPlease • Apr 20 '26
Using exec today. Am I a bad php dev?
Recently I needed to strip just location data from my user's jpg uploads and used ExifTool with exec to do the job. I tried to make this as safe as possible. I checked the file mime type, changed the file name, and escaped the path (escapeshellarg) when passing it to exec.
Now I need to try to read some text from a jpg and once again my research has pulled me towards running this with Tesseract OCR and exec.
Many years ago I heard that we should not use exec, but I was wondering have things gotten any better? Is it still recommend to not use exec, or is it more or less safe, as long you follow the general security steps of checking the file mime, keeping the call to the program hard coded in the method, and escaping the file path. Or am I a terrible PHP dev?
Thanks.
3
u/abrahamguo Apr 20 '26
This sounds like a perfectly fine and thought-through use case for exec (as long as the tools that you’re using it for don’t have PHP-specific libraries available).
I’d be curious if you have a source for where you heard before that you shouldn’t use exec.
4
u/sporadicPenguin Apr 20 '26
Exec is fine as long as you take the necessary precautions, which it sounds like you are. Were you thinking of eval() by any chance? Because there is never a reason to use that
11
u/martinbean Apr 20 '26
I wouldn’t say using
execimmediately makes you a bad dev, it’s just you have to be mindful of exactly all the security risks you’ve mentioned.Because you’re crossing the boundary of a visitor on a website running a PHP script, to that PHP script that interacting with the host machine, the general consensus is to use an alternative solution if possible before dropping down the executing arbitrary commands on your host machine.
6
u/quinnr Apr 20 '26
Be mindful (in addition to the comments of others on the general use of exec) that there may be weaknesses or exploits in the tool you drop into with exec. For example, old versions of ExifTool on MacOS appear to have a remote code execution exploit that can be snuck through certain .png or .jpg files.
6
u/-PM_me_your_recipes Apr 20 '26
Based on your post, I assume exec was your last resort, and you seem to recognize the potential impact. So no, not a bad dev, just a bad situation.
I'm very risk adverse. So I'd probably just toss them in a queue and make a cron process them via bash script or something. That or a dedicated API only server, away from my main system, to handle it and return the data, plus it would have its own resources.
Overkill? Probably. But like you, using exec in a real world system goes against everything we've been told the past 15+ years and makes me deeply uncomfortable.
1
u/Basic_Reporter9579 Apr 20 '26
As for deleting location data, could you just copy the content into a new file and skip the meta data?
An alternative is to use another small server with a language that can run that natively and connect via an api.
1
u/obstreperous_troll Apr 21 '26
Using
execjust means you have a sort of hybrid PHP program and shell script. That can be just fine, though if you need more control over your process, I'd encourage you to use something like symfony/process rather than raw exec.I don't recommend directly exposing code that executes external processes through a web endpoint: even a small amount of bursty traffic can knock the server over if the processes start hanging for any reason. You're better off running those in a queued job of some sort, since it'll just tie up one of a finite number of workers.
1
u/mikkolukas Apr 21 '26
Can't you just strip the data using a PHP package?
1
u/AshleyJSheridan Apr 21 '26
Have you looked at Image Magick, which has Exif methods for PHP: https://www.php.net/manual/en/imagick.getimageproperty.php
2
u/Sou_fdc Apr 20 '26
Still the thing if 2 months I had to use to run an external app, you have to be very careful with this function because you are giving access to your server’s terminal. If you’re careful, it will go well.
With great powers comes great responsibilities
1
u/MartinMystikJonas Apr 21 '26
Exec is dangerous because it is easy to miss something and allow RCE vulnerability. So it should not be used if not necessary and if you are not totally sure what yiu are doing. But you use cases seems totally valid and you are cautious about security.
1
u/phpMartian Apr 21 '26
The issue with exec is that it starts a whole new process. This is expensive in any operating system. Your solution will have issues scaling.
1
u/GreenRangerOfHyrule Apr 22 '26
Oof. I'm a bit late to the discussion. But as others have said. Make sure you protect the code.
Using things like exec/eval or the equivalent is one of those things that should be avoided. But there are times it is needed. I have a tool in Python that runs a bit of code. That bit of code determines what function gets called. Now the problem is that in theory the list of functions change. And instead of writing a long series of code to check for the version and what functions exist, it generates the call using something like that.
Is it the best way? Probably not. But if it works and more importantly doesn't open you to vulnerabilities, then why not? If you can find a better/safer way, I would recommend switching the code. But those features exist for a reasons. Just use them wisely!
-1
u/linkme99 Apr 21 '26
No if you are making money.
-2
u/This_Math_7337 Apr 20 '26
YOLO. who cares if you use it.
-2
u/isoAntti Apr 21 '26
If you got spare time you can also have a look on writing a php class to strip location data with the help of Gemini. It shouldn't be too difficult and an easy exercise for G.
e.g.