Skip to content

fix: pin sharp to 0.35.4 in ai-image-generator [AIS-610] - #11310

Open
Jared Jolton (jjolton-contentful) wants to merge 2 commits into
masterfrom
fix/aiig-pin-sharp-libheif
Open

Jared Jolton (jjolton-contentful) wants to merge 2 commits into
masterfrom
fix/aiig-pin-sharp-libheif

Conversation

@jjolton-contentful

@jjolton-contentful Jared Jolton (jjolton-contentful) commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

The AI Image Generator app action accepts caller-supplied image and mask URLs, fetches them, and pipes both into sharp. The current libheif advisory (GHSA-rgj7-g3m4-5g8c) requires sharp >= 0.35.4, which bundles patched libheif 1.23.2.

The deployed bundle was not running what any manifest claimed. The committed lockfile resolves sharp 0.33.4 / libvips 1.0.2, but neither the lockfile nor the installed tree reflected what actually ran:

  • install-linux-sharp force-installed @img/sharp-linux-x64 and @img/sharp-libvips-linux-x64 unpinned, so both resolved to latest (0.35.4 / libvips 1.3.3).
  • sharp itself stayed within the declared ^0.33.0 and resolved to 0.33.5.
  • npm reconciled that conflict by nesting the matching 0.33.5 / 1.0.4 pair under sharp/node_modules/, and Node resolution finds the nested copy first.

Verified on linux/amd64, the built bundle reported sharp=0.33.5 vips=8.15.3 heif=1.18.2 — the force-installed binaries sat unused at top level, and both copies of libvips shipped (35 MB). So the real exposure was libheif 1.18.2, neither the lockfile's 1.17.6 nor a patched 1.23.2.

Approach

Pin all three to exact, mutually consistent versions (sharp 0.35.4, @img/sharp-linux-x64 0.35.4, @img/sharp-libvips-linux-x64 1.3.3) in the root and backend manifests and in install-linux-sharp, so the declared range and the force-installed binaries can no longer drift apart.

sharp is marked esbuild --external, so it is resolved at runtime from the closure that bundle-sharp.js hand-copies. Three things had to change alongside the version bump:

  1. bundle-sharp.js module list. sharp 0.35 replaced the color/color-convert/color-name/color-string/is-arrayish/simple-swizzle chain with a single @img/colour, which the old copy list does not include. The script now iterates an explicit list and throws when an expected module is absent, so an incomplete bundle fails the build instead of shipping.
  2. sharp.RGBA removed. Its replacements (Colour/Color) alias a loose ColorLike union that will not typecheck structural .r/.g/.b access, so these helpers now use a local RGBA interface.
  3. Redundant pipe removed. replaceColorWithTransparent piped .raw() output back into this.maskImage — the same stream-backed instance the fetch stream had already ended. 0.33 tolerated it; 0.35 throws write after end. Piping already-raw pixels through another sharp was a no-op decode, so the pipe is gone. (Piping into a fresh sharp() instead does not work: raw pixels carry no dimensions and fail with Input buffer contains unsupported image format.)

Bundle size drops from 35 MB to 20 MB, since only one libvips now ships.

Keeping the three pins in sync. Two of them live in package.json and Renovate maintains those already (rangeStrategy: bump updates an exact pin in place, as it has for lodash, luxon and vitest). The third is embedded in the install-linux-sharp script, which the npm manager cannot see because it only reads dependency blocks, so Renovate would bump the manifests and silently leave the script behind. renovate.json now adds a regex customManager over that script plus a groupName so all three packages move in a single PR. Verified with renovate 44.107.2: renovate-config-validator passes with no migration warning, and a local --dry-run=extract shows the regex manager picking up all three versions alongside the npm manager's own sharp entry.

Testing steps

All verification ran in a linux/amd64 container, since the bundle targets Lambda and a host arm64 build proves nothing about it.

  • Bundle reports sharp=0.35.4 vips=8.18.6 heif=1.23.2
  • No nested @img/* duplicate under sharp/node_modules/; exactly one libvips-cpp.so in the bundle
  • All three built actions (aiig-select-edit, aiig-static, aiig-generate-image) require() cleanly against the bundled sharp
  • tsc --noEmit and the production tsc build are clean
  • Functional smoke test of the composite path used by the edit action
  • Backend suite matches its pre-change baseline at 12 passing
  • renovate-config-validator clean on the new renovate.json; local --dry-run=extract confirms the regex manager matches all three pinned versions and nothing else

To reproduce the bundle check:

npm ci && cd backend && npm ci && cd ..
node build-actions.js
npm run build-sharp
node -e "const s=require('./build/node_modules/sharp');console.log(s.versions)"

On the remaining test failures: the suite reports 7 failing both before and after this change. I captured a 0.33.5 baseline to confirm none of them are new — 3 are sinon double-wrap (Attempted to wrap fromOpenAiApiKey which is already wrapped) and 4 are mocha 2000 ms timeouts that pass in isolation (--grep PrepareImageForEdit is 4/4 green on both versions). The write after end failures this upgrade initially introduced are gone. Worth a separate cleanup ticket, but out of scope here.

Breaking Changes

None to the app's external contract. All changes are internal: the pinned versions, the bundle copy list, a type that was never exported from this package, and the Renovate config.

Worth flagging for reviewers: because sharp is external and hand-copied, a green CI run does not prove the bundle loads. Bumping the sharp range without the bundle-sharp.js change produces a bundle that dies at startup with Cannot find module '@img/colour' — reproduced while validating this. That is the main reason this is not a one-line dependency bump.

Dependencies and/or References

🤖 Generated with Claude Code

The AI Image Generator pipes caller-supplied image and mask URLs into
sharp, so it needs libheif 1.23.2 (GHSA-rgj7-g3m4-5g8c). Upgrade sharp to
0.35.4, which bundles libvips 1.3.3 / libheif 1.23.2.

The build force-installs the Linux binaries separately from the declared
range, which previously resolved @img/sharp-linux-x64 and
@img/sharp-libvips-linux-x64 to latest while sharp itself stayed within
^0.33.0. npm then nested the matching 0.33.5 / 1.0.4 pair under
sharp/node_modules, and Node resolved those first, so the deployed bundle
ran libheif 1.18.2 and shipped both copies of libvips. Pin all three to
exact, matching versions.

0.35 replaced sharp's color* dependency chain with @img/colour, which
bundle-sharp.js did not copy: bumping the range alone produces a bundle
that fails to load with "Cannot find module '@img/colour'". Drive the copy
from an explicit module list that fails loudly when an expected module is
absent.

0.35 also removed the sharp.RGBA type export and no longer tolerates
piping into an already-ended stream-backed instance. Replace RGBA with a
local interface, and drop the pipe into this.maskImage, which re-decoded
already-raw pixels through the instance the fetch stream had ended.

Verified on linux/amd64: the built bundle reports sharp 0.35.4 / vips
8.18.6 / heif 1.23.2 with no nested duplicate, all three actions load,
tsc is clean, and the backend suite matches its pre-change baseline
(12 passing; the 7 remaining failures reproduce on 0.33.5).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Pinning sharp in the two manifests leaves a third copy of the version
inside the install-linux-sharp script, which force-installs the Linux
binaries outside the dependency graph. Renovate's npm manager only reads
dependency blocks, so it would bump both manifests and leave the script
behind, recreating the skew this branch fixes with different numbers.

Add a regex customManager over that script and group the three packages
so they always move in one PR. Verified with renovate 44.107.2:
config-validator passes with no migration warning, and a local
dry-run=extract reports the regex manager finding sharp@0.35.4,
@img/sharp-linux-x64@0.35.4 and @img/sharp-libvips-linux-x64@1.3.3
alongside the npm manager's own sharp entry.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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