Milter body includes the headers, so they arrive twice

Issue Description

Milter: SMFIC_BODY carries the full raw message, headers arrive twice at the milter

Expected Behavior

Stalwart sends the headers with SMFIC_HEADER/SMFIC_EOH and then only the message body with SMFIC_BODY, as Postfix and Sendmail do. A milter that stores the message (e.g. an archive) receives every header exactly once.

Actual Behavior

The SMFIC_BODY chunks contain the complete raw message (headers + body). The milter receives the headers twice, the second copy inside the body. A message stored by benno-milter looks like this:

X-REAL-MAILFROM: <[email protected]>
X-REAL-RCPTTO: <[email protected]>
Subject: test
From: [email protected]

Subject: test
From: [email protected]

body line

Cause in crates/smtp/src/inbound/milter/message.rs (v0.16.24, unchanged in v0.16.27, v1.0.0 and main 47df4c0b):

// Message body
let (action, modifications) = client.body(message.raw_message()).await?;

The headers were sent just before via client.headers(…). Other places pass the body only: the milter body replacement in the same file uses message.raw_body(), the MTA hooks (inbound/hooks/message.rs) use message.raw_body(), and the milter test client in
tests/src/smtp/inbound/milter.rs sends &message.raw_message()[message.root_part().raw_body_offset() as usize..].

Proposed fix:

-            let (action, modifications) = client.body(message.raw_message()).await?;
+            let (action, modifications) = client.body(message.raw_body()).await?;

We run a 0.16.24 build with exactly this change on a test system.

Reproduction Steps

  1. Install a milter that writes the received message to disk, e.g. benno-milter 2.10 (Benno MailArchiv) listening on inet:[email protected].
  2. Add an MtaMilter: hostname 127.0.0.1, port 22500, protocol version 6, flagsProtocol 0x43 (see Additional Context).
  3. Deliver a message via SMTP port 25:
    Subject: test
    From: [email protected]
    
    body line
    
  4. Open the file written by the milter: the headers appear twice, once before the body and once at the start of the body.

Relevant Log Output

Stalwart accepts the message, the milter conversation itself succeeds (the defect is in the content of SMFIC_BODY, it does not produce an error):

2026-10-22T12:47:27Z INFO Milter action: Accept (milter.action-accept) listenerId = "submissions", localPort = 465, remoteIp = 203.0.113.10, remotePort = 56762, id = "MtaMilter with id jkyfwfcuadqa", elapsed = 0ms

Before setting flagsProtocol, the default negotiation failed:

2026-10-22T12:46:27Z WARN Milter disconnected (milter.disconnected) listenerId = "smtp", localPort = 25, remoteIp = 203.0.113.10, remotePort = 41502, id = "MtaMilter with id jkyfwfcuadqa", details = (null), elapsed = 0ms
benno-milter: st_optionneg[0]: 0x42 does not fulfill protocol requirements 0x3

Stalwart Version

v0.16.x

Installation Method

Binary (Linux)

Database Backend

PostgreSQL

Blob Storage

Filesystem

Search Engine

Meilisearch

Directory Backend

LDAP

Additional Context

  • Stalwart 0.16.24, own Debian 13 package (binary), PostgreSQL data store, filesystem blob store, Meilisearch, LDAP directory (Samba AD).
  • Negotiation: Stalwart offers the protocol flags 0x42. benno-milter is a libmilter milter that requests SMFIP_NOCONNECT|SMFIP_NOHELO (0x3) and disconnects when they are not offered. flagsProtocol = 0x43 on the MtaMilter works around it; offering these “no-*” flags by default might be worth considering.
  • Not related to #1882 (EOH omitted with NOHDRS) or #1786 (Sieve after milter), both already fixed.

I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.

on

I have searched this support forum (open and closed topics) and confirm this is not a duplicate.

on

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

I can’t confirm the whole report, but the part about the body is correct. In the current source, crates/smtp/src/inbound/milter/message.rs (around line 281) still has this line:

let (action, modifications) = client.body(message.raw_message()).await?;

That line sits in the milter conversation, right after the headers are sent. raw_message() returns the whole raw message, headers included. So SMFIC_BODY carries a second copy of the headers, as you describe. I did not re-check the other call sites you listed (raw_body() in the body replacement and in the MTA hooks, and the test client), so I can’t confirm those from my own reading.

The docs don’t cover this, so I can’t say whether the behaviour is intentional. Your proposed change to message.raw_body() matches what the protocol expects, where SMFIC_BODY holds only the body after SMFIC_HEADER and SMFIC_EOH. A human will need to review it.

The flagsProtocol workaround (0x43) for libmilter-based milters that request NOCONNECT and NOHELO is a separate point. I haven’t checked what Stalwart offers by default, so I can’t comment on changing it.

You’re on v0.16.x. This analysis is based on the current v1.0 source, where the same line is still present. The upgrade guide is here: Overview | Stalwart

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

The milter client was handing raw_message() to SMFIC_BODY, so after the headers went out one by one with SMFIC_HEADER, the body chunks started with the same header block again. It now sends raw_body(), the same slice the body replacement path and the MTA hooks already used, and there’s a regression test where the mock milter echoes back the body it received.

The fix ships in the next weekly Stalwart release, so you can drop your local patch once you’ve upgraded.

On the negotiation side, flagsProtocol = 0x43 is the right setting for benno-milter, and I’ve left the default as it is.