CKEditor 4 tableselection 粘贴集成 XSS 防护:xss 手动回归测试全解析
CKEditor 4 tableselection 粘贴集成 XSS 防护:xss 手动回归测试全解析
导读
本文围绕 CKEditor 4 仓库中 tests/plugins/tableselection/manual/integrations/clipboard/xss.md 这份手动测试文档展开,剖析 tableselection(表格框选)插件与 clipboard(剪贴板)插件集成时如何抵御粘贴内容中的 XSS 攻击。文档以一张携带 onerror 恶意事件属性的图片为攻击载体,验证「粘贴后不执行注入脚本」这一安全底线。读完本文,你将理解该测试的完整操作流程、配套测试页面与自动化测试的实现细节,以及背后由 Advanced Content Filter(ACF)与 dataProcessor 构成的内容净化链路。
测试文档定位:一条回归测试的“验收单”
在 CKEditor 4 仓库中,tests/plugins/tableselection/manual/ 目录存放的是基于 Bender.js 框架的手动(manual)测试,其配套 Markdown 文件既是测试说明,也是开发者的验收清单。xss.md 属于该目录下 integrations/clipboard/ 分组,专门验证 tableselection 与 clipboard 两个插件协作时的安全行为。
文档开头是三行 Bender 元信息注释,它们决定了测试的运行方式与适用环境:
@bender-ui: collapsed
@bender-tags: bug, trac16755, 4.7.0, tp2348
@bender-ckeditor-plugins: wysiwygarea, toolbar, tableselection, clipboard
@bender-ui: collapsed:测试页面 UI 默认折叠展示;@bender-tags:标注该测试关联的缺陷编号trac16755(tableselection 插件引入的系列回归问题)、引入版本4.7.0以及工单tp2348;@bender-ckeditor-plugins:声明测试所需的插件集合,其中wysiwygarea提供经典(iframe 型)编辑区域,toolbar提供工具栏,核心是tableselection与其依赖的clipboard。
值得注意的是,tableselection/plugin.js 中声明 requires: 'clipboard,tabletools',即该插件强制依赖 clipboard 与 tabletools,因此这条 XSS 测试天然落在「表格框选 × 剪贴板」的集成边界上。
攻击场景:一张“会说话”的图片
文档描述的测试场景非常精炼,只有两个步骤:
Procedure(操作步骤):
- Copy image with "Copy me!" alternative text.(复制一张带有 "Copy me!" 替代文本的图片)
- Paste it into the editor.(将图片粘贴进编辑器)
Expected result(预期结果):
- No alert is shown.(不弹出任何 alert 对话框)
Unexpected result(意外结果):
- "MEGA FAIL!" alert is shown.(弹出 "MEGA FAIL!" 警告框,即攻击成功)
表面看这只是一条“复制粘贴图片”的冒烟测试,实则是一个精心构造的 XSS 攻击场景:图片的替代文本(alt)本身是安全的,真正的攻击载荷藏在配套测试页面 xss.html 的图片标签中:
<img src="x" alt="Copy me!" onerror="window.parent.attack()">
攻击链路如下:
src="x"指向一个不存在的资源,浏览器加载图片必然失败;- 加载失败会触发
onerror事件处理器; onerror中调用window.parent.attack(),即父窗口中预先定义的攻击函数;attack()第一次被调用时会设置window.attack.armed = true并弹出'MEGA FAIL!',其防重复逻辑(见 xss.html)确保告警只触发一次,避免页面崩溃干扰观察。
如果粘贴该图片后没有弹出 alert,说明 onerror 事件属性在粘贴过程中被正确剥离;反之则证明恶意脚本穿透了编辑器的内容净化机制——这正是本次回归测试要拦截的缺陷。
测试页面剖析:如何“武装”一个编辑器
xss.html 是手动测试的实际运行页面,它完整展示了攻击环境的搭建方式:
<h2>Copy here…</h2>
<img src="x" alt="Copy me!" onerror="window.parent.attack()">
<div id="editor1">
<p>Paste here</p>
</div>
<script>
if ( CKEDITOR.env.ie || CKEDITOR.env.iOS ) {
bender.ignore();
}
window.attack = function() {
if ( window.attack.armed ) {
return alert( 'MEGA FAIL!' );
}
window.attack.armed = true;
}
CKEDITOR.replace( 'editor1', {
allowedContent: 'img[*]'
} );
</script>
页面设计有四个关键点:
1. 环境守卫:CKEDITOR.env.ie || CKEDITOR.env.iOS 时调用 bender.ignore() 跳过测试。这是因为该场景依赖复制粘贴的原生剪贴板行为与 onerror 触发机制,在 IE 与 iOS 上行为不一致,属于已知的平台限制。
2. 攻击函数定义在 window.attack:通过 window.parent.attack() 从编辑器 iframe 内部调用父窗口函数,模拟真实攻击中「编辑器内容反向影响宿主页面」的 XSS 传播路径。
3. allowedContent: 'img[*]' 是测试的关键:该配置通过 Advanced Content Filter 显式允许 img 元素的所有属性(* 通配符)。这意味着测试故意“放行”了图片本身,让过滤器无法通过简单拒绝 img 元素来兜底,从而把验证焦点精确对准事件属性 onerror 是否被单独剥离。若此时仍不弹窗,即可证明过滤逻辑对 on* 事件属性的处理是独立且可靠的。
4. 复制源在编辑器之外:<img> 位于页面正文而非编辑区内,模拟用户从外部网页复制恶意图片再粘贴进编辑器的真实场景,这正是富文本编辑器面临的最大攻击面之一。
自动化兜底:xss.js 把安全断言写进 CI
除了手动验收,仓库还提供了等价的自动化测试 tests/plugins/tableselection/integrations/clipboard/xss.js,把同一安全诉求固化为可重复执行的断言:
/* bender-tags: tableselection, clipboard */
/* bender-ckeditor-plugins: undo,tableselection */
/* bender-include: ../../_helpers/tableselection.js */
bender.editors = {
classic: {
config: {
allowedContent: 'img[*]'
}
},
inline: {
creator: 'inline',
config: {
allowedContent: 'img[*]'
}
}
};
- 自动化测试同时覆盖 classic(经典 iframe 型) 与 inline(内联型) 两种编辑器创建方式,确保净化逻辑与编辑器形态无关;
setUp中同样对 IE/iOS 执行assert.ignore(),与手动测试的环境守卫保持一致;- 攻击探测改用
sinon.spy()包装的window.attack,不再弹窗,而是统计调用次数:
'test JS code is not executed on paste': function( editor ) {
bender.tools.setHtmlWithSelection( editor, '<p>foobar^</p>' );
bender.tools.emulatePaste( editor, '<img src="x" onerror="window.parent.attack();">' );
editor.once( 'afterPaste', function() {
resume( function() {
assert.areSame( 0, window.attack.callCount, 'Function was not invoked' );
} );
} );
wait();
}
该用例通过 emulatePaste 模拟粘贴携带 onerror 的图片,在 afterPaste 事件后断言 attack.callCount === 0,从「不弹窗」进一步收敛为「函数一次都未被调用」的强断言,是手动测试的机器化升级版。
底层原理:粘贴内容如何被“消毒”
真正阻止攻击发生的是 tableselection 插件在粘贴流程中的内容净化处理。核心实现在 plugins/tableselection/plugin.js 的 fakeSelectionPasteHandler.findTableInPastedContent:
// Looks for a table in a given pasted content string. Returns it as a
// CKEDITOR.dom.element instance or null if mixed content, or more than one table found.
findTableInPastedContent: function( editor, dataValue ) {
var dataProcessor = editor.dataProcessor,
tmpContainer = new CKEDITOR.dom.element( 'body' );
if ( !dataProcessor ) {
dataProcessor = new CKEDITOR.htmlDataProcessor( editor );
}
// Pasted value must be filtered using dataProcessor to strip all unsafe code
// before inserting it into temporary container.
tmpContainer.setHtml( dataProcessor.toHtml( dataValue ), {
fixForBody: false
} );
return tmpContainer.getChildCount() > 1 ? null : tmpContainer.findOne( 'table' );
}
这段代码揭示了 XSS 防护的两个关键事实:
- 任何粘贴进编辑器的内容,都会先经过
dataProcessor.toHtml()过滤,注释明确写着 “Pasted value must be filtered using dataProcessor to strip all unsafe code”。toHtml会调用编辑器配置的 Advanced Content Filter(定义于 core/filter.js),依据allowedContent/extraAllowedContent/disallowedContent规则对 HTML 进行净化; - 净化发生在内容插入文档树之前:过滤后的字符串先被写入一个临时的
body容器,只有通过检查的干净内容才会进入后续的表格识别与粘贴逻辑。因此即使攻击者提供onerror事件属性,它也会在 ACF 阶段被剔除——事件属性(on*)属于典型的默认禁止内容,即便配置了img[*]放行图片的全部属性,onerror这类脚本注入属性依然不会通过过滤。
随后,pasteListener(plugin.js)通过 findTableInPastedContent 判断粘贴内容是否为表格,并在 isCustomPaste 的判定下决定走自定义的表格粘贴流程(按行列映射逐格替换、自动补行补列),还是退回默认粘贴逻辑(仅选中首个单元格、清空其余已选单元格)。无论走哪条分支,内容都已经过 dataProcessor 的过滤,这正是该测试文档所验证的安全契约。
从调用链上看,整个防护可以概括为:
粘贴事件 (clipboard 插件触发 'paste')
→ tableselection 的 fakeSelectionPasteHandler.onPaste
→ findTableInPastedContent
→ dataProcessor.toHtml(dataValue) ← XSS 载荷在此被 ACF 剥离
→ 临时 body 容器中解析过滤后的内容
→ 按表格/非表格分支完成粘贴
如何在本地复现与验收
要在本地运行这条手动测试,需要 CKEditor 4 的 Bender.js 测试环境(仓库根目录的 bender.js 与 bender-runner.config.json 已提供配置)。基本流程如下:
- 按仓库 README 说明安装依赖并启动 Bender 服务器;
- 在浏览器中打开
tests/plugins/tableselection/manual/integrations/clipboard/xss.html(或通过 Bender 的手动测试列表定位到 xss 用例); - 复制页面顶部带 "Copy me!" 替代文本的图片;
- 将其粘贴到编辑器内;
- 观察浏览器是否弹出 "MEGA FAIL!" 告警:
- 不弹窗 → 通过(expected result);
- 弹窗 → 回归失败,说明净化链路被绕过。
该目录下还存放着同属 trac16755 系列的其他 clipboard 集成测试,如 clipboard.md(单元格复制粘贴、Undo/Redo、粘贴后选中状态)、merge.md(合并单元格场景)与 mixedcontent.md(混合内容粘贴)等,可作为验证表格粘贴功能完整性的补充用例一并执行。
小结
xss.md 这份文档虽然只有短短十几行,却浓缩了 CKEditor 4 在表格选择场景下的安全设计核心:所有粘贴内容必须经过 dataProcessor + ACF 净化后才能进入文档模型,事件属性等脚本注入载体在源头上被剥离。通过「手动验收(xss.md + xss.html)+ 自动化断言(xss.js)」的双重保障,配合 allowedContent: 'img[*]' 的精确放行设计,仓库将「粘贴不执行脚本」固化为可重复验证的安全回归标准。理解这条测试,也就理解了 CKEditor 4 富文本安全边界的一道关键防线。