Describe the bug#
If query data has a "__proto__" key (JSON.parse keeps it as a normal property), it disappears after a refetch where anything else in that object changed.
replaceEqualDeep builds the new object with copy[key] = value. For __proto__ that assignment calls the setter and changes the copy's prototype instead of adding a property. So the key is gone, and its contents show up as inherited properties instead.
The first fetch is fine, because there is no previous data to share with.
Your minimal, reproducible example#
import { replaceEqualDeep } from '@tanstack/query-core'
const prev = JSON.parse('{"__proto__":{"admin":true},"count":1}')
const next = JSON.parse('{"__proto__":{"admin":true},"count":2}')
const result = replaceEqualDeep(prev, next)
Object.keys(result)
JSON.stringify(result)
result.admin
Steps to reproduce#
- Return JSON with a
__proto__ key from a queryFn, for example a map keyed by user input.
- Refetch so that some other field in that object changes.
data.__proto__ is no longer an own key, and the object's prototype is now that value.
Expected behavior#
__proto__ stays an own property, like any other key, and the prototype stays Object.prototype.
Platform#
Any. Reproduced on Node 24.
TanStack Query version#
5.104.5 (main)
Additional context#
I have a small fix with tests and will open a PR.