When diffJson is called with objects with the special __proto__ key, the diff result is unexpected.
This issue is happening to the latest v9.0.0.
Reproduction: https://github.com/aforemendude/bugs-reproduction/tree/main/jsdiff-proto-edge-case
import { diffJson } from 'diff'
function diffAndLog(label, oldJson, newJson) {
const oldObject = JSON.parse(oldJson)
const newObject = JSON.parse(newJson)
const changes = diffJson(oldObject, newObject)
console.log(`${label}:`, JSON.stringify(changes, null, 2))
}
diffAndLog('normal', '{"normal":"old"}', '{"normal":"new"}')
console.log('--------------------')
diffAndLog('__proto__', '{"__proto__":"old"}', '{"__proto__":"new"}')
Output:
normal: [
{
"count": 1,
"added": false,
"removed": false,
"value": "{\n"
},
{
"count": 1,
"added": false,
"removed": true,
"value": " \"normal\": \"old\"\n"
},
{
"count": 1,
"added": true,
"removed": false,
"value": " \"normal\": \"new\"\n"
},
{
"count": 1,
"added": false,
"removed": false,
"value": "}"
}
]
--------------------
__proto__: [
{
"count": 1,
"added": false,
"removed": false,
"value": "{}"
}
]
Potential solution (unverified):
Change {} to Object.create(null).
When
diffJsonis called with objects with the special__proto__key, the diff result is unexpected.This issue is happening to the latest
v9.0.0.Reproduction: https://github.com/aforemendude/bugs-reproduction/tree/main/jsdiff-proto-edge-case
Output:
Potential solution (unverified):
jsdiff/src/diff/json.ts
Line 106 in bf227c1
Change
{}toObject.create(null).