Skip to content

Commit 62a6540

Browse files
authoredDec 29, 2022
fix: add __proto__ to objects and arrays
1 parent 072eb40 commit 62a6540

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed
 

‎CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
### Unreleased [[code][c-unreleased], [diff][d-unreleased]]
2+
3+
[c-unreleased]: https://github.com/json5/json5/tree/v1
4+
[d-unreleased]: https://github.com/json5/json5/compare/v1.0.1...v1
5+
6+
- Fix: Properties with the name `__proto__` are added to objects and arrays.
7+
([#199])
8+
9+
110
### v1.0.1 [[code][c1.0.1], [diff][d1.0.1]]
211

312
[c1.0.1]: https://github.com/json5/json5/tree/v1.0.1
@@ -272,3 +281,4 @@ parser for the regular JSON format.
272281
[#108]: https://github.com/json5/json5/pull/108
273282
[#134]: https://github.com/json5/json5/pull/134
274283
[#154]: https://github.com/json5/json5/issues/154
284+
[#199]: https://github.com/json5/json5/issues/199

‎src/parse.js

+34-7
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,34 @@ export default function parse (text, reviver) {
4242
function internalize (holder, name, reviver) {
4343
const value = holder[name]
4444
if (value != null && typeof value === 'object') {
45-
for (const key in value) {
46-
const replacement = internalize(value, key, reviver)
47-
if (replacement === undefined) {
48-
delete value[key]
49-
} else {
50-
value[key] = replacement
45+
if (Array.isArray(value)) {
46+
for (let i = 0; i < value.length; i++) {
47+
const key = String(i)
48+
const replacement = internalize(value, key, reviver)
49+
if (replacement === undefined) {
50+
delete value[key]
51+
} else {
52+
Object.defineProperty(value, key, {
53+
value: replacement,
54+
writable: true,
55+
enumerable: true,
56+
configurable: true,
57+
})
58+
}
59+
}
60+
} else {
61+
for (const key in value) {
62+
const replacement = internalize(value, key, reviver)
63+
if (replacement === undefined) {
64+
delete value[key]
65+
} else {
66+
Object.defineProperty(value, key, {
67+
value: replacement,
68+
writable: true,
69+
enumerable: true,
70+
configurable: true,
71+
})
72+
}
5173
}
5274
}
5375
}
@@ -973,7 +995,12 @@ function push () {
973995
if (Array.isArray(parent)) {
974996
parent.push(value)
975997
} else {
976-
parent[key] = value
998+
Object.defineProperty(parent, key, {
999+
value,
1000+
writable: true,
1001+
enumerable: true,
1002+
configurable: true,
1003+
})
9771004
}
9781005
}
9791006

‎test/parse.js

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ describe('JSON5', () => {
3333
assert.deepStrictEqual(JSON5.parse('{\\u0061\\u0062:1,\\u0024\\u005F:2,\\u005F\\u0024:3}'), {ab: 1, $_: 2, _$: 3})
3434
})
3535

36+
it('preserves __proto__ property names', () => {
37+
// eslint-disable-next-line no-proto
38+
assert.strictEqual(JSON5.parse('{"__proto__":1}').__proto__, 1)
39+
})
40+
3641
it('parses multiple properties', () => {
3742
assert.deepStrictEqual(JSON5.parse('{abc:1,def:2}'), {abc: 1, def: 2})
3843
})

0 commit comments

Comments
 (0)