Skip to content

fix(plugin-cloud-storage): don't leave private keys on a reused request context - #18257

Open
adem-loghmari wants to merge 1 commit into
payloadcms:mainfrom
adem-loghmari:fix/cloud-storage-context-leak
Open

adem-loghmari wants to merge 1 commit into
payloadcms:mainfrom
adem-loghmari:fix/cloud-storage-context-leak

Conversation

@adem-loghmari

@adem-loghmari adem-loghmari commented Sep 22, 2026 •

Copy link
Copy Markdown

Fixes #18191

What

plugin-cloud-storage writes two private keys onto req.context and removes neither from the object the caller owns:

  • skipCloudStorage, set in the afterChange hook to guard the nested update that persists upload metadata.
  • _payloadCloudStorage, set by the preserveFileData beforeChange hook to keep the file buffer after req.file is cleared.

Why the existing cleanup misses

createLocalReq returns the caller's own context object while it is empty, and replaces req.context with a spread copy once it is not:

https://github.com/payloadcms/payload/blob/main/packages/payload/src/utilities/createLocalReq.ts#L12-L21

That assignment mutates the same req, so the nested req.payload.update({ req }) moves req.context off the object afterChange wrote to. The finally then ran delete req.context.skipCloudStorage against the copy, and the flag stayed set on the caller's object. _payloadCloudStorage was never deleted anywhere, and preserveFileData only writes it when the key is absent, so it never refreshed either.

Impact

A script that reuses one context object across Local API calls (the common disableRevalidate pattern in seed and import scripts) gets a document row for every upload after the first, complete with url, filename and sizes, but no object is ever handed to the adapter and nothing is logged. The hook returns early on line 36 because the stale flag is still there.

const ctx = { context: { disableRevalidate: true } } // module-level, reused
await payload.create({ collection: 'media', data, file: fileA, ...ctx }) // uploaded
await payload.create({ collection: 'media', data, file: fileB, ...ctx }) // row created, no object, no error

This is adapter-independent: the failure is in the shared hook, so any adapter built on plugin-cloud-storage behaves the same. The original report hit it through storage-s3.

The fix

Keep a reference to the object each key is actually written to, clear it from both that object and the current req.context, and drop the preserved buffer once the upload has finished. Both are plugin-internal keys, and neither is read anywhere outside the plugin, so nothing else can depend on them surviving the operation.

I kept the change inside the plugin rather than touching createLocalReq's reference semantics, which plenty of other code relies on.

Tests

Two regression tests in afterChange.spec.ts, driven through the real preserveFileData hook with an update mock that reproduces the spread-copy swap:

  • the caller's context keeps its own keys and comes back free of both private keys
  • two uploads sharing one context both reach the adapter, with the correct buffer each time (the second one previously never uploaded)

Both fail on main (expected true to be undefined, and expected "vi.fn()" to be called 2 times, but got 1 times) and pass with the fix. Full package suite is green (56 tests), eslint and prettier are clean, and tsc reports no errors beyond the pre-existing ones from the unbuilt workspace (identical output before and after the change).

🤖 Generated with Claude Code

…st context

The afterChange hook writes two private keys onto `req.context`:
`skipCloudStorage`, to guard against the nested `update` that persists
upload metadata, and `_payloadCloudStorage`, written by the
`preserveFileData` beforeChange hook to keep the file buffer after
`req.file` is cleared.

Neither was reliably removed from the object the caller owns.
`createLocalReq` returns the caller's own `context` object while it is
empty, and replaces `req.context` with a spread copy once it is not, so
the nested `update` moves `req.context` off the object the hook wrote
to. `delete req.context.skipCloudStorage` then cleared the flag from the
copy and left it set on the caller's object. `_payloadCloudStorage` was
never deleted at all, and `preserveFileData` only writes it when the key
is absent, so it never refreshed either.

A script that reuses one `context` across Local API calls, the common
pattern for `disableRevalidate`-style flags, therefore got a document row
for every upload after the first but no object in storage, with nothing
logged: the hook returned early on the stale `skipCloudStorage`.

Keep a reference to the object each key is written to and clear it from
both that object and the current `req.context`, and drop the preserved
buffer once the upload has finished.

Fixes payloadcms#18191

Co-Authored-By: Claude Opus 5 <[email protected]>

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

1 participant