Repository navigation
fix(plugin-cloud-storage): don't leave private keys on a reused request context - #18257
Open
adem-loghmari wants to merge 1 commit into
Open
adem-loghmari wants to merge 1 commit into
adem-loghmari wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #18191
What
plugin-cloud-storagewrites two private keys ontoreq.contextand removes neither from the object the caller owns:skipCloudStorage, set in theafterChangehook to guard the nestedupdatethat persists upload metadata._payloadCloudStorage, set by thepreserveFileDatabeforeChangehook to keep the file buffer afterreq.fileis cleared.Why the existing cleanup misses
createLocalReqreturns the caller's owncontextobject while it is empty, and replacesreq.contextwith 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 nestedreq.payload.update({ req })movesreq.contextoff the objectafterChangewrote to. Thefinallythen randelete req.context.skipCloudStorageagainst the copy, and the flag stayed set on the caller's object._payloadCloudStoragewas never deleted anywhere, andpreserveFileDataonly writes it when the key is absent, so it never refreshed either.Impact
A script that reuses one
contextobject across Local API calls (the commondisableRevalidatepattern in seed and import scripts) gets a document row for every upload after the first, complete withurl,filenameand 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.This is adapter-independent: the failure is in the shared hook, so any adapter built on
plugin-cloud-storagebehaves the same. The original report hit it throughstorage-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 realpreserveFileDatahook with anupdatemock that reproduces the spread-copy swap:contextkeeps its own keys and comes back free of both private keyscontextboth reach the adapter, with the correct buffer each time (the second one previously never uploaded)Both fail on
main(expected true to be undefined, andexpected "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, andtscreports no errors beyond the pre-existing ones from the unbuilt workspace (identical output before and after the change).🤖 Generated with Claude Code