JMAP Email/query: subject and header filter don’t match leading [tag] in Subject

Issue Description

JMAP Email/query with subject filter (and header filter on Subject) does not find the tag in square brackets at the start of Subject. Rest of the subject is searchable.

I have 5 emails in one mailbox, all with subject [GitHub] Payment Receipt for mrwogu. Sieve puts them in this mailbox, that works fine. I query this mailbox with inMailbox plus one more condition:

Condition Total
subject: "GitHub" 0
subject: "github" 0
subject: "[GitHub]" 0
header: ["Subject", "GitHub"] 0
subject: "Payment Receipt" 5
subject: "Payment" 5
subject: "mrwogu" 5
text: "GitHub" 5
no extra condition 5

text matches I think only because GitHub is also in From and body. No config changes on my side around search or indexing. My guess is subject is indexed after base subject normalization from RFC 5256, which strips list tags like [list]. Ok for threading, but not for search. I tested only with [GitHub] tag, not others.

Expected Behavior

RFC 8621 section 4.4.1 (RFC 8621: The JSON Meta Application Protocol (JMAP) for Mail | RFC Editor):

subject looks for the text in the Subject property,
header with [name, value] matches if the header field contains the value.

Subject is [GitHub] Payment Receipt for mrwogu, so subject: "GitHub", subject: "[GitHub]" and header: ["Subject", "GitHub"] should all return the 5 emails.

Actual Behavior

All three return 0 results. Other words from the same Subject (Payment, Payment Receipt, mrwogu) return 5. So only the leading bracket part is not searchable, in any form.

Reproduction Steps

  1. Deliver a few emails with subject [GitHub] Payment Receipt for mrwogu into one mailbox.
  2. Get the mailbox id with Mailbox/get.
  3. Run:
{
  "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
  "methodCalls": [
    ["Email/query", {
      "accountId": "<account id>",
      "filter": { "operator": "AND", "conditions": [
        { "inMailbox": "<mailbox id>" },
        { "subject": "GitHub" }
      ]},
      "calculateTotal": true
    }, "0"]
  ]
}
  1. total is 0.
  2. Change { "subject": "GitHub" } to { "subject": "Payment" }. total is 5.
  3. { "header": ["Subject", "GitHub"] } also gives 0.

Stalwart Version

v0.16.x

Installation Method

Binary (Linux)

Database Backend

FoundationDB

Blob Storage

Filesystem

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

Your diagnosis is correct. Stalwart indexes the Subject field using thread_name(), which implements the RFC 5256 base-subject algorithm used for threading. That function strips leading bracketed tags such as [GitHub] (treated as a mailing-list/subject tag) before the text is written to the search index, so the bracketed portion of the subject is never searchable as part of EmailSearchField::Subject.

The header: ["Subject", ...] filter is also affected, but for a different mechanical reason: raw header values are only indexed into the generic Headers search field for headers that don’t have a dedicated match arm. Subject has its own arm that indexes only the post-thread_name() stripped text into the Subject field, not the raw header into Headers, so a header filter targeting Subject can’t match on the stripped tag either.

So both filters effectively search the “base subject” (threading subject), not the raw RFC 8621 Subject property, which is inconsistent with the RFC 8621 semantics you quoted. This is a genuine indexing bug rather than a documented/intended behavior — I found nothing in the docs describing this stripping for search purposes (only for threading). A staff member will need to confirm the fix (e.g. indexing the raw subject separately from the threading-normalized one).

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

Thanks. I checked this in the source (current main and 0.16.24 are the same here) and tested on a clean 0.16.24 container. The subject part is right. The header part is worse than described.

Test message: Subject: [GitHub] Payment Receipt for mrwogu, plus a custom header X-Repro: Payment, body without “GitHub”.

Condition Default settings indexEmailFields + headers
subject: "GitHub" 0 0
subject: "[GitHub]" 0 0
subject: "Payment" 1 2
header: ["Subject", "GitHub"] 0 0
header: ["Subject", "Payment"] 0 0
header: ["Subject"] 0 0
header: ["X-Repro", "Payment"] 0 1
text: "GitHub" 0 0
no filter 1 2

Second column has one more message, imported after I enabled headers.

Repro (JMAP, any user account; <acc> = mail account id, <inbox> = Inbox id):

  1. Upload this message to /jmap/upload/<acc>/ as message/rfc822:
From: Billing <[email protected]>
To: [email protected]
Subject: [GitHub] Payment Receipt for mrwogu
Message-ID: <[email protected]>
Date: Sat, 24 Oct 2026 10:00:00 +0000
X-Repro: Payment
Content-Type: text/plain

Thanks for your payment.
  1. Import it:
["Email/import", {"accountId": "<acc>", "emails": {"e1": {"blobId": "<blobId>", "mailboxIds": {"<inbox>": true}}}}, "i"]
  1. Query (one call per condition from the table):
["Email/query", {"accountId": "<acc>", "calculateTotal": true, "filter": {"subject": "GitHub"}}, "0"]
["Email/query", {"accountId": "<acc>", "calculateTotal": true, "filter": {"header": ["Subject", "Payment"]}}, "1"]
["Email/query", {"accountId": "<acc>", "calculateTotal": true, "filter": {"header": ["X-Repro", "Payment"]}}, "2"]

To test with headers indexed, as admin:

["x:Search/set", {"accountId": "<admin acc>", "update": {"singleton": {"indexEmailFields": {"from": true, "to": true, "cc": true, "bcc": true, "subject": true, "body": true, "attachment": true, "receivedAt": true, "sentAt": true, "size": true, "hasAttachment": true, "headers": true}}}}, "s"]

then restart and import a new message (old ones are not reindexed).

Expected per RFC 8621 4.4.1: all of them should match the message.

What I found in the code:

subject filter: in crates/email/src/message/index/search.rs:141 the Subject goes through thread_name() before it is indexed, so [GitHub] is stripped. EmailFilter::Subject in crates/jmap/src/email/query.rs searches this same field.

header filter: EmailFilter::Header searches EmailSearchField::Headers (query.rs:149). I see two problems here:

  1. headers is not in the default indexEmailFields, so by default no header is indexed and every header condition returns 0. Even header: ["Subject"], which only checks that the header exists. RFC 8621 does not say this filter is optional, so I think it should not fail silently.
  2. With headers enabled, other headers work, but Subject is still never written to Headers. It has its own match arm in search.rs, and only the _ => branch (search.rs:169) writes to Headers.

In my mailbox text: "GitHub" matched only because “GitHub” is also in the body.

This code is shared by all search backends, so I think switching to Meilisearch or Elasticsearch would not help.

Possible fix: index the raw subject (or raw + base subject) in the Subject field, and write Subject to Headers too. Maybe the header filter should also fall back to the stored headers when headers is not indexed. Existing messages would need a reindex.

Fixed in v1.0.0, expected this week.