用 Pages/Keynote 制作的 docx/pptx 的 mimetypes 不正确
Incorrect mimetypes of a docx/pptx made with Pages/Keynote
我有一个 Symfony 2.7 项目。
最近我抱怨客户不能再上传 .pptx
或 .docx
文件。
经过一番研究后,我发现这是我正在检查的 mimetype,以防止上传不需要的文件。
Power Point 和 Word 制作的 .pptx
和 .docx
文件已正确上传并具有正确的 mimetype(application/vnd.ms-powerpoint
和 application/msword
),但创建的文件在 Pages 和 Keynote 中,然后导出到 .docx
/.pptx
的 mimetype 为 application/octet-stream
或 application/zip
(取决于使用的 Symfony 的 MimeTypeGuesser)。默认情况下使用 FileinfoMimeTypeGuesser
,它在内部调用 finfo
到 return application/octet-stream
。第二个猜测者,FileBinaryMimeTypeGuesser
,returns application/zip
(我相信那个人使用 file --mime
)。
另外,当我 运行 file --mime
处理这些文件时(在我的 CentOS 7 Vagrant 和 Mac 上),我得到了这些结果:
流浪者:
[vagrant@localhost ~]$ file --mime keynote_pptx_test.pptx
keynote_pptx_test.pptx: application/zip; charset=binary
[vagrant@localhost ~]$ file --mime powerpoint_pptx_test.pptx
powerpoint_pptx_test.pptx: application/vnd.ms-powerpoint; charset=binary
本地:
user@MacBook-Pro $ file --mime keynote_pptx_test.pptx
keynote_pptx_test.pptx: application/zip; charset=binary
user@MacBook-Pro $ file --mime powerpoint_pptx_test.pptx
powerpoint_pptx_test.pptx: application/zip; charset=binary
显然,出于安全原因,我不想启用 application/zip
上传。
默认 mime 类型猜测器 (FileinfoMimeTypeGuesser
) returns application/octet-stream
。我不确定是否要为上传启用该 mimetype,也是出于安全原因。
我能做些什么来使这些上传发生,比如以不同的方式检查 mimetype(也许 Symfony 中有更多的猜测者?),或者我应该启用 application/octet-stream
(如果是这样,安全性是什么影响),还是只是 Apple 的软件需要改进?
Carlos Granados的评论就是答案:
As the name imples, MimeTypeGuesser is just "guessing" the MimeType.
From my experience it is really difficult to get the right mime type
for files coming from many different sources, so you will need to end
up opening the range of types that you accept. If you are worried
about security , you can always apply more tests to those files, for
example use a docx parser to verify that it is a real docx file
我有一个 Symfony 2.7 项目。
最近我抱怨客户不能再上传 .pptx
或 .docx
文件。
经过一番研究后,我发现这是我正在检查的 mimetype,以防止上传不需要的文件。
Power Point 和 Word 制作的.pptx
和 .docx
文件已正确上传并具有正确的 mimetype(application/vnd.ms-powerpoint
和 application/msword
),但创建的文件在 Pages 和 Keynote 中,然后导出到 .docx
/.pptx
的 mimetype 为 application/octet-stream
或 application/zip
(取决于使用的 Symfony 的 MimeTypeGuesser)。默认情况下使用 FileinfoMimeTypeGuesser
,它在内部调用 finfo
到 return application/octet-stream
。第二个猜测者,FileBinaryMimeTypeGuesser
,returns application/zip
(我相信那个人使用 file --mime
)。
另外,当我 运行 file --mime
处理这些文件时(在我的 CentOS 7 Vagrant 和 Mac 上),我得到了这些结果:
流浪者:
[vagrant@localhost ~]$ file --mime keynote_pptx_test.pptx
keynote_pptx_test.pptx: application/zip; charset=binary
[vagrant@localhost ~]$ file --mime powerpoint_pptx_test.pptx
powerpoint_pptx_test.pptx: application/vnd.ms-powerpoint; charset=binary
本地:
user@MacBook-Pro $ file --mime keynote_pptx_test.pptx
keynote_pptx_test.pptx: application/zip; charset=binary
user@MacBook-Pro $ file --mime powerpoint_pptx_test.pptx
powerpoint_pptx_test.pptx: application/zip; charset=binary
显然,出于安全原因,我不想启用 application/zip
上传。
默认 mime 类型猜测器 (FileinfoMimeTypeGuesser
) returns application/octet-stream
。我不确定是否要为上传启用该 mimetype,也是出于安全原因。
我能做些什么来使这些上传发生,比如以不同的方式检查 mimetype(也许 Symfony 中有更多的猜测者?),或者我应该启用 application/octet-stream
(如果是这样,安全性是什么影响),还是只是 Apple 的软件需要改进?
Carlos Granados的评论就是答案:
As the name imples, MimeTypeGuesser is just "guessing" the MimeType. From my experience it is really difficult to get the right mime type for files coming from many different sources, so you will need to end up opening the range of types that you accept. If you are worried about security , you can always apply more tests to those files, for example use a docx parser to verify that it is a real docx file