Repository navigation
fix: don't let a nested local API call mutate the caller's locale - #18248
Open
Emmanuelzyronis wants to merge 1 commit into
Open
Emmanuelzyronis wants to merge 1 commit into
Emmanuelzyronis wants to merge 1 commit into
Conversation
`createLocalReq` assigned the resolved `locale` and `fallbackLocale` directly onto the `req` it was handed. When a caller passed its own `req` — which the docs recommend for transaction safety — along with a different `locale`, the nested call's locale stayed on the caller's request after it returned. Hooks that read `req.locale` afterwards, such as `mergeLocaleActions`, then merged the submitted data into the wrong locale, so a single-locale write could report success while updating a different locale than the one requested. Assign onto a copy instead, so the caller's request is left untouched, and add regression tests covering `locale` and `fallbackLocale`. Fixes payloadcms#18246
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.
What?
createLocalReqno longer mutates thereqit is given while resolvinglocaleandfallbackLocale. It assigns those onto a shallow copy instead.Why?
Fixes #18246.
When a caller passes its own
req— which the docs recommend for transaction safety — together with a differentlocale, the old code wrote the resolvedlocaleandfallbackLocalestraight onto that request. The nested call's locale therefore stayed on the caller's request after it returned.Hooks that read
req.localeafterwards, such asmergeLocaleActionsinfields/hooks/beforeChange/promise.ts, then merged the submitted data into the wrong locale. A single-locale write (PATCH ?locale=es) could return 200 while leavingesuntouched and writing the value into the default locale, and the snapshot version written by that save was wrong in the same way.How?
The locale is now resolved into a local before the fallback is sanitized, so
sanitizeFallbackLocalestill receives the resolved locale, and the caller's request object is left alone.Regression tests were added to the existing
packages/payload/src/utilities/createLocalReq.spec.ts, covering bothlocaleandfallbackLocale.Verification
createLocalReq.spec.ts: 9 passed.I used an AI coding assistant for this change and its tests, and have reviewed and verified the result.
Fixes #18246