如何在允许用户发布外部图片的同时防止XSS注入
一位用户最近向我报告,他们可以利用通过论坛提供的 BBCode 标签 [img]。
[img=http://url.to.external.file.ext][img]
当然,它会显示为损坏的图像,但是浏览器会在那里检索文件。我自己测试了一下,果然是合法的。
除了下载图像并通过 PHP 检查它是否是合法图像之外,我不知道如何防止这种类型的 XSS 注入。这很容易被一个极其巨大的文件滥用。
对此还有其他解决方案吗?
A user recently reported to me that they could exploit the BBCode tag [img] that was available to them through the forums.
[img=http://url.to.external.file.ext][img]
Of course, it would show up as a broken image, however the browser would retrieve the file over there. I tested it myself and sure enough it was legit.
I'm not sure how to prevent this type of XSS injection other than downloading the image and checking if it is a legitimate image through PHP. This easily could be abused with a insanely huge file.
Are there any other solutions to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以请求标头并检查文件是否实际上是图像。
编辑:
抱歉,我无法更深入地回答;我正在享受晚餐。
我有两种查看方式:
检查远程文件内容类型的基本方法:
You could request the headers and check if the file is actually an image.
Edit:
Sorry that I couldn't answer in more depth; I was enjoying dinner.
There are two ways I see it:
A basic way to check the remote files content type:
这个问题只有两个解决方案。下载图像并从您的网络服务器提供服务,或者仅允许图像的 url 模式白名单。
如果您决定下载图像,可能会遇到一些问题 -
There's only two solutions to this problem. Either download the image and serve from your webserver, or only allow a white-list of url patterns for the images.
Some gotchas if you decide to download the images -