r/programming • • Nov 25 '15

Don't use the OWASP PHPSec Crypto Library

https://gist.github.com/paragonie-scott/91893fdb18ee4d1a1b95
41 Upvotes

80 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Nov 25 '15

[deleted] — view deleted comment

27

u/heptara Nov 25 '15

It's much easier to write bad code in PHP than in most other languages, and its more common to not care about this in the community, leading to poor training for new developers.

If you know what you're doing, and care, then don't change - but you're not typical.

0

u/[deleted] Dec 08 '15

[deleted] — view deleted comment

1

u/sarciszewski Dec 08 '15

many developers start out with at 13 years old when making a website for their guild or whatever

I started out with PHP development when I was 12, because I started mucking around with RPG Maker 2000 and most of the people in that community was an asshole, so I set up my own space.

None of my early websites were secure. I learned this the hard way.

-1

u/sarciszewski Nov 25 '15

It's much easier to write bad code in PHP than in most other languages, and its more common to not care about this in the community, leading to poor training for new developers.

These are challenges that I believe need to be taken, not a reason to dismiss PHP entirely, which is what a lot of programmers and infosec people do. It's a shame, really.

→ More replies (9)

0

u/[deleted] Nov 25 '15

[deleted] — view deleted comment

14

u/terrkerr Nov 25 '15

PHP has very bad error-handling because it doesn't have any standard. When is f(X) an error or exceptional case? When it's FALSE? NULL? -1? Throws an exception? All of these can readily be true with even really commonly used and accepted PHP facilities.

Hell, htmlentities() returns the empty string if there's a problem with one of the characters you send it... despite the fact that the empty string is a perfectly valid output in non-error situations!

If I want to store the htmlentities() of an optional text-field in an HTML form, say, I can very easily write problematic code.

$escaped = htmlentities($_POST['the_text_box']);
if ($escape === '') report_error();

That's bad code. It'll catch an intentionally empty text input as being in error when it isn't. If I want an empty string to be allowed then I must do a check on the string first to make sure it isn't already empty instead of empty after passing through htmlentities because of an error.

If PHP settled on only reasonable error signalling? I wouldn't. Even NULL checking beats that handily, and exceptions beat NULL checking in most high-level contexts or at least are no more or less useful.

5

u/[deleted] Nov 25 '15

but it has its own fair share of warts.

Python is pretty good in the fact it has very very few warts. I can think of at most 1 actual 'wat' in the language. Care to give some examples?

0

u/[deleted] Nov 25 '15

[deleted] — view deleted comment

7

u/Schmittfried Nov 25 '15

No switch statement, not even one without fall-through.

No multi-loop break.

No real gotchas, just missing features.

x is y can mistakenly be confused for x == y due to implementation details.

Could you elaborate on that?

An integer divided by an integer returns an integer instead of a float.

That's the usual and expected behavior in every language I know. Values shouldn't just change types. If I want floats, I use floats.

Python 3 assumes unicode strings in a lot of inappropriate places.

For instance? Actually, nowadays people are annoyed when a language does not assume (or makes it hard to use) unicode and just uses ASCII for everything.

5

u/[deleted] Nov 25 '15 edited Nov 26 '15

Some of those are superficially valid.

Pretty much everything about the Python 2 to 3 upgrade debacle - breaking a lot of working code for not a lot of gain.

Debatable. The whole transition was horribly horribly botched, but Py3 is here and it's fine for most use cases. Unless you're doing scientific computing.

Two different types of classes (old style vs new style).

One kind.

Anonymous functions are limited to a single line.

They should be a single line. Otherwise it should be a method. This isn't JavaScript.

No switch statement, not even one without fall-through.

I've been writing Python for many many years and I've never missed it. You can almost always do it in a better way through use of duck typing, if/elseif/dictionaries. But yes, some people might miss it coming from other languages.

No multi-loop break.

Somewhat annoying in some specific cases. Makes up for it with the for/else + while/else construct.

x is y can mistakenly be confused for x == y due to implementation details.

What? You shouldn't really need to use is unless you're doing if x is False/True, or comparing class types. is and == are very different concepts. Unless your talking about the integer cache, in which case why are you using is to compare ints?

An integer divided by an integer returns an integer instead of a float.

Python 2 horribleness.

Python 3 assumes unicode strings in a lot of inappropriate places.

Python 3 is unicode everywhere and it's awesome, because assuming things are ascii leads to Python2's fantastic encoding issues.

So yeah, some warts in that list granted, but no real 'wats'. For some real life examples check out this talk.

→ More replies (4)

2

u/Schmittfried Nov 25 '15

I find JS definitely saner than PHP, especially regarding its type casts.

2

u/heptara Nov 26 '15

How would you rate the ecosystems on their tolerance of bad code or unsound standard practises?

How would you rate the severity of "warts" as leading towards bad code? One of the examples you gave, for example, anonymous functions being limited to a single line in Python, doesn't appear to affect code quality. it means you have an extra function with a name, and in fact, that probably improves your code. Multi-line lambdas are terrible for readability.

4

u/mekanikal_keyboard Nov 25 '15

You can write bad code in any language

this is the last refuge....that its possible to make anything as awful as php if you try hard enough

-8

u/sarciszewski Nov 25 '15

If you're so convinced that PHP is bad, hack paragonie.com.

Go ahead, I give you authorization to try, so CFAA violations won't be an issue. Do it. Hack me because I run PHP.

If you can't, at least admit that you're on shaky ground.

15

u/tdammers Nov 25 '15

The argument is not that it's impossible to write a reasonably secure web application with PHP. The argument is that it is much harder than in pretty much any other language. This is basically a variation of the common "it can be done" argument that is so popular among PHP apologists that it's not even funny anymore. Yes, it can be done in PHP, but it can be done better (by some metric) in everything else.

1

u/sarciszewski Nov 25 '15 edited Nov 25 '15

Look, my background is in infosec and cryptography. I catch more shit from my peers for trying to help the PHP community than you can imagine, and my standard retort is this:

80% of the Internet runs PHP, like it or not. Instead of telling people "you should use a different language because I like it more", I've opted to try to improve the language.

PHP 7 got a CSPRNG at least in part because of my efforts. 7.1 will have serious security improvements, and future iterations will improve.

Instead of saying "PHP is bad don't use it", I look for things that can be fixed in a future version of the language. But when I pressure people, all they do is bike-shed heavily about the type system (and completely ignore the changes coming in version 7).

Yes, it can be done in PHP, but it can be done better (by some metric) in everything else.

Just so you know, modern PHP encourages the use of shared code (e.g. through Composer). A lot of things that you suspect you have to go out of your way to make secure? Most developer just use a library to take care of those concerns for them.

7

u/coredumperror Nov 25 '15

Have you read this blog post? I found it to be a real eye-opener.

I don't know anything about PHP 7, though, so if you can point to a few issues brought up in that article that are fixed in that version of the language, I bet the author would appreciate being notified. He's made several notes about problems fixed in versions that came after he original posted it, after all.

2

u/[deleted] Nov 26 '15

Everyone has read that post. It's not relevant to getting work done. The core of that post is "It's not how I WANT IT so therefore it's wrong".

The most relevance it has to php is that it has php in the fucking title.

→ More replies (1)

2

u/sarciszewski Nov 25 '15 edited Nov 25 '15

Have you read this blog post? I found it to be a real eye-opener.

Yes, I've read it. The author raises a lot of good points and objects to a lot of faults in the language that the language designers should read and learn from. Sadly, it's mostly used by trolls who want to bully PHP programmers instead of put to any constructive use.

I don't know anything about PHP 7, though, so if you can point to a few issues brought up in that article that are fixed in that version of the language,

PHP 7 comes out soon.

I bet the author would appreciate being notified.

I don't think Eevee cares to update a blog post from 3 years ago just because I tell him PHP is less terrible now. (I follow him on Twitter.)

→ More replies (1)

3

u/1s4c Nov 26 '15

Instead of saying "PHP is bad don't use it", I look for things that can be fixed in a future version of the language.

PHP is here for a very long time and if you compare the "improvement" trend of PHP with it's competitions you realize how bad the situation is

and I don't even care about the language, but the underlying framework implementation is just terrible with insane "baggage" from the past

although I'm following PHP since version 3 I wouldn't recommend it to anyone unless there were some insanely specific reasons to use it

2

u/beerdude26 Nov 25 '15

A CSPRNG only in 2016. Good job, PHP. Real security-minded like

2

u/sarciszewski Nov 25 '15

A CSPRNG only in 2016. Good job, PHP. Real security-minded like

PHP has CSPRNG interfaces in 5.3+ or 5.x with the mcrypt extension, but it's clunky and unreliable (OpenSSL). On Unix-based OSes you can also read from /dev/urandom.

What PHP 7 offers is a simple and secure interface:

  • string random_bytes(int $howMany)
  • int random_int(int $min, int $max)

It also happens to leverage getrandom(2) if you're on a newer version of Linux.

If you want to use the same interface in PHP 5, I maintain random_compat.

But y'know what? I'm disappointed that it took this long to happen.

→ More replies (6)
→ More replies (16)

1

u/[deleted] Nov 25 '15

Considering how many of php builtins turned out be shit, only to be fixed... and still be shit....

Don't use PHP... you can never be sure

→ More replies (18)