Had no luck finding an answer on the internet so I figured I'd ask the experts. What are the security threats that come with downloading files using url query, for example a code like this:
if(isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != ""){
$file = urldecode($_SERVER['QUERY_STRING']);
if(file_exists("files/".$file)){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize("files/".$file));
ob_clean();
flush();
readfile("files/".$file);
exit;
}
}
would be vulnerable to query strings like "?..\..\..\any\file\here" but isn't that all? Can't this be filtered out by a single if?
$file = urldecode($_SERVER['QUERY_STRING']);
if(strpos($file, ".\\") !== false || strpos($file, "./") !== false){
echo "No you won't get my system files";
exit;
}
If there's no "upload" feature, a .htaccess file preventing access to \files\ directory and the "if strpos" then is this safe?
Edit: bad code example