image xss 3.ruby防止图片xss攻击的方法

1、Rmagick插件中允许的content_type类型

\vendor\plugins\attachment_fu\lib\technoweenie\attachment_fu.rb 5~35行

image/jpeg | image/pjpeg | image/jpg | image/gif | image/png | image/x-png


2、ruby Rmagick content_type、Magick::ImageList.new(file_path).format与图片实际类型及后缀名的关系


3、由1和2得出下表为安全图片


图片的实际类型,后缀名和content_type一致的话,能确保图片不能发动xss攻击。


4、代码

 is_safe_image = false

 image_real_format = real_content_type(file_path)

is_safe_image = true if "GIF" == image_real_format && "gif" == image_suffix && %q|image/gif| == image_content_type

 is_safe_image = true if "PNG" == image_real_format && "png" == image_suffix && %w(image/png image/x-png).member?(image_content_type)

is_safe_image = true if "JPEG" == image_real_format && %w(jpg jpeg jpe jfif).member?(image_suffix) && %w(image/jpeg image/pjpeg image/jpg).member?(image_content_type)


def real_content_type(file_path)

    Magick::ImageList.new(file_path).format

end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值