File inclusion path manipulation

File inclusion path manipulation

Description

Using unvalidated input as an include or require path may expose files or execute unintended PHP code. The files that can be included and the impact depend on file permissions, remote inclusion settings and other configuration.

Potential impact

Sensitive files may be exposed, authentication may be bypassed, or unintended PHP code may run.

Remediation

Fix includable files in a key-based allow-list. Do not use input directly as a path string.

Examples

Before

php
<?php
$file = $_GET['page'];
include($file);

After

php
<?php
$pages = [
    'home' => __DIR__ . '/pages/home.php',
    'help' => __DIR__ . '/pages/help.php',
];

$page = $_GET['page'] ?? 'home';
if (!isset($pages[$page])) {
    http_response_code(404);
    exit;
}

include $pages[$page];

The server includes only the files mapped to home and help. It uses home when page is missing and returns 404 for an unknown key.

References