簡體   English   中英

如何防止用戶使用用於在 Web 應用程序中下載 pdf 文件的 PHP 下載選項下載源代碼文件?

[英]How to prevent users from downloading source code files using the PHP download option I have for downloading pdf files in my web application?

下載.php

<?php 
$file = $_GET['file'];
if(file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>

以下是我用來將 url 傳遞給 php 腳本的 JavaScript 代碼: Download.js

init:function(){
    this.control({
        '#downloadSite': {
            load:function(tree, node, records, successful, eOpts)
            {

            },

            itemclick:  function(tree, record, item, index)
            {
                if(record.get('id') == 300){
                    window.open('Download.php?file=../TAB/'+record.get('url'));
                }
                else{
                    window.open('Download.php?file=../PDF/'+record.get('url'));
                }                   
            },
            beforeitemclick:  function(tree, record, item, index)
            {
                if(record.get('leaf') == false) return false;
            },
            beforeitemdblclick: function(){
                return false;
            }
        }
    });
}

如果我在 url 中輸入“Download.php?file=../web.config”,則正在下載 web.config 文件。 我想阻止直接下載源代碼。 下載選項用於下載我存儲在主目錄中 pdf 文件夾中的 pdf 文件。

請幫忙 !!

您在這里遇到了一個非常糟糕的設計決策,它使您容易受到文件系統遍歷的影響

你可能會考慮:

  1. 確保請求的文件以 .pdf 結尾
  2. 確保正在讀取的文件以 .pdf 結尾
  3. 刪除文件參數包含..

鑒於 Download.php 看起來根本無法確保請求者通過身份驗證,我建議也許將您的 PDF 文檔保存在 Web 可訪問目錄中並直接鏈接到它們,而不是創建可能危及您的服務器的攻擊向量。

在 web.config 中使用它,

<authorization>
    <allow users="user1, user2"/>
    <deny users=”?”/>
</authorization>

https://support.microsoft.com/en-us/help/815151/how-to-restrict-specific-users-from-gaining-access-to-specified-web-re

正如邁克爾 M 所說,不要讓代碼規避這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM