将 fckeditor 图像限制为仅来自我的域的图像

发布于 2024-11-27 06:03:49 字数 383 浏览 1 评论 0原文

我有一个会员网站,我们使用精彩的 fckeditor 的非常锁定版本来发布会员内容。最近,我们开始允许使用表情符号,这让会员感到高兴,但也引入了一个潜在的漏洞,因为现在可以插入来自其他域的图像以及我们提供的表情符号。

发布的所有内容都会经过预览阶段,在此期间对发布的内容进行清理,所以我想我需要一些额外的 php 来删除任何 src 表明它不是来自我们域的 img 标签(假设它是“xyz.com”) ”)。正如 drf 在第一条评论中指出的那样,这并不像最初看起来那么简单。

我确信这也适用于其他人,但我没有找到解决方案和解决方案。正则表达式不是我的强项。一如既往,任何和所有的帮助&如有建议,我们将不胜感激。

I have a members website where we use a very locked-down version of the wonderful fckeditor for posting of member content. Recently we've started allowing smileys, which makes the members happy but has introduced a potential vulnerability in that it's now possible to insert images from other domains, as well as the smileys which are served from ours.

Everything posted goes through a preview stage, during which the posted content is sanitized, so I'm thinking I need some extra php which removes any img tag whose src indicates it doesn't come from our domain (let's say it's "xyz.com"). As pointed out by drf in the first comment, this is not as straightforward as it may initially seem.

I'm sure this would apply to others too, but I haven't had any luck finding a solution & regex is not my strong point. As always, any and all help & suggestions would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一杆小烟枪 2024-12-04 06:03:49

有些人会告诉您 RegExp 不适合解析 HTML/XHTML。我就是其中之一。尝试使用 XML 解析器:

<?php
$dom = new DOMDocument;
$dom->loadHTML(file_get_contents('input.html'));
$xpath = new DOMXpath($dom);
$img = $xpath->query('//img');
foreach($img as $i) {
    $url = parse_url($i->getAttribute('src'));
    if(isset($url['host']) && in_array($url['host'], array('yourdomain.com', 'www.yourdomain.com')) == false) {
        // show an error
        // -- or --
        // remove the tag: $i->parentNode->removeChild($i)
        echo sprintf('[FAIL] %s' . PHP_EOL, $i->getAttribute('src'));
    }
    else {
        echo sprintf('[PASS] %s' . PHP_EOL, $i->getAttribute('src'));
    }
}

示例输入:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p><img src="/image.jpg"></p>
<p><img src="http://yourdomain.com/image.jpg"></p>
<p><img src="http://www.yourdomain.com/image.jpg"></p>
<p><img src="http://otherdomain.com/image.jpg"></p>

示例输出:

[PASS] /image.jpg
[PASS] http://yourdomain.com/image.jpg
[PASS] http://www.yourdomain.com/image.jpg
[FAIL] http://otherdomain.com/image.jpg

Some people will tell you that RegExp is not the right thing for parsing HTML/XHTML. I am one of them. Try using an XML parser instead:

<?php
$dom = new DOMDocument;
$dom->loadHTML(file_get_contents('input.html'));
$xpath = new DOMXpath($dom);
$img = $xpath->query('//img');
foreach($img as $i) {
    $url = parse_url($i->getAttribute('src'));
    if(isset($url['host']) && in_array($url['host'], array('yourdomain.com', 'www.yourdomain.com')) == false) {
        // show an error
        // -- or --
        // remove the tag: $i->parentNode->removeChild($i)
        echo sprintf('[FAIL] %s' . PHP_EOL, $i->getAttribute('src'));
    }
    else {
        echo sprintf('[PASS] %s' . PHP_EOL, $i->getAttribute('src'));
    }
}

Sample input:

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p><img src="/image.jpg"></p>
<p><img src="http://yourdomain.com/image.jpg"></p>
<p><img src="http://www.yourdomain.com/image.jpg"></p>
<p><img src="http://otherdomain.com/image.jpg"></p>

Sample output:

[PASS] /image.jpg
[PASS] http://yourdomain.com/image.jpg
[PASS] http://www.yourdomain.com/image.jpg
[FAIL] http://otherdomain.com/image.jpg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文