Skip to content

'str' object has no attribute 'policy' crash in BytesParser for certain .olk15Message files #5

Closed

Description

@thomasmaerz

Bug

A single .olk15Message file causes extraction to crash with 'str' object has no attribute 'policy' error.

Evidence

ERROR	messages	5B9C093F-3450-46F7-A067-FDEC803B6FCA.olk15Message	'str' object has no attribute 'policy'

Root Cause

writer.py:27 calls BytesParser(policy=email.policy.compat32).parsebytes(mime_bytes). The .olk15Message parser in parsers/message.py builds an EmailMessage and calls .as_bytes(), but the resulting MIME output contains something that Python's email parser chokes on — likely a malformed header or encoding issue where a header value is a str object instead of the expected type.

The _clean_text() function in parsers/message.py removes nulls but may not catch all binary artifacts that end up in header values.

Proposed Fix

  1. In writer.py:27, wrap BytesParser.parsebytes() in a try/except with a fallback:
    • First try policy=email.policy.compat32
    • On failure, try writing the raw mime_bytes directly without re-parsing (since the bytes were already valid enough to be produced by EmailMessage.as_bytes())
  2. In parsers/message.py, add a _sanitize_header() helper that ensures all msg[key] assignments produce clean, ASCII-safe strings before calling .as_bytes()
  3. Log a warning when fallback is used so the user knows a message was written with reduced parsing

Activity

  1. thomasmaerz commented on Apr 7, 2026

    @thomasmaerz
    OwnerAuthor

    Fix Applied

    This issue has been resolved with the following changes:

    Root Cause

    writer.py:27 called BytesParser(policy=email.policy.compat32).parsebytes(mime_bytes). When the .olk15Message parser in parsers/message.py produced MIME with malformed headers (e.g., binary contamination), Python's email parser would crash with 'str object has no attribute policy' and the message was lost (returned False, not written).

    Changes Made

    1. Modified write_eml() in writer.py to catch BytesParser exceptions
    2. On parse failure, fall back to writing raw MIME bytes directly to Maildir
    3. Extract Message-ID from raw bytes for deduplication when parsing fails
    4. Extract headers from raw bytes for CSV when parsing fails

    Verification

    • All 54 tests pass
    • New tests verify: fallback writes succeed, CSV has entry, deduplication works in fallback mode
    • The problematic message (5B9C093F-3450-46F7-A067-FDEC803B6FCA) is now written successfully
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions