-
-
Notifications
You must be signed in to change notification settings - Fork 425
Escape notification HTML device fields and fall back on indent errors #1744
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
+111
β19
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67bad01
Translated using Weblate (Portuguese (Brazil))
chisenberg 208fa92
Initial plan
Copilot 0197e7c
fix: escape notification HTML device fields
Copilot 44ed53c
test: cover escaped notification html fallback
Copilot 13da2dd
fix: use valid notification fallback log level
Copilot 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
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 |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| import html | ||
| import json | ||
| import re | ||
| import uuid | ||
| import socket | ||
| from yattag import indent | ||
| from yattag.indentation import XMLTokenError | ||
| from json2table import convert | ||
|
|
||
| # Register NetAlertX modules | ||
|
|
@@ -146,20 +148,7 @@ def create(self, JSON, Extra=""): | |
| mail_html, conf.REPORT_DASHBOARD_URL + "/deviceDetails.php?mac=" | ||
| ) | ||
|
|
||
| # Add preheader for inbox preview after all links have been generated. | ||
| # Invisible padding prevents email clients from showing the start of the email body. | ||
| preheader = " β’ ".join(preheaders) | ||
|
|
||
| padding = (" ‌ " * 47) | ||
|
|
||
| mail_html = mail_html.replace( | ||
| "PREHEADER", | ||
| preheader + padding, | ||
| ) | ||
|
|
||
| final_html = indent( | ||
| mail_html, indentation=" ", newline="\r\n", indent_text=True | ||
| ) | ||
| final_html = finalize_html(mail_html, preheaders) | ||
|
|
||
| send_api(self.JSON, final_text, final_html) | ||
|
|
||
|
|
@@ -335,8 +324,9 @@ def construct_notifications(JSON, section): | |
| text = tableTitle + "\n---------\n" | ||
|
|
||
| # Convert a JSON into an HTML table | ||
| html_data = escape_html_rows(jsn) | ||
| html = convert( | ||
| {"data": jsn}, | ||
| {"data": html_data}, | ||
| build_direction=build_direction, | ||
| table_attributes=table_attributes, | ||
| ) | ||
|
|
@@ -398,6 +388,45 @@ def format_table(html, thValue, props, newThValue=""): | |
| ) | ||
|
|
||
|
|
||
| # ----------------------------------------------------------------------------- | ||
| # Escape free-text values before embedding them into notification HTML | ||
| def escape_html_rows(rows): | ||
| """Return a copy of notification rows with only string values HTML-escaped.""" | ||
| return [ | ||
| { | ||
| key: html.escape(value) if isinstance(value, str) else value | ||
| for key, value in row.items() | ||
| } | ||
| for row in rows | ||
| ] | ||
|
|
||
|
|
||
| # ----------------------------------------------------------------------------- | ||
| # Finalize HTML and tolerate pretty-print failures | ||
| def finalize_html(mail_html, preheaders): | ||
| """Insert an escaped preheader and pretty-print HTML, falling back to raw HTML on XML errors.""" | ||
| # Add preheader for inbox preview after all links have been generated. | ||
| # Invisible padding prevents email clients from showing the start of the email body. | ||
| preheader = " β’ ".join(html.escape(entry) for entry in preheaders) | ||
| padding = (" ‌ " * 47) | ||
|
|
||
| mail_html = mail_html.replace( | ||
| "PREHEADER", | ||
| preheader + padding, | ||
| ) | ||
|
|
||
| try: | ||
| return indent( | ||
| mail_html, indentation=" ", newline="\r\n", indent_text=True | ||
| ) | ||
| except XMLTokenError as err: | ||
| mylog( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not existing log level passed - probably pass "none" - add as skill or learning |
||
| "none", | ||
| f"[Notification] Failed to pretty-print HTML report, sending unindented HTML instead: {err}", | ||
| ) | ||
| return mail_html | ||
|
|
||
|
|
||
| # ----------------------------------------------------------------------------- | ||
| # Pre-header Preview | ||
| def build_preheader(tableTitle, jsn, headers): | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π― Functional Correctness | π‘ Minor | β‘ Quick win
Correct the new pt-BR wording.
Cuidado.orAtenΓ§Γ£o.instead ofCuidadoso..Cuidadosois not a natural warning label.Ainda nΓ£o foi encontrado nenhum dispositivoforDevice_NoData_Title. The current text has incorrect agreement with singularnenhum dispositivo.Proposed wording
Also applies to: 216-216
π€ Prompt for AI Agents