Created
November 24, 2013 20:42
-
-
Save mintindeed/7632151 to your computer and use it in GitHub Desktop.
Unintended global scope access. Example shows that even if you don't intend for a variable to be global, any code (by you or a 3rd party) can gain access and have its way with it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function innocent_function() { | |
global $foo; | |
$foo = new Foobar($foo); | |
} | |
class Foobar { | |
public function __construct($original_value) { | |
echo 'I just did something evil.' . PHP_EOL; | |
$this->original_value = $original_value; | |
} | |
public function __toString() { | |
return $this->original_value; | |
} | |
} | |
//EOF |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$foo = 'bar'; | |
function foobar() { | |
global $foo; | |
$foo = 'foo' . $foo; | |
} | |
foobar(); | |
// Prints "foobar" | |
echo $foo . PHP_EOL; | |
// Include innocuous 3rd party library | |
include 'evil-include.php'; | |
innocent_function(); | |
// Does prints something evil and then prints original value of $foo | |
echo $foo . PHP_EOL; | |
//EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment