39 ms·
Of course you can write safe code in PHP, it is a complete general purpose programming language. But just like one can blame C's design and standard library for
by jeltz 10y ago
Of course you can write safe code in PHP, it is a complete general purpose programming language. But just like one can blame C's design and standard library for many security bugs I also think it is fair to blame PHP's standard library and design.
For example PHP's PDO library makes it less convenient to supply parameters to a query the safe way than it is to do it the unsafe way. This is not necessary as can be seen from the excellent Ruby library called Sequel where it is just as easy to do things the safe way.
Another example is PHP itself being a template language, but which lack of automatic escaping makes it very dangerous to use. Since just one missing escape call in the wrong place can fuck up your day. To be fair PHP is not the only one who has fucked up here. This is also an issue with Ruby's ERB templates in the standard library, but fortunately Rails has added automatic escaping to its ERB templates.
One important thing about designing safe software is to make it easy for the end users to do things the right way, and here I think PHP has done a pretty poor job.
- Prefinem 10y agoI would say it's unfair to compare a library to the basic PDO. If you want to compare apples to apples, try looking at Sequel versus Doctrine or some other third party SQL library. Just like any other template language (thinking of something like ColdFusion or ASP), of course one unescaped variable is going to be a headache. But you don't have to use it that way, and probably shouldn't. While PHP started as a template language, it has clearly moved past that.
- jeltz 10y agoI do not think it is an unfair comparison since Sequel has both basic parts on the same level as PDO, and then implements the more advanced features on top of these basic parts. The basic parts of Sequel has superior usability and safety to PDO. Sequel's right way: user = db['SELECT name FROM users WHERE id = ?', id].first Sequel's wrong way: user = db["SELECT name FROM users WHERE id = #{id}"].first PDO's right way: $stmt = $db->prepare('SELECT name FROM users WHERE id = ?'); $stmt->execute([$id]); $user = $stmt->fecth(); PDO's wrong way: $user = $db->query("SELECT name FROM users WHERE id = $id")->fetch(); As you can see PDO is optimized for doing things easily the unsafe way while Sequel has the safe way as the normal way of running queries which is just as easy to use as the unsafe way. > Just like any other template language (thinking of something like ColdFusion or ASP), of course one unescaped variable is going to be a headache. That is simply not true. Look at Twig which by default escapes everything automatically, or Rails's patched ERB which keeps track of escaped an unescaped strings based on data types and makes sure everything is automatically escaped in the end. In those template languages you need the explicitly do something unsafe to get an unescaped value. While in PHP you need to audit all places where echo can be called directly or indirectly.
- CiPHPerCoder 10y agoCompare Sequel to EasyDB then: https://github.com/paragonie/easydb https://github.com/paragonie/easydb
- jeltz 10y agoSure, I know there are good libraries for PHP, I mentioned Twig for example, but my issue is that the standard library of PHP is shitty and contains traps for beginners.
- Prefinem 10y agoHonest question, is Sequel a standard library for Ruby? I assumed it was something you had to install after you installed Ruby.
- Prefinem 10y agoOkay, so instead of Doctrine, look at another library that has the basic parts and then the advanced parts. Here is one from a different library that is safe and "easy" $user = $db->find('user', 'first_name = ?', ['Name']); > As you can see PDO is optimized for doing things easily the unsafe way while Sequel has the safe way as the normal way of running queries which is just as easy to use as the unsafe way. PDO isn't "optimized" to do them the wrong way or the right way. Yes, it's less code to do it the wrong way, but that isn't a true definition of easier. As for the escaping, I would expect Twig to escape things, it's want it's meant for (and the reason you use it instead of just vanilla PHP). And Rails is a subset of Ruby correct? So not a true comparison again.
- sbarre 10y agoMost languages let you cut corners and code in a sloppy way. This is not limited to PHP. I think anyone can show anecdotes to support "coding in [language you don't like] is bad". Replace "unsafe" with "non-performant" and go back to early Rails days and you'll have plenty of examples where naive framework implementation led to programmers shooting themselves in the foot with the "easy way" in a different context.. Like I said, all languages and frameworks have their caveats and learning to code properly is the developer's job, and the "right way" or "best way" is often a work in progress that evolves as the community develops best practices..
- jonknee 10y ago> Most languages let you cut corners and code in a sloppy way. This is not limited to PHP. Completely true, but for years PHP made it extraordinarily easy to shoot yourself in the foot right out of the box. register_globals for example... http://php.net/manual/en/security.globals.php http://php.net/manual/en/security.globals.php
- Revisor 10y agoDeprecated 9 years ago.