Skip to content

fix: don't let a nested local API call mutate the caller's locale - #18248

Open
Emmanuelzyronis wants to merge 1 commit into
payloadcms:mainfrom
Emmanuelzyronis:fix/local-req-locale-leak
Open

Emmanuelzyronis wants to merge 1 commit into
payloadcms:mainfrom
Emmanuelzyronis:fix/local-req-locale-leak

Conversation

@Emmanuelzyronis

Copy link
Copy Markdown

What?

createLocalReq no longer mutates the req it is given while resolving locale and fallbackLocale. 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 different locale, the old code wrote the resolved locale and fallbackLocale straight onto that request. The nested call's locale therefore stayed on the caller's request after it returned.

Hooks that read req.locale afterwards, such as mergeLocaleActions in fields/hooks/beforeChange/promise.ts, then merged the submitted data into the wrong locale. A single-locale write (PATCH ?locale=es) could return 200 while leaving es untouched and writing the value into the default locale, and the snapshot version written by that save was wrong in the same way.

How?

- req.locale = …resolvedLocale
- req.fallbackLocale = …sanitizedFallback
+ req = {
+   ...req,
+   fallbackLocale: sanitizedFallback!,
+   locale: resolvedLocale,
+ }

The locale is now resolved into a local before the fallback is sanitized, so sanitizeFallbackLocale still 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 both locale and fallbackLocale.

Verification

  • createLocalReq.spec.ts: 9 passed.
  • Reverting only the source change makes exactly the 2 new tests fail while the 6 pre-existing tests still pass, confirming they cover the reported behaviour.
  • I did not run the full monorepo suite locally, so CI is the authority for that.

I used an AI coding assistant for this change and its tests, and have reviewed and verified the result.

Fixes #18246

`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

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

Development

Successfully merging this pull request may close these issues.

Nested Local API call with req + a different locale rewrites the parent operation's req.locale, so a save's localized values land in the wrong locale

1 participant