Skip to content

fix: strip __proto__ keys from validation errors before merging - #4087

Open
SahinurDEV wants to merge 1 commit into
jaredpalmer:mainfrom
SahinurDEV:fix-validation-errors-proto-merge
Open

SahinurDEV wants to merge 1 commit into
jaredpalmer:mainfrom
SahinurDEV:fix-validation-errors-proto-merge

Conversation

@SahinurDEV

Copy link
Copy Markdown

Closes #4085.

Problem

runAllValidations merges field-level, schema and validate errors with deepmerge.all(). Formik resolves deepmerge@2.2.1, which copies every own key of a source object onto the result, including an own __proto__ key like the one JSON.parse creates. So an error object such as

validate: () => JSON.parse('{"__proto__": {"name": "poisoned"}, "age": "required"}')

doesn't merge as data. It replaces the prototype of the merged errors: errors.name reads "poisoned" through the prototype even though no name error was reported, while Object.keys(errors) only shows age. I reproduced this with deepmerge 2.2.1 directly, at the top level and in nested objects and array items. The global Object.prototype is not modified, but the merged errors object Formik hands to the app is.

Fix

Before merging, each of the three error objects goes through a small stripUnsafeErrorKeys helper. It walks plain objects and arrays and drops own __proto__ keys:

  • It returns the input unchanged (same reference) when there's nothing to strip, so normal validation doesn't allocate anything new.
  • When it does copy, it builds the copy key by key, so __proto__ is never assigned. (Object spread can compile to _extends, which would assign it.)
  • It doesn't descend into React elements (anything with $$typeof), class instances or other non-plain objects, so rich error messages pass through untouched.

This keeps the current deepmerge version and its nested-object/array merge behaviour (arrayMerge is unchanged), so there's no dependency bump. It also doesn't conflict in approach with #4080, which adds an isMergeableObject option to the same call.

Tests

Two regression tests in test/Formik.test.tsx:

  • a top-level own __proto__ key in validate errors leaves the merged errors with Object.prototype and no inherited name/isAdmin
  • nested __proto__ keys (inside an array item and an object) are dropped while schema + validate errors still merge into users[0]

Both tests fail on main and pass with the fix. Full formik suite: 8 suites / 228 tests passing. tsc -p tsconfig.build.json --noEmit is clean. I added a patch changeset.

AI disclosure: I used an AI coding assistant to help write this fix and its tests. I reproduced the issue, reviewed the change and ran the tests myself.

@changeset-bot

changeset-bot Bot commented Oct 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a5e70fb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
formik Patch
formik-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Oct 15, 2026

Copy link
Copy Markdown

@SahinurDEV is attempting to deploy a commit to the Formik Team on Vercel.

A member of the Team first needs to authorize it.

@codesandbox-ci

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

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.

[Security] Harden validation error merging against deepmerge prototype poisoning (CVE-2026-93753)

1 participant