fix(store): harden setState against prototype pollution#1
Merged
afiiif merged 1 commit intoafiiif:mainfrom Apr 13, 2026
Merged
Conversation
The change-detection loop in `setState` used `for...in` over the
incoming partial, which walks the full prototype chain. Any enumerable
property inherited from `Object.prototype` (whether introduced by a
polluted global, a faulty polyfill, or an untrusted payload parsed into
the call site) would be treated as a legitimate state key, pushed into
`changedKeys`, and forwarded to subscribers. Downstream consumers
tracking those keys via the proxy-based `useStore` hook would then
observe phantom updates for fields the caller never actually set.
Replace the iteration with `Object.keys(newValue)` so only own
enumerable string keys participate in the shallow diff, and explicitly
skip `__proto__`, `constructor`, and `prototype` as defense-in-depth
against payloads (e.g. `JSON.parse` output) where these names appear as
own properties. The subsequent `{ ...prevState, ...newValue }` merge is
already safe because object spread uses `CreateDataProperty` semantics,
but filtering upstream keeps the dangerous keys from ever reaching
`changedKeys` or the subscriber notification path.
Behaviour is unchanged for well-formed state objects: `Object.keys`
enumerates the same own keys `for...in` did, with no measurable perf
impact. Full test suite (90 tests) passes.
RakulAgn
commented
Apr 13, 2026
Owner
|
Nice! 🙌 Thanks @RakulAgn |
Contributor
Author
Have you Checked the Email and Concept |
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.
The change-detection loop in
setStateusedfor...inover the incoming partial, which walks the full prototype chain. Any enumerable property inherited fromObject.prototype(whether introduced by a polluted global, a faulty polyfill, or an untrusted payload parsed into the call site) would be treated as a legitimate state key, pushed intochangedKeys, and forwarded to subscribers. Downstream consumers tracking those keys via the proxy-baseduseStorehook would then observe phantom updates for fields the caller never actually set.Replace the iteration with
Object.keys(newValue)so only own enumerable string keys participate in the shallow diff, and explicitly skip__proto__,constructor, andprototypeas defense-in-depth against payloads (e.g.JSON.parseoutput) where these names appear as own properties. The subsequent{ ...prevState, ...newValue }merge is already safe because object spread usesCreateDataPropertysemantics, but filtering upstream keeps the dangerous keys from ever reachingchangedKeysor the subscriber notification path.Behaviour is unchanged for well-formed state objects:
Object.keysenumerates the same own keysfor...indid, with no measurable perf impact. Full test suite (90 tests) passes.