Milter: EndOfHeader is omitted when NOHDRS is negotiated without NOEOH

Issue Description

I encountered a reproducible Milter protocol interoperability issue where Stalwart stalls before the BODY callback when a Milter negotiates:

  • SMFIP_NOHDRS = set
  • SMFIP_NOEOH = not set

In this state, the Milter is requesting that individual header callbacks be omitted, but it has not requested that the end-of-headers (SMFIC_EOH) command be omitted.

Stalwart appears to omit both.

With a real libmilter/PyMilter listener, the Milter consequently remains waiting for SMFIC_EOH. When Stalwart subsequently begins the BODY phase, the Milter never advances to its body callback and the session eventually times out.

In an SMTP configuration with tempFailOnError=true, this results in the SMTP transaction receiving a temporary failure rather than continuing normally.

The problem was originally reproduced on Stalwart 0.16.18 using Milter protocol v6 and PyMilter 1.0.5.

I subsequently reproduced the relevant behavior independently of the production application using a temporary real PyMilter/libmilter listener and a Stalwart-compatible wire client. This isolated the problem from message storage, malware scanning, application hooks and network infrastructure.

I have also checked Stalwart 0.16.25 source, where the relevant MilterClient::headers() control flow still appears to be present. I have not separately reproduced the complete SMTP failure against a running 0.16.25 server.

The relevant current implementation is conceptually:

if !self.has_option(SMFIP_NOHDRS) {
    for (name, value) in headers {
        // Send SMFIC_HEADER
        ...
    }

    // Write EndOfHeaders
    self.write(Command::EndOfHeader).await?;

    if !self.has_option(SMFIP_NR_EOH) {
        return self.read().await?.into_action();
    }
}

Ok(Action::Accept)

Because the EndOfHeader write is inside the !SMFIP_NOHDRS branch, negotiating NOHDRS also suppresses EOH even when NOEOH was not negotiated.

I found the existing issue #2346 concerning headers being included in Milter BODY chunks, but this appears to be a separate problem. This report concerns omission of SMFIC_EOH when SMFIP_NOHDRS is negotiated while SMFIP_NOEOH remains unset.

Expected Behavior

SMFIP_NOHDRS and SMFIP_NOEOH should be handled independently.

For the negotiated state:

NOHDRS = true
NOEOH  = false

I would expect Stalwart to:

  1. omit individual SMFIC_HEADER commands;
  2. still send SMFIC_EOH;
  3. wait for the EOH response unless SMFIP_NR_EOH is set;
  4. then proceed to SMFIC_BODY.

The Stalwart Milter documentation describes flagsProtocol as the protocol flags advertised during Milter option negotiation and treats the Milter as a protocol-level filter with distinct message-processing stages.

Conceptually, I would expect the implementation to separate the two conditions:

if !self.has_option(SMFIP_NOHDRS) {
    for (name, value) in headers {
        self.write(Command::Header {
            name: name.as_ref().trim().as_bytes(),
            value: value.as_ref().trim().as_bytes(),
        })
        .await?;

        if !self.has_option(SMFIP_NR_HDR) {
            match self.read().await? {
                Response::Action(Action::Accept | Action::Continue) => (),
                Response::Action(action) => return Ok(action),
                response => return Err(Error::Unexpected(response)),
            }
        }
    }
}

if !self.has_option(SMFIP_NOEOH) {
    self.write(Command::EndOfHeader).await?;

    if !self.has_option(SMFIP_NR_EOH) {
        return self.read().await?.into_action();
    }
}

Ok(Action::Accept)

A regression test for NOHDRS=true, NOEOH=false would also seem useful.

Actual Behavior

When NOHDRS=true and NOEOH=false, Stalwart does not send SMFIC_EOH.

Using a real PyMilter/libmilter listener, the callback sequence reaches:

negotiate
envfrom
envrcpt
DATA

but never reaches:

body
eom

Stalwart sends/attempts the BODY stage while libmilter is still waiting for EOH, and the transaction eventually times out.

I tested several offered protocol-flag combinations using the same isolated listener:

Offered   Negotiated   NOHDRS   NOEOH   Result
-------   ----------   ------   -----   -------------------------
0x00      0x23         true     false   FAIL — timeout pre-BODY
0x20      0x20         true     false   FAIL — timeout pre-BODY
0x40      0x40         false    true    PASS
0x42      0x42         false    true    PASS
0x60      0x60         true     true    PASS

The differentiating condition is consistently:

NOHDRS=true && NOEOH=false

Explicitly using flagsProtocol=0x60 (NOHDRS | NOEOH) works as a configuration workaround for a Milter that does not require separate HEADER or EOH callbacks.

With 0x60, the same real libmilter test proceeds successfully through:

negotiate
envfrom
envrcpt
body
capture
eom

Reproduction Steps

  1. Run Stalwart 0.16.x with a Milter configured at the DATA stage using protocol version 6.

  2. Use a real libmilter-compatible implementation. The reproduction used PyMilter 1.0.5.

  3. Configure the Milter so that during option negotiation it does not require individual header callbacks, producing a negotiated state in which:

     SMFIP_NOHDRS = true
     SMFIP_NOEOH  = false
     SMFIP_NOBODY = false
    

    For example, in the reproduction, advertising Stalwart flagsProtocol=0 resulted in PyMilter negotiating:

     protocol = 0x23
    

    corresponding to:

     NOCONNECT | NOHELO | NOHDRS
    

    NOEOH was not set.

  4. Submit a normal RFC822 message through the Milter path.

  5. Observe that MAIL FROM, RCPT TO and DATA complete normally.

  6. Observe that no BODY callback occurs in the Milter.

  7. The Milter connection eventually times out because libmilter is still waiting for the missing SMFIC_EOH.

  8. Repeat with protocol flags that make EOH handling consistent, for example:

     0x60 = NOHDRS | NOEOH
    
  9. Observe that the same listener then receives BODY and EOM normally and completes processing.

A minimal protocol matrix from the reproduction:

flagsProtocol=0x00
→ negotiated 0x23
→ NOHDRS=true, NOEOH=false
→ timeout before BODY

flagsProtocol=0x20
→ negotiated 0x20
→ NOHDRS=true, NOEOH=false
→ timeout before BODY

flagsProtocol=0x40
→ negotiated 0x40
→ NOHDRS=false, NOEOH=true
→ PASS

flagsProtocol=0x42
→ negotiated 0x42
→ NOHDRS=false, NOEOH=true
→ PASS

flagsProtocol=0x60
→ negotiated 0x60
→ NOHDRS=true, NOEOH=true
→ PASS

Relevant Log Output

The ordinary Stalwart/filter logs did not contain a useful application-level error for the original failure, so I reproduced the issue with an instrumented real-libmilter listener.

Failing flagsProtocol=0 case:

{"hasCrLfBoundary": true, "rawLength": 275, "section": "synthetic-message"}
{"actions": 15, "protocol": 35, "protocolHex": "0x23", "section": "option-negotiation", "version": 6}
{"response": "CONTINUE", "responseHex": "63", "stage": "mail-from"}
{"response": "CONTINUE", "responseHex": "63", "stage": "rcpt-to"}
{"response": "CONTINUE", "responseHex": "63", "stage": "data"}

Instrumented callbacks:
{"event": "negotiate", "before": [15, 1044671, 0, 0], "after": [15, 35, 0, 0], "rc": 0}
{"event": "envfrom", "rc": 0}
{"event": "envrcpt", "rc": 0}

TimeoutError: timed out

Protocol-matrix summary:

current-zero             offered=0x0  negotiated=0x23 NOHDRS=True  NOEOH=False pass=False
stalwart-default         offered=0x42 negotiated=0x42 NOHDRS=False NOEOH=True  pass=True  catalog=['staged']
no-eoh                   offered=0x40 negotiated=0x40 NOHDRS=False NOEOH=True  pass=True  catalog=['staged']
no-headers               offered=0x20 negotiated=0x20 NOHDRS=True  NOEOH=False pass=False
no-headers-and-eoh       offered=0x60 negotiated=0x60 NOHDRS=True  NOEOH=True  pass=True  catalog=['staged']

Successful 0x60 callback sequence:

negotiate
envfrom
envrcpt
body
store-capture
eom

I can provide the complete isolated reproduction script if useful.

Stalwart Version

v0.16.x

Installation Method

Binary (Linux)

Database Backend

RocksDB

Blob Storage

S3-compatible

Search Engine

Internal

Directory Backend

Internal

Additional Context

The Milter implementation used for the reproduction was PyMilter 1.0.5 / libmilter-compatible protocol v6.

The issue was initially observed as an SMTP temporary failure with tempFailOnError=true:

451 4.3.5 Unable to accept message at this time.

Before isolating the protocol behavior, the following were independently verified and ruled out:

  • Milter TCP listener reachable;
  • message/quarantine storage writable and healthy;
  • SQLite catalog healthy;
  • direct capture/storage of the same normal RFC822 message successful;
    -manual envfrom → envrcpt → body → eom callback sequence successful;
  • no dependency on malware scanning for reproduction.

The real wire-level reproduction then showed that the failure occurs before the Milter’s BODY callback when NOHDRS=true and NOEOH=false.

The current workaround is to advertise:

flagsProtocol = 96
hex           = 0x60
meaning       = NOHDRS | NOEOH

For this application that is semantically acceptable because separate HEADER and EOH callbacks are not required. It avoids the timeout, but it appears to work around rather than fix the underlying protocol-state issue.

No reverse proxy or unusual NAT behavior is involved in the isolated reproduction; the Milter listener and protocol client can reproduce the problem locally.

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

This confirms the report exactly: the source currently in the repository matches the bug description precisely. This is a code-level correctness issue, not something I can resolve through documentation, but I can confirm the code matches the report so a human/staff member can act on it.

I’ve confirmed this against the current source: in crates/smtp/src/inbound/milter/client.rs, the headers() function nests the EndOfHeader write inside the !SMFIP_NOHDRS check, so negotiating NOHDRS alone (without NOEOH) suppresses the SMFIC_EOH command entirely, exactly as you describe. Your proposed fix (splitting the header-loop and EOH-send into two independent if blocks gated on SMFIP_NOHDRS and SMFIP_NOEOH respectively) matches the protocol semantics and would resolve the stall.

This is a genuine bug in the Milter client implementation rather than a documentation or configuration issue, so there’s no workaround to point you to beyond the flagsProtocol=0x60 one you already found. A staff member will need to review and patch this in the code; I can’t merge or schedule the fix myself.

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

Fixed in v0.16.26 (yet to be released).

Thanks, good to hear!