有没有办法过滤 href 中允许的协议

Is there a way to filter the protocols allows in a href

我正在使用 ckeditor 4.5,我们最近在后端添加了 OWASP Html Sanitizer 以筛选 XSS 攻击。该工具的功能之一是过滤 href 元素允许的协议。

我们发现,即使我们的协议列表与 link 中的协议列表相匹配,人们仍然使用我们不支持的不同协议将内容粘贴到 ckeditor 中,例如我们想从 ckeditor 中过滤掉它,这样我们就不会将天真的剪切和粘贴标记为 xss 攻击。

AdvancedContentFilter 似乎能够指定单独的 类 和样式,但不能指定 href 协议或更通用的属性值。

是否可以在 CKEDITOR 中执行此操作?

编辑: 我想越来越近了,我补充说:

config.disallowedContent = {
    a: {
        match: function(element) {
            var allowedProtcols = /^(?:http|https|mailto|tel|ftp|news):/;
            return !allowedProtcols.test(element.attributes.href);
        }
    }
};

不幸的是,link 按钮似乎从菜单栏中消失了。

解决方案是匹配函数和特殊大小写的组合cke-test。

config.allowedContent = {
    'a': {
        match: function(element) {
            if (element.attributes.href === 'cke-test' ) { return true; }
            var allowedProtcols = /^(?:http|https|mailto|tel|ftp|news):/;
            return allowedProtcols.test(element.attributes.href);
        },
        attributes: '!href'
    },
...
};