Skip to content

fix(email): strip trailing NUL characters from .msg properties and body - #5552

Merged
dolfim-ibm merged 6 commits into
docling-project:mainfrom
DhyaanKanoja11:fix/email-msg-trailing-nul
Nov 26, 2026
Merged

dolfim-ibm merged 6 commits into
docling-project:mainfrom
DhyaanKanoja11:fix/email-msg-trailing-nul

Conversation

@DhyaanKanoja11

Copy link
Copy Markdown
Contributor

Fixes #5535

What

Strip trailing NUL (\x00) characters from string properties (subject, sender, recipients, attachment names, MIME types, and bodies) when converting Outlook .msg files in EmailDocumentBackend._msg_to_rfc822_bytes and _recover_msg_body.

Why

In non-Unicode (8-bit) .msg files, PtypString8 streams are often null-terminated. python-oxmsg decodes the full stream without trimming trailing null bytes, which causes:

  1. From: and recipient headers to fail parsing in mailparser when embedded null bytes are present, causing sender information to be omitted.
  2. Trailing \x00 characters to leak into document text items and markdown exports.

Changes

  • docling/backend/email_backend.py:
    • Sanitize _header_safe by stripping any \x00 before splitting/folding headers.
    • Strip trailing \x00 from subject, sender, recipient names/addresses, attachments (file names, MIME types), and bodies (plain_body, html_body, and _recover_msg_body).
  • tests/test_backend_email.py:
    • Add test_msg_strips_trailing_nul_from_properties_and_body verifying that trailing null bytes do not leak into text elements or markdown exports and that From: headers parse cleanly.

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @DhyaanKanoja11, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 All 2 merge protections satisfied — ready to merge.

Show 2 satisfied protections

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

🟢 Require two reviewer for test updates

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working email issue related to email (EML, MSG) backend priority:medium labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit 3ed8fcff3f. The analysis read the diff only. It did not run code from this pull request.

The PR strips trailing NUL characters from .msg subject, sender, recipients, attachment names, MIME types and bodies in EmailDocumentBackend. It also makes _header_safe remove NUL characters. It adds a binary .msg fixture and tests.

CI safety: no concern found. A maintainer must still approve CI.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

I, Dhyaan Kanoja <[email protected]>, hereby add my Signed-off-by to this commit: 85aff28

Signed-off-by: Dhyaan Kanoja <[email protected]>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 24, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI first review (advisory): no blocker found

This review covers the full diff at 66629af553. It read the code and did not run it.

The changes correctly strip trailing NUL characters from Outlook .msg properties, headers, bodies, and attachment metadata in docling/backend/email_backend.py:287. I checked the header sanitization, attachment handling, body recovery fallbacks, and the regression test.

A maintainer makes the merge decision. This review never approves the pull request.

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.23077% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/email_backend.py 69.23% 3 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

…cipients, attachments and body recovery

Signed-off-by: Dhyaan Kanoja <[email protected]>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 24, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI first review (advisory): no blocker found

This review covers the changes since 66629af553 at 2fa82d0ed1. It read the code and did not run it.

I found no defect in the new commit. The new test lines cover recipients, attachment names and MIME types, the HTML body, and the _recover_msg_body fallback. The test is mock-heavy, but the mocks are the only way to put NUL bytes into these properties.

A maintainer makes the merge decision. This review never approves the pull request.

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 24, 2026
Add msg_8bit_nul_terminated.msg: msg_with_attachment.msg with every
PtypString property stored as PtypString8, the ASCII text followed by
NUL bytes, message code page 1252. The stream sizes are unchanged, so
the file is a valid OLE file that python-oxmsg reads with the NUL bytes
kept in every value.

The new test converts it through DocumentConverter. On main, the title,
body and attachment names keep the NUL bytes, and From: and To: are
lost. With this fix, no NUL reaches the document and both headers are
back. This covers the real python-oxmsg path, including the sender
that python-oxmsg builds as "name\0" <address\0>.

Signed-off-by: Daniel Nguyen <[email protected]>
@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 26, 2026
DanielNg0729
DanielNg0729 previously approved these changes Nov 26, 2026

@DanielNg0729 DanielNg0729 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @DhyaanKanoja11 for the fix, LGTM! I tested it with real-shaped files. On main, NUL bytes leak into the title, body and attachment names, and both From: and To: are lost. With this PR, all of them are clean. All email tests pass, also after a merge with current main.

To make it a bit more complete, I added one commit with a real fixture, msg_8bit_nul_terminated.msg. It is msg_with_attachment.msg with every string property stored as an 8-bit stream that ends with NUL bytes. The new test converts it through DocumentConverter, so it covers the real python-oxmsg path, including the sender that python-oxmsg builds as "Alice\x00" <[email protected]\x00>. It fails on main and passes here.

Thanks again!

@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 26, 2026
Merged into current main, tests/test_backend_email.py has 1537 lines,
over the 1500-line limit of the max-lines hook, so the lint check fails.
Move the two NUL tests of this PR, unchanged, to
tests/test_backend_email_msg_nul.py.

Signed-off-by: Daniel Nguyen <[email protected]>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 26, 2026
@DhyaanKanoja11

Copy link
Copy Markdown
Contributor Author

Thanks so much @DanielNg0729 for testing with real-shaped files and adding the \msg_8bit_nul_terminated.msg\ fixture! Really glad to see the end-to-end \DocumentConverter\ path covered and passing cleanly. Appreciate the review and support!

@dolfim-ibm
dolfim-ibm merged commit 490746a into docling-project:main Nov 26, 2026
34 checks passed
dolfim-ibm added a commit that referenced this pull request Nov 26, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working email issue related to email (EML, MSG) backend priority:medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Email backend: trailing NUL characters in subject, sender and attachment names of 8-bit .msg files

3 participants