I have a slight problem, I am trying to create a simple security check in my PHP application.
Let's assume for the argument's sake that I have the following folder structure:
website home (httpdocs)
|_file_1
|_file_2
|
|_DIR_1
| |_file_3
| |_file_4
|
|_DIR_2
| |_something.php
| |_file_5
|
|_public
|_index.php
In this example let's say that the user can visit the /DIR_2/something.php
(though the request will be routed through /public/index.php
), and can also provide an argument in the url such as ?get_file=file_5
. Normally only DIR_2
is publicly accessible and the router which is inside public/index.php
would handle the file reading and return the output with correct headers.
Now let's say that the user has provided the following url /DIR_2/something.php?get_file=../DIR_1/file_3
. the code will execute it without any problems but that a security risk since only DIR_2 should be accessible.
So, my question is: Is there a PHP function I could use to check if the path to a file (or dir) leads outside a specific directory, so something like
$filePath = $_GET['get_file'];
if(file_outside(__DIR__, $filePath)){
echo "file is outside the dir";
} else {
echo "file is inside the dir";
}
I've tried searching but unfortunately all results are about checking whether the path is to a file or a dir or whether the file exists or not, which is not what I need.
I am using php 8.0 in case it's important.