Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(designer-ui): Prevent certain malicious HTML from executing in raw HTML editor #4554

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions libs/designer-ui/src/lib/html/plugins/toolbar/helper/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { encodeStringSegmentTokensInDomContext } from '../../../../editor/base/utils/parsesegments';
import type { ValueSegment } from '@microsoft/logic-apps-shared';
import DomPurify from 'dompurify';

const htmlUnsafeCharacters = ['<', '>'];
const htmlUnsafeCharacterEncodingMap: Record<string, string> = htmlUnsafeCharacters.reduce(
Expand Down Expand Up @@ -95,12 +96,17 @@ export const encodeOrDecodeSegmentValue = (value: string, encodingMap: Record<st
};

export const getDomFromHtmlEditorString = (htmlEditorString: string, nodeMap: Map<string, ValueSegment>): HTMLElement => {
const encodedHtmlEditorString = encodeStringSegmentTokensInDomContext(htmlEditorString, nodeMap);
// Comments at the start of a DOM are lost when parsing HTML strings, so we wrap the HTML string in a <div>.
const wrappedHtmlEditorString = `<div>${htmlEditorString}</div>`;

const tempElement = document.createElement('div');
const purifiedHtmlEditorString = DomPurify.sanitize(wrappedHtmlEditorString, { ADD_TAGS: ['#comment'] });
const encodedHtmlEditorString = encodeStringSegmentTokensInDomContext(purifiedHtmlEditorString, nodeMap);

const tempElement = document.createElement('div', {});
tempElement.innerHTML = encodedHtmlEditorString;

return tempElement;
// Unwrap the wrapper <div>.
return tempElement.children[0] as HTMLElement;
};

export const isAttributeSupportedByHtmlEditor = (tagName: string, attribute: string): boolean => {
Expand Down
42 changes: 42 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"consola": "2.15.3",
"core-js": "3.24.1",
"d3-shape": "3.1.0",
"dompurify": "^3.0.11",
"elkjs": "^0.8.2",
"fuse.js": "6.6.2",
"github-markdown-css": "5.1.0",
Expand Down Expand Up @@ -153,6 +154,7 @@
"@testing-library/react": "13.4.0",
"@types/adm-zip": "^0.5.1",
"@types/d3-shape": "3.1.1",
"@types/dompurify": "^3.0.5",
"@types/jest": "^28.1.8",
"@types/js-yaml": "4.0.5",
"@types/lodash.frompairs": "^4.0.1",
Expand Down
Loading