Skip to content

Lesson 9 of 9 · 28m

Inbound Parsing, Reply Automation, and the Hostile Edge of Your Mailbox

Design inbound email pipelines for loops, spoofing, MIME chaos, webhooks, malware scanning, idempotency, and human-support reality.

Outbound email is mostly about convincing receivers to trust you. Inbound email is the opposite: the internet gets to send you arbitrary MIME-shaped input, at arbitrary size, with forged headers, malicious attachments, encoded replies, auto-generated loops, and emotionally important human context. Treat inbound parsing as a hostile-input boundary, because that is what it is.

Decide the trust boundary before parsing

Inbound pipelines usually arrive through one of three shapes:

MX -> mailbox -> polling/IMAP -> parser
MX/provider -> HTTP webhook -> parser
SMTP receiver -> queue -> worker parser

Webhook-driven providers are operationally convenient, but now your parser depends on provider authentication, retries, and event schema. Direct SMTP gives control and pain in equal measure. Polling a mailbox introduces latency and state management. Choose based on threat model and operational capacity, not fashion.

Verify webhook signatures and normalize events before processing:

if not verify_signature(raw_body, headers["x-provider-signature"], secret):
    raise PermissionError("invalid inbound webhook signature")
event = parse_provider_event(raw_body)

Never trust the visible sender, reply-to, or subject alone. Authentication results matter, but they are inputs to policy rather than blanket proof of user intent. Persist provider event IDs and process them idempotently; retries are expected.

Loops are the first availability bug

Automated replies can reply to automated replies forever. Prevent that before launch:

do not auto-reply to Auto-Submitted, null-envelope, or bulk/list mail
rate-limit auto-replies per sender and thread
send your own automation with Auto-Submitted: auto-replied
watch Auto-Submitted, Precedence, X-Auto-Response-Suppress, List-Id, Return-Path

A support system that creates tickets from inbound email and sends notifications to the same inbound address can melt itself in minutes. Draw the loop before production does.

MIME parsing is adversarial parsing

A real inbound message can contain malformed boundaries, nested multiparts, malformed encoded words, charset mismatches, huge quoted-printable bodies, calendar parts, winmail.dat attachments, and HTML with tracking and remote content. Use a battle-tested parser. Then impose resource limits:

maximum message size, MIME depth, part count, decompressed attachment size
parsing timeout, attachment allowlist or scanning path

Extracting the "reply text" is harder than it sounds. Users reply inline, clients quote differently, signatures repeat, and HTML may be the only content. Sanitize before rendering it in your application, scan or quarantine attachments, and preserve the raw message even if you derive a clean body. The raw artifact is your forensic and reprocessing source.

Identity matching is authorization, not convenience

Suppose inbound replies update support tickets. If the only key is ticket-842 in the subject, anyone who can guess or see that subject can append to the ticket. Use opaque routing tokens in the reply address and validate sender relationship to the conversation:

To: support+ticket-842.v9f2k7@sudosecurity.org

Then enforce authorization. A customer may reply to their own thread; a random outside sender should not mutate it merely because they know the token. Consider whether forwarded replies are legitimate business behavior. That affects how strict you can be.

Field notes

  • Verify DKIM/SPF/DMARC results where available, but remember forwarding and list behavior can produce legitimate failures.
  • Never shell out to a command using filenames or header values without strict escaping and validation.
  • Do not trust attachment Content-Type; sniff and scan content while respecting ambiguity.
  • Email addresses are not stable proof of identity. Accounts can be compromised and forwarded mail changes context.
  • Test replies from Gmail, Outlook, Apple Mail, mobile clients, mailing lists, and automated monitoring systems. They quote differently.

Was this lesson helpful?

Sign in to rate