Skip to content

fix(message-view): infer MIME type from file name when saving octet-stream attachments - #11829

Merged
wmontwe merged 1 commit into
thunderbird:mainfrom
Sanilas-ai:fix-9141-octet-stream-duplicate-name
Nov 5, 2026
Merged

wmontwe merged 1 commit into
thunderbird:mainfrom
Sanilas-ai:fix-9141-octet-stream-duplicate-name

Conversation

@Sanilas-ai

Copy link
Copy Markdown
Contributor

Contribution Summary

Linked Issue/Ticket: Closes #9141

Description

When an attachment is sent as application/octet-stream (e.g. the REWE receipts from the issue), saving a duplicate of file.pdf produced file.pdf (1) instead of file (1).pdf.

Cause: the save flow passes the attachment's MIME type to ACTION_CREATE_DOCUMENT. Document providers (e.g. the Downloads provider) only split the display name into name and extension when the extension matches that MIME type. With application/octet-stream, the whole file.pdf is treated as the base name, and the counter gets added after it.

Fix: in MessageViewFragment.onSaveAttachment(), if the attachment uses the generic application/octet-stream type, the MIME type sent to the create-document dialog is inferred from the file name with MimeTypeUtil.getMimeTypeByExtension(). ViewIntentFinder already does the same thing when viewing attachments. Non-generic MIME types are kept as is, and unknown or missing extensions still fall back to application/octet-stream. The change is in a small helper (getSaveAttachmentMimeType) so it can be unit tested.

On the concern raised in the earlier PR (#9187) about trusting file extensions: this only changes the type hint given to the document picker, which it uses for naming. The saved file name and its bytes stay exactly as received, and the file already has that extension today. Nothing is opened or interpreted based on the inferred type.

Testing

  • Added SaveAttachmentMimeTypeTest (Robolectric): non-generic types are kept (even when they don't match the extension), octet-stream plus .pdf gives application/pdf (case-insensitive), and unknown, missing, or absent extensions keep application/octet-stream.
  • Not run locally: my dev environment has no Android SDK, so I couldn't run gradlew :legacy:ui:legacy:testDebugUnitTest or spotlessCheck. I'm relying on CI and will fix anything it reports. That's why this is opened as a draft.
  • Manual repro (from the issue): save an application/octet-stream attachment named file.pdf to Downloads twice. Expected: file (1).pdf.

AI Disclosure

  • This contribution does not include any changes created or assisted by AI.
  • This contribution includes changes assisted by AI.
  • This contribution includes changes created by AI.

Contribution Checklist

  • I have read, and I affirm that my contribution adheres to Mozilla’s Community Participation Guidelines
  • This contribution is in Kotlin where possible
  • This contribution does not use merge commits
  • This contribution adheres to the existing codestyle (gradlew spotlessCheck not run locally, pending CI)
  • This contribution does not break existing unit tests (gradlew testDebugUnitTest not run locally, pending CI)
  • This contribution includes tests for any new functionality and maintains tests for any updated functionality.
  • This contribution adheres to our Engineering process (RFC/Technical Design/ADR)
  • This PR has a descriptive title and body that accurately outlines all changes made and contains a reference to any issues that it fixes (e.g. Closes #XXX or Fixes #XXX).

…tream attachments

Document providers only treat the part after the last dot as a file
extension when it matches the MIME type passed to ACTION_CREATE_DOCUMENT.
Attachments sent as application/octet-stream were therefore saved as
"file.pdf (1)" instead of "file (1).pdf" when a file with the same name
already existed.

When the attachment uses the generic MIME type, pass the type inferred
from the file name (as ViewIntentFinder already does for viewing). The
file name and content are unchanged.

Closes thunderbird#9141

Signed-off-by: Sanilas-ai <konstantinsanilas@gmail.com>
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

✅ Validation Passed: All report and feature-flag labels are correctly set.

@Sanilas-ai

Copy link
Copy Markdown
Contributor Author

Hi! As an outside contributor I can't set labels, so the require-report-label check is failing. Could a maintainer please add the report label? I think report: include fits, since this is a user-visible fix for #9141, but please use whatever you think is right.

The other workflows (Build - Android, Quality - Checks, etc.) are waiting for approval because this is my first PR here. Could someone approve them? I don't have an Android SDK in my local setup, so I'm relying on CI for the Robolectric test (SaveAttachmentMimeTypeTest). I'll mark the PR ready for review once CI is green. Thanks!

@Sanilas-ai
Sanilas-ai marked this pull request as ready for review November 2, 2026 15:29
@Sanilas-ai
Sanilas-ai requested a review from a team as a code owner November 2, 2026 15:29
@Sanilas-ai
Sanilas-ai requested a review from wmontwe November 2, 2026 15:29
@Sanilas-ai

Copy link
Copy Markdown
Contributor Author

Update: local unit tests now pass in our Docker sandbox with Android SDK.

./gradlew :legacy:ui:legacy:testDebugUnitTest --tests com.fsck.k9.ui.messageview.SaveAttachmentMimeTypeTest

Result: 7 tests, 0 failures (compileSdk 37 / Robolectric). Marked ready for review. Still need a maintainer to add a report: label and approve first-time-contributor workflows.

@thunderbird-botmobile thunderbird-botmobile Bot added the pr-sentinel: ready for review Passed all automated PR Sentinel checks. Ready for maintainer review. label Nov 2, 2026
@wmontwe wmontwe added the report: include Include changes in user-facing reports. label Nov 5, 2026

@wmontwe wmontwe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank your for the contribution!

@wmontwe
wmontwe merged commit 9a520d4 into thunderbird:main Nov 5, 2026
16 checks passed
@thunderbird-botmobile

Copy link
Copy Markdown
Contributor

Thanks for your contribution! Your pull request has been merged and will be part of Thunderbird 27. We appreciate the time and effort you put into improving Thunderbird. If you haven’t already, you’re welcome to join our Matrix chat for contributors. It’s where we discuss development and help each other out. https://matrix.to/#/#tb-android-dev:mozilla.org
Hope to see you there! 🚀📱🐦

@thunderbird-botmobile thunderbird-botmobile Bot added this to the Thunderbird 27 milestone Nov 5, 2026

This branch was previously deployed

2 inactive deployments
botmobile — 123e8441 Deployed Nov 5, 2026 by wmontwe via pr-merged #679
review — 123e8441 Deployed Nov 2, 2026 by Sanilas-ai
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-sentinel: ready for review Passed all automated PR Sentinel checks. Ready for maintainer review. report: include Include changes in user-facing reports.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect numbering when saving duplicate attachments for application/octet-stream attachments

2 participants