Repository navigation
fix(email): keep display name of Exchange sender in .msg files - #5420
Conversation
Merge Protections🟢 All 2 merge protections satisfied — ready to merge. Show 2 satisfied protections🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
🟢 Require two reviewer for test updatesWhen test data is updated, we require two reviewers
|
For .msg files sent inside an Exchange organisation, python-oxmsg returns the sender as the display name with the legacy Exchange DN in the angle brackets (e.g. "Display Name </O=ORG/OU=.../CN=...>"), which is not a valid addr-spec. mailparser drops the whole mailbox, so convert() emitted no From: line at all. When the sender has no SMTP address, write the display name alone into the projected From header and carry it through as a fallback so convert() renders it. A recorded PidTagSenderSmtpAddress is preferred when present (oxmsg's sender string shows the DN even then), and senders with an SMTP address are passed through unchanged. Adds a regression test with a synthetic .msg fixture whose sender is an internal Exchange sender, plus unit tests for the sender handling. Fixes docling-project#5415 Signed-off-by: Junyi Yao <[email protected]>
86de8be to
65c89a8
Compare
|
✅ DCO Check Passed Thanks @Kylinny, all your commits are properly signed off. 🎉 |
AI triage (advisory)Checked commit This PR fixes the missing CI safety: no concern found. A maintainer must still approve CI. 2 related pull request(s) in the same code
The |
There was a problem hiding this comment.
AI first review (advisory): no blocker found
This review covers the full diff at 65c89a81c6. It read the code and did not run it.
I found no defect in the .msg sender change. The sender split, the display-name fallback and the getaddresses and unquote handling are consistent with _quote_display_name and _format_addresses. I could not read the python-oxmsg source, because it is not installed in the workspace. The calls message.properties.str_prop_value(...) and message.sender are therefore unconfirmed. The existing code already calls recipient.properties.int_prop_value, which is a close sibling API.
A maintainer makes the merge decision. This review never approves the pull request.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
DanielNg0729
left a comment
There was a problem hiding this comment.
Hi @Kylinny, thank you for the fix and the clear write-up! I ran the PR here: the 47 email tests pass, and the new fixture renders From: "Doe, Jonathan" where main shows no From: line. I may be missing something, but two things came up when I tried other sender strings:
-
A real legacy DN puts "(FYDIBOHF23SPDLT)" into the name. Since Exchange 2007, and in Exchange Online, the DN usually looks like
/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=....getaddresses()reads the part in parentheses as a comment and appends it to the display name:>>> getaddresses(['"Doe, Jonathan" </O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=4F5E6A7B8C9D-JDOE>']) [('Doe, Jonathan (FYDIBOHF23SPDLT)', '/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP /CN=RECIPIENTS/CN=4F5E6A7B8C9D-JDOE')]
So
_msg_sender_headerreturns"Doe, Jonathan (FYDIBOHF23SPDLT)", and with a recorded SMTP address it returns"Doe, Jonathan (FYDIBOHF23SPDLT)" <[email protected]>. I see the same on Python 3.10, 3.12, 3.13 and 3.14. To check it end to end, I changed the DN in your fixture to/O=EX (FYD)/CN=U1(same length), and the output isFrom: "Doe, Jonathan (FYD)". When the sender string has angle brackets, python-oxmsg built it from PidTagSenderName, which the function already reads. Would it make sense to prefer that property over the parsed name? It also keeps the quotes in a name such asJonathan "JD" Doe, which the parse turns intoJonathan JD Doe. -
A bare name with a comma still loses the
From:line (minor). When no sender address is recorded, python-oxmsg returns PidTagSentRepresentingName without quotes, e.g.Doe, Jonathan.getaddresses()splits that into two entries, so the multi-address branch passes it through unchanged, and mailparser drops it. The unit test uses'"Doe, Jonathan"'with quotes, so it does not catch this. Checking for a bare name beforegetaddresses()covers it.
Something like the suggestion below fixes both. With it, your 47 tests and the test below pass, and ruff is clean:
def test_msg_sender_header_ignores_parentheses_in_legacy_dn():
"""A real legacy DN holds "(FYDIBOHF23SPDLT)"; it must not reach the name."""
header = EmailDocumentBackend._msg_sender_header
dn = (
"/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)"
"/CN=RECIPIENTS/CN=4F5E6A7B8C9D-JDOE"
)
sender = f'"Doe, Jonathan" <{dn}>'
assert header(sender, "Doe, Jonathan", None) == (
'"Doe, Jonathan"',
'"Doe, Jonathan"',
)
assert header(sender, "Doe, Jonathan", "[email protected]") == (
'"Doe, Jonathan" <[email protected]>',
"",
)
# oxmsg returns PidTagSentRepresentingName unquoted.
assert header("Doe, Jonathan", None, None) == (
'"Doe, Jonathan"',
'"Doe, Jonathan"',
)A heads-up, too: #5439 changes the same From block in _msg_to_rfc822_bytes and wraps the oxmsg string reads in except (UnicodeDecodeError, LookupError). The two PRs conflict there, so the new str_prop_value() reads for 0x0C1A and 0x5D01 may need the same guard after a rebase.
Also I think this keeps the name from PidTagSenderName when the sender string has angle brackets, and handles a bare name before getaddresses() splits it on the comma:
if "<" not in sender and "@" not in sender:
# oxmsg's sent-representing fallback: a bare display name, which
# can hold a comma ("Doe, Jonathan").
name = unquote(sender)
else:
parsed = getaddresses([sender])
if len(parsed) != 1:
return sender, ""
name, address = parsed[0]
if "@" in address:
return sender, ""
# oxmsg built this string from PidTagSenderName. getaddresses()
# reads the "(FYDIBOHF23SPDLT)" group of a real legacy DN as a
# comment and appends it to the name, so prefer the property.
name = sender_name or name
Thanks again!
Resolve the conflict with docling-project#5439 in _msg_to_rfc822_bytes: keep the UnicodeDecodeError/LookupError guard on message.sender and pass the guarded value to _msg_sender_header. Keep both _msg_sender_header and _recover_msg_body, and merge the two docstrings. The docling-project#5439 tests call _msg_to_rfc822_bytes directly, so unpack the new (bytes, sender_fallback) return value there too. Signed-off-by: Daniel Nguyen <[email protected]>
After the merge with main, tests/test_backend_email.py has 1522 lines, over the 1500-line limit of the max-lines hook. Move the five sender tests of this PR, unchanged, to tests/test_backend_email_msg_sender.py. Signed-off-by: Daniel Nguyen <[email protected]>
A real legacy Exchange DN holds a group in parentheses, for example "/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/...". getaddresses() reads that group as a comment and appends it to the display name, so the From line showed "Doe, Jonathan (FYDIBOHF23SPDLT)". When the sender string has angle brackets, python-oxmsg built it from PidTagSenderName, so take the name from that property instead. This also keeps quotes in a name such as 'Jonathan "JD" Doe'. When no sender address is recorded, python-oxmsg returns PidTagSentRepresentingName without quotes, e.g. "Doe, Jonathan". getaddresses() split it on the comma, and the From line was lost. Check for a bare name before getaddresses() runs. Signed-off-by: Daniel Nguyen <[email protected]>
Since docling-project#5439, every python-oxmsg string read in _msg_to_rfc822_bytes catches UnicodeDecodeError and LookupError, and the docstring says so. The new PidTagSenderName and PidTagSenderSmtpAddress reads did not. When they fail, log a warning and build the From header from the sender string alone. Signed-off-by: Daniel Nguyen <[email protected]>
DanielNg0729
left a comment
There was a problem hiding this comment.
Hi @Kylinny, thanks again for this fix! The PR had a conflict with #5439 and no update for a week, so I pushed a few commits to move it forward. I hope that is OK with you:
5a56fdcfmerges main. It keeps theUnicodeDecodeError/LookupErrorguard from #5439 onmessage.senderand passes the guarded value to_msg_sender_header. The #5439 tests call_msg_to_rfc822_bytesdirectly, so they now unpack(bytes, sender_fallback).5d7fabb9moves the sender tests, unchanged, totests/test_backend_email_msg_sender.py. After the merge,tests/test_backend_email.pyhad 1522 lines, over the 1500-line limit of the max-lines hook.38d190fdapplies the two points from my review. When the sender string has angle brackets, the name now comes from PidTagSenderName, so a real DN no longer adds(FYDIBOHF23SPDLT)to it. A bareDoe, Jonathannow keeps itsFrom:line.6e9a8c73guards the PidTagSenderName and PidTagSenderSmtpAddress reads like the other.msgstrings.
With the fixture DN changed to /O=EX (FYD)/CN=U1, the output is now From: "Doe, Jonathan" instead of From: "Doe, Jonathan (FYD)". All 66 email tests pass, and ruff and the max-lines check are clean. Please tell me if you would like anything changed.
Thanks! lgtm now
…est (#5615) fix(tests): unpack the tuple from _msg_to_rfc822_bytes in the NUL test The NUL test from #5552 assigns the result of EmailDocumentBackend._msg_to_rfc822_bytes() to one name. #5420 changed the helper to return (rfc822_bytes, sender_fallback), so the test now passes a tuple to BytesIO and fails with a TypeError on main. Unpack the tuple, like the other callers do. Signed-off-by: Michele Dolfi <[email protected]>
Thank you @Antoine-BRETZNER for the clear report — the missing
From:line for internal Exchange senders is a real gap, and the detail that python-oxmsg returns the display name with the legacy Exchange DN made the cause quick to find.Fixes #5415
Problem
For
.msgfiles sent inside an Exchange organisation, python-oxmsg returns the sender as the display name with the legacy Exchange DN in the angle brackets (Display Name </O=ORG/OU=.../CN=RECIPIENTS/CN=...>), which is not a valid addr-spec. The email backend wrote that value into the projectedFromheader, mailparser then dropped the whole mailbox, andconvert()emitted noFrom:line at all.Reproducer
The reporter could not share their file, so this PR adds a synthetic fixture,
tests/data/email/sources/msg_exchange_sender.msg—msg_simple.msgwith only the sender rewritten (sender nameDoe, Jonathan, sender address the legacy DN/O=EX/OU=HQ/CN=U1). Run from the repo root:Before (on
main):After (this PR):
Fix
_msg_sender_header()(new): splits the oxmsg sender into aFromheader value and a display-name fallback. A sender with an SMTP address is passed through exactly as before; a sender with no SMTP address (legacy Exchange DN in the angle brackets, or no address at all) keeps its display name instead. A recordedPidTagSenderSmtpAddressis preferred when present — oxmsg's sender string shows the DN even then, since it prefersPidTagSenderEmailAddress. Display names holding specials are quoted with the existing_quote_display_namehelper, and header values go through_header_safeas before._msg_to_rfc822_bytes()now also returns the sender display name to show when the projectedFromheader does not survive parsing. A bare display name cannot be a mailbox — mailparser only keeps mailboxes with an address — so writing the name alone into the header is not enough on its own;convert()uses the fallback when mailparser yields no sender. No address is invented: when there is no name either, theFromheader is omitted, as before. The one existing direct caller of this private helper (test_msg_to_rfc822_keeps_attachment_metadata_without_payload) was updated to unpack the returned tuple.convert()passes the fallback to_format_addresses();.emlinput and SMTP senders keep the empty fallback, so their behaviour is unchanged.Invariants:
From:line shows only the display name recorded in the file, never a synthesised address.Tests
test_msg_exchange_sender_keeps_display_name: end-to-end regression test on the new fixture, plus assertions on the projectedFromheader and the fallback value._msg_sender_header: SMTP senders unchanged, an Exchange DN keeps the display name, a recorded SMTP address wins over the DN, bare sender names (oxmsg's sent-representing fallback), and multi-address senders passed through verbatim.pytest tests/test_backend_email.py: 46 passed. One test fails both with and without this change (test_email_backend_converts_html_body_to_text_paragraphs) — a pre-existing failure on this base, unrelated to this change.make validatepasses.