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:
- Valid UTF-8 subject headers
- ISO-8859-1 encoded subjects
- Malformed encoding declarations
- Missing subject headers
- Subject headers with invalid byte sequences
Variable
fixed_subjectis only assigned inside a try block. If decoding raises an exception, the code attempts to return undefinedfixed_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-170Current Code
Recommended Fix
Testing
Need to test with emails containing: