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/getlists in different orders. A client that picks “the root mailbox namedNotes” (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
- Create a root mailbox
Notes. - Create a mailbox
archive, and inside it a mailboxNotes. - Move
archive/Notesto the root:Mailbox/setwithupdate: { "<id>": { "parentId": null } }. Mailbox/get: two root mailboxes namedNotesare 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/RENAMEcheckmailbox_names.contains_key(&full_path), which is case-sensitive, while JMAP compares withto_lowercase(). So over IMAP,notescan be created next toNotes, which JMAP would reject. It may be worth aligning the two checks. - Possibly related: https://support.stalw.art/t/390. There, Sieve
fileinto :createproduces 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