Skip to content

Commit

Permalink
Trust HTML for rich text clipboard (#6755)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx authored Oct 22, 2024
1 parent 4e1a3f4 commit 409c65e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"@types/prismjs": "^1.26.0",
"@types/react": "^18.0.8",
"@types/react-dom": "^18.0.3",
"@types/trusted-types": "^2.0.7",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"child-process-promise": "^2.2.1",
Expand Down
15 changes: 14 additions & 1 deletion packages/lexical-clipboard/src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ export function $insertDataTransferForRichText(
if (htmlString) {
try {
const parser = new DOMParser();
const dom = parser.parseFromString(htmlString, 'text/html');
const dom = parser.parseFromString(
trustHTML(htmlString) as string,
'text/html',
);
const nodes = $generateNodesFromDOM(editor, dom);
return $insertGeneratedNodes(editor, nodes, selection);
} catch {
Expand Down Expand Up @@ -192,6 +195,16 @@ export function $insertDataTransferForRichText(
}
}

function trustHTML(html: string): string | TrustedHTML {
if (window.trustedTypes && window.trustedTypes.createPolicy) {
const policy = window.trustedTypes.createPolicy('lexical', {
createHTML: (input) => input,
});
return policy.createHTML(html);
}
return html;
}

/**
* Inserts Lexical nodes into the editor using different strategies depending on
* some simple selection-based heuristics. If you're looking for a generic way to
Expand Down

0 comments on commit 409c65e

Please sign in to comment.