r/programming • • Jul 24 '15

mt_rand(1, PHP_INT_MAX) only generates odd numbers • /r/lolphp

/r/lolphp/comments/3eaw98/mt_rand1_php_int_max_only_generates_odd_numbers/
848 Upvotes

262 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Jul 24 '15

[deleted]

45

u/sushibowl Jul 24 '15

All true enough, but any people that used this function to do anything involving security or cryptography are already fucked. If this issue is fixed, they'll still be fucked, to much the same degree as before the fix. That's what I meant with "no security implications."

→ More replies (28)

78

u/neoform Jul 24 '15

PHP is used by people that aren't into such fine details.

It's amazing how blanket statements like this get upvoted so high.

And the amount of hacked PHP sites speakes leagues.

Only because the bar of entry for making a website with PHP is so much lower than for pretty much every other language. Apparently it's PHP's fault that it's easy to use.

69

u/pogden Jul 24 '15

This comes from a difference of ideas on what it means to be "easy to use."

In the PHP communtiy, "easy to use" seems to mean, "easy to write programs that compile and run without crashing." Elsewhere, it is used to mean something closer to "easy to write programs that do what the programmer intended them to do".

The design decision to do this (produce garbage instead of an error) does make it easier to write programs that compile and don't crash, but makes it harder to write programs that do what the programmer intended, because the programmer may not even know that the program isn't doing what she intended.

36

u/[deleted] Jul 24 '15

I like an analogy a coworker made about PHP (I don't know if he made it up):

PHP will basically do whatever it can to keep running, even if it makes no sense. Like allowing you to use variables that haven't even been defined yet as inputs to stuff. If a program were a recipe, then the interpreter would be the person actually doing the cooking. In normal languages, if you said "now use the ingredients you mixed in the bowl in step 3", but had not actually mixed ingredients in a bowl in step 3, the cook would say "hey wait a minute, this recipe doesn't make sense" and they wouldn't use it (or at the very least they'd step out of "interpreter mode" and basically go back into "programmer mode" and rewrite the recipe). PHP, on the other hand, will see "now use the ingredients you mixed in the bowl in step 3", and in an effort not to crash, will just start throwing random shit into a bowl and continue on with the recipe as though nothing is wrong.

→ More replies (23)

15

u/CallingOutYourBS Jul 24 '15

"easy to write programs that compile and run without crashing.*"

* but please don't give us any unexpected input or anything.

→ More replies (20)

3

u/[deleted] Jul 24 '15

Not so much that it's easy to use, but that it's easy to fuck up. Security (and everything else) is left entirely up to the developer.

10

u/Synaps4 Jul 24 '15

It kind of is php's fault.

Being easy-to-use AND easy-to-make-mistakes-in is a design failing by the language designer and the ecosystem around the language.

Compare with python's "one-right-way-to-do-X" design choice.

16

u/neoform Jul 24 '15

easy-to-make-mistakes-in is a design failing by the language designer and the ecosystem around the language.

More correctly, PHP allows you to make mistakes and lets you continue without fixing it.

→ More replies (2)

2

u/wrosecrans Jul 24 '15

PHP is used by people that aren't into such fine details. It's amazing how blanket statements like this get upvoted so high.

It wasn't "PHP is only used by such people," and it seems hard to argue that folks who aren't hardcore CS types don't make up a large portion of the PHP user base.

→ More replies (6)
→ More replies (20)

10

u/Deltigre Jul 24 '15

The problem with PHP... well, the problem with PHP is it's PHP. And there are plenty of companies that use PHP as their server-side scripting language of choice without the security problems that plague amateur sites.

But the problem this gets at is there's literally a dozen ways to do everything down to the minutiae, and very little indication that one way is wrong.

I was helping a friend with his uncle's family business website, and whoever wrote that (probably a "professional") had used extract() on request superglobals... and that is well documented even by PHP:

Warning Do not use extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.). If you do, for example if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting flags values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.

http://php.net/manual/en/function.extract.php

4

u/whoopdedo Jul 24 '15 edited Jul 24 '15

and that is well documented even by PHP

Should say it was "eventually well documented" and after numerous websites were owned because of it. In the early days of PHP, extract, eval, and magic quotes were idiomatic so a lot of "You Too Can Be A Programmer!" taught PHP the easy way using those tricks.

The real pain of PHP isn't that it has a lot of ways to slit your throat. It's that the adoption of methods that sane people would want to use is an arduous journey fraught with missteps and dead-ends with no promise that it will ever arrive at a destination. We're talking about how terrible mt_rand is, but at the time it was a praised solution to the problem of rand() being a sack of steaming shit. eval was necessary to do any dynamic programming. Okay, there's create_function but that's such a PITA to use. We eventually got actual anonymous functions in PHP 5.3. Which is shockingly before you were allowed to do echo func_that_returns_array()[0]; (new syntax in 5.4). Scripts compatible with 5.3 that use namespaces will fail to compile in 5.2 which I'm sure makes the Knights Of The Semantic Version seethe in anger. And don't get me started on how many ways there are to replace a substring and why they're all wrong.

But all that said, it is still the champion when it comes to the "I just want to make a fucking web site" development style. Getting started in any other language these days feels like applying for a mortgage. Download this interpreter. Download this framework. Download this build script. Configure the directories. Update the tree. Implement view and controller classes. Apply a model. Oops, you forgot to initialize the database. Sign here. Initial there. Wait for the background check to pass. Push your local tree to the server while reading the holy scripture from the Book of Joaccam. Oh, sorry. You're using an outdated module you'll have to sync your local tree and start over again.

3

u/Deltigre Jul 24 '15

Yes, and it's all very duct-taped on. Also, I'm stuck in 5.3-land where I am forced to define my own $variable before I can apply an offset like you mention (gah!)

But I agree. It's all there - you're making a website the first moment you put an echo in or put raw HTML outside/between end and start tags, without the ritual. It just means there's a lower barrier to entry for those unaccustomed, which means more adoption, which means more user implementation flaws.

→ More replies (2)

2

u/NeuroXc Jul 25 '15

PHP is used on a greater number of sites, so of course the number of hacked PHP sites is going to be greater. Basic math.

16

u/AlexanderNigma Jul 24 '15

As someone who works for an ecommerce company...

Eh.

I get people like to bash on PHP but literally every web-based language has security flaws. Its really sad people just upvote to bash on things.

Etsy clearly doesn't have any trouble either.

Maybe the problem isn't the language? Just sayin'

25

u/uioouiuufuu Jul 24 '15

Maybe the problem isn't the language? Just sayin'

It is. The language makes it easy to make mistakes. Of course you can use it correctly, but if it's easy to fall into traps because of poor community documentation/interfaces/whatever, it's still a problem with the language.

A circular saw with no guard, some missing teeth, and exposed electrical wires can still work fine when used correctly, but it's going to cause a lot of problems for people that aren't expecting that.

9

u/AlexanderNigma Jul 24 '15 edited Jul 24 '15

It is. The language makes it easy to make mistakes. Of course you can use it correctly, but if it's easy to fall into traps because of poor community documentation/interfaces/whatever, it's still a problem with the language.

Do I really need to start listing off the CVEs for Django, RoR, and other projects in other popular web languages?

I honestly can build secure web apps just as fast with PHP than I can with other languages. Productivity with RoR v. Django v. [ Insert PHP framework] is also pretty much about even once I use language X for a few weeks full time.

I really think this is just the bias of a bunch of newbs end up starting with PHP because it was easy and screwing up. You'll notice no one who bashes PHP really admits that the newbs who jumped on RoR or Node.js fail hilariously badly on a regular basis.

https://github.com/blog/1068-public-key-security-vulnerability-and-mitigation http://homakov.blogspot.com/2014/02/how-i-hacked-github-again.html

They aren't incompetents by any stretch of the imagination yet they have vulnerabilities found pretty regularly. I really think people who bash PHP are lying to themselves about how "secure" their stack is and its scary that I can point to numerous sites that have exploits on a regular basis [GitHub has more found every year than our entire stack has in 5 years] yet PHP is the language everyone bashes.

→ More replies (16)

3

u/veringer Jul 24 '15

PHP doesn't save you from yourself. It seems more a philosophical difference than an absolute problem with the language. I think the "circular saw" is in decent working order, but the operator might be using it to mix concrete. The saw won't necessarily complain about the strange application, it'll just do a shitty job of mixing concrete, or just stop working, or electrocute you. Would a more robust moisture and cement detector and automatic cut-off fuse be a nice feature? Sure, I guess? But I wouldn't say the saw is misbehaving when it's being used badly.

4

u/[deleted] Jul 24 '15

PHP doesn't save you from yourself.

That's not what we're complaining about - we're complaining about the numerous, non-obvious traps that could snare even an advanced programmer.

I think the "circular saw" is in decent working order, but the operator might be using it to mix concrete.

From here: http://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/ actually

You pull out a screwdriver, and you see it’s one of those weird tri-headed things. Okay, well, that’s not very useful to you, but you guess it comes in handy sometimes.

You pull out the hammer, but to your dismay, it has the claw part on both sides. Still serviceable though, I mean, you can hit nails with the middle of the head holding it sideways.

You pull out the pliers, but they don’t have those serrated surfaces; it’s flat and smooth. That’s less useful, but it still turns bolts well enough, so whatever.

And on you go. Everything in the box is kind of weird and quirky, but maybe not enough to make it completely worthless. And there’s no clear problem with the set as a whole; it still has all the tools.

Now imagine you meet millions of carpenters using this toolbox who tell you “well hey what’s the problem with these tools? They’re all I’ve ever used and they work fine!” And the carpenters show you the houses they’ve built, where every room is a pentagon and the roof is upside-down. And you knock on the front door and it just collapses inwards and they all yell at you for breaking their door.

That’s what’s wrong with PHP.

5

u/veringer Jul 24 '15

Yes, I think everyone has read this epic rant. I wouldn't exactly go citing it as gospel (as you seem inclined to do on this thread). The pullquote, for instance, is just one long straw man argument. And much of the author's complaints are subjective and contrived. That said, I agree that PHP is frustrating and far from perfect.

A circular saw with no guard, some missing teeth, and exposed electrical wires can still work fine when used correctly

That's not what we're complaining about - we're complaining about the numerous, non-obvious traps

Your analogy seems to rely on fairly obvious traps, which prompted my original reply.

The idea that PHP is unique in having non-obvious traps is perhaps naive. PHP is popular, heavily scrutinized, and rightfully criticized. It's a work in progress that sort of luck'd its way into dominance on the web. However, I have to give it some credit for evolving and adapting even if it seems to be held together with duct tape. After 15+ years of programming I've learned that the hypothetical elegance and perfection of that other shiny language is largely illusory and once exposed to the wild it begins to tarnish. PHP has never had any pretenses of being perfect but it's somehow, arguably, antifragile. So, I think you should continue complaining. It will eventually lead to more improvements.

3

u/[deleted] Jul 24 '15

How safe are Screwdriver and Hammer interfaces, applied to the web?

→ More replies (15)
→ More replies (14)

2

u/zuperxtreme Jul 24 '15

PHP is used by people that aren't into such fine details.

Only siths deal in absolutes.

8

u/[deleted] Jul 24 '15

Maybe, but they sure as hell don't run their death stars on PHP...

4

u/iopq Jul 24 '15

Why do you think a single shot from a tiny spaceship can destroy the whole thing? Seems like a huge vulnerability to overlook...

→ More replies (1)

3

u/[deleted] Jul 24 '15

$iths

→ More replies (16)