Skip to content

Bug 2100601 - Add a button to download all of a bug's attachments as a ZIP file - #2855

Open
dklawren wants to merge 5 commits into
mozilla:masterfrom
dklawren:2100601
Open

dklawren wants to merge 5 commits into
mozilla:masterfrom
dklawren:2100601

Conversation

@dklawren

@dklawren dklawren commented Dec 3, 2026

Copy link
Copy Markdown
Collaborator

Some bugs, such as AI security reports, carry dozens of attachments, and analyzing them locally means saving each one by hand. The attachments section of the bug page now has a Download All (ZIP) button that downloads every attachment the user can access as one file, bug-<id>-attachments.zip.

Who gets what

Requester Gets
Not logged in No ZIP, login is required
Can't see the bug The usual access error, no ZIP
Can see the bug Public attachments
Attacher of a private attachment Also their own private attachments
Member of the insider group Also all private attachments

Obsolete and deleted attachments are always left out.

How it works

  • New attachment.cgi?action=zip&bugid=<id>. The bug is loaded with Bugzilla::Bug->check, and the attachments come from Bugzilla::Attachment->get_attachments_by_bug, the same visibility rule the bug page uses.
  • Login is required because building a ZIP of every attachment costs far more than serving one, which would make an anonymous endpoint an easy way to abuse the system.
  • New param attachment_zip_max_size (KB, default 51200 = 50 MB) caps the total. It is checked against the stored sizes before any data is fetched from storage, and it also bounds the memory one request can use, since the ZIP is built in memory.
  • The ZIP is served as a download only (Content-Disposition: attachment, nosniff, no-store, no charset), and each file in it is logged as attachment-get, like a single download.
  • Files are named <attach_id>-<filename>, so attachments that share a filename don't overwrite each other. Path separators, : and control characters become _, so no name can extract outside its folder. Names are stored as UTF-8.
  • The logic lives in the new Bugzilla::Attachment::Zip. attachment.cgi only handles login, logging and the response.

Checked

  • New t/bmo/attachment-zip.t passes 26/26 against MySQL and the test web server. Over HTTP it logs in as an insider, a non-insider, the attacher and a bug-group member, and also requests anonymously, then checks exactly which files are in each ZIP and that nothing from a restricted bug leaks.
  • test_sanity passes: 70 files, 18,251 tests.

Copilot AI balanced review requested due to automatic review settings December 3, 2026 02:47
@dklawren dklawren self-assigned this Dec 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

ZIP generation blocks web workers and retains all attachment payloads in memory, while the feature still has filename compatibility issues, duplicate tab stops, and a download control that can appear when no files are downloadable.

5 open issues
What changed in this PR

This PR adds a Download All (ZIP) button to Bugzilla’s attachments panel so signed-in users can save a bug’s files without downloading them one at a time.

Changes:

  • Adds a ZIP download endpoint that respects existing bug and attachment permissions and excludes obsolete and deleted files.
  • Caps the combined attachment size at 50 MiB by default, prefixes filenames with attachment IDs, and logs each included download.
  • Adds tests for archive contents, filename handling, size limits, and access permissions.
File Description
template/​en/​default/​global/​user-error.html.tmpl Explains why an empty or oversized ZIP request was rejected.
template/​en/​default/​global/​code-error.html.tmpl Reports ZIP creation failures.
template/​en/​default/​admin/​params/​attachment.html.tmpl Describes the ZIP size setting for administrators.
t/​bmo/​attachment-zip.t Tests archive contents, size checks, and access rules.
extensions/​BugModal/​template/​en/​default/​bug_modal/​attachments.html.tmpl Adds the Download All (ZIP) control.
Bugzilla/​Config/​Attachment.pm Defines the configurable size cap.
Bugzilla/​Attachment/​Zip.pm Selects eligible attachments, checks their size, and builds the archive.
attachment.cgi Requires login, checks bug access, logs downloads, and serves the ZIP.

Review effort: Balanced

Comment thread Bugzilla/Attachment/Zip.pm Resolved
Comment thread Bugzilla/Attachment/Zip.pm Resolved
Comment thread attachment.cgi Resolved
dklawren and others added 2 commits December 3, 2026 17:34
- Read attachment data through the storage while building the ZIP, so
  each attachment's data is not cached in memory alongside the archive.
- Replace characters Windows does not allow in member names, and trim
  names to 255 UTF-8 bytes, keeping the id prefix and extension.
- Show the "Download All (ZIP)" button only when at least one active
  attachment still has data, matching what the endpoint serves.
- Use a single button instead of a link wrapping a button, so the
  control has one tab stop.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment thread Bugzilla/Attachment/Zip.pm Resolved
Comment thread Bugzilla/Attachment/Zip.pm Resolved
Comment thread Bugzilla/Attachment/Zip.pm Resolved
Comment thread Bugzilla/Attachment/Zip.pm Resolved
…ownload

- Recheck attachment privacy on the loaded rows, closing the window between
  get_attachments_by_bug's id query and its row load.
- Hold the bytes actually read from storage to attachment_zip_max_size, since
  datasize can count characters rather than bytes.
- Throw attachment_zip_missing_data when an attachment's data is gone during
  the build, instead of writing an empty member.
- Strip trailing periods and spaces from member names, again after truncation.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

ZIP downloads can include attachments marked obsolete during loading, and authenticated HEAD requests build the full archive and log downloads even though they send no body.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Recheck obsolescence on loaded attachments

Bugzilla/​Attachment/​Zip.pm:40

exclude_obsolete filters IDs before get_attachments_by_bug loads the rows. If an attachment is marked obsolete between those queries, this filter still includes it even though the loaded row is already obsolete. Recheck isobsolete on the loaded row, just as this code does for privacy.

Medium severity Avoid building and logging ZIPs for HEAD requests

attachment.cgi:531

An authenticated HEAD request builds the entire ZIP and records an attachment-get for each member when logging is enabled, even though no body is sent. After checking access and the stored sizes, return just the ZIP headers for HEAD without building or logging the archive. Don’t set Content-Length on that path because the compressed size is unknown.

4 resolved since last review

Review effort: Balanced

…ownload

get_attachments_by_bug applies exclude_obsolete to its id query and loads the
rows in a second query, so an attachment made obsolete in between was still
added to the ZIP. Check isobsolete again on the loaded rows, as is already
done for privacy.
@dklawren

dklawren commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator Author

Replying to the two items in the latest Copilot review:

Recheck obsolescence on loaded attachments (Bugzilla/Attachment/Zip.pm:40): fixed in 22a5897. attachments_for_bug now checks isobsolete again on the loaded rows, next to the privacy recheck. The existing test now also hands an obsolete row through the id query and checks that it is dropped. The follow-up bug for moving these rechecks into get_attachments_by_bug will cover obsolescence as well as privacy.

HEAD requests (attachment.cgi:531): not changing this. A single-attachment view() behaves the same way on HEAD: it builds the response and logs an attachment-get. Nothing else in attachment.cgi handles HEAD separately, so a ZIP-only branch would be a one-off. log_user_request records the request method, so a HEAD is logged as HEAD and can't be mistaken for a download. A HEAD costs no more than a GET, and attachment_zip_max_size caps both.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants