Issue Description
Within one account, IMAP COPY does not create a copy. It adds the destination mailbox to the same stored message, and that message keeps one set of flags. So \Deleted stored on the message in mailbox C1 also shows on the “copy” in C2. A client that then runs EXPUNGE with C2 selected removes the C2 copy, which the user never marked for deletion. The C1 copy that the user did mark survives, and its \Deleted flag is cleared.
The same happens when a Sieve script files one delivery into two mailboxes (fileinto :copy "B1"; fileinto "B2";).
Expected Behavior
In IMAP, each mailbox’s messages have their own flags. RFC 9051 §6.4.7: COPY “copies the specified message(s) to the end of the specified destination mailbox”, and a STORE in one mailbox does not change a copy in another. EXPUNGE removes “all messages that have the \Deleted flag set from the currently selected mailbox” (RFC 9051 §6.4.3), so an EXPUNGE in C2 should remove nothing here. Dovecot and Cyrus, for example, store a separate copy, with its own flags, per mailbox.
Actual Behavior
Tested on v1.0.3: the official stalwartlabs/stalwart:v1.0.3 image. Two IMAP sessions, A and B, logged in to the same account (untagged SELECT details trimmed):
A: a4 APPEND C1 {73}
A: a4 OK [APPENDUID 3389567515 1] APPEND completed
A: a5 SELECT C1
A: a5 OK [READ-WRITE] SELECT completed
A: a6 COPY 1 C2
A: a6 OK [COPYUID 439401335 1 1] COPY completed
B: b2 SELECT C2
B: b2 OK [READ-WRITE] SELECT completed
A: a7 STORE 1 +FLAGS (\Deleted)
A: * 1 FETCH (FLAGS ($hasnoattachment \Deleted))
B: b3 FETCH 1 FLAGS
B: * 1 FETCH (FLAGS ($hasnoattachment \Deleted)) <- C2's copy is marked too
B: b4 EXPUNGE
B: * 1 EXPUNGE <- C2's copy is removed
A: a8 NOOP
A: * 1 FETCH (FLAGS ($hasnoattachment) UID 1) <- C1's copy is kept, \Deleted cleared
B: b5 STATUS C2 (MESSAGES)
B: * STATUS "C2" (MESSAGES 0)
So the copy the user kept is gone, and the copy they deleted is kept and no longer marked deleted. This is mail loss for ordinary clients. A “copy to folder, then delete the original” in any
client that marks messages deleted and expunges later (mutt, Thunderbird’s “mark as deleted” mode, a client that expunges on folder close) loses the copy, if the destination folder is expun
ged before the source. It also breaks programs that process a mailbox over IMAP while another program works on a different mailbox holding the same message: each sees the other’s \Deleted and retry keywords.
Reproduction Steps
- Log in twice to one account, as sessions A and B.
- A: APPEND any message to C1, SELECT C1,
COPY 1 C2. - B: SELECT C2.
- A:
STORE 1 +FLAGS (\Deleted). - B:
FETCH 1 FLAGSshows\Deleted. ThenEXPUNGE: C2 is now empty. - A: C1 still holds the message, without
\Deleted.
The same result follows if, instead of step 2, a Sieve script files one delivery into C1 and C2 (fileinto :copy "C1"; fileinto "C2";).
Stalwart Version
v1.x
Installation Method
Docker
Database Backend
RocksDB
Blob Storage
RocksDB
Search Engine
Internal
Directory Backend
Internal
Additional Context
Cause
Flags (keywords) belong to the message document, not to its membership in a mailbox, and a same-account COPY adds a mailbox to the existing document:
crates/imap/src/op/copy_move.rs, v1.0.3, line 355: same-account COPY isnew_data.add_mailbox(dest_mailbox_id)on the source message’s data. No new message is created.crates/imap/src/op/expunge.rs, line 177: the messages to expunge arecache.in_mailbox_with_keyword(mailbox_id, &Keyword::Deleted), i.e. any message in the selected mailbox whose single, shared keyword set contains\Deleted.expunge.rs, lines 318-323: when the message is in more than one mailbox, expunge removes the selected mailbox and thenremove_keyword(&Keyword::Deleted). That is why the copy in C1 loses its\Deletedmark.
Sieve delivery to several mailboxes makes the same single document (crates/email/src/sieve/ingest.rs, line 829: one email_ingest with mailbox_ids: sieve_message.file_into).
Suggested Fix
Make \Deleted per mailbox. For example, store the mailboxes in which the message is marked deleted with each mailbox membership (next to its UID), use that in STORE, FETCH, SEARCH and EXPUNGE, and do not show it on other mailboxes. Alternatively, have same-account COPY create a new message that shares the blob, as a cross-account copy already does (line 540 onwards).
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