Skip to content

Subject Decoding NameError #51

Description

@sargeant

Variable fixed_subject is only assigned inside a try block. If decoding raises an exception, the code attempts to return undefined fixed_subject, causing a NameError that crashes the milter handler.

Emails with malformed subject headers cause milter crashes, blocking email delivery.

File: src/milter/processor.py:158-170

Current Code

if mail_subject:
    try:
        fixed_subject = bytes(decode_header(mail_subject)[0][0]).decode(decode_header(mail_subject)[0][1])
    except:
        fixed_subject = mail_subject

try:
    return (fixed_subject, mail_headers)
except NameError:
    return ('', mail_headers)

Recommended Fix

fixed_subject = mail_subject  # default value
if mail_subject:
    try:
        decoded = decode_header(mail_subject)[0]
        if decoded[1]:
            fixed_subject = decoded[0].decode(decoded[1])
        else:
            fixed_subject = decoded[0] if isinstance(decoded[0], str) else decoded[0].decode('utf-8')
    except Exception as e:
        logger.warning(f"Failed to decode subject header: {e}")

return (fixed_subject if fixed_subject else '', mail_headers)

Testing

Need to test with emails containing:

  1. Valid UTF-8 subject headers
  2. ISO-8859-1 encoded subjects
  3. Malformed encoding declarations
  4. Missing subject headers
  5. Subject headers with invalid byte sequences

Activity

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

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions