fix: stop unified-mailbox from mutating client-returned email objects#487
Merged
rathlinus merged 1 commit intoJun 24, 2026
Merged
Conversation
fetchUnifiedEmails, fanOutUnifiedQuery and the cross-account fanOutCrossQuery stamped accountId/accountLabel/source* directly onto each email object returned by the per-account client. Those objects are shared references; mutating them in place could surprise any caller that retained them (and corrupt an account-state snapshot). Decorate shallow copies instead, at all three fan-out sites. The original fix/unified-mailbox-no-mutation branch predated the cross-account "All accounts" feature and only covered two sites; this re-applies the fix to main's current code, including the third (shared/group) fan-out site, and preserves all five stamped fields. Flips the characterisation test to assert the client's object is left untouched.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The cross-account fan-out helpers in lib/unified-mailbox.ts stamp the source
account info (accountId, accountLabel, sourceClientAccountId, sourceAccountId,
sourceFolder) onto every email before merging. main does this by MUTATING each
email object in place:
Those objects are shared references handed back by the per-account JMAP client.
Mutating them in place could surprise any caller that retained them and corrupt
an account-state snapshot. The cross-account "All accounts" feature (a29c33b)
widened the bug: it carried the original two mutation sites forward and added a
third in the shared/group fan-out, so there are now three sites:
Changes
lib/unified-mailbox.ts: at all THREE fan-out sites, decorate shallow copies
instead of mutating the client's objects:
All five stamped fields are preserved; the merged result is identical, only the
client's original objects are now left untouched.
lib/tests/unified-mailbox.test.ts: flips the existing CHARACTERISATION test
("mutates the source email objects in place") into "does NOT mutate the source
email objects (decorates copies)" - asserts the returned email carries the
account info while the input object has neither accountId nor accountLabel.
Related Issues
Type of Change
Checklist
npm run typecheck && npm run lintand there are no errorsnpm run build)locales/) if my changes affect user-facing textScreenshots / Demo
Notes for Reviewers