Fix Unicode surrogate encoding errors in email sending#2106
Merged
duckduckgrayduck merged 2 commits intoMuckRock:masterfrom Jan 29, 2026
Merged
Fix Unicode surrogate encoding errors in email sending#2106duckduckgrayduck merged 2 commits intoMuckRock:masterfrom
duckduckgrayduck merged 2 commits intoMuckRock:masterfrom
Conversation
Contributor
|
Seeing pylint errors: ************* Module muckrock.foia.tests.test_foia_request |
Fixes pylint C0415 (import-outside-toplevel) errors.
duckduckgrayduck
approved these changes
Jan 29, 2026
jamditis
pushed a commit
to jamditis/muckrock
that referenced
this pull request
Mar 24, 2026
The existing surrogate sanitization covers email subject and body (PR MuckRock#2106), but attach_files_to_email() was missed. When chardet decodes a text file attachment with a misidentified encoding, the result can contain surrogate characters that crash email serialization. Also handles the edge case where chardet returns encoding=None (e.g., empty files) which would cause a TypeError on decode(). Addresses MuckRock#2098
3 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2098
The
foia_send_emailtask fails withUnicodeEncodeErrorwhen email subject or body contains surrogate characters (U+D800 to U+DFFF) that cannot be encoded in UTF-8.Adds a
sanitize_surrogatesutility function that replaces invalid surrogates before sending, applied to both subject and body insend_delayed_email.