Last active
August 24, 2019 19:48
-
-
Save ziadoz/5649b3ff8533e09a546de403f3f28fc0 to your computer and use it in GitHub Desktop.
Prevent Variable Leakage In PHP Includes
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 | |
// Use a closure so nothing leaks out when included. | |
return (function () { | |
$array = ['foo', 'bar']; | |
foreach ($array as $string) { | |
// Some exciting logic. | |
} | |
return 'FOOBAR'; | |
})(); |
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 | |
// The standard leaky include. | |
$array = ['foo', 'bar']; | |
foreach ($array as $string) { | |
// Some exciting logic. | |
} | |
return 'FOOBAR'; |
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 | |
$array = ['foo', 'bar']; | |
foreach ($array as $string) { | |
// Some exciting logic. | |
} | |
unset($array, $string); // Unset variables so nothing leaks out when included. | |
return 'FOOBAR'; |
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 | |
$returned = include __DIR__ . '/include-me-unset.php'; | |
echo (isset($array) ? '$array variable is set' : '$array variable is not set') . PHP_EOL; | |
echo (isset($string) ? '$string variable is set' : '$string variable is not set') . PHP_EOL; | |
echo '$returned is ' . $returned . PHP_EOL; | |
$returned = include __DIR__ . '/include-me-func.php'; | |
echo (isset($array) ? '$array variable is set' : '$array variable is not set') . PHP_EOL; | |
echo (isset($string) ? '$string variable is set' : '$string variable is not set') . PHP_EOL; | |
echo '$returned is ' . $returned . PHP_EOL; | |
$returned = (function () { return include __DIR__ . '/include-me-leaky.php'; })(); | |
echo (isset($array) ? '$array variable is set' : '$array variable is not set') . PHP_EOL; | |
echo (isset($string) ? '$string variable is set' : '$string variable is not set') . PHP_EOL; | |
echo '$returned is ' . $returned . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment