skip to content

After pointing a domain's MX records at an inbound filtering service, why can some mail still reach the origin mail server unfiltered, and how do you close that path?

level: seniorimportance: should knowfreq 26%

answer

  1. MX advertises, it does not enforce
  2. a leftover higher-numbered entry
  3. temporary failure moves the sender on
  4. port 25 open to everyone
  5. accept only the filter's ranges

basics

~20 s

MX records only tell senders where to deliver. Mail still reaches the origin through a leftover backup MX entry when the filter's hosts fail, or by direct connection. Remove the origin from MX and accept port 25 only from the filter.

solid answer

~50 s

An MX record is advice for well-behaved senders, not an access control. If the origin stays in the MX set as a higher-numbered backup, RFC 5321 senders move to it whenever the filter's hosts are unreachable or answer with a temporary failure, and that mail skips the filter. Separately, any client that knows or finds the origin's address can connect to its port 25 directly. To close both paths, take the origin out of the MX records, and at the network edge or in the MTA accept port 25 only from the filter's published address ranges. In Postfix that is `smtpd_client_restrictions` with a `check_client_access` table followed by `reject`. Submission on 587 stays a separate, authenticated service. Then verify from the `Received` chain: in every inbound message, the entry your origin wrote should show a filter address as the connecting client.

code

ini · 6 lines
ini
# Postfix main.cf on the origin (port 25 service)
smtpd_client_restrictions =
    check_client_access cidr:/etc/postfix/filter_ranges.cidr,
    reject
# filter_ranges.cidr holds one line per range, e.g.
# 198.51.100.0/24   OK

go deeper

for a junior

Remember that MX records only tell senders where to deliver. Anyone can still connect to the origin's port 25 unless it refuses them.

for a middle

Explain the MX walk: a sender moves to the next preference when the preferred hosts are unreachable or fail temporarily, so a backup entry naming the origin receives unfiltered mail.

for a senior

Run the cutover in order: TTL, MX change, wait, then restrict port 25 to the filter's ranges. Then prove it from the origin's Received entries and logs, and route the filter to the origin by host.

for a principal

Decide what the origin may still accept directly, such as partner links or internal systems, and who owns the filter's address list, since its ranges change and a stale list drops legitimate mail.

## MX is an advertisement, not an access control When a company puts an inbound filtering service in front of its own mail server, the usual first step is to change the domain's MX records so they name the filter's hosts. Honest senders then deliver to the filter, which inspects each message and relays the clean ones to the company's **origin** server. The change says nothing about who may connect to the origin. MX records tell a cooperating sender where it should deliver. They do not stop any host from opening a TCP connection to port 25 on the origin's address. ## Two bypass paths 1. **A leftover backup entry.** Teams often keep the origin in the MX set "for safety" with a higher preference number, for example the filter at 10 and the origin at 20. RFC 5321 §5.1 requires a sender to be able to try each address in the list in order until a delivery attempt succeeds. When the filter's hosts are unreachable or answer with a temporary failure, the sender moves on to the origin. Every brief filter outage, and every sender that cannot reach the filter, then delivers straight to the origin, unfiltered. 2. **Direct connections.** Nothing obliges a client to use the MX records at all. The origin's address may be in an old cached record, in the `A` record of a name such as `mail.example.com`, or discovered some other way. If port 25 on the origin accepts any client, that mail never touches the filter. | Path | Why it happens | Control | |---|---|---| | Backup MX names the origin | The MX walk moves on whenever the preferred hosts fail temporarily | Remove the origin from the MX set; list only the filter's hosts | | Direct connection | Port 25 on the origin is reachable from anywhere | Accept port 25 only from the filter's address ranges | | Old cached MX | Senders hold the previous records until their TTL expires | Wait out the TTL before rejecting other sources | ## Closing them - **List only the filter in MX.** If the filter offers several hosts, use those for redundancy. The origin must not appear at any preference. - **Restrict port 25 on the origin.** Do it at the firewall, in the MTA, or both. In Postfix, `smtpd_client_restrictions` can check the client address against a `cidr` table of the filter's ranges and `reject` everyone else. Postfix delays that rejection until `RCPT TO` by default, so the refused attempt is logged with its sender and recipient. - **Keep submission separate.** Users and applications that send through the origin should use the submission port, 587, with authentication. They must not depend on port 25 staying open. - **Route from the filter by host, not by MX.** The filter must deliver to the origin's host name or address. If it looked up the domain's MX, it would find itself. RFC 5321 tells a relay to discard MX entries at its own preference and higher; if nothing is left, the message is returned as undeliverable. If the filter fails to recognise the listed name as its own, the message loops instead. ## Detecting bypass The origin's own trace shows whether the controls hold. In each inbound message, the `Received` entry written by the origin records the connecting client's address, taken from the TCP connection. After the cutover, every one of those addresses should belong to the filter. An address outside the filter's ranges is a bypass, and the entries below it show where the message came from. The origin's log shows the same, together with any connections the new restriction refused. ## Cutover order and side effects 1. **Lower the MX TTL** ahead of the change, so senders pick up the new records quickly. 2. **Switch the MX records** to the filter's hosts only. 3. **Wait at least the old TTL** before rejecting other sources. Senders that cached the old records still deliver to the origin, and a rejection would bounce their mail. 4. **Then restrict port 25** to the filter's ranges, and watch the logs for legitimate senders that still arrive directly. One side effect needs a decision. Once the filter sits in front, the origin sees the filter's address as the connecting client, so sender checks run at the origin evaluate the filter instead of the real sender. Those checks should run at the filter, or the origin should rely on the filter's recorded results. How SPF evaluates the connecting address is covered with SPF, and which `Authentication-Results` fields to trust is covered with envelope and headers.

  • Why not keep the origin as a backup MX in case the filtering service goes down?
    Because senders use it whenever the filter is unreachable or answers with a temporary failure, and that mail arrives unfiltered. Senders already queue and retry when every MX fails, so a short filter outage delays mail rather than losing it. If you need resilience, use more of the filter's own hosts in the MX set.
  • Inbound mail started looping after the cutover. What is the likely cause?
    Most often the origin no longer treats the domain as a final destination, so it relays each message back out by MX to the filter, which delivers it to the origin again. Each hop adds a `Received` entry until a hop limit bounces the message. Make sure the origin accepts the domain as its own, and point the filter at the origin's host explicitly rather than at the domain's MX.

  • Once MX points at the filter, all inbound mail passes through it.
  • Keeping the origin as a high-numbered backup MX is a harmless safety net.
  • Senders always wait for the preferred MX host instead of trying others.
  • Requiring STARTTLS on the origin's port 25 blocks direct deliveries.
  • The filter should find the origin by looking up the domain's MX.