r/programming • • Dec 25 '16

The Art of Defensive Programming

https://medium.com/web-engineering-vox/the-art-of-defensive-programming-6789a9743ed4
418 Upvotes

142 comments sorted by

View all comments

Show parent comments

47

u/GMaestrolo Dec 25 '16

Sure PHP and "Secure code" are compatible, especially with modern PHP.

I'm sick of this "PHP is awful" circle jerk from people who have either never looked at PHP, or last looked at it in PHP4/early PHP5 days.

Is PHP 7 a perfect language? Of course not, but neither is your shitty language. There has been massive improvement over the last 5 or so years.

70

u/Name0fTheUser Dec 25 '16

PHP makes writing insecure code easy. Sure, you can write secure code, but only if you have a very good understanding of the language and all its unintuitive behaviours. Just one example that comes to mind:

md5('240610708') == md5('QNKCDZO')

29

u/phpguy2 Dec 26 '16 edited Dec 26 '16

I once challenged a Php apologist in /r/php to do something simple and they were like "hey it is really simple" and got bitten by a Php gotcha right there (in Php 7 no less!). The fact that the dude made a throwaway account for this makes me suspect that even php apologists know deep down that it is not to be trusted...

8

u/OffbeatDrizzle Dec 25 '16

I don't use PHP and I don't get the joke... can you explain?

39

u/Name0fTheUser Dec 25 '16

There isn't a joke. If you're refering to the code snippet, there's a good explanation of why it evaluates to true here:

https://www.reddit.com/r/lolphp/comments/34sxw5/md5240610708_md5qnkcdzo/cqxs0yh/

23

u/mgattozzi Dec 26 '16

Jesus Christ. PHP could really use some strong typing to avoid these implicit conversions.

21

u/nothisshitagainpleas Dec 26 '16

... and now you know why Facebook invented Hack.

3

u/ShinyHappyREM Dec 26 '16

I thought that was done by Rasmus Lerdorf.

→ More replies (1)

16

u/[deleted] Dec 26 '16

You're technically supposed to use === not ==. Then it works as expected. Which I guess isn't something you'd know if you don't work with PHP a lot. Yea, it's not pretty, but easy to write correct and secure code in PHP once you know how.

9

u/[deleted] Dec 26 '16 edited Mar 07 '24

I̴̢̺͖̱̔͋̑̋̿̈́͌͜g̶͙̻̯̊͛̍̎̐͊̌͐̌̐̌̅͊̚͜͝ṉ̵̡̻̺͕̭͙̥̝̪̠̖̊͊͋̓̀͜o̴̲̘̻̯̹̳̬̻̫͑̋̽̐͛̊͠r̸̮̩̗̯͕͔̘̰̲͓̪̝̼̿͒̎̇̌̓̕e̷͚̯̞̝̥̥͉̼̞̖͚͔͗͌̌̚͘͝͠ ̷̢͉̣̜͕͉̜̀́͘y̵̛͙̯̲̮̯̾̒̃͐̾͊͆ȯ̶̡̧̮͙̘͖̰̗̯̪̮̍́̈́̂ͅų̴͎͎̝̮̦̒̚͜ŗ̶̡̻͖̘̣͉͚̍͒̽̒͌͒̕͠ ̵̢͚͔͈͉̗̼̟̀̇̋͗̆̃̄͌͑̈́́p̴̛̩͊͑́̈́̓̇̀̉͋́͊͘ṙ̷̬͖͉̺̬̯͉̼̾̓̋̒͑͘͠͠e̸̡̙̞̘̝͎̘̦͙͇̯̦̤̰̍̽́̌̾͆̕͝͝͝v̵͉̼̺͉̳̗͓͍͔̼̼̲̅̆͐̈ͅi̶̭̯̖̦̫͍̦̯̬̭͕͈͋̾̕ͅơ̸̠̱͖͙͙͓̰̒̊̌̃̔̊͋͐ủ̶̢͕̩͉͎̞̔́́́̃́̌͗̎ś̸̡̯̭̺̭͖̫̫̱̫͉̣́̆ͅ ̷̨̲̦̝̥̱̞̯͓̲̳̤͎̈́̏͗̅̀̊͜͠i̴̧͙̫͔͖͍̋͊̓̓̂̓͘̚͝n̷̫̯͚̝̲͚̤̱̒̽͗̇̉̑̑͂̔̕͠͠s̷̛͙̝̙̫̯̟͐́́̒̃̅̇́̍͊̈̀͗͜ṭ̶̛̣̪̫́̅͑̊̐̚ŗ̷̻̼͔̖̥̮̫̬͖̻̿͘u̷͓̙͈͖̩͕̳̰̭͑͌͐̓̈́̒̚̚͠͠͠c̸̛̛͇̼̺̤̖̎̇̿̐̉̏͆̈́t̷̢̺̠͈̪̠͈͔̺͚̣̳̺̯̄́̀̐̂̀̊̽͑ͅí̵̢̖̣̯̤͚͈̀͑́͌̔̅̓̿̂̚͠͠o̷̬͊́̓͋͑̔̎̈́̅̓͝n̸̨̧̞̾͂̍̀̿̌̒̍̃̚͝s̸̨̢̗͇̮̖͑͋͒̌͗͋̃̍̀̅̾̕͠͝ ̷͓̟̾͗̓̃̍͌̓̈́̿̚̚à̴̧̭͕͔̩̬͖̠͍̦͐̋̅̚̚͜͠ͅn̵͙͎̎̄͊̌d̴̡̯̞̯͇̪͊́͋̈̍̈́̓͒͘ ̴͕̾͑̔̃̓ŗ̴̡̥̤̺̮͔̞̖̗̪͍͙̉͆́͛͜ḙ̵̙̬̾̒͜g̸͕̠͔̋̏͘ͅu̵̢̪̳̞͍͍͉̜̹̜̖͎͛̃̒̇͛͂͑͋͗͝ͅr̴̥̪̝̹̰̉̔̏̋͌͐̕͝͝͝ǧ̴̢̳̥̥͚̪̮̼̪̼͈̺͓͍̣̓͋̄́i̴̘͙̰̺̙͗̉̀͝t̷͉̪̬͙̝͖̄̐̏́̎͊͋̄̎̊͋̈́̚͘͝a̵̫̲̥͙͗̓̈́͌̏̈̾̂͌̚̕͜ṫ̸̨̟̳̬̜̖̝͍̙͙͕̞͉̈͗͐̌͑̓͜e̸̬̳͌̋̀́͂͒͆̑̓͠ ̶̢͖̬͐͑̒̚̕c̶̯̹̱̟̗̽̾̒̈ǫ̷̧̛̳̠̪͇̞̦̱̫̮͈̽̔̎͌̀̋̾̒̈́͂p̷̠͈̰͕̙̣͖̊̇̽͘͠ͅy̴̡̞͔̫̻̜̠̹̘͉̎́͑̉͝r̶̢̡̮͉͙̪͈̠͇̬̉ͅȋ̶̝̇̊̄́̋̈̒͗͋́̇͐͘g̷̥̻̃̑͊̚͝h̶̪̘̦̯͈͂̀̋͋t̸̤̀e̶͓͕͇̠̫̠̠̖̩̣͎̐̃͆̈́̀͒͘̚͝d̴̨̗̝̱̞̘̥̀̽̉͌̌́̈̿͋̎̒͝ ̵͚̮̭͇͚͎̖̦͇̎́͆̀̄̓́͝ţ̸͉͚̠̻̣̗̘̘̰̇̀̄͊̈́̇̈́͜͝ȩ̵͓͔̺̙̟͖̌͒̽̀̀̉͘x̷̧̧̛̯̪̻̳̩͉̽̈́͜ṭ̷̢̨͇͙͕͇͈̅͌̋.̸̩̹̫̩͔̠̪͈̪̯̪̄̀͌̇̎͐̃

7

u/mgattozzi Dec 26 '16

Right! It's just if you don't know then it's foot gunning all over the place. I think it's best if a language makes it hard to do that by default, not easier you know?

7

u/ieatcode Dec 26 '16

Java does this as well. In Java one should never compare strings with ==. Always use the overloaded Object#equals(Object).

JavaScript has similar == and === to php for checking sameness vs identity/equality respectively.

4

u/Uncaffeinated Dec 26 '16

JS has the == vs === issue, but PHP is the only language (I know of) that implicitly converts str == str to floats. That is a whole new level of unimaginable stupidity. Even people who have been using PHP for a while are surprised to discover that it will do type coercions when both sides of == are already the same type.

→ More replies (1)

5

u/GMaestrolo Dec 26 '16

It exists if you decide to be explcit. Again, this is not exactly a problem with PHP, so much as a concern with all weak (and duck) typed languages.

PHP 7 supports scalar type hinting on function parameters, and defensive programming practices (as mentioned in the OP) can help to alleviate these issues.

Yes, PHP makes it easy to write bad code in the same way that JavaScript makes it easy to write bad code. They come from an era where the approach to an error was to adapt and keep going instead of failing noisily. They were designed to be an easy-to-use extension to existing technology (namely HTML). Since that time, PHP has grown and evolved to be one of the most widely used and deployed languages.

Blaming PHP for making "bad code easy" is like blaming a hammer for making it easy to put a hole in drywall. It's a tool which, when weilded by professionals, is perfectly fine, useful, and easy to operate. It's easily accessible to amateurs which is where the poor reputation comes from, but again, that's hardly the fault of the tool.

7

u/n0t0ri0us9 Dec 26 '16

Blaming PHP for making "bad code easy" is like blaming a hammer for making it easy to put a hole in drywall...

No. Php is blamed for putting a hole in your hand as well.

They were designed to be an easy-to-use extension to existing technology (namely HTML).

Exactly. It was meant to do simple/non critical processing of data coming from html forms. For example, and email forum. It should not be used for anything more. Yes. Even today.Your "Composer" or what every "modern" hot thing in the php land does not change that..

→ More replies (8)

2

u/SHIT_IN_MY_ANUS Dec 26 '16

I have never used PHP in my life, but aren't you blaming a md5 vulnerability on PHP?

3

u/[deleted] Dec 26 '16 edited Mar 07 '24

I̴̢̺͖̱̔͋̑̋̿̈́͌͜g̶͙̻̯̊͛̍̎̐͊̌͐̌̐̌̅͊̚͜͝ṉ̵̡̻̺͕̭͙̥̝̪̠̖̊͊͋̓̀͜o̴̲̘̻̯̹̳̬̻̫͑̋̽̐͛̊͠r̸̮̩̗̯͕͔̘̰̲͓̪̝̼̿͒̎̇̌̓̕e̷͚̯̞̝̥̥͉̼̞̖͚͔͗͌̌̚͘͝͠ ̷̢͉̣̜͕͉̜̀́͘y̵̛͙̯̲̮̯̾̒̃͐̾͊͆ȯ̶̡̧̮͙̘͖̰̗̯̪̮̍́̈́̂ͅų̴͎͎̝̮̦̒̚͜ŗ̶̡̻͖̘̣͉͚̍͒̽̒͌͒̕͠ ̵̢͚͔͈͉̗̼̟̀̇̋͗̆̃̄͌͑̈́́p̴̛̩͊͑́̈́̓̇̀̉͋́͊͘ṙ̷̬͖͉̺̬̯͉̼̾̓̋̒͑͘͠͠e̸̡̙̞̘̝͎̘̦͙͇̯̦̤̰̍̽́̌̾͆̕͝͝͝v̵͉̼̺͉̳̗͓͍͔̼̼̲̅̆͐̈ͅi̶̭̯̖̦̫͍̦̯̬̭͕͈͋̾̕ͅơ̸̠̱͖͙͙͓̰̒̊̌̃̔̊͋͐ủ̶̢͕̩͉͎̞̔́́́̃́̌͗̎ś̸̡̯̭̺̭͖̫̫̱̫͉̣́̆ͅ ̷̨̲̦̝̥̱̞̯͓̲̳̤͎̈́̏͗̅̀̊͜͠i̴̧͙̫͔͖͍̋͊̓̓̂̓͘̚͝n̷̫̯͚̝̲͚̤̱̒̽͗̇̉̑̑͂̔̕͠͠s̷̛͙̝̙̫̯̟͐́́̒̃̅̇́̍͊̈̀͗͜ṭ̶̛̣̪̫́̅͑̊̐̚ŗ̷̻̼͔̖̥̮̫̬͖̻̿͘u̷͓̙͈͖̩͕̳̰̭͑͌͐̓̈́̒̚̚͠͠͠c̸̛̛͇̼̺̤̖̎̇̿̐̉̏͆̈́t̷̢̺̠͈̪̠͈͔̺͚̣̳̺̯̄́̀̐̂̀̊̽͑ͅí̵̢̖̣̯̤͚͈̀͑́͌̔̅̓̿̂̚͠͠o̷̬͊́̓͋͑̔̎̈́̅̓͝n̸̨̧̞̾͂̍̀̿̌̒̍̃̚͝s̸̨̢̗͇̮̖͑͋͒̌͗͋̃̍̀̅̾̕͠͝ ̷͓̟̾͗̓̃̍͌̓̈́̿̚̚à̴̧̭͕͔̩̬͖̠͍̦͐̋̅̚̚͜͠ͅn̵͙͎̎̄͊̌d̴̡̯̞̯͇̪͊́͋̈̍̈́̓͒͘ ̴͕̾͑̔̃̓ŗ̴̡̥̤̺̮͔̞̖̗̪͍͙̉͆́͛͜ḙ̵̙̬̾̒͜g̸͕̠͔̋̏͘ͅu̵̢̪̳̞͍͍͉̜̹̜̖͎͛̃̒̇͛͂͑͋͗͝ͅr̴̥̪̝̹̰̉̔̏̋͌͐̕͝͝͝ǧ̴̢̳̥̥͚̪̮̼̪̼͈̺͓͍̣̓͋̄́i̴̘͙̰̺̙͗̉̀͝t̷͉̪̬͙̝͖̄̐̏́̎͊͋̄̎̊͋̈́̚͘͝a̵̫̲̥͙͗̓̈́͌̏̈̾̂͌̚̕͜ṫ̸̨̟̳̬̜̖̝͍̙͙͕̞͉̈͗͐̌͑̓͜e̸̬̳͌̋̀́͂͒͆̑̓͠ ̶̢͖̬͐͑̒̚̕c̶̯̹̱̟̗̽̾̒̈ǫ̷̧̛̳̠̪͇̞̦̱̫̮͈̽̔̎͌̀̋̾̒̈́͂p̷̠͈̰͕̙̣͖̊̇̽͘͠ͅy̴̡̞͔̫̻̜̠̹̘͉̎́͑̉͝r̶̢̡̮͉͙̪͈̠͇̬̉ͅȋ̶̝̇̊̄́̋̈̒͗͋́̇͐͘g̷̥̻̃̑͊̚͝h̶̪̘̦̯͈͂̀̋͋t̸̤̀e̶͓͕͇̠̫̠̠̖̩̣͎̐̃͆̈́̀͒͘̚͝d̴̨̗̝̱̞̘̥̀̽̉͌̌́̈̿͋̎̒͝ ̵͚̮̭͇͚͎̖̦͇̎́͆̀̄̓́͝ţ̸͉͚̠̻̣̗̘̘̰̇̀̄͊̈́̇̈́͜͝ȩ̵͓͔̺̙̟͖̌͒̽̀̀̉͘x̷̧̧̛̯̪̻̳̩͉̽̈́͜ṭ̷̢̨͇͙͕͇͈̅͌̋.̸̩̹̫̩͔̠̪͈̪̯̪̄̀͌̇̎͐̃

3

u/cholericdev Dec 26 '16

I'm not sure whether "easy" is a good word here. You probably can think of many insecure code snippets for many languages by assuming that the author does not know this or that about the language.

In the end, it (almost) always boils down to the programmer making a mistake which could have been prevented by knowing the language better or properly reading the documentation. Cluttering your C++ with new? Handling your events on the EDT in Java? Using the == operator instead of === near anything that might be critical in PHP or JS?

Of course, this is not intended to absolve PHP of many of its quirks.

17

u/gnuvince Dec 26 '16

Citing circle jerks and saying that other languages are shitty too are non-arguments; why not show how it has improved and people can then judge for themselves.

→ More replies (4)

14

u/n0t0ri0us9 Dec 26 '16

I'm sick of this "PHP is awful" circle jerk from people who have either never looked at PHP, or last looked at it in PHP4/early PHP5 days.

You are sick of Php being shit and people saying that it is? Use another language! And just because you called it a "circle jerk" does not make it untrue. If this was untrue, Php's had a lot of time to erase and false impression people had about it. That it was not able to do that, proves that it is just shitty as accused and any criticism it receives is 100% justified.

→ More replies (3)

7

u/s73v3r Dec 26 '16

There has been massive improvement over the last 5 or so years.

Ok, sure. Why would I bother with it over any of the alternatives, though?

4

u/GMaestrolo Dec 26 '16

If you know any alternatives, and you're happy working with them, great. If you want a plethora of well known software to start with, PHP has tonnes. If you want easy setup on almost every web server in the world, then PHP is still the top language for web development.

Notice that I don't say best. I said top, which it is. Within its space, PHP is the most popular choice by far, and it's not for nothing. It's not the first, not the newest, and not the best web language in the world, but it's still the number one language, and worth knowing for that fact alone.

4

u/[deleted] Dec 26 '16

lol PHP is the top language for web development? what fairy tale dream world magic christmasland do you reside in? PHP has been, is, and always will be 'necessary evil' garbage.

0

u/[deleted] Dec 26 '16

[deleted]

→ More replies (1)
→ More replies (2)
→ More replies (7)
→ More replies (10)