简体   繁体   English

在Ruby On Rails中安全地上传文件

[英]Secure File Upload in Ruby On Rails

I built a photo gallery which uses Paperclip and validates the content-type using validates_attachment_content_type. 我建立了一个使用Paperclip的照相馆,并使用validates_attachment_content_type验证了内容类型。

The application runs on a shared host with Passenger. 该应用程序在与Passenger共享的主机上运行。

Is it possible to bypass the validation and run malicious scripts from the public/pictures directory? 是否可以绕过验证并从public / pictures目录运行恶意脚本? If so, is there anything that I can do to avoid evil scripts from running or from being uploaded? 如果是这样,我有什么办法可以避免运行或上传恶意脚本?

Is it possible to bypass the validation and run malicious scripts from the public/pictures directory? 是否可以绕过验证并从public / pictures目录运行恶意脚本?

Yes. 是。 You can have a perfectly valid renderable image file that also contains HTML with script injection. 您可以拥有一个完全有效的可渲染图像文件,其中还包含带有脚本注入的HTML。 Thanks for the bogus content-sniffing, IE, you have ruined everything. 感谢虚假的内容嗅探,即IE,您已经毁了一切。

See http://webblaze.cs.berkeley.edu/2009/content-sniffing/ for a summary. 有关摘要,请参见http://webblaze.cs.berkeley.edu/2009/content-sniffing/

If so, is there anything that I can do to avoid evil scripts from running or from being uploaded? 如果是这样,我有什么办法可以避免运行或上传恶意脚本?

Not really. 并不是的。 In theory you can check the first 256 bytes for HTML tags, but then you have to know the exact details of what browsers content-sniff for, and keeping that comprehensive and up-to-date is a non-starter. 从理论上讲,您可以检查HTML标记的前256个字节,但是随后您必须知道浏览器对什么内容进行嗅探的确切详细信息,并且保持全面而最新的内容是没有开始的。

If you are processing the images and re-saving them yourself that can protect you. 如果您正在处理图像并自己重新保存,可以保护您。 Otherwise, do one or both of: 否则,请执行以下一项或两项操作:

  • only serve user-uploaded files from a different hostname, so they don't have access to the cookie/auth details that would allow an injected script to XSS into your site. 仅提供来自其他主机名的用户上传文件,因此它们无权访问cookie / auth详细信息,而cookie / auth详细信息将使注入的脚本XSS进入您的站点。 (but look out for non-XSS attacks like general JavaScript/plugin exploits) (但要注意非XSS攻击,例如常规JavaScript /插件攻击)

  • serve user-uploaded files through a server-side script that includes the 'Content-Disposition: attachment' header, so browsers don't attempt to view the page inline. 通过包含“ Content-Disposition:附件”标头的服务器端脚本为用户上传的文件提供服务,因此浏览器不会尝试内联查看页面。 (but look out of old versions of Flash ignoring it for Flash files) This approach also means you don't have to store files on your server filesystem under the filename the user submits, which saves you some heavy and difficult-to-get-right filename validation work. (但是请注意旧版本的Flash会忽略Flash文件)。这种方法还意味着您不必将文件存储在服务器文件系统中的用户提交的文件名下,从而节省了一些繁重且难以获取的文件正确的文件名验证工作。

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

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