Skip to content

Propagate body decode errors instead of swallowing them (#24)#81

Merged
kurok merged 1 commit into
masterfrom
fix-issue-24-decode-errors
Jun 12, 2026
Merged

Propagate body decode errors instead of swallowing them (#24)#81
kurok merged 1 commit into
masterfrom
fix-issue-24-decode-errors

Conversation

@kurok

@kurok kurok commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes issue #24 (high-priority bug): body decode errors were silently swallowed.

The MIME parts loop in src/mail_parser.rs decoded each part body with get_body_raw().unwrap_or_default() and get_body().unwrap_or_default(). When mailparse failed to decode a body (broken base64/quoted-printable or an undecodable charset), the error was discarded and an empty value stored — hiding corruption from the caller.

Change

  • Propagate the MailParseError from get_body_raw/get_body up through Mail::new (which already returns Result<_, MailParseError>). The PyO3 layer already maps MailParseError to the Python ParseError, so callers now see decode failures rather than silently-empty bodies.
  • No behavior change for valid emails (including valid base64): the full existing suite (87 tests) still passes unchanged.

Tests

Added tests/test_decode_errors.py:

  • broken-base64 text body raises ParseError (the get_body() path)
  • broken-base64 named attachment raises ParseError (the get_body_raw() path)
  • valid base64 body still parses
  • plain unencoded message still parses

Full suite: 91 passed (87 existing + 4 new), pytest -q --ignore tests/benchmark tests.

Note: quoted-printable decoding in mailparse 0.15 is tolerant of invalid sequences (it passes them through rather than erroring), so the deterministic broken-encoding tests use base64.

Closes #24

The MIME parts loop in src/mail_parser.rs decoded each part body with
`get_body_raw().unwrap_or_default()` and `get_body().unwrap_or_default()`.
When mailparse failed to decode a body (broken base64/quoted-printable or
an undecodable charset) the error was silently discarded and an empty value
stored, hiding corruption from the caller.

Propagate the `MailParseError` from `get_body_raw`/`get_body` up through
`Mail::new` instead. The PyO3 layer already maps `MailParseError` to the
Python `ParseError`, so callers now see decode failures rather than empty
bodies. Valid messages (including valid base64) are unaffected.

Add tests/test_decode_errors.py covering broken-base64 text bodies and
attachments raising ParseError, plus valid base64 and plain messages still
parsing.

Closes #24

Signed-off-by: yuriyryabikov <22548029+kurok@users.noreply.github.com>
@kurok kurok merged commit 52bb07a into master Jun 12, 2026
7 checks passed
@kurok kurok deleted the fix-issue-24-decode-errors branch June 12, 2026 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Body decode errors silently swallowed via unwrap_or_default()

1 participant