Skip to content

fix: Don't delete unnecrypted messages that were downloaded while force_encryption was off - #8930

Open
Hocuri wants to merge 9 commits into
hoc/remove-unencryptedfrom
hoc/dont-delete-old-unencrypted
Open

Hocuri wants to merge 9 commits into
hoc/remove-unencryptedfrom
hoc/dont-delete-old-unencrypted

Conversation

@Hocuri

@Hocuri Hocuri commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is WIP, but the PR description is "ready" already. Also see the comments below.

This PR fixes an issue with the unencrypted-removal,
where Delta Chat would suddenly delete unencrypted emails on old shared accounts.
Therefore, this PR must be stack-merged together with the
unencrypted-removal.

Without this PR, the following problem could occur:

  • bcc_self=0 and force_encryption=0
  • Delta Chat downloads some unencrypted messages
  • Then, the user upgrades to a version of Delta Chat that doesn't
    support unencrypted, or enables force_encryption
  • Then, the MARK_DOWNLOADED_FOR_DELETION statement would suddenly mark all the
    previously-downloaded unencrypted messages for deletion

In the future, Delta Chat won't even look at unencrypted messages
anymore, so this won't be a problem anymore, but it will be a problem
while upgrading, and I don't want to accidentally delete someone's
emails.

So, after consulting with link2xt, this PR marks messages for deletion
when downloading them rather than in every iteration of the imap loop.
I anyways find this new logic easier to reason about.

I first wanted to make this PR against main, but it was too hard to
make the logic work well with force_encryption=0.

test_is_encrypted_copy_of_downloaded_msg() is kind of testing the implementation, not sure if we should just remove it.

TODO:

@Hocuri
Hocuri added this pull request to stack #8925 November 20, 2026 15:59
@Hocuri
Hocuri force-pushed the hoc/dont-delete-old-unencrypted branch from c12a444 to decc55d Compare November 20, 2026 16:01
@Hocuri
Hocuri marked this pull request as draft November 20, 2026 16:03
@Hocuri

Hocuri commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator Author

This PR still has one problem, which is that even with bcc_self=0, post-messages may not be deleted from all relays:

If a post-message is received via multiple relays, and then it's downloaded later, it will only be deleted on the relay it was downloaded from. This can maybe be fixed by marking all messages as to-be-deleted.

And whether this PR is the best approach is up for discussion, but I can't think of any other good solutions.

@Hocuri
Hocuri force-pushed the hoc/dont-delete-old-unencrypted branch from decc55d to 6ab70a8 Compare November 20, 2026 16:42
@hpk42

hpk42 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Marking for deletion on download sounds good to me. There is btw an edge category next to encrypted/unencrypted messages: "undecipherable". We probably don't want to delete them.

"This can maybe be fixed by marking all messages as to-be-deleted": marking by Message-ID when a message is downloaded should delete it on all relays.

@Hocuri
Hocuri force-pushed the hoc/dont-delete-old-unencrypted branch 2 times, most recently from f6dfbb0 to 6460e43 Compare November 23, 2026 12:11
@Hocuri
Hocuri force-pushed the hoc/dont-delete-old-unencrypted branch from d8d56a1 to ef080a6 Compare November 25, 2026 14:23
@Hocuri
Hocuri force-pushed the hoc/dont-delete-old-unencrypted branch 2 times, most recently from 93f0f5b to 643fe4a Compare November 26, 2026 14:49
…rce_encryption` was off

This PR fixes an issue with the unencrypted-removal,
where Delta Chat would suddenly delete unencrypted emails on old shared accounts.
Therefore, this PR must be stack-merged together with the
unencrypted-removal.

Without this PR, the following problem could occur:
- `bcc_self=0` and `force_encryption=0`
- Delta Chat downloads some unencrypted messages
- Then, the user upgrades to a version of Delta Chat that doesn't
  support unencrypted, or enables `force_encryption` again
- Then, the MARK_DOWNLOADED_FOR_DELETION statement would suddenly mark all the
  previously-downloaded unencrypted messages for deletion

In the future, Delta Chat won't even look at unencrypted messages
anymore, so this won't be a problem anymore, but it _will_ be a problem
while upgrading, and I don't want to accidentally delete someone's
emails.

So, after consulting with link2xt, this PR marks messages for deletion
when downloading them rather than in every iteration of the `imap` loop.
I anyways find this new logic easier to reason about.

I first wanted to make this PR against `main`, but it was too hard to
make the logic work well with `force_encryption=0`.
@Hocuri
Hocuri force-pushed the hoc/dont-delete-old-unencrypted branch from 643fe4a to 97741a4 Compare November 26, 2026 16:07
Comment thread src/imap.rs Outdated
Comment on lines +1597 to +1602
/// Returns whether a newly seen message is a copy of an already downloaded message,
/// e.g. a copy that arrived on another transport.
///
/// Unencrypted copies are excluded, so that an old unencrypted email that appears with a new
/// UID (e.g. because the user moved it with a classical MUA) is not deleted.
async fn is_encrypted_copy_of_downloaded_msg(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this returns true for tombstones of undecryptable or unsigned messages. So, if an undecryptable message arrives a second time some reason (maybe because the user moved it out of INBOX and then back into INBOX) then the second copy will be deleted.

This was the case before this PR already. It's a super rare edgecase of an edgecase, and fixing it would require to persist some extra info in the database, but I thought I'd point it out regardless as a trade-off.

@Hocuri
Hocuri force-pushed the hoc/dont-delete-old-unencrypted branch from 2bb28ae to e8e34e3 Compare November 27, 2026 16:46
Comment thread src/download.rs
// trigger a re-check by interrupting the inbox folder.
// This is mainly needed to make the tests pass;
// on real devices, it would be fine to delete the message at the next iteration.
context.scheduler.interrupt_inbox().await;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call was moved into mark_msg_for_deletion()

Comment thread src/test_utils.rs

/// Sends a message with an attachment larger than [`PRE_MSG_ATTACHMENT_SIZE_THRESHOLD`]
/// and returns the sent pre- and post-message.
pub(crate) async fn send_large_file_message<'a>(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this function here from pre_messages/util.rs

@Hocuri
Hocuri marked this pull request as ready for review November 27, 2026 17:06

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants