Skip to content

Commit

Permalink
Merge pull request #4841 from novuhq/nv-3026-on-update-button-click-c…
Browse files Browse the repository at this point in the history
…ss-styles-are-removed-in-in-app

feat(api): Allow style tags and attributes in the In-App & Email Editor
  • Loading branch information
jainpawan21 authored Nov 14, 2023
2 parents 0e8d5d8 + 45da02d commit 635af2b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
36 changes: 36 additions & 0 deletions apps/api/src/app/message-template/shared/sanitizer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,40 @@ describe('HTML Sanitizer', function () {
]);
expect(result[0].content).to.equal('hello <b>bold</b> ');
});

it('should NOT sanitize style tags', function () {
const result = sanitizeMessageContent([
{
type: EmailBlockTypeEnum.TEXT,
content: '<style>p { color: red; }</style><p>Red Text</p>',
url: '',
},
]);

expect(result[0].content).to.equal('<style>p { color: red; }</style><p>Red Text</p>');
});

it('should NOT sanitize style attributes', function () {
const result = sanitizeMessageContent([
{
type: EmailBlockTypeEnum.TEXT,
content: '<p style="color: red;">Red Text</p>',
url: '',
},
]);

expect(result[0].content).to.equal('<p style="color: red;">Red Text</p>');
});

it('should NOT format style attributes', function () {
const result = sanitizeMessageContent([
{
type: EmailBlockTypeEnum.TEXT,
content: '<p style="color:red;">Red Text</p>',
url: '',
},
]);

expect(result[0].content).to.equal('<p style="color:red;">Red Text</p>');
});
});
36 changes: 35 additions & 1 deletion apps/api/src/app/message-template/shared/sanitizer.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
import * as sanitize from 'sanitize-html';
import { IEmailBlock } from '@novu/shared';

/**
* Options for the sanitize-html library.
*
* @see https://www.npmjs.com/package/sanitize-html#default-options
*/
const sanitizeOptions: sanitize.IOptions = {
/**
* Additional tags to allow.
*/
allowedTags: sanitize.defaults.allowedTags.concat(['style']),
allowedAttributes: {
...sanitize.defaults.allowedAttributes,
/**
* Additional attributes to allow on all tags.
*/
'*': ['style'],
},
/**
* Required to disable console warnings when allowing style tags.
*
* We are allowing style tags to support the use of styles in the In-App Editor.
* This is a known security risk through an XSS attack vector,
* but we are accepting this risk by dropping support for IE11.
*
* @see https://cheatsheetseries.owasp.org/cheatsheets/XSS_Filter_Evasion_Cheat_Sheet.html#remote-style-sheet
*/
allowVulnerableTags: true,
/**
* Required to disable formatting of style attributes. This is useful to retain
* formatting of style attributes in the In-App Editor.
*/
parseStyleAttributes: false,
};

export function sanitizeHTML(html: string) {
if (!html) return html;

return sanitize(html);
return sanitize(html, sanitizeOptions);
}

export function sanitizeMessageContent(content: string | IEmailBlock[]) {
Expand Down

0 comments on commit 635af2b

Please sign in to comment.