在PHP中使用file_get_contents时的会话身份验证

问题描述 投票:0回答:1

我的项目中的下载受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:');

想象错误:

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
php file-get-contents
1个回答
1
投票

您实际上是在尝试欺骗用户会话:当您实际上是(可能是恶意的)第三方时,执行假装您是用户的操作。如果您的会话安全设置,那将无效。

你应该做的是验证用户在代码中的访问权限,并通过文件系统读取图像。

另一种方法是创建一个更复杂的系统,其中服务对后端进行身份验证,并传递“此用户授权我为他们执行此操作”的信息

© www.soinside.com 2019 - 2024. All rights reserved.