fix(v4): prevent formatError from throwing on Object.prototype keys in path#6083
Open
mahitha-ada wants to merge 1 commit into
Open
fix(v4): prevent formatError from throwing on Object.prototype keys in path#6083mahitha-ada wants to merge 1 commit into
mahitha-ada wants to merge 1 commit into
Conversation
…n path
formatError builds its nested tree using a plain object as a dictionary
and initializes each node with `curr[el] = curr[el] || { _errors: [] }`.
When a validation issue's path segment matches a member inherited from
Object.prototype (e.g. "toString", "constructor", "valueOf"), `curr[el]`
resolves to the inherited member instead of undefined, so the `||`
short-circuits, `curr` is assigned that member (which has no `_errors`
array), and the subsequent `curr[el]._errors.push(...)` throws a
TypeError.
Guard node initialization with Object.prototype.hasOwnProperty.call,
mirroring the existing fix in flattenError (colinhacks#5265) and treeifyError.
Adds a regression test that fails without this change.
Contributor
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — guards formatError against Object.prototype key collisions in path segments, matching the approach used in treeifyError and flatten().
- Guard node initialization with
hasOwnProperty.call— replacing the previouscurr[el] || { _errors: [] }pattern that would resolve inherited members (e.g.toString) to functions, causing aTypeErroron_errors.push(). - Regression test —
toString,constructor, andvalueOfpath segments informatError.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
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.
fix(v4): prevent
formatErrorfrom throwing onObject.prototypekeys in pathProblem
formatErrorbuilds its nested error tree using a plain object as a dictionary, initializing each node with:When a validation issue's
pathsegment matches a member inherited fromObject.prototype(e.g.toString,constructor,valueOf,hasOwnProperty),curr[el]resolves to the inherited member instead ofundefined. The||short-circuits,curris assigned that member (a function, with no_errorsarray), and the subsequentcurr[el]._errors.push(...)throws:Reproduction
This is the same class of bug as the reported
treeifyErrorissue (#6070) and the previously-fixedflatten()issue (#5265 → #5266). #6070's fix addressestreeifyError'spropertiesdictionary;formatErrorhas the identical vulnerability and is not covered by that fix.Fix
Guard node initialization with
Object.prototype.hasOwnProperty.call(...)before assigning, so inherited members are never mistaken for existing nodes — mirroring the guard used intreeifyErrorand theObject.create(null)approach used forflatten():Behavior is unchanged for all non-prototype keys.
Tests
Adds a regression test (
formatError does not throw on Object.prototype keys in path) that parses a schema withtoString,constructor, andvalueOffields and assertsformatErrorreturns the populated tree instead of throwing. The test fails on the current code (TypeError) and passes with this change.Verified locally:
vitest run error.test→ 118 passed, 0 type errors (with fix)TypeErrorabove (confirming it's a true regression test)biome checkclean