Open Bug 2063173 Opened 3 months ago Updated 3 months ago

Junk folder resolved by hardcoded name instead of nsMsgFolderFlags::Junk, creating a duplicate Junk folder on IMAP accounts

Categories

(MailNews Core :: Backend, defect)

Thunderbird 141
defect

Tracking

(Not tracked)

UNCONFIRMED

People

(Reporter: nathan, Unassigned)

Details

(Keywords: dupeme)

Steps to reproduce:

  1. Configure an IMAP account whose server provides its own special-use \Junk
    folder. I used Purelymail; its LIST returns (\Junk) "." "Junk".
  2. Account Settings > Junk Settings: enable "Move new junk messages to",
    targeting the account's own Junk folder. This sets moveOnSpam=true, which
    is the gate for the bug.
  3. Ensure no local junk index exists: fresh profile, or close Thunderbird and
    delete <profile>/ImapMail/<account>/Junk*.msf
  4. Start Thunderbird and let folder discovery finish.

Thunderbird 140.13.0esr (BuildID 20260721190809), comm-esr140 faf78db7dfb1,
Linux flatpak. Reproduces on all 8 of my accounts with this provider.

Actual results:

Thunderbird created its own local "Junk" folder instead of adopting the
server's. The server's real folder then lost the filename collision and was
stored as Junk-1.msf. Both now exist:

Junk.msf boxFlags=0 hierDelim=94 flags=0x40000000
Junk-1.msf boxFlags=4160 hierDelim=46 flags=0x40082014

Junk.msf is the one Thunderbird created; hierDelim 94 is the unset default,
i.e. that folder was never returned by LIST. Junk-1.msf is the real server
folder.

Cause: the junk folder is located by hardcoded name, never by flag.
nsSpamSettings::GetSpamFolderURI builds the URI as account + "/Junk"
(nsSpamSettings.cpp:500 on esr140) and nothing on that path ever queries
nsMsgFolderFlags::Junk. Full analysis in the first comment.

Expected results:

Thunderbird should adopt the server's existing \Junk folder as the account's
junk folder and create nothing.

Compare Trash, which is resolved correctly by flag:
nsImapMailFolder::GetTrashFolder (nsImapMailFolder.cpp:4800) calls
rootFolder->GetFolderWithFlags(nsMsgFolderFlags::Trash, ...). Inbox, Drafts,
Sent and Archive are handled the same way, and nsMsgFolderFlags::Junk is
already looked up that way at nsImapMailFolder.cpp:8881. Junk is the only
special folder that falls back to a string literal.

Root cause

Source is comm-esr140 at faf78db7dfb1 (the revision my build came from).
Equivalent lines on central given where they differ.

nsSpamSettings.cpp:36 mMoveTargetMode defaults to MOVE_TARGET_MODE_ACCOUNT
nsSpamSettings.cpp:422 UpdateJunkFolderState() calls GetOrCreateJunkFolder()
whenever mMoveOnSpam is set
nsSpamSettings.cpp:473 comment: "the spam folder URI = account uri + /Junk"
nsSpamSettings.cpp:500 folderURI.AppendLiteral("/Junk") [central: 476]
nsMsgUtils.cpp:785 GetOrCreateJunkFolder(): when GetParent() fails it
takes the create branch
nsMsgUtils.cpp:834-836 SetName("Junk"); SetFlag(nsMsgFolderFlags::Junk);
CreateStorageIfMissing() [central: 838]
nsImapMailFolder.cpp:1096 EnsureFolderExists() -> IMAP CREATE

Nothing on that path queries nsMsgFolderFlags::Junk. The folder is identified
purely by name, so a server-provided \Junk folder is never considered.

nsImapMailFolder.cpp:1073 describes the situation in its own words:

// parent is probably not set because *this* was probably created by rdf
// and not by folder discovery. So, we have to compute the parent.

Still present on central: AppendLiteral("/Junk") at nsSpamSettings.cpp:476 and
SetName("Junk") at nsMsgUtils.cpp:838, checked 2026-08-13. GetFolderWithFlags
does not appear in nsSpamSettings.cpp on either branch.

Why this reads as a defect rather than a quirk

Every other special folder is resolved by flag. Trash:

nsImapMailFolder::GetTrashFolder (nsImapMailFolder.cpp:4800)
rootFolder->GetFolderWithFlags(nsMsgFolderFlags::Trash, pTrashFolder);

Same pattern for Trash at :1214 and :2140, and nsMsgFolderFlags::Junk is
already looked up this way at :8881. Junk is the only special folder that
falls back to a string literal.

Proposed fix

In the MOVE_TARGET_MODE_ACCOUNT branch of nsSpamSettings::GetSpamFolderURI,
prefer an existing folder carrying nsMsgFolderFlags::Junk before falling back
to the hardcoded name:

rootFolder->GetFolderWithFlags(nsMsgFolderFlags::Junk, ...)

getFolderWithFlags is declared at nsIMsgFolder.idl:436 and is already the
established pattern, so this makes Junk consistent with Trash, Inbox, Drafts,
Sent and Archive rather than adding anything new.

Workaround, and two things that do NOT work

Works, survives restarts, running on 8 accounts: with Thunderbird closed,
delete the placeholder Junk.msf, rename Junk-1.msf to Junk.msf, and move its
key in folderCache.json to the new path. The name lookup then succeeds and
Thunderbird stops creating its own.

Does not work: deleting both files. Thunderbird recreates the placeholder on
the next start and the real folder collides again. Renaming is what makes it
stable.

Does not work: moveTargetMode=1 with an explicit spamActionTargetFolder
pointing at the real server folder. GetSpamFolderURI then returns that same
URI and GetOrCreateJunkFolder still reaches the create branch.

Verified vs inferred

Verified: the source lines above at both revisions; the folderCache.json
values; that the server returns (\Junk) "." "Junk" in LIST; that the
workaround holds across restarts on 8 accounts.

Inferred: that the CREATE is issued before discovery returns. The code implies
it, since GetParent() failing means the hierarchy is not yet built, but I have
not captured an IMAP:4 log showing the ordering on the wire. I can record one
if that would be useful.

A note on a dead end, in case it saves someone the detour: this server
advertises neither LIST-EXTENDED nor SPECIAL-USE, and its LSUB returns no
attributes, so it is tempting to blame capability negotiation. That is not the
cause. Discovery takes the kListingForFolderFlags path
(nsImapProtocol.cpp:7489), doing a plain LIST to harvest flags and then LSUB,
merging the stored flags at :5081, and nsImapServerResponseParser maps \Junk to
kImapSpam unconditionally. Thunderbird does obtain the flag correctly. It has
simply already created the folder by then.

Keywords: dupeme
Version: unspecified → Thunderbird 141

A lot changed around that after 140. How is v153?

You need to log in before you can comment on or make changes to this bug.