fix(smtp-server): encode subject to UTF-8 before database insertion#3594
Open
DamienLGRCA wants to merge 1 commit into
Open
fix(smtp-server): encode subject to UTF-8 before database insertion#3594DamienLGRCA wants to merge 1 commit into
DamienLGRCA wants to merge 1 commit into
Conversation
Emails with subjects containing non-UTF-8 characters (e.g. ISO-8859-1 encoded accented characters like 'é' as raw \xE9) cause a Mysql2::Error 'Incorrect string value' when Postal attempts to insert the message into the database. This results in the email being silently dropped. This commit adds an encode_utf8 helper that: 1. Returns the string as-is if already valid UTF-8 2. Attempts to re-interpret bytes as UTF-8 3. Falls back to ISO-8859-1 to UTF-8 conversion (most common case for European accented characters) 4. As a last resort, replaces invalid characters with '?' This fixes the issue reported in postalserver#1636 where emails sent with ISO-8859-1 encoded subjects without RFC 2047 MIME encoding are rejected by MySQL.
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.
Problem
Emails with subjects containing raw non-UTF-8 characters (e.g. ISO-8859-1
\xE9for "é") without RFC 2047 MIME encoding causeMysql2::Error: Incorrect string valueduring insertion into the messages table. The email is silently dropped — never delivered to the endpoint.This is an issue that has existed since at least 2021 (see #1636). In environments receiving mail from legacy systems (ERPs, invoicing software), this can affect hundreds of emails per day.
Root cause
In
lib/postal/message_db/message.rb, the subject is extracted viaheaders["subject"]&.last.to_s[0, 200]and inserted as-is. TheMailgem'sfield.decodedcorrectly handles RFC 2047 encoded headers (=?ISO-8859-1?Q?...?=), but when the sender does not encode the subject per RFC 2047 (raw ISO-8859-1 bytes), the string is passed through unchanged. MySQL in strict mode then rejects the invalid UTF-8.Fix
Added a private
encode_utf8method that safely converts the subject to valid UTF-8:?Reproduction
Results in:
Closes #1636