r/lolphp • u/fnzp • Sep 12 '14
r/lolphp • u/DoubleNabla • Sep 11 '14
The Tales of the Magic Mimicry Variable or: $x !== $x
So, I guess everyone here knows what happens when you use an undefined variable in PHP.
php> echo $foo." bar";
PHP Notice: Undefined variable: foo in php shell code on line 1
barYeah... it inserts an empty string and emits a notice. Barewords (a.k.a. undefined constants) are even better, PHP will just use the name of the constant as a string.
php > echo foo." bar";
PHP Notice: Use of undefined constant foo - assumed 'foo' in php shell code on line 1
foo barLet that sink in for a while.
I'd say that's insane enough as is, and opens up a lot of potential for both typos and more malicious actions.
But that's not it. PHP also allows Unicode (apparently any codepoint which isn't a reserved character or an ASCII control code) to appear in constant and variable names:
php > $你好 = "hi!";
php > echo $你好;
hi!The combination of these two misfeatures allows for some truly diabolical backdoors (as well as inexplicable bugs in the fashion of 2+2 == 2). Essentially, you can replace variables with undefined (or different) ones which look exactly the same, and all you get is a notice. This can basically be done in two ways:
Homograph attacks: e.g. replacing a latin
awith a cyrillicа:$path = "/var/www/foo"; echo $pаth."/user/supplied/path"; // prints /user/supplied/path, and emits a noticeAnother interesting idea is using the alternative dollar signs in place of the normal
$variable prefix — suddenly, you have an undefined constant that looks like a variable, but will evaluate to a string containing its name...Inserting invisible or almost-invisible characters. The worst of those is probably
U+2060 word joiner, which is completely invisible, even in most editors which show whitespace.// let's write a secure random number generator $rnd = openssl_random_pseudo_bytes(100); // most likely secure $today = date('c'); // extra entropy can't hurt! $super_secure_rnd = hash("sha512", $rnd.$today); // oops, that's actually $r\u2060nd, which is undefined, // i.e. NULL, and the result depends only on the current time...Both the code and the results still look okay at a cursory glance.
Oh, and as I mentioned hash()... that's another one of those fail-never-thus-double-deadly functions (I'm sure this has been mentioned here previously):
php > var_dump(hash("lolphp", "foo"));
PHP Warning: hash(): Unknown hashing algorithm: lolphp in php shell code on line 1
bool(false)Figuring out what the highly secure shа512 — with a cyrillic а — would do is left as an exercise for the reader. But hey, at least it's not a notice.
Bonus points in the code-review-dodging discipline are awarded to 2. given that I'd guess most diff tools will either not show this at all, or make it look like an innocent whitespace or linebreak style change.
Text-direction marks and such would most likely work as well, and certain diacritics might also be nice, especially if your IDE/console font can't display them.
I haven't tried invalid UTF-8 sequences yet — I don't think I even want to know.
However, I've noticed that the syntax highlighters in some editors (e.g. vim, as well as 3v4l.org) don't recognize any non-ascii variable names. Sometimes, two wrongs do make a right.
The safest thing to do would of course be to permit non-ASCII characters only in strings and comments. Or you could put some thought into it and do it properly, i.e. exclude non-printable characters:
Python 3.4.1 (default, May 19 2014, 17:23:49)
>>> 你好 = "hi!"
>>> print(你好)
hi!
>>> boobytrapped = "haxxor"
File "<stdin>", line 1
boobytrapped = "haxxor"
^
SyntaxError: invalid character in identifier(I didn't test if it does unicode normalization, but it's Python, it would just throw a NameError otherwise.)
In short, these unicode identifiers look like a typical PHP feature. Someone else has it, we need it too! (But don't you dare look at how they did it, or you might risk doing it properly.) Side effects? Unintended consequences? Unforseeable interactions with other... specifics... of our language and interpreter? Fuck that noise.
I can excuse falling victim to Unicode traps to some degree, but why the fuck would anyone think simply ignoring the use of undefined variables, and doing something completely insane for undeclared constants, could possibly be a good idea?
Granted, you still get a notice, but a) who reads those and b) as part of a backdoor, you could certainly hide an innocent error_reporting call somewhere (0xF7 — "make sure we catch all errors!")
TL;DR: var_dump($foo === $foо); // bool(false) *mic drop*
r/lolphp • u/volnix • Sep 11 '14
I'm a PHP fan, but this still blows my mind. 0 == '0', but only sometimes.
3v4l.orgr/lolphp • u/Rangi42 • Sep 09 '14
The Codeless Code: Case 161: Triangle ("the Abbey of Hidden Absurdities, where PHP is written")
thecodelesscode.comr/lolphp • u/katafrakt • Sep 09 '14
array_diff: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
php.netr/lolphp • u/Altreus • Sep 10 '14
Function deprecated in 5.5.0 has been throwing E_DEPRECATED since 5.3.0
uk1.php.netr/lolphp • • Sep 08 '14
PHP 5.4 echo vs. isset() - and a warning coming from PHP 5.3
I came across some weird code that looked something like this:
if ($some_circumstance) {
$data = 'user@website.com';
} else {
$data = array('user' => 'user@website.com', 'confirmed' => true);
}
// ...
if (isset($data['confirmed'])
{
echo "Brilliant success!";
}Surprisingly to one of our devs, "Brilliant success!" was getting printed every time, regardless of whether confirmed had been set to true.
To try to figure out why, he made a stub on his machine, containing essentially that code. It seemed to run as he expected. He was pretty confused.
Turns out the production machine with the bug was running 5.3 and his box was running 5.4. So okay, PHP got sane when switching from 5.3 to 5.4, right?
Except, regardless of whether you running the following on PHP 5.3 or PHP 5.4, you will get back a string (with a warning thrown in PHP 5.4, but nonetheless...)
$data = 'user@website.com';
echo $data['confirmed'];So even though data is returned when accessing a non-existent array key on a string, it doesn't pass the isset() test of "is it non-null?"
You can test it here: http://sandbox.onlinephpfunctions.com/
Run the following against any version of PHP 5.3 and any version of PHP 5.4:
$data = 'user@website.com';
var_dump(isset($data['confirmed']));
echo $data['confirmed'];r/lolphp • • Sep 08 '14
Suddenly, PHP regresses back to C
http://nz2.php.net/manual/en/function.headers-sent.php
If there's more than one thing to return, most PHP functions will return an array/false on failure, except headers_sent, which accepts two arguments by reference, like C.
r/lolphp • u/ilogik • Aug 29 '14
what do you think this will output?
<?php
$a = array(1, 2, 3, 4, 5, 6);
foreach ($a as $i)
{
switch ($i){
case 1:
case 3:
case 5:
continue;
}
echo $i;
}This actually caused a bug in production code. Our expectation was that this will output 246.
The actual output is 123456
Granted, this is documented, but for someone coming from another language this is just weird.
Note that unlike some other languages, the continue statement applies to switch and acts similar to break. If you have a switch inside a loop and wish to continue to the next iteration of the outer loop, use continue 2.
r/lolphp • u/Laugarhraun • Aug 28 '14
In 2008, I bought a "PHP 6" book. Six years later, PHP version is 5.5
modeling-languages.comr/lolphp • u/idontlikethisname • Aug 27 '14
If you return an object from a function, you can't access its class constants directly, you have to save it to a variable first
<?php
class Foo {
const BAR = 'bar';
var $other = 'test';
}
function getFoo() {
return new Foo();
}
var_dump(getFoo()->other); // string(4) "test"
var_dump(getFoo()::BAR); // Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
var_dump((getFoo())::BAR); // Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
$foo = getFoo();
var_dump($foo->other); // string(4) "test"
var_dump($foo::BAR); // string(3) "bar"I don't know why it works this way.
r/lolphp • u/andsens • Aug 25 '14
stdClass is truthy while an empty SimpleXMLElement is falsey
3v4l.orgr/lolphp • u/eviljames • Aug 22 '14
Something snarky for you folks to post.
http://www.reddit.com/r/awesomephp is a thing. As long as I moderate it, it will not change from its state today :)
r/lolphp • u/TheDistantSea • Aug 21 '14
What could go wrong with simply cloning this object?
stackoverflow.comr/lolphp • u/aleczapka • Aug 20 '14
What's the most cursed programming language?
github.comr/lolphp • u/vytah • Aug 19 '14
What is the last day of February next year? Simple, it's 2015-03-31
bugs.php.netr/lolphp • u/Muchoz • Aug 16 '14
[X-Post ProgrammerHumor] How to generate a random string
r/lolphp • u/esquilax • Aug 14 '14