Do not use user input to form file paths.
- Rule ID: php_lang_path_using_user_input
- Languages: php
- Source: path_using_user_input.yml
Description
Using raw unsanitized input when forming filenames or file paths is bad practice. It can lead to path manipulation, by which attackers can gain access to resources outside of the intended scope.
Remediations
❌ Avoid wherever possible
✅ Restrict the user input to known values
$allowed_filenames = array("resource-1", "resource-2");
$filename = $_GET["resource_name"];
if (in_array($filename, $allowed_filenames)) {
readfile("/files/${filename}");
} else {
// filename is unexpected
}
✅ Validate expected file paths
$path = realpath("/safe/prefix/" . $_GET["resource_name"]);
if (str_starts_with($path, "/safe/prefix/")) {
readfile($path);
} else {
// path is unexpected
}
Resources
Associated CWE
- CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
- CWE-73: External Control of File Name or Path
OWASP Top 10
Configuration
To skip this rule during a scan, use the following flag
bearer scan /path/to/your-project/ --skip-rule=php_lang_path_using_user_input
To run only this rule during a scan, use the following flag
bearer scan /path/to/your-project/ --only-rule=php_lang_path_using_user_input
Ready to take the next step? Learn more about Bearer Cloud.