-
Notifications
You must be signed in to change notification settings - Fork 17.6k
fix(reports): ensure CSV attachments use UTF-8 BOM when encoding is utf-8-sig #37245
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -506,6 +506,20 @@ def _get_pdf(self) -> bytes: | |
|
|
||
| return pdf | ||
|
|
||
| def _ensure_utf8_bom(self, csv_data: bytes, encoding: str) -> bytes: | ||
| """ | ||
| Ensure CSV bytes contain UTF-8 BOM when encoding is utf-8-sig. | ||
| Avoid double BOM. | ||
| """ | ||
| if not csv_data: | ||
| return csv_data | ||
|
|
||
| enc = (encoding or "").lower().replace("_", "-") | ||
| if enc in ("utf-8-sig", "utf8-sig"): | ||
| if not csv_data.startswith(b"\xef\xbb\xbf"): | ||
| return b"\xef\xbb\xbf" + csv_data | ||
| return csv_data | ||
|
Comment on lines
+509
to
+521
|
||
|
|
||
| def _get_csv_data(self) -> bytes: | ||
| start_time = datetime.utcnow() | ||
| url = self._get_url(result_format=ChartDataResultFormat.CSV) | ||
|
|
@@ -550,6 +564,9 @@ def _get_csv_data(self) -> bytes: | |
| ) from ex | ||
| if not csv_data: | ||
| raise ReportScheduleCsvFailedError() | ||
|
|
||
| encoding = app.config.get("CSV_EXPORT", {}).get("encoding", "utf-8") | ||
| csv_data = self._ensure_utf8_bom(csv_data, encoding) | ||
| return csv_data | ||
|
|
||
| def _get_embedded_data(self) -> pd.DataFrame: | ||
|
|
||
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.
The encoding normalization at line 447 replaces underscores with hyphens, converting both "utf_8_sig" and "utf-8-sig" to "utf-8-sig". Therefore, checking for "utf8-sig" (without any separator) in line 448 is unnecessary after this normalization.
Consider simplifying to just check for "utf-8-sig":
Alternatively, if you want to handle "utf8sig" (no separator), you could check for both "utf-8-sig" and "utf8sig", but the current check for "utf8-sig" (with hyphen but no separators elsewhere) won't match any real input after normalization.