Skip to content

fix: avoid buffering large client uploads into memory - #17856

Merged
paulpopus merged 5 commits into
mainfrom
fix/large-file-uploads-being-buffered-into-memory
Aug 26, 2026
Merged

paulpopus merged 5 commits into
mainfrom
fix/large-file-uploads-being-buffered-into-memory

Conversation

@paulpopus

@paulpopus paulpopus commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Client uploads send a file straight from the browser to storage, bypassing the Payload server. The server still reads the file afterward, to save it locally or to read metadata such as width and height. It previously always downloaded the whole file into one in-memory buffer for that step, with no size limit. This is a real risk for the multi-gigabyte files that client uploads exist to support. The server now reads only the bytes each save operation actually needs, and streams any full-file read straight to disk instead of memory.

Why

Azure's client-upload support removes the old five-gigabyte upload ceiling, so a single upload can now be far larger than the server's available memory. Re-downloading that whole file into a buffer, just to check its size or resave it unchanged, does not scale with that change.

How

  • A new content-requirement check decides how much of a file the server needs before it fetches anything: nothing, when the client-reported metadata is enough; a small byte range, when only the image dimensions are needed; or the full file, when local storage needs the real bytes, the file will be resized or reformatted, or a configured mime-type allow list needs to inspect the content.
  • A full-file read now streams straight to a temporary file on disk, replacing the single in-memory buffer.
  • An unmodified temporary file that only needs saving to disk-disabled storage is left untouched, instead of being read into memory and written back unchanged.
  • Fixed a check for the disableLocalStorage option. Collections that leave it unset, the most common case, could take the reduced-fetch path meant only for disabled local storage. That risked saving a truncated or empty file.
  • Fixed the byte-range probe's request clone. It broke native request properties that real storage handlers read, such as an abort signal. Every adapter in this repository that reads that property, including S3 and Azure, crashed against a client upload needing only its image dimensions.
  • Corrected the list of image formats treated as animated. It skipped multi-page TIFF files and wrongly flagged every AVIF file as animated, even single-frame ones.

Testing

Added tests for the new content-requirement decision and the streaming behavior. Added integration tests that complete a real client upload against the S3 and Azure storage adapters for a file needing only its dimensions, the exact path the request-clone fix above corrects. No integration test exercised that path before, which is how the bug shipped unnoticed.

Alternatives considered

Tried splitting a full-file read into repeated smaller range requests, on the idea that shorter-lived reads would free memory sooner. Measured memory during the change and found no improvement, so it was not kept.

Related work

Related to #17318 and #17319, which enabled Azure client uploads larger than 5 GB.

@github-actions

github-actions Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖

Meta File Out File Size (raw) Note
packages/next/meta_index.json esbuild/index.js 214.77 KB 🆕 Added
packages/payload/meta_index.json esbuild/index.js 1.41 MB 🆕 Added
packages/payload/meta_shared.json esbuild/exports/shared.js 213.69 KB 🆕 Added
packages/richtext-lexical/meta_client.json esbuild/exports/client_optimized/index.js 286.50 KB 🆕 Added
packages/ui/meta_client.json esbuild/exports/client_optimized/index.js 36.54 KB 🆕 Added
packages/ui/meta_shared.json esbuild/exports/shared_optimized/index.js 18.95 KB 🆕 Added
Largest paths These visualization shows top 20 largest paths in the bundle.

Meta file: packages/next/meta_index.json, Out file: esbuild/index.js

Path Size
../../node_modules ${{\color{Goldenrod}{ ████████████████████████▊ }}}$ 99.0%, 210.74 KB
dist/adapters/router.js ${{\color{Goldenrod}{ }}}$ 0.3%, 718 B
dist/adapters/server.js ${{\color{Goldenrod}{ }}}$ 0.3%, 533 B
dist/adapters/layout.js ${{\color{Goldenrod}{ }}}$ 0.2%, 526 B
dist/adapters/views.js ${{\color{Goldenrod}{ }}}$ 0.2%, 409 B
dist/esbuildEntry.js ${{\color{Goldenrod}{ }}}$ 0.0%, 0 B

Meta file: packages/payload/meta_index.json, Out file: esbuild/index.js

Path Size
../../node_modules ${{\color{Goldenrod}{ ████████████████▊ }}}$ 67.4%, 944.33 KB
dist/fields/hooks ${{\color{Goldenrod}{ ▊ }}}$ 3.2%, 44.38 KB
dist/collections/operations ${{\color{Goldenrod}{ ▊ }}}$ 3.1%, 43.46 KB
dist/utilities/configToJSONSchema.js ${{\color{Goldenrod}{ ▎ }}}$ 1.1%, 15.99 KB
dist/auth/operations ${{\color{Goldenrod}{ ▎ }}}$ 1.1%, 15.57 KB
dist/queues/operations ${{\color{Goldenrod}{ ▎ }}}$ 1.0%, 14.29 KB
dist/fields/config ${{\color{Goldenrod}{ ▎ }}}$ 1.0%, 13.63 KB
dist/globals/operations ${{\color{Goldenrod}{ ▎ }}}$ 1.0%, 13.36 KB
dist/fields/validations.js ${{\color{Goldenrod}{ ▏ }}}$ 0.8%, 10.69 KB
dist/collections/config ${{\color{Goldenrod}{ ▏ }}}$ 0.7%, 9.93 KB
dist/bin/generateImportMap ${{\color{Goldenrod}{ ▏ }}}$ 0.7%, 9.84 KB
dist/config/orderable ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 8.07 KB
dist/uploads/fetchAPI-multipart ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 7.87 KB
dist/index.js ${{\color{Goldenrod}{ ▏ }}}$ 0.6%, 7.78 KB
dist/hierarchy/utils ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 7.64 KB
dist/database/migrations ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 7.56 KB
dist/config/sanitize.js ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 7.07 KB
dist/utilities/telemetry ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 6.35 KB
dist/collections/endpoints ${{\color{Goldenrod}{ }}}$ 0.4%, 6.12 KB
dist/auth/strategies ${{\color{Goldenrod}{ }}}$ 0.4%, 5.61 KB
(other) ${{\color{Goldenrod}{ ████████▏ }}}$ 32.6%, 456.44 KB

Meta file: packages/payload/meta_shared.json, Out file: esbuild/exports/shared.js

Path Size
../../node_modules ${{\color{Goldenrod}{ █████████████████▉ }}}$ 71.8%, 150.13 KB
dist/fields/validations.js ${{\color{Goldenrod}{ █▎ }}}$ 5.1%, 10.69 KB
dist/fields/config ${{\color{Goldenrod}{ ▋ }}}$ 2.8%, 5.83 KB
dist/utilities/traverseFields.js ${{\color{Goldenrod}{ ▌ }}}$ 2.1%, 4.45 KB
dist/collections/config ${{\color{Goldenrod}{ ▍ }}}$ 1.6%, 3.33 KB
dist/config/orderable ${{\color{Goldenrod}{ ▍ }}}$ 1.5%, 3.13 KB
dist/fields/baseFields ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 2.79 KB
dist/utilities/deepCopyObject.js ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 2.69 KB
dist/config/client.js ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 2.69 KB
dist/auth/cookies.js ${{\color{Goldenrod}{ ▏ }}}$ 0.7%, 1.55 KB
dist/utilities/flattenTopLevelFields.js ${{\color{Goldenrod}{ ▏ }}}$ 0.7%, 1.41 KB
dist/utilities/getVersionsConfig.js ${{\color{Goldenrod}{ ▏ }}}$ 0.5%, 1.04 KB
dist/globals/config ${{\color{Goldenrod}{ }}}$ 0.4%, 939 B
dist/utilities/flattenAllFields.js ${{\color{Goldenrod}{ }}}$ 0.4%, 793 B
dist/utilities/unflatten.js ${{\color{Goldenrod}{ }}}$ 0.4%, 779 B
dist/utilities/sanitizeUserDataForEmail.js ${{\color{Goldenrod}{ }}}$ 0.3%, 713 B
dist/auth/extractJWT.js ${{\color{Goldenrod}{ }}}$ 0.3%, 696 B
dist/utilities/getFieldPermissions.js ${{\color{Goldenrod}{ }}}$ 0.3%, 651 B
dist/utilities/getSafeRedirect.js ${{\color{Goldenrod}{ }}}$ 0.3%, 632 B
dist/errors/ValidationError.js ${{\color{Goldenrod}{ }}}$ 0.3%, 577 B
(other) ${{\color{Goldenrod}{ ███████ }}}$ 28.2%, 58.98 KB

Meta file: packages/richtext-lexical/meta_client.json, Out file: esbuild/exports/client_optimized/index.js

Path Size
dist/features/blocks ${{\color{Goldenrod}{ ███▎ }}}$ 13.1%, 37.20 KB
dist/lexical/ui ${{\color{Goldenrod}{ ███ }}}$ 12.1%, 34.20 KB
dist/lexical/plugins ${{\color{Goldenrod}{ ██▉ }}}$ 11.7%, 33.01 KB
dist/features/table ${{\color{Goldenrod}{ ██▍ }}}$ 9.6%, 27.22 KB
dist/features/link ${{\color{Goldenrod}{ █▋ }}}$ 6.6%, 18.82 KB
dist/features/toolbars ${{\color{Goldenrod}{ █▌ }}}$ 6.2%, 17.45 KB
dist/features/upload ${{\color{Goldenrod}{ █▎ }}}$ 5.0%, 14.28 KB
dist/features/textState ${{\color{Goldenrod}{ ▉ }}}$ 3.9%, 11.08 KB
dist/lexical/utils ${{\color{Goldenrod}{ ▉ }}}$ 3.5%, 10.02 KB
dist/features/relationship ${{\color{Goldenrod}{ ▊ }}}$ 3.4%, 9.61 KB
dist/features/converters ${{\color{Goldenrod}{ ▊ }}}$ 3.0%, 8.36 KB
dist/utilities/fieldsDrawer ${{\color{Goldenrod}{ ▋ }}}$ 2.9%, 8.12 KB
dist/features/debug ${{\color{Goldenrod}{ ▋ }}}$ 2.6%, 7.40 KB
dist/lexical/config ${{\color{Goldenrod}{ ▍ }}}$ 1.8%, 5.14 KB
dist/features/lists ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 3.64 KB
dist/features/format ${{\color{Goldenrod}{ ▎ }}}$ 1.2%, 3.28 KB
dist/lexical/LexicalEditor.js ${{\color{Goldenrod}{ ▎ }}}$ 1.1%, 3.23 KB
dist/features/horizontalRule ${{\color{Goldenrod}{ ▎ }}}$ 1.1%, 3.18 KB
dist/field/Field.js ${{\color{Goldenrod}{ ▎ }}}$ 1.0%, 2.88 KB
dist/lexical/nodes ${{\color{Goldenrod}{ ▏ }}}$ 0.9%, 2.66 KB
(other) ${{\color{Goldenrod}{ █████████████████████▋ }}}$ 86.9%, 246.09 KB

Meta file: packages/ui/meta_client.json, Out file: esbuild/exports/client_optimized/index.js

Path Size
dist/exports/client ${{\color{Goldenrod}{ █████████████████████████ }}}$ 100.0%, 26.90 KB

Meta file: packages/ui/meta_shared.json, Out file: esbuild/exports/shared_optimized/index.js

Path Size
dist/graphics/Logo ${{\color{Goldenrod}{ ███████▋ }}}$ 30.5%, 5.57 KB
../../node_modules ${{\color{Goldenrod}{ ███▌ }}}$ 14.5%, 2.65 KB
dist/graphics/Icon ${{\color{Goldenrod}{ ██ }}}$ 8.3%, 1.51 KB
dist/utilities/formatDocTitle ${{\color{Goldenrod}{ █▊ }}}$ 7.2%, 1.32 KB
dist/providers/TableColumns ${{\color{Goldenrod}{ █▏ }}}$ 4.7%, 866 B
dist/utilities/getGlobalData.js ${{\color{Goldenrod}{ █ }}}$ 4.2%, 762 B
dist/utilities/api.js ${{\color{Goldenrod}{ █ }}}$ 4.1%, 756 B
dist/utilities/groupNavItems.js ${{\color{Goldenrod}{ █ }}}$ 4.1%, 745 B
dist/elements/Translation ${{\color{Goldenrod}{ ▋ }}}$ 2.7%, 493 B
dist/utilities/handleTakeOver.js ${{\color{Goldenrod}{ ▌ }}}$ 2.4%, 440 B
dist/utilities/traverseForLocalizedFields.js ${{\color{Goldenrod}{ ▌ }}}$ 2.3%, 419 B
dist/elements/withMergedProps ${{\color{Goldenrod}{ ▍ }}}$ 1.9%, 339 B
dist/utilities/getNavGroups.js ${{\color{Goldenrod}{ ▍ }}}$ 1.9%, 338 B
dist/utilities/getVisibleEntities.js ${{\color{Goldenrod}{ ▍ }}}$ 1.8%, 329 B
dist/elements/WithServerSideProps ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 232 B
dist/layouts/Root ${{\color{Goldenrod}{ ▎ }}}$ 1.3%, 230 B
dist/utilities/handleGoBack.js ${{\color{Goldenrod}{ ▎ }}}$ 1.0%, 180 B
dist/fields/mergeFieldStyles.js ${{\color{Goldenrod}{ ▏ }}}$ 0.9%, 158 B
dist/forms/Form ${{\color{Goldenrod}{ ▏ }}}$ 0.8%, 152 B
dist/utilities/handleBackToDashboard.js ${{\color{Goldenrod}{ ▏ }}}$ 0.8%, 152 B
(other) ${{\color{Goldenrod}{ █████████████████▍ }}}$ 69.5%, 12.68 KB
Details

Next to the size is how much the size has increased or decreased compared with the base branch of this PR.

  • ‼️: Size increased by 20% or more. Special attention should be given to this.
  • ⚠️: Size increased in acceptable range (lower than 20%).
  • ✅: No change or even downsized.
  • 🗑️: The out file is deleted: not found in base branch.
  • 🆕: The out file is newly found: will be added to base branch.

@paulpopus paulpopus changed the title fix: large file uploads being buffered into memory fix: avoid buffering large client uploads into memory Aug 19, 2026
@paulpopus
paulpopus marked this pull request as ready for review August 20, 2026 12:51
@paulpopus
paulpopus dismissed nathanlentz’s stale review August 25, 2026 20:12

Addressed feedback

@paulpopus
paulpopus merged commit 6b1f1cb into main Aug 26, 2026
283 checks passed
@paulpopus
paulpopus deleted the fix/large-file-uploads-being-buffered-into-memory branch August 26, 2026 14:51
AlessioGr pushed a commit that referenced this pull request Sep 2, 2026
## Summary

PR #17856 fixed client uploads so the server reads only the bytes each
save operation needs, instead of always downloading the whole file into
memory. This PR adds tests that fail if that behavior regresses: if a
handler is called when no file content is needed, if a full-file read
goes through a whole-body buffering API instead of a stream, or if a
header read goes past its byte boundary.

## Why

The original fix was correct, but nothing stopped a future change from
reintroducing the same problem. Size-based assertions alone could pass
even after a regression, because small test fixtures hide the difference
between a bounded read and an unbounded one. These tests catch the
regression at its source instead.

## How

- Each unit test tripwire throws the moment a forbidden operation
happens, instead of only checking file size afterward. The no-content
handler throws if called at all. The full-content response throws if
`arrayBuffer()` is read. The header-probe stream throws if pulled past
the 1 MiB boundary.
- The Azure integration suite now spies on the real
`BlockBlobClient.getProperties` and `download` methods and checks exact
call counts and arguments, for the `none`, `header`, and `full`
content-requirement paths.
- Spies install only after the browser-equivalent upload finishes, so
the client's own PUT to Azure does not get counted as a server-side
read.
- Two Azure test collections gained a `beforeValidate` hook that checks
`req.file`'s shape (buffered data vs. temp file on disk) for two
specific test fixtures, so the document lifecycle itself proves the file
was not over-fetched.
- The header-only Azure test now uses `test/uploads/2mb.jpg` instead of
a small PNG, so the 1 MiB range request is a true partial read rather
than one that happens to cover the whole file.

Written with AI
paulpopus added a commit that referenced this pull request Sep 10, 2026
Backport of #17856

## Summary

Client uploads send a file straight from the browser to storage,
bypassing the Payload server. The server still reads the file afterward,
to save it locally or to read metadata such as width and height. It
previously always downloaded the whole file into one in-memory buffer
for that step, with no size limit. This is a real risk for the
multi-gigabyte files that client uploads exist to support. The server
now reads only the bytes each save operation actually needs, and streams
any full-file read straight to disk instead of memory.

## Why

Azure's client-upload support removes the old five-gigabyte upload
ceiling, so a single upload can now be far larger than the server's
available memory. Re-downloading that whole file into a buffer, just to
check its size or resave it unchanged, does not scale with that change.

## How

- A new content-requirement check decides how much of a file the server
needs before it fetches anything: nothing, when the client-reported
metadata is enough; a small byte range, when only the image dimensions
are needed; or the full file, when local storage needs the real bytes,
the file will be resized or reformatted, or a configured mime-type allow
list needs to inspect the content.
- A full-file read now streams straight to a temporary file on disk,
replacing the single in-memory buffer.
- An unmodified temporary file that only needs saving to disk-disabled
storage is left untouched, instead of being read into memory and written
back unchanged.
- Fixed a check for the `disableLocalStorage` option. Collections that
leave it unset, the most common case, could take the reduced-fetch path
meant only for disabled local storage. That risked saving a truncated or
empty file.
- Fixed the byte-range probe's request clone. It broke native request
properties that real storage handlers read, such as an abort signal.
Every adapter in this repository that reads that property, including S3
and Azure, crashed against a client upload needing only its image
dimensions.
- Corrected the list of image formats treated as animated. It skipped
multi-page TIFF files and wrongly flagged every AVIF file as animated,
even single-frame ones.

## Testing

Added tests for the new content-requirement decision and the streaming
behavior. Added integration tests that complete a real client upload
against the S3 and Azure storage adapters for a file needing only its
dimensions, the exact path the request-clone fix above corrects. No
integration test exercised that path before, which is how the bug
shipped unnoticed.

## Alternatives considered

Tried splitting a full-file read into repeated smaller range requests,
on the idea that shorter-lived reads would free memory sooner. Measured
memory during the change and found no improvement, so it was not kept.

## Related work

Related to #17318 and #17319, which enabled Azure client uploads larger
than 5 GB.
paulpopus added a commit that referenced this pull request Sep 21, 2026
…#17996)

## Summary

This PR applies review follow-ups from #17856. It removes duplicate
logic, simplifies a temp file save decision, and adds error logging for
temp file cleanup.

## How

- Add a shared `hasCropOrResizeEdit` helper. `generateFileData` and
`getFileFromUploadInstructions` now use it, instead of each keeping its
own copy of the same crop-or-resize check.
- Replace two boolean flags with one `resolveTempFileHandling` function.
It returns one outcome for an unprocessed temp file: copy it, read it
into a buffer, or skip the save.
- Rename `isClientUploadTempFile` to `isClientUploadReference` in
`unlinkTempFiles`. The new name states that the file has an upload
reference, not that it is a temp file.


## Related work

- Follow-up to #17856
nathanlentz added a commit that referenced this pull request Nov 9, 2026
…#19122)

2/3 for stack
[19374](https://github.com/payloadcms/payload/stack/19374).

Upload transformers receive only `file` today. They need to also get
`source`, `originalSource`, `doc`, and `originalDoc`. This PR adds those
arguments. `file` stays for now and existing transformers work without
changes.

- Transformers can now read and change transform state
- Transformers now see the stored document.
- Transformers get _lazy_ file sources. `source` (the current file) and
`originalSource` (the retained original file) are `FileSource` objects.
They read byte ranges or streams only when called.
- `FileSource` is a new type I am introducing which will be needed by
the Metadata API

### API

Before:

```ts
transformFile: async ({ file }) => {
  return { status: 'continue' }
}
```

After:

```ts
transformFile: async ({ doc, source }) => {
  const header = await source.readFileBytes({ numberOfBytes: 1024, startByte: 0 })

  doc._transforms = { ...doc._transforms, myTransformer: { score: score(header) } }

  return { status: 'continue' }
}
```

### Note
For direct uploads to cloud storage, Payload no longer decides before
the upload document exists whether a transformer needs the whole file.
It first reads only what the upload needs without transformers, then
calls `canTransform` with the real `doc`. It will download the whole
file only when a transformer accepts the upload in order to preserve
work done in #17856
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants