Skip to content

PostSRSd — Sender Rewriting Scheme for forwarded mail

Install PostSRSd to rewrite the envelope sender of forwarded mail so SPF passes at the next-hop, without losing the original sender for bounce handling.

When your Postfix forwards a message — alice@example.com aliases to alice.smith@gmail.com — and Postfix sends it onward, the envelope sender is still the original sender at the original domain. Gmail checks SPF against the forwarding IP and the original domain’s SPF record; the record doesn’t list your IP, so SPF fails. SRS (Sender Rewriting Scheme, RFC 7505 background) is the universally adopted workaround: rewrite the envelope sender at forwarding time to a reversible address at your domain, so SPF passes for your domain, and the bounce can still be routed back to the original sender. This article is the PostSRSd install we apply to every forwarder we run.

How to verify

Audit the current forwarding posture.

sudo postconf | grep -E 'sender_canonical|recipient_canonical|smtp_generic'
dpkg -l | grep postsrsd
systemctl is-active postsrsd 2>/dev/null
echo "test" | mail -s "srs probe" some-alice@example.com  # an alias that forwards out
sudo journalctl -u postfix | grep alice | tail -10

You’re looking for the from= field in the log when the forward fires. If it reads the original external sender (from=external-user@somewhere.com), SPF will fail at the next hop. If it reads a rewritten address at your domain (from=SRS0=xxx=yy=somewhere.com=external-user@example.com), SRS is doing its job.

What’s happening

The forwarding problem in detail:

  1. external-user@somewhere.com sends mail to alice@example.com.
  2. Postfix at mx1.example.com accepts, looks up alice in virtual_alias_maps, sees the alias goes to alice.smith@gmail.com.
  3. Postfix sends the message to Gmail with MAIL FROM: external-user@somewhere.com and the original headers.
  4. Gmail checks SPF. The record at somewhere.com lists somewhere.com’s mail servers — not your 203.0.113.10. SPF returns fail.
  5. With DMARC p=quarantine or p=reject at somewhere.com, Gmail drops the message.

SRS rewrites step 3:

  • The envelope sender becomes SRS0=secret_hash=timestamp=somewhere.com=external-user@example.com.
  • SPF check at Gmail now evaluates example.com’s SPF record against your IP — which lists you correctly.
  • If Gmail bounces, the bounce arrives back at MAIL FROM, which is at example.com and reversible — Postfix decodes the SRS address back to external-user@somewhere.com and forwards the bounce there.

The “secret” in the encoded address is an HMAC keyed with a local secret, so attackers can’t forge SRS addresses for spoofed bounces.

The procedure

  1. Install PostSRSd. It’s a small C daemon that exposes a Postfix policy daemon socket on TCP:

    sudo apt update
    sudo apt install -y postsrsd
    sudo systemctl status postsrsd --no-pager
    ss -ltnp | grep -E ':(10001|10002)\b'

    PostSRSd listens on two ports — 10001 for sender rewriting (outbound), 10002 for sender unrewriting (inbound bounces).

  2. Configure the local domain and secret. Edit /etc/default/postsrsd:

    SRS_DOMAIN=example.com
    SRS_EXCLUDE_DOMAINS=example.com,partner.com
    SRS_SECRET=/etc/postsrsd.secret
    SRS_HASHLENGTH=4
    SRS_HASHMIN=4
    SRS_FORWARD_PORT=10001
    SRS_REVERSE_PORT=10002

    The secret file is auto-generated on first start if missing:

    sudo ls -l /etc/postsrsd.secret
    # -rw------- 1 postsrsd postsrsd 32 ...

    SRS_EXCLUDE_DOMAINS lists local domains you do NOT want rewritten — mail you originate yourself shouldn’t be SRS-encoded.

  3. Wire Postfix to PostSRSd. Add to /etc/postfix/main.cf:

    # Forward-direction rewriting (outbound)
    sender_canonical_maps = tcp:127.0.0.1:10001
    sender_canonical_classes = envelope_sender
    
    # Reverse-direction unrewriting (inbound bounces)
    recipient_canonical_maps = tcp:127.0.0.1:10002
    recipient_canonical_classes = envelope_recipient, header_recipient

    sender_canonical_classes = envelope_sender means Postfix only rewrites the envelope MAIL FROM, not the From: header. The From: stays the original — clients see who really sent it.

  4. Restart both daemons and verify:

    sudo systemctl restart postsrsd
    sudo postfix check
    sudo systemctl reload postfix
    sudo journalctl -u postsrsd -n 20 --no-pager
  5. Send a probe through an external→alias→external chain. From an external sender (a mailbox you control on Gmail), send to alice@example.com which aliases to a second external mailbox you control:

    # From your laptop, with Gmail's SMTP or any external
    swaks --to alice@example.com --from real-you@gmail.com

    Then check the headers of the delivered message at the second mailbox — the Return-Path: and Received-SPF: lines should reference SRS0=...@example.com, and the SPF check at the receiver should pass.

  6. Confirm the bounce path works. Send a probe to a recipient you know is a hard bounce (use a known dead address, or a special bounce@simulator.amazonses.com if you have SES):

    swaks --to dead-address@somewhere.com --from real-you@gmail.com --server mx1.example.com

    The bounce that comes back addressed to SRS0=hash=ts=gmail.com=real-you@example.com should be unwrapped by PostSRSd’s reverse port and routed back to real-you@gmail.com. Tail the log to confirm.

Operational notes

  • The SRS secret is the cryptographic key that prevents bounce forgery. Treat it like a database password — back it up, rotate annually, store in your secrets manager.
  • The SRS_HASHLENGTH=4 means 4 base32 characters of HMAC — about 20 bits. Enough to defeat casual forgery; not enough to defeat a determined attacker. Bump to 6 or 8 for high-value forwarders.
  • SRS-encoded addresses look weird (long, base32, equal-sign-laden) and some legacy spam filters score them as suspicious. We have seen ~1% of messages get a small DMARC score increase from this. Acceptable trade-off.
  • Without SRS, every legitimate forwarded message from a DMARC-enforcing domain (Google Workspace, Microsoft 365, banks) silently disappears. The fix is non-optional in 2026.
  • SRS is incompatible with some receivers’ DMARC reporting because the rewritten envelope confuses ARC. ARC (Authenticated Received Chain, RFC 8617) is the long-term solution; PostSRSd is a pragmatic interim until ARC is ubiquitous.

Common pitfalls

  • PostSRSd is started before its secret file is generated; both subsequent restarts produce different addresses, making old bounces undecodable. Once a secret is in place, do not regenerate without coordinated cutover.
  • sender_canonical_classes defaults to envelope_sender, header_sender — leaving header_sender in the list rewrites the From: header too, which breaks DKIM signatures on the upstream message. Restrict to envelope_sender only.
  • The SRS_EXCLUDE_DOMAINS list omits a local domain, and your own outbound mail gets SRS-encoded — receivers see weird envelope senders for routine company mail. Include every local virtual domain in the exclude list.
  • A forwarded message bounces back, the bounce hits the reverse port, but recipient_canonical_maps was set with tcp:127.0.0.1:10001 (forward port by mistake) — bounces stay encoded and double-bounce out.
  • PostSRSd listens only on localhost by default; a hardened deployment leaves this and uses unix-socket Postfix integration. Don’t expose the SRS ports outside the host.

Stack Harbor stands up PostSRSd on every Postfix that forwards mail in the engagements we run — secret stored in a managed vault, exclude list pinned to the tenant’s known domains, and the reverse path verified by a deliberate bounce probe before declaring the forward complete. If you have a Postfix that aliases mail and customers report missing messages, that audit is part of our Managed Operations practice.