Dev.to Security 🔐 Cybersecurity 👁 0 📖 4 min read

One webhook receives every email sent to our domain, and six small decisions keep it from answering a stranger's pitch

Munchable has a support page, and behind it a mailbox situation that I did not want to maintain by hand. The domain's MX record points at Resend, which turns every inbound email into a signed webhook. That means mail to

Munchable has a support page, and behind it a mailbox situation that I did not want to maintain by hand. The domain's MX record points at Resend, which turns every inbound email into a signed webhook. That means mail to any address at the domain, including ones nobody created, lands on one route. This post is about what that route does with it, because almost every line in it exists to answer the question "what is the worst thing this could do if it were wrong?"

Three destinations

Each message goes to one of three places, depending on who it was sent to:

  • A reply to a ticket we opened. The address carries a thread token (support+<token>@), which only an email we sent can contain, so it is appended to that thread.
  • A new support conversation, sent to one of the support-style addresses. It opens a ticket, which lands in the admin queue.
  • Anything else is forwarded to a personal inbox with Reply-To set to the sender, so answering goes straight back to them.

A message that becomes a ticket is deliberately not also forwarded. A duplicate in a personal inbox is how two people, or the same person twice, end up replying to one customer.

Six decisions, six failure directions

1. Verify before anything else, and fail closed. The route rate limits, rejects oversized bodies before buffering them, and verifies the Svix signature. If the webhook secret is not configured the route answers 500, not "open". A misconfiguration must not degrade into a public endpoint that sends mail from our account.

2. Ignore mail for other domains. Resend webhooks belong to the account, not the domain. When a second app was added to the same account, its inbound mail started arriving here too, and every one of its emails reached the inbox twice, once from each app. The fix is four lines: check that a recipient (to, cc, or the envelope recipient for a bcc) is at our domain, and return 200 with skipped otherwise. It answers 200 so the sender is not asked to retry something that is not ours.

3. A claim that expires quickly and is confirmed late. Duplicate deliveries are caught with an idempotency key, but the key is first set for a short processing window and only promoted to the full day on success. A hard crash mid-processing then lets the retry run again. The trade is explicit in the comment: dropping a real email is worse than forwarding a duplicate.

4. Do not fetch a URL you did not expect. The raw message is downloaded from a URL in the webhook payload. Even though the payload is signed, the host is checked against an allowlist before fetching, a failure is logged by host only, and the response is size-capped twice, once from the header and once after reading, because a header can lie.

5. Spam is discarded, and the check fails open. Forwarding re-sends every message from our own authenticated domain, so the receiving inbox never sees the real sender and its own spam filter cannot help. Worse, mail to a support address opens a ticket and sends the sender an acknowledgement, so an unfiltered pitch both fills the queue and gets answered. The route therefore runs one cheap classification on mail from senders who are not registered users and are not replying on a thread. Anything it flags is dropped: no ticket, no forward, no reply.

The direction of the failure is the interesting part. A missing key, a timeout, a database error and an unparseable reply all count as "not spam". A missed pitch costs a second of someone's time. A missed customer costs a customer. And the instruction to the classifier is the same: when unsure, say not spam.

6. Automated mail is forwarded, never answered. Bounces and out-of-office replies are recognised from their headers, and they skip ticket creation. They fall through to the plain forward, because a human should see what bounced, but a robot should never be sent an acknowledgement that triggers another autoreply. DMARC reports are the opposite case: they stay in Resend and are not forwarded at all, which took an extra check because the subject of a report from one large provider does not mention DMARC, only the recipient address does.

Threading without a token

Some mail clients drop plus-addressing when a person replies to a forwarded copy, which would restart a conversation in front of someone who is mid-sentence. So if there is no token, the route looks for that same sender's most recent live ticket. The match is only ever made against an active ticket from the same address, so a stranger's mail cannot be attached to somebody else's thread.

What I would carry to another project

Write down, for each branch of a catch-all handler, which way it should fail. Here the answers were: unauthenticated callers fail closed, real customers fail open, retries prefer a duplicate to a loss, and robots never get a reply. Once those are on paper, the code is nearly mechanical, and the support page it all sits behind stays pleasantly boring.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.