【问题标题】:Connection to this site is not Fully Secure与此站点的连接不是完全安全的
【发布时间】:2018-04-16 07:09:13
【问题描述】:

我有一个网站,用户可以在上面写博客文章。我使用stackoverflow pagedown editor 允许用户通过插入链接来添加内容和图像。

但问题是,如果用户插入以http:// 开头的链接,例如http://example.com/image.jpg,浏览器会显示警告,

Your Connection to this site is not Fully Secure.

Attackers might be able to see the images you are looking at
& trick you by modifying them

我想知道我们如何强制浏览器仅使用正在插入图像的站点的https:// 版本,尤其是当用户插入以http:// 开头的链接时?

或者这个问题有其他解决方案吗?

image

【问题讨论】:

    标签: http security ssl https pagedown


    【解决方案1】:

    不幸的是,浏览器希望通过 ssl 提供所有已加载的资源。在您的情况下,您别无选择,只能自行存储所有图像或创建或代理从 http 到 https 的请求。但我不确定这样做是否真的安全。

    例如,您可以执行以下操作:
    我假设代码是 php,并且通过 https

    <?php
    define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of tiles chunk
    
    // Read a file and display its content chunk by chunk
    function readfile_chunked($filename, $retbytes = TRUE) {
        $buffer = '';
        $cnt    = 0;
        $handle = fopen($filename, 'rb');
    
        if ($handle === false) {
            return false;
        }
    
        while (!feof($handle)) {
            $buffer = fread($handle, CHUNK_SIZE);
            echo $buffer;
            ob_flush();
            flush();
    
            if ($retbytes) {
                $cnt += strlen($buffer);
            }
        }
    
        $status = fclose($handle);
    
        if ($retbytes && $status) {
            return $cnt; // return num. bytes delivered like readfile() does.
        }
    
        return $status;
    }
    
    
    $filename = 'http://domain.ltd/path/to/image.jpeg';
    $mimetype = 'image/jpeg';
    header('Content-Type: '.$mimetype );
    readfile_chunked($filename);
    

    Credit for code sample

    _ 更新 1 _
    Alternate solution to proxify steamed downloaded file in Python

    _ 更新 2 _
    在以下代码中,您可以将数据从远程服务器流式传输到前端客户端,如果您的 Django 应用程序通过 https,则内容将正确传递。

    目标是按 1024 位组读取您的原​​始图像,然后将每个组流式传输到您的浏览器。当您尝试加载重图像时,此方法可避免超时问题。

    我建议您添加另一层以具有本地缓存​​而不是下载 -> 每个请求的代理。

    import requests
    # have this function in file where you keep your util functions 
    def url2yield(url, chunksize=1024):
       s = requests.Session()
       # Note: here i enabled the streaming
       response = s.get(url, stream=True)
    
       chunk = True
       while chunk :
          chunk = response.raw.read(chunksize)
    
          if not chunk:
             break
    
          yield chunk
    
    
    # Then creation your view using StreamingHttpResponse
    def get_image(request, img_id):
       img_url = "domain.ltd/lorem.jpg"
       return StreamingHttpResponse(url2yield(img_url), content_type="image/jpeg")
    

    【讨论】:

    • 先生,我用的是python,django。
    • @Dev 我已经用 Python 解决方案更新了我的答案。你明白我们为什么要做这样的事情吗?如果没有,请随时在这里发表评论,我会尽力为您提供更多详细信息。
    • 先生,此解决方案显示将图像保存到数据库中,然后将其显示给用户。但是,现在我只想从用户插入的URL 中提取它,然后在网页上显示。不将图像保存到数据库中就不可能吗?或者做类似stackoverflow的事情,它从URL中提取图像,将它们保存在imgur服务器返回图像https://?或者我们可以在编辑器中保存内容之前将http:// 更改为https:// 吗? (如果某些图像以这种方式中断也没关系。)
    • 您可以在 imgur 中上传您编辑器中插入的每张图片,但对我来说,这有点矫枉过正。我更喜欢“代理”方法,它只是:客户端通过 myapp.ltd/img/{path/to/image} 询问您的应用程序,然后下载 Django 代理图像下载。请用新鲜样品检查我的更新
    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 2023-03-31
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2021-10-22
    相关资源
    最近更新 更多