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

feat(editor): Implement HTML sanitization for Notification and Message components #4081

Merged
merged 5 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
88 changes: 88 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 packages/editor-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"luxon": "^2.3.0",
"monaco-editor": "^0.30.1",
"n8n-design-system": "~0.33.1",
"sanitize-html": "^2.7.1",
"timeago.js": "^4.0.2",
"v-click-outside": "^3.1.2",
"vue-fragment": "1.5.1",
Expand All @@ -52,6 +53,7 @@
"@types/luxon": "^2.0.9",
"@types/node": "^16.11.22",
"@types/quill": "^2.0.1",
"@types/sanitize-html": "^2.6.2",
"@types/uuid": "^8.3.2",
"@vue/cli-plugin-babel": "~4.5.19",
"@vue/cli-plugin-typescript": "~4.5.19",
Expand Down
3 changes: 2 additions & 1 deletion packages/editor-ui/src/components/PageAlert.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import mixins from 'vue-typed-mixins';

import { showMessage } from './mixins/showMessage';
import { ElMessageComponent } from 'element-ui/types/message';
import { sanitizeHtml } from '@/utils';

export default mixins(
showMessage,
Expand All @@ -28,7 +29,7 @@ export default mixins(
},
mounted() {
this.alert = this.$showAlert({
message: this.message,
message: sanitizeHtml(this.message),
type: 'warning',
duration: 0,
showClose: true,
Expand Down
9 changes: 7 additions & 2 deletions packages/editor-ui/src/components/mixins/showMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ExecutionError } from 'n8n-workflow';
import { ElMessageBoxOptions } from 'element-ui/types/message-box';
import { ElMessage, ElMessageComponent, ElMessageOptions, MessageType } from 'element-ui/types/message';
import { isChildOf } from './helpers';
import { sanitizeHtml } from '@/utils';

let stickyNotificationQueue: ElNotificationComponent[] = [];

Expand All @@ -17,6 +18,8 @@ export const showMessage = mixins(externalHooks).extend({
track = true,
) {
messageData.dangerouslyUseHTMLString = true;
OlegIvaniv marked this conversation as resolved.
Show resolved Hide resolved
messageData.message = messageData.message ? sanitizeHtml(messageData.message) : messageData.message;

if (messageData.position === undefined) {
messageData.position = 'bottom-right';
}
Expand Down Expand Up @@ -159,7 +162,8 @@ export const showMessage = mixins(externalHooks).extend({
...(type && { type }),
};

await this.$confirm(message, headline, options);
const sanitizedMessage = sanitizeHtml(message);
await this.$confirm(sanitizedMessage, headline, options);
return true;
} catch (e) {
return false;
Expand All @@ -176,7 +180,8 @@ export const showMessage = mixins(externalHooks).extend({
...(type && { type }),
};

await this.$confirm(message, headline, options);
const sanitizedMessage = sanitizeHtml(message);
await this.$confirm(sanitizedMessage, headline, options);
return 'confirmed';
} catch (e) {
return e as string;
Expand Down
11 changes: 11 additions & 0 deletions packages/editor-ui/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sanitizeHtmlModule from 'sanitize-html';
OlegIvaniv marked this conversation as resolved.
Show resolved Hide resolved

export const omit = (keyToOmit: string, { [keyToOmit]: _, ...remainder }) => remainder;

export function isObjectLiteral(maybeObject: unknown): maybeObject is { [key: string]: string } {
Expand All @@ -12,3 +14,12 @@ export function isJsonKeyObject(item: unknown): item is {

return Object.keys(item).includes('json');
}

export function sanitizeHtml(dirtyHtml: string) {
return sanitizeHtmlModule(dirtyHtml, {
allowedAttributes: {
'*': ['href','name', 'target', 'data-*', 'title'],
},
allowedTags: ['p', 'strong', 'b', 'code', 'a', 'br', 'i', 'em', 'small' ],
});
}