安全问题.PHP 窗口位置


Security issues. PHP window.location

>我创建了这个应用程序,它从数据库的给定列表创建一个csv文件,并允许用户下载它!当我完成工作时,我意识到存在安全问题!

这是我用来发出请求的ajas:

$.ajax({
            type: "POST",
            url:HTTPS + '/lib/model/data/ctlRates.php?export=1',
            data: {
                ext: ext,
                filter: filter,
                fileName: fileName
            },
            dataType: 'json',
            success: function(data){
                if(data['results'] == "success"){
                    console.log(data['results']);
                    var filename = data['filename'];
                    var extension = data['ext'];
                    window.location = HTTPS + '/lib/model/data/ctlRates.php download=1&filename='+filename+'&ext='+extension;
                } else {
                    console.log(data['results']);
                }
            }
        });
    } else {
        alert("Error: File name and extension must be provided");
    }
});

我想你们中的大多数人已经看到了问题!
但这是 Windows.Location 指向的控制器:

if(isset($_GET['download']) && $_GET['download'] == 1){
$filePath = "/path/to/file/dir";
$name = $_GET['filename'];
$extension = $_GET['ext'];
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Description: File Transfer');
header('Content-type: text/'.$extension.'');
header('Content-Disposition: attachment; filename="'.$name.".".$extension.'');
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
ob_clean();
flush();
readfile($filePath.$name.".".$extension);
}

开始测试并意识到每个人都可以随心所欲地结束文件名和扩展名,并从该目录中获取文件。有什么想法吗?

我想你们中的大多数人已经看到了问题!

不是真的 - 我很少有安全问题 javascript - 这不会表现出任何问题 - 问题是你如何处理你的数据服务器端。不要尝试使用 javascript 更改来服务器安全漏洞。

如果要限制对特定目录树的访问...

if(isset($_GET['download']) && $_GET['download'] == 1){
    $filePath = "/path/to/file/dir";
    $fetchfrom = realpath($filepath . '/' 
        . /* basename( */ $name /* ) */ 
        . "." . $extension);
    if (substr($fetchfrom, 0, strlen($filepath))!=$filepath) {
         header("HTTP/1.0 404 Not Found");
         print "Directory traversal not allowed";
         exit;
    }