As zerkms has already pointed out , depending on the version of PHP, file_exists and include may be unsafe when handling NULL bytes . Only with PHP version 5.4.3, file system functions are considered NULL bytes .
So, you should check the value before using it, for example, using a white list of valid values:
$allowedPages = array(); if (in_array($_GET["page"], $allowedPages)) {
You can also expand this whitelist to any existing file under the root directory of the document:
if (strpos($_GET["page"], "\0") !== false) { // NULL byte detected } $path = realpath("pages/" . $_GET["page"] . ".php"); $base = realpath($_SERVER['DOCUMENT_ROOT']) . "/"; if ($path !== false && substr($path, 0, strlen($base)) === $base) { // allowed }
However, this can still be used to circumvent other access control measures, such as location-based HTTP authentication.
source share