-
Notifications
You must be signed in to change notification settings - Fork 103
Standardize HTML sanitizing when preview email #3223
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f64cc96
Standardize HTML sanitizing when preview email
dab246 3efed22
fixup! Standardize HTML sanitizing when preview email
dab246 ceb14bd
fixup! fixup! Standardize HTML sanitizing when preview email
dab246 a038b8b
fixup! fixup! fixup! Standardize HTML sanitizing when preview email
dab246 342f5e1
fixup! fixup! fixup! fixup! Standardize HTML sanitizing when preview …
dab246 14040ad
fixup! fixup! fixup! fixup! fixup! Standardize HTML sanitizing when p…
dab246 4ffca69
fixup! fixup! fixup! fixup! fixup! fixup! Standardize HTML sanitizing…
dab246 d5dff90
fixup! fixup! fixup! fixup! fixup! fixup! fixup! Standardize HTML san…
dab246 f19ac7d
fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Standardize H…
dab246 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
core/lib/presentation/utils/html_transformer/sanitize_html.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import 'package:sanitize_html/sanitize_html.dart'; | ||
|
|
||
| class SanitizeHtml { | ||
| String process({ | ||
| required String inputHtml, | ||
| List<String>? allowAttributes, | ||
| List<String>? allowTags, | ||
| }) { | ||
| final outputHtml = sanitizeHtml( | ||
| inputHtml, | ||
| allowAttributes: allowAttributes, | ||
| allowTags: allowTags, | ||
| ); | ||
| return outputHtml; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ib/presentation/utils/html_transformer/text/standardize_html_sanitizing_transformers.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import 'dart:convert'; | ||
| import 'package:core/presentation/utils/html_transformer/base/text_transformer.dart'; | ||
| import 'package:core/presentation/utils/html_transformer/sanitize_html.dart'; | ||
|
|
||
| class StandardizeHtmlSanitizingTransformers extends TextTransformer { | ||
|
|
||
| static const List<String> mailAllowedHtmlAttributes = [ | ||
| 'style', | ||
| 'public-asset-id', | ||
| 'data-filename', | ||
| 'bgcolor', | ||
| 'id', | ||
| 'class', | ||
| ]; | ||
|
|
||
| static const List<String> mailAllowedHtmlTags = [ | ||
| 'font', | ||
| 'u', | ||
| 'center', | ||
| 'style', | ||
| 'body', | ||
| ]; | ||
|
|
||
| const StandardizeHtmlSanitizingTransformers(); | ||
|
|
||
| @override | ||
| String process(String text, HtmlEscape htmlEscape) => | ||
| SanitizeHtml().process( | ||
hoangdat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| inputHtml: text, | ||
| allowAttributes: mailAllowedHtmlAttributes, | ||
| allowTags: mailAllowedHtmlTags, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
core/test/utils/standardize_html_sanitizing_transformers_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import 'package:core/presentation/utils/html_transformer/text/standardize_html_sanitizing_transformers.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'dart:convert'; | ||
|
|
||
| void main() { | ||
| group('StandardizeHtmlSanitizingTransformers::test', () { | ||
| const transformer = StandardizeHtmlSanitizingTransformers(); | ||
| const htmlEscape = HtmlEscape(); | ||
|
|
||
| test('SHOULD remove all `on*` attributes tag', () { | ||
| const listOnEventAttributes = [ | ||
| 'mousedown', | ||
| 'mouseenter', | ||
| 'mouseleave', | ||
| 'mousemove', | ||
| 'mouseover', | ||
| 'mouseout', | ||
| 'mouseup', | ||
| 'load', | ||
| 'unload', | ||
| 'loadstart', | ||
| 'loadeddata', | ||
| 'loadedmetadata', | ||
| 'playing', | ||
| 'show', | ||
| 'error', | ||
| 'message', | ||
| 'focus', | ||
| 'focusin', | ||
| 'focusout', | ||
| 'keydown', | ||
| 'keydpress', | ||
| 'keydup', | ||
| 'input', | ||
| 'ended', | ||
| 'drag', | ||
| 'drop', | ||
| 'dragstart', | ||
| 'dragover', | ||
| 'dragleave', | ||
| 'dragend', | ||
| 'dragenter', | ||
| 'beforeunload', | ||
| 'beforeprint', | ||
| 'afterprint', | ||
| 'blur', | ||
| 'click', | ||
| 'change', | ||
| 'contextmenu', | ||
| 'cut', | ||
| 'copy', | ||
| 'dblclick', | ||
| 'abort', | ||
| 'durationchange', | ||
| 'progress', | ||
| 'resize', | ||
| 'reset', | ||
| 'scroll', | ||
| 'seeked', | ||
| 'select', | ||
| 'submit', | ||
| 'toggle', | ||
| 'volumechange', | ||
| 'touchstart', | ||
| 'touchmove', | ||
| 'touchend', | ||
| 'touchcancel' | ||
| ]; | ||
|
|
||
| for (var i = 0; i < listOnEventAttributes.length; i++) { | ||
| final inputHtml = '<img src="1" href="1" on${listOnEventAttributes[i]}="javascript:alert(1)">'; | ||
| final result = transformer.process(inputHtml, htmlEscape); | ||
|
|
||
| expect(result, equals('<img src="1">')); | ||
| } | ||
| }); | ||
|
|
||
| test('SHOULD remove all `on*` attributes for any tags', () { | ||
| const listOnEventAttributes = [ | ||
| 'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseover', | ||
| 'mouseout', 'mouseup', 'load', 'unload', 'loadstart', 'loadeddata', | ||
| 'loadedmetadata', 'playing', 'show', 'error', 'message', 'focus', | ||
| 'focusin', 'focusout', 'keydown', 'keypress', 'keyup', 'input', 'ended', | ||
| 'drag', 'drop', 'dragstart', 'dragover', 'dragleave', 'dragend', 'dragenter', | ||
| 'beforeunload', 'beforeprint', 'afterprint', 'blur', 'click', 'change', | ||
| 'contextmenu', 'cut', 'copy', 'dblclick', 'abort', 'durationchange', | ||
| 'progress', 'resize', 'reset', 'scroll', 'seeked', 'select', 'submit', | ||
| 'toggle', 'volumechange', 'touchstart', 'touchmove', 'touchend', 'touchcancel' | ||
| ]; | ||
|
|
||
| const listHTMLTags = [ | ||
| 'div', 'span', 'p', 'a', 'u', 'i', 'table' | ||
| ]; | ||
|
|
||
| for (var tag in listHTMLTags) { | ||
| for (var event in listOnEventAttributes) { | ||
| final inputHtml = '<$tag on$event="javascript:alert(1)"></$tag>'; | ||
| final result = transformer.process(inputHtml, htmlEscape); | ||
|
|
||
| expect(result, equals('<$tag></$tag>')); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test('SHOULD remove attributes of IMG tag WHEN they are invalid', () { | ||
| const inputHtml = '<img src="1" href="1" onerror="javascript:alert(1)">'; | ||
hoangdat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final result = transformer.process(inputHtml, htmlEscape); | ||
|
|
||
| expect(result, equals('<img src="1">')); | ||
| }); | ||
|
|
||
| test('SHOULD remove all SCRIPTS tags', () { | ||
| const inputHtml = '<script>alert("This is an alert message!");</script>'; | ||
| final result = transformer.process(inputHtml, htmlEscape).trim(); | ||
|
|
||
| expect(result, equals('')); | ||
| }); | ||
|
|
||
| test('SHOULD remove all IFRAME tags', () { | ||
| const inputHtml = '<iframe style="xg-p:absolute;top:0;left:0;width:100%;height:100%" onmouseover="prompt(1)">'; | ||
| final result = transformer.process(inputHtml, htmlEscape); | ||
|
|
||
| expect(result, equals('')); | ||
| }); | ||
|
|
||
| test('SHOULD remove href attribute of A tag WHEN it is invalid', () { | ||
| const inputHtml = '<a href="javascript:alert(1)" id="id1">test</a>'; | ||
| final result = transformer.process(inputHtml, htmlEscape); | ||
|
|
||
| expect(result, equals('<a id="id1">test</a>')); | ||
| }); | ||
|
|
||
| test('SHOULD persist value src attribute of IMG tag WHEN it is base64 string', () { | ||
| const inputHtml = '<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA">'; | ||
| final result = transformer.process(inputHtml, htmlEscape); | ||
|
|
||
| expect(result, equals('<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA">')); | ||
| }); | ||
|
|
||
| test('SHOULD persist value src attribute of IMG tag WHEN it is CID string', () { | ||
| const inputHtml = '<img src="cid:email123">'; | ||
| final result = transformer.process(inputHtml, htmlEscape); | ||
|
|
||
| expect(result, equals('<img src="cid:email123">')); | ||
| }); | ||
| }); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.