如何安全地检查上传文件的大小?(How to check size of uploaded file safely in bottlepy?)
我真的害怕read()操作,因为它使用内存。 例如,任何人都可以通过上传1GB文件来对服务器进行DDoS,对吗?
name = request.forms.get('name')
data = request.files.get('data')
if name and data.file:
raw = data.file.read() # This is dangerous for big files
filename = data.filename
return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
有没有安全的解决方案来获取上传的文件大小? 一个猜测是从文件系统获取文件大小; request.files.get('data')可能存储在临时文件的某个地方?
I'm really afraid of that read() operation because it uses memory. For instance, anybody could DDoS my server by uploading a 1gb file, correct?
name = request.forms.get('name')
data = request.files.get('data')
if name and data.file:
raw = data.file.read() # This is dangerous for big files
filename = data.filename
return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
Is there any safe solution to get the uploaded file size? One guess would be to get file size from the file system; request.files.get('data') is probably stored somewhere in temp file right?
原文:https://stackoverflow.com/questions/11218422
更新时间:2020-01-20 23:45
最满意答案
你能否检查一次是否可以读取大块数据。
如果这是可能的,那么:
name = request.forms.get('name')
data = request.files.get('data')
raw = ""
if name and data.file:
while True:
datachunk = data.file.read(1024)
if not datachunk:
break
raw = raw + datachunk
filename = data.filename
return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
如果这是可能的,那么你应该也可以添加一个跟踪机制来确定你想要读取的文件大小,如果超过这个大小,就会中止这个操作。
这只能解决DDOS的一种可能的方式。
Can you check if you can read chunks of data, one at a time.
If this is possible then:
name = request.forms.get('name')
data = request.files.get('data')
raw = ""
if name and data.file:
while True:
datachunk = data.file.read(1024)
if not datachunk:
break
raw = raw + datachunk
filename = data.filename
return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
If this is possible, then you should be able to also add a tracking mechanism on how large a file you want to read and if exceeded abort this operation.
How ever this solves only one of the possible ways of DDOS.
2012-07-10
相关问答
关于这个问题,有一个名为file-uploader的插件可以解决问题。 使用此插件,您可以在config.groovy中定义一些限制。 这是一个例子: fileuploader {
avatar {
maxSize = 1024 * 256 //256 kbytes
allowedExtensions = ["jpg","jpeg","gif","png"]
path = "/tmp/avatar/"
}
docs {
...
您可以使用commons-fileupload设置setSizeThreshold 这是相同的例子 注意: * 如果不直接将其直接发送到服务器,您无法确定文件大小 * You can set setSizeThreshold using commons-fileupload Here is example of the same Note: *You can't determine the file size without streaming it up directly to server*
从PHP 4.2.0开始,PHP会返回适当的错误代码以及文件数组。 所以, <?php
if ($_FILES['txtImage']['error'] === UPLOAD_ERR_NO_FILE) {
$msg .= "Opss, you forgot the image.
";
}
?>
http://php.net/manual/en/features.file-upload.errors.php Since PHP 4.2.0, PHP returns an approp
...
使用._size文件属性 if thisFile._size > 5242880:
return "This file is more than 5mb"
._size以字节表示。 5242880 - 5MB def handle_uploaded_file(thisFile):
if thisFile._size > 5242880:
return "This file is more than 5mb"
else:
with open('s
...
if(!file_exists($_FILES['name_of_field']['tmp_name'])
{
# No file uploaded
}
if(!file_exists($_FILES['name_of_field']['tmp_name'])
{
# No file uploaded
}
问题在于我试图从使用jQuery的文件输入中选择文件的方式。 以下是一些示例解决方案: 如果您有一个文件输入,并且用户只能选择一个文件: // Get the file
var file = $('input[type="file"]').get(0).files[0];
// File size, in bytes
var size = file.size;
如果您有一个文件输入,请选择多个文件: // Get an array of the files
var files = $('inp
...
保存到服务器之前检查文件大小。 (正在上传时检查文件大小) PHP在解码POST请求时正在检查最大文件大小。 它是在php.ini中使用upload_max_filesize 。 通常大约10MB左右。 但您可以通过简单的测试轻松设置特定于应用程序的最大文件大小: if ($_FILES["image"]["size"] >= 500000) {
然后相应地做出反应并打印错误消息。 对于个人资料图片和头像,500K应该足够了。 扫描文件内容 您需要在服务器上安装病毒扫描程序。 有各种各样的可用。
...
我发现它,正确的组合原来是参数dFileSize和事件 - postWebfileCreation I found it, the right combination turned out to be the parameter dFileSize and the event - postWebfileCreation
你能否检查一次是否可以读取大块数据。 如果这是可能的,那么: name = request.forms.get('name')
data = request.files.get('data')
raw = ""
if name and data.file:
while True:
datachunk = data.file.read(1024)
if not datachunk:
break
raw = raw + d
...
问题是在运行服务器时使用reloader=True 。 将此设置为False可防止出现第二个窗口。 The issue was using reloader=True when running the server. Setting this to False prevents the second window from appearing.