Milter: a header edit whose value is not valid UTF-8 makes Stalwart tempfail the message on every retry

Issue Description

This is the reply-direction half of Milter: header values that are not valid UTF-8 are sent as empty strings. v1.0.2 fixed the request side: Stalwart now sends header names and values that are not valid UTF-8 to the milter as raw bytes. A milter’s reply is still decoded as UTF-8, though. If a milter sends SMFIR_ADDHEADER, SMFIR_CHGHEADER or SMFIR_INSHEADER with a value that is not valid UTF-8, Stalwart treats the whole response as an invalid frame. With tempFailOnError (the default) it answers the SMTP client with 451, and does so on every retry, so the message bounces in the end.

Expected Behavior

The milter protocol carries header names and values as NUL-terminated byte strings with no UTF-8 requirement. Postfix and Sendmail accept the bytes and write them into the message. Since Stalwart now hands such bytes to the milter, a milter that edits that header, for example to tag a raw ISO-8859-1 Subject, sends them back.

Actual Behavior

Tested on v1.0.3 (the grepular/stalwart-trimmed build, same source) with a minimal milter speaking protocol v6. The milter answered end-of-message with:

SMFIR_CHGHEADER index=1 name="Subject" value="Caf\xe9 tagged"    <- raw ISO-8859-1 byte 0xE9
SMFIR_ACCEPT

and the SMTP client got:

451 4.3.5 Unable to accept message at this time.

The same message with an ASCII value in the same reply is accepted. The reproduction milter below was run against v1.0.3 and gives the 451. A real milter that tags subjects (a spam filter adding [SPAM], for instance) hits this on any message whose Subject arrived as raw 8-bit, and the sender eventually gets a bounce for a message the milter meant to accept.

Reproduction Steps

Configure a milter on the DATA stage (x:MtaMilter: host 127.0.0.1, port 19898, stages.data: true, tempFailOnError: true), run this milter, and send any message that has a Subject:

import socket, struct
def recvn(c, n):
    b = b''
    while len(b) < n:
        x = c.recv(n - len(b))
        if not x: raise EOFError
        b += x
    return b
def send(c, cmd, data=b''):
    c.sendall(struct.pack('>I', len(data) + 1) + cmd + data)
srv = socket.socket(); srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(('127.0.0.1', 19898)); srv.listen(5)
while True:
    c, _ = srv.accept()
    try:
        while True:
            n = struct.unpack('>I', recvn(c, 4))[0]
            f = recvn(c, n); cmd = f[:1]
            if cmd == b'O':   # actions: ADDHDRS | CHGHDRS
                send(c, b'O', struct.pack('>III', 6, 0x01 | 0x10, 0))
            elif cmd == b'D':
                pass
            elif cmd == b'E':
                send(c, b'm', struct.pack('>I', 1) + b'Subject\0Caf\xe9 tagged\0')
                send(c, b'a')
            elif cmd == b'Q':
                break
            else:
                send(c, b'c')
    except EOFError:
        pass
    c.close()

Stalwart Version

v1.x

Installation Method

Binary (Linux)

Database Backend

RocksDB

Blob Storage

RocksDB

Search Engine

Internal

Directory Backend

Internal

Additional Context

Cause
crates/smtp/src/inbound/milter/protocol.rs (v1.0.3), read_nul_terminated (line 272) ends with String::from_utf8(buf).ok() (line 280). For a value that is not UTF-8 it returns None, so Response::deserialize returns None, and client.rs:344 returns Error::FrameInvalid. The Modification::{AddHeader, InsertHeader, ChangeHeader} variants (mod.rs, around line 126) hold name: String, value: String.

Suggested Fix
Read header names and values as Vec<u8>, as ReplaceBody already does, and keep them as bytes through apply_milter_modifications, which only copies them into the new message (lines 497 and 517). The name could stay a String if it is checked against RFC 5322’s field-name characters, but the value should not be.

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

Your source reading matches what I found in the current source, but I can’t give an authoritative answer on a fix.

In crates/smtp/src/inbound/milter/protocol.rs, read_nul_terminated still ends with String::from_utf8(buf).ok() (line 280). SMFIR_ADDHEADER reads its name and value through that function (lines 205-207), so a non-UTF-8 value makes the whole reply fail to parse. That is consistent with the FrameInvalid and 451 you’re seeing.

The docs don’t say anything about this. Your proposed change is to keep header names and values as bytes in the modification variants, as ReplaceBody already does. A staff member will need to decide on it.

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

Hi,

This is fixed. Header values in SMFIR_ADDHEADER, SMFIR_CHGHEADER and SMFIR_INSHEADER replies are now read as raw bytes and written into the message as they are, so a milter that tags a raw ISO-8859-1 Subject no longer makes Stalwart treat the reply as an invalid frame and tempfail the message. Header names still have to be valid UTF-8, which is no problem since RFC 5322 field names are ASCII.

It ships in next week’s Stalwart release.