【发布时间】:2018-05-24 00:14:40
【问题描述】:
正在开发 React/Django 应用程序。我有用户通过 React 前端上传的文件,这些文件最终在 Django/DRF 后端。我们在服务器上不断运行防病毒 (AV),但我们希望在将其写入磁盘之前添加流扫描。
如何设置它有点让我头疼。以下是我正在查看的一些来源。
How do you virus scan a file being uploaded to your java webapp as it streams?
虽然公认的最佳答案描述了它“...非常容易”设置,但我正在苦苦挣扎。
我显然需要 cat testfile | clamscan - 每个帖子和相应的文档:
How do you virus scan a file being uploaded to your java webapp as it streams?
如果我的后端如下所示:
class SaveDocumentAPIView(APIView):
permission_classes = [IsAuthenticated]
def post(self, request, *args, **kwargs):
# this is for handling the files we do want
# it writes the files to disk and writes them to the database
for f in request.FILES.getlist('file'):
max_id = Uploads.objects.all().aggregate(Max('id'))
if max_id['id__max'] == None:
max_id = 1
else:
max_id = max_id['id__max'] + 1
data = {
'user_id': request.user.id,
'sur_id': kwargs.get('sur_id'),
'co': User.objects.get(id=request.user.id).co,
'date_uploaded': datetime.datetime.now(),
'size': f.size
}
filename = str(data['co']) + '_' + \
str(data['sur_id']) + '_' + \
str(max_id) + '_' + \
f.name
data['doc_path'] = filename
self.save_file(f, filename)
serializer = SaveDocumentSerializer(data=data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(status=HTTP_200_OK)
# Handling the document
def save_file(self, file, filename):
with open('fileupload/' + filename, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
我想我需要在save_file 方法中添加一些内容,例如:
for chunk in file.chunks():
# run bash comman from python
cat chunk | clamscan -
if passes_clamscan:
destination.write(chunk)
return HttpResponse('It passed')
else:
return HttpResponse('Virus detected')
所以我的问题是:
1) 如何从 Python 运行 Bash?
2) 如何从扫描中接收结果响应,以便将其发送回用户,并且可以通过后端的响应完成其他事情? (例如创建逻辑以向用户和管理员发送一封电子邮件,说明他们的文件有病毒)。
我一直在玩这个,但运气不佳。
Running Bash commands in Python
此外,Github 存储库声称将 Clamav 与 Django 结合得很好,但它们要么多年未更新,要么现有文档非常糟糕。请参阅以下内容:
https://github.com/vstoykov/django-clamd
【问题讨论】:
-
文件的一部分不太可能被检测为病毒。扫描仪可能需要整个文件。
标签: python django web-applications antivirus antivirus-integration