簡體   English   中英

在PHP中使用file_get_contents時的會話身份驗證

[英]Session Authentication when using file_get_contents in PHP

我項目中的下載受PHP下載腳本和會話身份驗證的保護。

在生成TCPDF時,我使用file_get_contents和下面的腳本來獲取圖像並生成pdf。

stream_context_create發送標頭PHPSESSID,但仍然沒有身份驗證。

pdfexport.php:

    $opts = array( 'http'=>array( 'method'=>"GET",
                  'header'=>"Accept-language: de\r\n" .
                  "Cookie: ".session_name()."=".session_id()."\r\n" ) );
    $context = stream_context_create($opts);
    session_write_close();  

foreach($data['we_files'] as $we_file){ 

    $getimage1 = file_get_contents( URLROOT . "/file.php?path=" .$we_file->image, false, $context);
    $image1_name = tempnam("/tmp", $we_file->image);
    file_put_contents($image1_name, $getimage1);
    $image1_image = new Imagick($image1_name);
    $image1_image->setImageCompression(imagick::COMPRESSION_JPEG);
    $image1_image->setImageCompressionQuality(100);
    $image1_image->thumbnailImage(500, 0);
    $image1 = '@'.base64_encode($image1_image);

echo $image1;
} 

file.php

$path = $_GET["path"];
$search = 'uploads' ;
$pathnew = str_replace($search, '', $path) ;

header('X-Accel-Redirect: /uploads/' . $pathnew);
header('Content-Type:');

Imagick錯誤:

Fatal error: Uncaught ImagickException: no decode delegate for this image format `' @ error/constitute.c/ReadImage/509

調試:

Warning: file_get_contents(https://domain.de/file.php?path=uploads/481/8979fc24e116c4577a44424a8814c79b0d5c73d9-19-03-2019-08-28-11-SA-150.jpg): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in /var/www/clients/...

// DIE(print_r($opts));
Array
(
    [http] => Array
        (
            [method] => GET
            [header] => Accept-language: de
            Cookie: PHPSESSID=5krl856uibhugaf6p6n6hluufq

        )

)
1

//DIE(print_r($_COOKIE));
    Array(
     [PHPSESSID] => 5krl856uibhugaf6p6n6hluufq
    )
    1

您實質上是在試圖欺騙用戶會話:在您實際上是(可能是惡意的)第三方時,假裝您是用戶。 如果您的會話設置安全,那將不起作用。

相反,您應該做的是驗證代碼中的用戶訪問權限,並通過文件系統讀取圖像。

一種替代方法是創建一個更復雜的系統,在該系統中,服務將根據后端對自身進行身份驗證,並傳遞表示“此用戶已授權我為他們執行此操作”的信息。

暫無
暫無

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

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