# Milter: header values that are not valid UTF-8 are sent as empty strings

**URL:** https://support.stalw.art/t/milter-header-values-that-are-not-valid-utf-8-are-sent-as-empty-strings/2098
**Category:** Issues
**Created:** [October 28, 2026, 5:00pm UTC](https://support.stalw.art/t/milter-header-values-that-are-not-valid-utf-8-are-sent-as-empty-strings/2098 "2026-10-28T17:00:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![grepular](https://support.stalw.art/letter_avatar_proxy/v4/letter/g/a9a28c/32.png) [@grepular](https://support.stalw.art/u/grepular)
#### Post date: [October 28, 2026, 5:00pm UTC](https://support.stalw.art/t/milter-header-values-that-are-not-valid-utf-8-are-sent-as-empty-strings/2098/1 "2026-10-28T17:00:50Z")

</div>

### Issue Description

Stalwart sends a header whose name or value is not valid UTF-8 to the milter (SMFIC\_HEADER) with an empty value. The milter sees the header, but not its content.

### Expected Behavior

The milter protocol carries header names and values as NUL-terminated byte strings and has no UTF-8 requirement. Postfix and Sendmail pass the raw bytes through. Stalwart should send the header bytes as received.

### Actual Behavior

Tested on v1.0.0 with a minimal milter (protocol v6) that logs each frame. The message was:

```
From: Alice <alice@sender.example.net>
To: mike@example.org
Subject: Caf\xe9 au lait <- raw ISO-8859-1 byte 0xE9
X-Ok: plain ascii
...

```

The milter received:

```
HEADER b'Subject' b''
HEADER b'X-Ok' b'plain ascii'

```

The delivered message still has the original `Subject: Caf\xe9 au lait`, so a filter scores or acts on something different from what the recipient gets. Raw 8-bit headers are not standards-conformant, but they are common in spam and in legacy mail, which is where a filter needs to see them most.

**Cause**  
`crates/smtp/src/inbound/milter/message.rs` (v1.0.0, around line 271):

```
client.headers(message.headers().iter().map(|(k, v)| {
    (
        std::str::from_utf8(k).unwrap_or_default(),
        std::str::from_utf8(v).unwrap_or_default(),
    )
}))

```

`unwrap_or_default()` turns an invalid header into `""`. `MilterClient::headers` (client.rs:222) takes `AsRef<str>`, but only to call `.trim().as_bytes()`, so it could take `&[u8]` and send the bytes unchanged (trimming ASCII whitespace with `trim_ascii()`).

The reply direction has the same restriction. `read_nul_terminated` (protocol.rs:272) uses `String::from_utf8(buf).ok()`, so an SMFIR\_ADDHEADER / SMFIR\_CHGHEADER / SMFIR\_INSHEADER whose value is not UTF-8 fails to parse.

### Reproduction Steps

See “Actual Behaviour”

### Stalwart Version

v1.x

### Installation Method

Binary (Linux)

### Database Backend

RocksDB

### Blob Storage

RocksDB

### Search Engine

Internal

### Directory Backend

Internal

### 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

---

<div class="post-metadata">

### Author: ![helpbot](https://support.stalw.art/user_avatar/support.stalw.art/helpbot/32/13_2.png) [@helpbot](https://support.stalw.art/u/helpbot)
#### Post date: [October 28, 2026, 5:00pm UTC](https://support.stalw.art/t/milter-header-values-that-are-not-valid-utf-8-are-sent-as-empty-strings/2098/2 "2026-10-28T17:00:59Z")

</div>

The code you quoted matches the current source, so I could confirm your analysis.

In `crates/smtp/src/inbound/milter/message.rs`, around lines 271–276, the header list sent to the milter is built with `std::str::from_utf8(k).unwrap_or_default()` for the name and the same call for the value. Any header whose bytes aren’t valid UTF-8 is therefore sent as an empty string, as you describe.

In `crates/smtp/src/inbound/milter/protocol.rs`, the reply parser calls `read_nul_terminated` for the `SMFIR_ADDHEADER` name and value (around lines 205–207). I didn’t open that function, so I haven’t checked your claim that it uses `String::from_utf8(buf).ok()`. The call sites do return `String` values, which fits your description of the reply-side parsing failing on non-UTF-8 bytes.

The docs I searched don’t mention this limitation or a workaround, so I can’t offer a setting that changes the behaviour. A human will follow up on whether to change the milter client to pass raw bytes in both directions.

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

---

<div class="post-metadata">

### Author: ![stalwart](https://support.stalw.art/user_avatar/support.stalw.art/stalwart/32/12_2.png) [@stalwart](https://support.stalw.art/u/stalwart)
#### Post date: [October 29, 2026, 11:10am UTC](https://support.stalw.art/t/milter-header-values-that-are-not-valid-utf-8-are-sent-as-empty-strings/2098/3 "2026-10-29T11:10:39Z")

</div>

On the sending side, `MilterClient::headers` only needed each header as text to trim it, so the server forced every name and value through UTF-8 and sent an empty string when that failed. It now takes the raw bytes, trims ASCII whitespace and sends them as received, so `Subject: Caf\xe9 au lait` reaches the milter byte for byte. A regression test pushes a raw 0xE9 subject through a mock milter and checks the exact bytes it saw.

This ships in the next weekly Stalwart release.

I left the reply direction alone. An SMFIR\_ADDHEADER, SMFIR\_CHGHEADER or SMFIR\_INSHEADER with a non-UTF-8 value is still rejected when it’s parsed. The milters people usually run here add ASCII headers, and supporting raw bytes in replies would mean changing how header modifications are represented across the MTA, so it didn’t make sense to fold into this fix.
