Repository navigation
fix: strip __proto__ keys from validation errors before merging - #4087
Open
SahinurDEV wants to merge 1 commit into
Open
SahinurDEV wants to merge 1 commit into
SahinurDEV wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: a5e70fb The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
|
@SahinurDEV is attempting to deploy a commit to the Formik Team on Vercel. A member of the Team first needs to authorize it. |
|
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
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.
Closes #4085.
Problem
runAllValidationsmerges field-level, schema andvalidateerrors withdeepmerge.all(). Formik resolvesdeepmerge@2.2.1, which copies every own key of a source object onto the result, including an own__proto__key like the oneJSON.parsecreates. So an error object such asdoesn't merge as data. It replaces the prototype of the merged
errors:errors.namereads"poisoned"through the prototype even though nonameerror was reported, whileObject.keys(errors)only showsage. I reproduced this with deepmerge 2.2.1 directly, at the top level and in nested objects and array items. The globalObject.prototypeis 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
stripUnsafeErrorKeyshelper. It walks plain objects and arrays and drops own__proto__keys:__proto__is never assigned. (Object spread can compile to_extends, which would assign it.)$$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 (
arrayMergeis unchanged), so there's no dependency bump. It also doesn't conflict in approach with #4080, which adds anisMergeableObjectoption to the same call.Tests
Two regression tests in
test/Formik.test.tsx:__proto__key invalidateerrors leaves the merged errors withObject.prototypeand no inheritedname/isAdmin__proto__keys (inside an array item and an object) are dropped while schema +validateerrors still merge intousers[0]Both tests fail on
mainand pass with the fix. Fullformiksuite: 8 suites / 228 tests passing.tsc -p tsconfig.build.json --noEmitis 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.