如其他地方所见、docx、xlsx和pttx都是ZIPs。当将它们上传到我的web应用程序时,file
(通过libmagic
和python-magic
)检测到它们是ZIP。
我将文件的内容作为blob存储在数据库中,但很自然地,我不想相信用户这是什么样的文件类型。因此,我希望在下载过程中信任file
并自动生成文件名。
我知道可以修改/etc/magic
,但是格式(magic(5)
)对我来说太复杂了。我找到了关于Debian问题的错误报告,但由于它是从2008年开始的,它似乎不会在短期内被修复。
我想我唯一的选择是确实信任用户(但仍然将内容存储为blob),并且只根据文件名检查文件扩展名。这样,我就可以不允许某些扩展,也可以允许其他扩展。当用户重新下载他的文件时,他可以以任何方式上传它。但是,如果与其他人共享该文件,则此解决方案是不安全的,因为您可以简单地重命名该文件以允许上传它。
有什么想法吗?
最后,我找到了docx等的幻数列表,但是我无法将这些转换成magic(5)
格式。
发布于 2013-05-16 21:33:10
文件( 5.13之前的版本)将将MIME类型截断为64个字符。因此,使用msooxml的内容,文件-bi命令中的MIME类型将变成"mime -bi charset=binary“。
发布于 2017-02-24 06:41:34
如果使用libreoffice的docx,可以将内容(如下)添加到/etc/魔术中:
# start by checking for ZIP local file header signature
0 string PK\003\004
!:strength +10
>1104 search/300 PK\003\004
# and check the subdirectory name to determine which type of OOXML
# file we have. Correct the mimetype with the registered ones:
# http://technet.microsoft.com/en-us/library/cc179224.aspx
>>&26 string word/ Microsoft Word 2007+
!:mime application/vnd.openxmlformats-officedocument.wordprocessingml.document
>>&26 string ppt/ Microsoft PowerPoint 2007+
!:mime application/vnd.openxmlformats-officedocument.presentationml.presentation
>>&26 string xl/ Microsoft Excel 2007+
!:mime application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
>>&26 default x Microsoft OOXML
https://serverfault.com/questions/338087
复制相似问题