Php $GET关于文件名和路径(安全相关)
今天下午我制作了一个简单的下载脚本,如下所示 基本上,我将Php $GET关于文件名和路径(安全相关),php,security,get,Php,Security,Get,今天下午我制作了一个简单的下载脚本,如下所示 基本上,我将file='whatever.something'传递给它,并将下载内容呈现给用户 后来我开始考虑安全问题。。。因为任何人都可以传递自己的变量,如果他们知道页面的url。。。我需要担心什么吗 是否有人可以传递类似于“../../../../../something的内容,并实际返回到我的/home/目录,在那里他们可以下载他们想要的任何文件 if(isset($_GET['file'])) { $file = '/home/tes
file='whatever.something'
传递给它,并将下载内容呈现给用户
后来我开始考虑安全问题。。。因为任何人都可以传递自己的变量,如果他们知道页面的url。。。我需要担心什么吗
是否有人可以传递类似于“../../../../../something
的内容,并实际返回到我的/home/
目录,在那里他们可以下载他们想要的任何文件
if(isset($_GET['file']))
{
$file = '/home/test/user-data/'.$_SESSION['user']['account_id'].'/downloads/'.$_GET['file'];
if(file_exists($file))
{
//get current ts
$now = time();
//set headers
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$now.'_file.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($file).'');
//download
readfile($file);
//delete it
unlink($file);
}
}
最后,我做了一些如下的事情:
if(isset($_GET['file']))
{
// sanitize request and keep just the name and extension
$file_path = $_GET['file'];
$path_parts = pathinfo($file_path);
$file_name = $path_parts['basename'];
$file_ext = $path_parts['extension'];
// replaces the file location with a preset one
$file_path = '/home/test/db-backups/'.$file_name;
// make sure the file exists
if (is_file($file_path))
{
.....
这不仅是可能的,而且是一个时间问题(数小时或数天),直到有人尝试。。。有什么建议吗?除了为没有$GET的每个文件创建一个特定的文件之外,还有什么可以做的吗?您可以筛选出包含
的路径。