[v4] Decompression bomb: transformer-sharp no cap on source pixels

Open
#19,534 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
node.js, typescript
Domain
backend, security

Research direction

Start by reading packages/transformer-sharp's transformFile.ts, handleRequest.ts, and prepareLegacyUpload.ts, which the issue identifies as the relevant entry points. Trace how options reach each Sharp constructor and how tryProbe obtains source dimensions; then check the package's tests for transformer and dynamic resize behavior. Done means oversized sources are rejected before pixel decoding with a clear 400, under a configurable budget across the paths described.

Written by the indexing model from the issue text.

Description

area: core Bug invalid-reproduction Payload OSS status: needs-triage v3
Describe the Bug

transformer-sharp: no cap on source pixels, a 2 KB AVIF can exhaust server memory

Summary

@payloadcms/transformer-sharp opens every uploaded image with Sharp's default input limit (268 megapixels) and offers no way to lower it for variants, crops or dynamic resizing. A highly compressible image (a flat-colour AVIF is the cheapest) is a few kilobytes on disk and several gigabytes once decoded, so upload.limits.fileSize does not help. Any account allowed to upload to a collection with a variant can push the server process past its memory limit with one request.

Versions

  • payload and @payloadcms/transformer-sharp: 4.0.0-beta.8
  • sharp: 0.35.5 (prebuilt libvips)
  • Node 24, macOS arm64

Measurements

I measured the Sharp calls the transformer makes, in isolation (same Sharp build, one fresh Node process per call, process.resourceUsage().maxRSS), not through a running Payload instance:

Source File size sharp(buf).resize({ width: 600 }) sharp(buf).webp()
7000 x 7000 (49 MP) 1,525 bytes 1.1 GB peak RSS 1.3 GB
12000 x 12000 (144 MP) 2,079 bytes 3.1 GB 3.4 GB
16000 x 16000 (256 MP) 3,691 bytes 5.2 GB, then throws 5.2 GB, then throws
  • sharp(buf).metadata() on the same files: 69 MB, 5 ms. Reading the header is safe.
  • sharp(buf, { limitInputPixels: 50_000_000 }) on the 256 MP file: throws Input image exceeds pixel limit in 2 ms at 69 MB.
  • The 256 MP case ends with libheif's own error (Security limit exceeded: Memory usage of 2304003546 bytes ... exceeds the security limit of 2147483648 bytes), but only after the process has reached 5.2 GB.

Where it happens

In packages/transformer-sharp (read from the published dist of beta.8):

  • transformFile.ts, transformSize and transformCrop: the constructor options are hard-coded to { animated: true } or {}. Nothing a project configures reaches them.
  • transformFile.ts, transformMain: constructorOptions is spread into the constructor, so limitInputPixels can be set there, but only for the main file, and only when the collection has adjustments.
  • handleRequest.ts (dynamic resize): maxWidth, maxHeight and maxPixels bound the output. The source is still decoded in full by sharp(sourceBuffer, sharpOptions).rotate().resize(...).
  • prepareLegacyUpload.ts: tryProbe already reads the source dimensions, and core's probeImageSize reads them from the header without decoding. Neither value is used to refuse an upload.

AVIF, GIF and WebP are also opened with animated: true, so a many-frame file multiplies the cost.

Expected behaviour

An upload whose decoded size exceeds a configurable budget is refused with a 400 before any pixel is decoded.

Suggested fix

  1. Add a maxInputPixels option to sharpTransformer (global, overridable per collection) with a conservative default, well under Sharp's 268 MP.
  2. Pass it as limitInputPixels to every Sharp constructor in the package: main file, crop, variants, probes and the dynamic handler.
  3. Refuse early from the dimensions the probe already returns (times the frame count for animated sources), so the error is a clean 400 rather than a Sharp exception.
  4. Apply constructorOptions to variants and crops as well, or document that it only covers the main file.
  5. In the dynamic handler, check the source pixel count from metadata() before resizing.

Workaround

A beforeOperation hook that reads sharp(file.data).metadata() and rejects when width * height * (pages ?? 1) is above a budget, since it runs before the transformer.

Link to the code that reproduces this issue

pnpx create-payload-app@latest -t blank

Reproduction Steps

Generate the file:

import sharp from 'sharp';

await sharp({ create: { width: 12000, height: 12000, channels: 3, background: '#336699' } })
  .avif({ effort: 0, quality: 30 })
  .toFile('bomb.avif'); // 2,079 bytes

Upload it to a collection configured like this:

upload: {
  limits: { fileSize: 5_000_000 },
  transformers: [
    sharpTransformer({ sharp, collections: { media: { variants: [{ name: 'thumbnail', width: 600 }] } } }),
  ],
}
Which area(s) are affected?

area: core

Environment Info
- `payload` and `@payloadcms/transformer-sharp`: 4.0.0-beta.8
- `sharp`: 0.35.5 (prebuilt libvips)
- Node 24, macOS arm64
Dominant language
TypeScript
Stars
45.8k
Forks
4.4k
Avg merge
2d 16h
Merged PRs (30d)
251

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from payloadcms/payload

All issues in payloadcms/payload

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.