简体   繁体   English

Python CGI脚本拒绝包含数据URI的数据(403)

[英]Python CGI script rejects data that includes data URI (403)

I have a webapp that creates small images using SVG and I want it to be able to save these images to the server and then load them again (as innerHTML of a SVG element). 我有一个使用SVG创建小图像的webapp,我希望它能够将这些图像保存到服务器,然后再次加载它们(作为SVG元素的innerHTML)。 Using ajax, I send the svg's inner HTML to my server side Python script, which looks more or less like this: 使用ajax,我将svg的内部HTML发送到服务器端Python脚本,该脚本看起来或多或少像这样:

#!/usr/bin/python
import cgi

data=cgi.FieldStorage()
filename=data.getfirst('filename')
svg=data.getfirst('svg')

f = open('/filepath/'+filename,'w')
f.write(svg)
f.close()

The ajax call looks like this: ajax调用看起来像这样:

function savetoserver() {
    var filename=$('#savename').val()
    var svg = document.getElementById("svg_element").innerHTML

    dictionary={'svg':svg,'filename':filename}

    $.ajax({
        url:'http://myserver.com/savetoserver.py',
        type: 'post',
        datatype: 'html',
        data: dictionary,
        success: function(response){
            closedialog()
        }
    })
}

This all works great for simple inputs. 所有这些都非常适合简单的输入。 However, if my SVG has a data URI of any sort in it, it fails, and the script returns 403 (forbidden). 但是,如果我的SVG中包含任何类型的数据URI,它将失败,并且脚本返回403(禁止)。 Unfortunately, I kinda want to pass it data URIs. 不幸的是,我有点想向它传递数据URI。 In some cases a small image is placed inside the SVG and in almost all cases there are fonts embedded in the SVG as base64 data URIs. 在某些情况下,SVG内会放置一个小图像,并且在几乎所有情况下,SVG中都嵌入了一些字体作为base64数据URI。

From what little I can find out from Googling, it seems like this is a security issue, to prevent attacks on the server via data URI. 从谷歌搜索中几乎看不到,这似乎是一个安全问题,它可以防止通过数据URI攻击服务器。 Is there a standard alternative available to me that will allow me to store this data and not upset Python/Apache/whoever is being upset? 有没有我可以使用的标准替代方法,它可以让我存储这些数据而不会使Python / Apache /其他人感到不安?

I am not exactly sure how your svg variable looks like (I am guessing 'some text' + URI) In any case you may want to use encodeURIComponent() which encodes special characters in your URI. 我不确定您的svg变量是什么样子(我猜是“某些文本” + URI)在任何情况下,您都可能想使用encodeURIComponent()来对URI中的特殊字符进行编码。

So, you may try to split your svg into 'text part' and 'URI' part (using split() for example) and then send it in ajax request with updated dictionary, where svg variable will be something like 'some text' + encodeURIComponent('URI') 因此,您可以尝试将svg分为“文本部分”和“ URI”部分(例如,使用split()),然后在带有更新字典的ajax请求中发送,其中svg变量将类似于“某些文本” + encodeURIComponent('URI')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM