phpsafe 𝕏 GitHub
Official CA 421DTkQ3TSG3n7F9SkNZj9NxvdFGbypwkctPd2uupump This is the only official address.

File access from user input

Severity: High CWE-22 OWASP A01:2021 Broken Access Control rule: path

Why it matters

Sequences such as ../ let a request escape the intended folder and read configuration files, keys or source code.

βœ• Vulnerable
readfile('/var/files/' . $_GET['file']);
βœ“ Secure
$base = realpath('/var/files');
$path = realpath($base . '/' . basename($_GET['file'] ?? ''));
if ($path === false || !str_starts_with($path, $base . '/')) {
    http_response_code(404);
    exit;
}
readfile($path);

How to fix

Use basename() and a fixed directory, then confirm with realpath() that the file stays inside it.

Scan my code

Paste a file and PHP Safe points to every line that matches this rule.

Scan my code β†’