-
Notifications
You must be signed in to change notification settings - Fork 109
[MU] Fix missing email content form sanitized #4184
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
Closed
dab246
wants to merge
4
commits into
maintenance-v0.19.x
from
bugfix/mu-missing-email-content-form-sanitized
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b154507
fix(mu): email content not displayed due to form tag removal
dab246 ccebcd0
fix(mu): avoid unwanted word-break in table elements
dab246 f513e88
fix(mu): introduce `EMAIL_HTML_TAGS_TO_PRESERVE` for HTML sanitizatio…
dab246 f66d8b8
fix(mu): add unit test for sanitize html with `form` tag
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
26 changes: 26 additions & 0 deletions
26
core/lib/presentation/utils/html_transformer/html_sanitize_config.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,26 @@ | ||
| import 'package:flutter_dotenv/flutter_dotenv.dart'; | ||
|
|
||
| import 'package:core/utils/app_logger.dart'; | ||
|
|
||
| class HtmlSanitizeConfig { | ||
| /// Safely load list of HTML tags from `EMAIL_HTML_TAGS_TO_PRESERVE` | ||
| static List<String> loadPreservedHtmlTags() { | ||
| try { | ||
| final raw = dotenv.get('EMAIL_HTML_TAGS_TO_PRESERVE', fallback: ''); | ||
|
|
||
| if (raw.trim().isEmpty) { | ||
| return const []; | ||
| } | ||
|
|
||
| return raw | ||
| .split(',') | ||
| .map((e) => e.trim()) | ||
| .where((e) => e.isNotEmpty) | ||
| .toSet() | ||
| .toList(growable: false); | ||
| } catch (e) { | ||
| logError('HtmlSanitizeConfig::loadPreservedHtmlTags:Exception = $e'); | ||
| return const []; | ||
| } | ||
| } | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import 'package:core/presentation/utils/html_transformer/html_sanitize_config.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:flutter_dotenv/flutter_dotenv.dart'; | ||
|
|
||
| void main() { | ||
| setUp(() { | ||
| dotenv.testLoad(); | ||
| }); | ||
|
|
||
| group('HtmlSanitizeConfig.loadPreservedHtmlTags', () { | ||
| test('returns empty list when env variable is missing', () { | ||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect(result, isEmpty); | ||
| }); | ||
|
|
||
| test('returns empty list when env variable is empty string', () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': '', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect(result, isEmpty); | ||
| }); | ||
|
|
||
| test('returns empty list when env only contains spaces', () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': ' ', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect(result, isEmpty); | ||
| }); | ||
|
|
||
| test('parses a valid comma-separated list of tags', () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': 'form,section,figure', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect(result, ['form', 'section', 'figure']); | ||
| }); | ||
|
|
||
| test('trims whitespace around tags', () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': ' form , section , figure ', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect(result, ['form', 'section', 'figure']); | ||
| }); | ||
|
|
||
| test('ignores empty entries after splitting', () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': 'form,, ,section,,', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect(result, ['form', 'section']); | ||
| }); | ||
|
|
||
| test('parses tags containing hyphens, underscores, and uppercase letters', | ||
| () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': | ||
| 'CUSTOM-TAG, custom_tag , MyTag , X-LARGE, label_item', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect( | ||
| result, | ||
| [ | ||
| 'CUSTOM-TAG', | ||
| 'custom_tag', | ||
| 'MyTag', | ||
| 'X-LARGE', | ||
| 'label_item', | ||
| ], | ||
| ); | ||
| }); | ||
|
|
||
| test('handles mixed-case and special-character tags with extra spaces', () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': | ||
| ' My-Tag , MY_TAG , ultra-WIDE ,Test_TAG ', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect( | ||
| result, | ||
| [ | ||
| 'My-Tag', | ||
| 'MY_TAG', | ||
| 'ultra-WIDE', | ||
| 'Test_TAG', | ||
| ], | ||
| ); | ||
| }); | ||
|
|
||
| test('removes duplicate tags even with different spacing', () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': | ||
| 'form, form , FORM, form , section , SECTION ', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect( | ||
| result, | ||
| [ | ||
| 'form', | ||
| 'FORM', | ||
| 'section', | ||
| 'SECTION', | ||
| ], | ||
| ); | ||
| }); | ||
|
|
||
| test('parses tags containing numbers', () { | ||
| dotenv.testLoad(mergeWith: { | ||
| 'EMAIL_HTML_TAGS_TO_PRESERVE': | ||
| 'tag123, v2-tag, item_01, BLOCK100, x-apple-123', | ||
| }); | ||
|
|
||
| final result = HtmlSanitizeConfig.loadPreservedHtmlTags(); | ||
|
|
||
| expect( | ||
| result, | ||
| [ | ||
| 'tag123', | ||
| 'v2-tag', | ||
| 'item_01', | ||
| 'BLOCK100', | ||
| 'x-apple-123', | ||
| ], | ||
| ); | ||
| }); | ||
| }); | ||
| } |
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
64 changes: 64 additions & 0 deletions
64
...69-introduce-email-html-tags-to-preserve-for-html-sanitization-extensibility.md
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,64 @@ | ||
| # 69. Introduce `EMAIL_HTML_TAGS_TO_PRESERVE` for HTML sanitization extensibility | ||
|
|
||
| **Date:** 2025-11-27 | ||
|
|
||
| ## Status | ||
| Accepted | ||
|
|
||
| ## Context | ||
|
|
||
| The email sanitization pipeline uses the class `StandardizeHtmlSanitizingTransformers` to process and clean incoming HTML content before rendering it in the UI. | ||
|
|
||
| This class defines internal static allowlists: | ||
|
|
||
| - `mailAllowedHtmlAttributes` | ||
| - `mailAllowedHtmlTags` | ||
|
|
||
| These default allowlists are fixed at compile-time. When new HTML tags appear in real-world emails (for example: `<form>`, `<section>`, `<font>`, `<center>`, or custom tags generated by email gateways or online editors), they may be stripped by the sanitization process. | ||
|
|
||
| Some environments or deployments may require additional custom tags to be preserved depending on operational constraints, customer integrations, or specific email formatting use cases. | ||
|
|
||
| Without configurability, adjusting the sanitization behavior requires code modifications and redeployment, which is not desirable. | ||
|
|
||
| ## Decision | ||
|
|
||
| Introduce a new environment variable: **`EMAIL_HTML_TAGS_TO_PRESERVE`**. | ||
|
|
||
| This variable contains a comma-separated list of HTML tags that should be appended to `mailAllowedHtmlTags` at runtime. | ||
|
|
||
| The `StandardizeHtmlSanitizingTransformers` class will be updated to: | ||
|
|
||
| 1. Read `EMAIL_HTML_TAGS_TO_PRESERVE` from the env configuration. | ||
| 2. Parse the list into a trimmed `List<String>`. | ||
| 3. Merge these tags into the existing `mailAllowedHtmlTags` list. | ||
| 4. Ensure the final allowlist (default tags + preserve tags) is passed to `SanitizeHtml().process()` via the `allowTags` parameter. | ||
|
|
||
| The order of sanitization remains unchanged: | ||
|
|
||
| 1. tags (including merged allowlist) | ||
| 2. attributes | ||
| 3. className | ||
|
|
||
| ### Example usage inside the transformer: | ||
|
|
||
| ```dart | ||
| final extendedTags = [ | ||
| ...mailAllowedHtmlTags, | ||
| ...env.EMAIL_HTML_TAGS_TO_PRESERVE, | ||
| ]; | ||
| ``` | ||
|
|
||
| ## Consequences | ||
|
|
||
| - The sanitization behavior becomes runtime-configurable and environment-specific. | ||
| - Ops teams can adjust preserved HTML tags without modifying source code or triggering a deployment. | ||
| - Rendering fidelity improves in cases where forwarded email chains, enterprise gateways, or WYSIWYG editors introduce non-standard HTML tags. | ||
| - The default behavior remains safe, while new tags can be adopted gradually and controllably. | ||
| - Any future modifications to tag/attribute/class preservation logic must be reflected in this ADR or new ADRs to maintain traceability. | ||
|
|
||
| ## Example | ||
|
|
||
| ### `.env` | ||
| ``` | ||
| EMAIL_HTML_TAGS_TO_PRESERVE=form,section,figure,figcaption | ||
| ``` |
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
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.