Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(sheets-ui): sanitize clipboard HTML parsing (#6738)
白熱57f9ba830c80
diff --git a/packages/network/src/services/web-socket/__tests__/web-socket.service.spec.ts b/packages/network/src/services/web-socket/__tests__/web-socket.service.spec.tsindex a40ba99be7..be5e5fcf20 100644--- a/packages/network/src/services/web-socket/__tests__/web-socket.service.spec.ts+++ b/packages/network/src/services/web-socket/__tests__/web-socket.service.spec.ts@@ -70,10 +70,10 @@ describe('WebSocketService', () => {it('creates sockets, forwards events, and removes listeners on close', () => {const service = new WebSocketService();- const socket = service.createSocket('ws://example.com/socket');+ const socket = service.createSocket('wss://example.com/socket');expect(socket).not.toBeNull();- expect(socket?.URL).toBe('ws://example.com/socket');+ expect(socket?.URL).toBe('wss://example.com/socket');const createdSocket = FakeWebSocket.instances[0];const openEvents: Event[] = [];@@ -130,7 +130,7 @@ describe('WebSocketService', () => {const service = new WebSocketService();- expect(service.createSocket('ws://example.com/fail')).toBeNull();+ expect(service.createSocket('wss://example.com/fail')).toBeNull();expect(errorSpy).toHaveBeenCalled();errorSpy.mockRestore();diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/__tests__/utils.spec.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/__tests__/utils.spec.tsnew file mode 100644index 0000000000..dc16c5d4ac--- /dev/null+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/__tests__/utils.spec.ts@@ -0,0 +1,37 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import { describe, expect, it } from 'vitest';+import parseToDom from '../utils';++describe('parseToDom', () => {+ it('parses clipboard html without retaining active content sinks', () => {+ const dom = parseToDom(`+ <style>.rich-text { color: red; }</style>+ <div onclick="alert('xss')">foo\r\n bar</div>+ <script>window.__sheetPasteXss = true;</script>+ <table><tbody><tr><td>1</td></tr></tbody></table>+ <iframe src="https://example.com"></iframe>+ `);++ expect(dom.querySelector('style')?.textContent).toContain('.rich-text');+ expect(dom.querySelector('div')?.getAttribute('onclick')).toBeNull();+ expect(dom.querySelector('div')?.textContent).toBe('foo bar');+ expect(dom.querySelector('script')).toBeNull();+ expect(dom.querySelector('iframe')).toBeNull();+ expect(dom.querySelector('table td')?.textContent).toBe('1');+ });+});diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.tsindex 9ddbc9a9b8..b5376fe04f 100644--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts@@ -19,6 +19,8 @@ import type { ICellDataWithSpanInfo } from '../type';import { DataStreamTreeTokenType, Tools } from '@univerjs/core';import { ptToPixel } from '@univerjs/engine-render';+const STRIPPED_HTML_SELECTOR = 'script, iframe, object, embed';+function cleanTextNodes(node: Node) {if (node.nodeType === Node.TEXT_NODE) {/**@@ -36,11 +38,29 @@ function cleanTextNodes(node: Node) {node.childNodes.forEach(cleanTextNodes);}+function sanitizeParsedHtml(root: ParentNode) {+ root.querySelectorAll(STRIPPED_HTML_SELECTOR).forEach((element) => element.remove());+ root.querySelectorAll<HTMLElement>('*').forEach((element) => {+ for (const { name } of Array.from(element.attributes)) {+ if (name.startsWith('on')) {+ element.removeAttribute(name);+ }+ }+ });+}+export default function parseToDom(rawHtml: string) {- const template = document.createElement('body');- template.innerHTML = rawHtml;- cleanTextNodes(template);- return template;+ const doc = document.implementation.createHTMLDocument('');+ const range = doc.createRange();+ range.selectNodeContents(doc.body);++ const fragment = range.createContextualFragment(rawHtml);++ sanitizeParsedHtml(fragment);+ cleanTextNodes(fragment);+ doc.body.append(fragment);++ return doc.body;}// TODO: @JOCS, Complete other missing attributes that exist in IParagraphStyle