JMAP Mailbox/set: changing only parentId skips the sibling-name uniqueness check (duplicate sibling mailboxes)

Issue Description

A JMAP Mailbox/set update that changes only parentId is accepted even when the new parent already has a child with the same name. The result is two sibling mailboxes with identical names.

RFC 8621 §2 (Mailbox name) says: “There MUST NOT be two sibling Mailboxes with both the same parent and the same name.”

The cause is in crates/jmap/src/mailbox/set.rs (v0.16.20; the file is identical in v0.16.25). The uniqueness check at line 586 ff. only runs when the name changes:

if update
    .as_ref()
    .is_none_or(|(_, m)| m.inner.name != changes.name)
    && let Some(existing) = cached_mailboxes.mailboxes.items.iter().find(|m| {
        m.name.to_lowercase() == lower_name
            && m.parent_id().map_or(0, |id| id + 1) == changes.parent_id
    })

For an update that changes only parent_id, m.inner.name != changes.name is false, so the lookup under the new parent never happens. The parent-change block above it (line 486 ff.) validates depth, cycles, the existence of the parent and ACLs, but not names.

Suggested fix: run the check when either the name or the parent changes, and exclude the mailbox being updated from the lookup:

if update.as_ref().is_none_or(|(_, m)| {
    m.inner.name != changes.name || m.inner.parent_id != changes.parent_id
}) && let Some(existing) = cached_mailboxes.mailboxes.items.iter().find(|m| {
    update.as_ref().is_none_or(|(id, _)| m.document_id != *id)
        && m.name.to_lowercase() == lower_name
        && m.parent_id().map_or(0, |id| id + 1) == changes.parent_id
})

Expected Behavior

The update is rejected with notUpdated / alreadyExists, as it already is for a create or a rename into that position (RFC 8621 §2: sibling names MUST be unique).

Actual Behavior

The update succeeds. Mailbox/get then lists two sibling mailboxes with the same name.

Consequences we observed:

  • IMAP: IMAP identifies a mailbox by its full path, and the IMAP session builds its name map from these paths. Of two siblings with the same path, only one is reachable over IMAP. The other one keeps its messages but cannot be addressed.
  • Clients that look a mailbox up by name become non-deterministic. We run two Stalwart nodes, and they return Mailbox/get lists in different orders. A client that picks “the root mailbox named Notes” (Apple Notes over IMAP or ActiveSync works this way) got a different mailbox depending on which node answered. On the phone, the Notes folder was deleted and re-synced every few minutes.

Reproduction Steps

  1. Create a root mailbox Notes.
  2. Create a mailbox archive, and inside it a mailbox Notes.
  3. Move archive/Notes to the root: Mailbox/set with update: { "<id>": { "parentId": null } }.
  4. Mailbox/get: two root mailboxes named Notes are listed.

Relevant Log Output

No error is logged. The request simply succeeds.

Stalwart Version

v0.16.x

Installation Method

Docker

Database Backend

FoundationDB

Blob Storage

S3-compatible

Search Engine

Internal

Directory Backend

Internal

Additional Context

  • IMAP compares case-sensitively, JMAP does not. IMAP CREATE/RENAME check mailbox_names.contains_key(&full_path), which is case-sensitive, while JMAP compares with to_lowercase(). So over IMAP, notes can be created next to Notes, which JMAP would reject. It may be worth aligning the two checks.
  • Possibly related: https://support.stalw.art/t/390. There, Sieve fileinto :create produces several mailboxes with the same name, and Thunderbird shows only one of them. That is the same end state reached through a different path. A uniqueness guarantee enforced at the store level, rather than per code path, would cover both.

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

Confirmed, this matches the current source at crates/jmap/src/mailbox/set.rs around line 592: the sibling-uniqueness lookup is gated on m.inner.name != changes.name, so an update that only changes parentId never triggers the check, allowing two siblings with identical names under the new parent. Your analysis and fix direction (gate on name OR parentId change, and exclude the mailbox being updated from the candidate list) line up with what’s in the code.

This is a solid, well-documented bug report with a concrete repro and a proposed patch. I can’t merge code or file it on your behalf, but a staff or community member who handles the JMAP mailbox code will follow up here to confirm and apply a fix.

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).