Skip to contentSkip to conversation

git-patchwork-bot: prevent wrong encoded split in To: field

PATCH New Alexis Lothoré 1 participant 1 message Aug 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-patchwork-bot: prevent wrong encoded split in To: field

notify_submitters() build the submitter To: header by RFC 2047-encoding the entire 'Name <addr>' string through email.header.Header(). For long non-ASCII display names the encoded-word exceeds the 75-character folding limit and is split mid-token, shredding the adjacent address; MTAs may then interpret the leftover fragments as bare local-parts and append a hostname, producing a mangled and undeliverable To: header (e.g. '...@aws-us-west-2-korg-oddjob- rhel9-1.codeaurora.org').

Add the _format_recipient() helper which uses email.utils.formataddr() to encode only the display name while keeping the bare <addr> literal and intact. While at it, make sure to use the same logic for both branches generating the 'To' field. Add a small test validating that the address part remains untouched.

---
An example of the issue in the wild can be found in [1], resulting in the submitter never receiving the merge confirmation mail.

[1] https://lore.kernel.org/bpf/178734001048.1447337.676425481174448454.git-patchwork-notify@kernel.org/

---
 git-patchwork-bot.py        | 12 +++++++++---
 tests/test_patchwork_bot.py | 25 +++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 3 deletions(-)
git-patchwork-bot.py +9 -3
index 0fa112e85262..2aa5b2db6037 100755
@@ -32,7 +32,7 @@ import time
 from email import charset
 from email.header import Header
 from email.mime.text import MIMEText
-from email.utils import formatdate, getaddresses, make_msgid
+from email.utils import formataddr, formatdate, getaddresses, make_msgid
 from fcntl import LOCK_EX, LOCK_NB, lockf
 from string import Template
 from typing import Any, Dict, List, Optional, Set, Tuple, Union
@@ -998,6 +998,12 @@ def send_summary(serieslist: List[Dict[str, Any]],
     return str(msg['Message-Id'])
 
 
+def _format_recipient(name: Optional[str], addr: str) -> str:
+    """Build a 'Name <addr>' header value, encoding only the display name."""
+
+    return formataddr((name or '', addr))
+
+
 def get_tweaks(pconfig: Dict[str, str], hconfig: Dict[str, Any]) -> Dict[str, str]:
     fields = ['from', 'summaryto', 'onlyto', 'neverto', 'onlyifcc', 'neverifcc',
               'alwayscc', 'alwaysbcc', 'cclist', 'ccall']
@@ -1155,10 +1161,10 @@ def notify_submitters(serieslist: List[Dict[str, Any]],
 
         if 'onlyto' in tweaks:
             targets = listify(tweaks['onlyto'])
-            msg['To'] = '%s <%s>' % (submitter.get('name'), targets[0])
+            msg['To'] = _format_recipient(submitter.get('name'), targets[0])
         else:
             targets = [submitter.get('email')]
-            msg['To'] = Header('%s <%s>' % (submitter.get('name'), submitter.get('email'))).encode()
+            msg['To'] = _format_recipient(submitter.get('name'), submitter.get('email'))
 
             ccaddrs: List[str] = list()
             if tweaks.get('alwayscc'):
tests/test_patchwork_bot.py +25 -0
index 2a6a6c53a887..658dbe523d8d 100644
@@ -488,6 +488,31 @@ class TestGetTweaks:
             assert result[f] == f'p-{f}'
 
 
+class TestFormatRecipient:
+    """Tests for _format_recipient() header construction."""
+
+    def test_ascii_name_and_addr(self):
+        value = pwbot._format_recipient('Alexis Lothe', '[email protected]')
+        assert value == 'Alexis Lothe <[email protected]>'
+
+    def test_name_without_addr_falls_back(self):
+        value = pwbot._format_recipient(None, '[email protected]')
+        assert value == '[email protected]'
+
+        value = pwbot._format_recipient('', '[email protected]')
+        assert value == '[email protected]'
+
+    def test_nonascii_name_keeps_addr_intact(self):
+        name = 'Alexis Lothoré (eBPF Foundation)'
+        addr = '[email protected]'
+        value = pwbot._format_recipient(name, addr)
+        from email.utils import getaddresses
+        (parsed_name, parsed_addr), = getaddresses([value])
+        assert parsed_addr == addr
+        assert addr in value
+        assert '<%s>' % addr in value
+
+
 class TestGitGetNewRevs:
     """Tests for git_get_new_revs() with mocked git output."""
 
Signature · 1 line
2.55.0