如何在允许用户发布外部图片的同时防止XSS注入

发布于 2024-10-17 01:15:25 字数 261 浏览 0 评论 0原文

一位用户最近向我报告,他们可以利用通过论坛提供的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风尘浪孓 2024-10-24 01:15:25

您可以请求标头并检查文件是否实际上是图像。

编辑:

抱歉,我无法更深入地回答;我正在享受晚餐。

我有两种查看方式:

  1. 您在提交或查看帖子时检查提供的地址是否实际上是图像,您可以通过检查标题(确保它实际上是图像)或使用文件扩展名来完成此操作。这并不是万无一失的,并且存在一些明显的问题(动态更改图像等)。
  2. 确保您的网站安全,即使 [img] 标签遭到破坏,也不会出现真正的问题,例如:恶意代码无法使用窃取的 cookie。
  3. 使用请求外部图像并修改标头的脚本。

检查远程文件内容类型的基本方法:

$Headers = get_headers('http://url.to.external.file.ext');
if($Headers[8] == 'text/html') {
    echo 'Wrong content type.';
    exit;
}

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:

  1. You check to see if the supplied address is actually a image when the post is submitted or viewed, you could accomplish this by checking the headers (making sure it's actually an image) or by using file extension. This isn't fool-proof and has some obvious issues (changing the image on the fly, etc.).
  2. Secure your site that even if there is a compromise with the [img] tag there is no real problem, for example: the malicious code can't use stolen cookies.
  3. Use a script that requests an external image and modifies the headers.

A basic way to check the remote files content type:

$Headers = get_headers('http://url.to.external.file.ext');
if($Headers[8] == 'text/html') {
    echo 'Wrong content type.';
    exit;
}
孤者何惧 2024-10-24 01:15:25

这个问题只有两个解决方案。下载图像并从您的网络服务器提供服务,或者仅允许图像的 url 模式白名单。

如果您决定下载图像,可能会遇到一些问题 -

  1. 确保您已验证最大文件大小。如果文件超过一定大小,有多种方法可以停止下载,但这些方法特定于语言。
  2. 检查该文件是否确实是图像。
  3. 如果将其存储在硬盘上,请务必重命名。您不应允许用户控制系统上的文件名。
  4. 当您提供图像时,请使用一次性域名,或使用裸 IP 地址来提供图像。如果浏览器被欺骗,认为图像是可执行代码,同源策略将防止进一步的损害。

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 -

  1. Make sure you have a validation for the maximum file size. There are ways to stop the download if the file exceeds a certain size, but these are language specific.
  2. Check that the file is actually an image.
  3. If you store it on the hard-disk, be sure to rename it. You shouldn't allow the user to control the file name on the system.
  4. When you serve the images, use a throw-away domain, or use naked ip address to serve the images. If the browser is ever tricked in thinking the image is executable code, the same-origin policy will prevent further damage.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文