Skip to content

Commit 34af643

Browse files
committed
fix: An issue where objects with a custom "hasOwnProperty" could fail to be serialized
1 parent 8fd4def commit 34af643

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

deno/StructureClone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ function _serializeObject(value: unknown, map: Map<any, MapRef>) {
230230
obj: ref,
231231
});
232232
for (let prop in value) {
233-
if (value.hasOwnProperty(prop)) {
233+
if (Object.hasOwnProperty.call(value, prop)) {
234234
root[prop] = _serializeObject((<any>value)[prop], map);
235235
}
236236
}
@@ -395,7 +395,7 @@ function _deserializeRef(
395395
let obj = {} as any;
396396
map.set(ref, obj);
397397
for (let prop in refData.root) {
398-
if (refData.root.hasOwnProperty(prop)) {
398+
if (Object.hasOwnProperty.call(refData.root, prop)) {
399399
const value = refData.root[prop];
400400
obj[prop] = Array.isArray(value)
401401
? _deserializeRef(structure, value[0], map, transfered)

deno/StructureClone_test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,22 @@ Deno.test('serializeStructure() should support MessagePort objects', () => {
342342
});
343343
});
344344

345+
Deno.test(
346+
'serializeStructure() should not error when given an object without hasOwnProperty',
347+
() => {
348+
let obj = {
349+
myProp: 'abc',
350+
hasOwnProperty: null,
351+
};
352+
assertEquals(serializeStructure(obj), {
353+
root: {
354+
hasOwnProperty: null,
355+
myProp: 'abc',
356+
},
357+
});
358+
}
359+
);
360+
345361
Deno.test(
346362
'deserializeStructure() should return the root value for primitives',
347363
() => {
@@ -715,3 +731,27 @@ Deno.test(
715731
assert(deserialized.data !== deserialized.transferred[0]);
716732
}
717733
);
734+
735+
Deno.test(
736+
'deserializeStructure() should not error when given an object without hasOwnProperty',
737+
() => {
738+
const deserialized = deserializeStructure({
739+
root: ['$0'],
740+
refs: {
741+
$0: {
742+
root: {
743+
hasOwnProperty: null,
744+
myProp: 'abc',
745+
},
746+
},
747+
},
748+
});
749+
assertEquals(deserialized, {
750+
data: {
751+
hasOwnProperty: null,
752+
myProp: 'abc',
753+
},
754+
transferred: [],
755+
});
756+
}
757+
);

src/StructureClone.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,19 @@ describe('StructureClone', () => {
333333
},
334334
});
335335
});
336+
337+
it('should not error when given an object without hasOwnProperty', () => {
338+
let obj = {
339+
myProp: 'abc',
340+
hasOwnProperty: null as any,
341+
};
342+
expect(serializeStructure(obj)).toEqual({
343+
root: {
344+
hasOwnProperty: null,
345+
myProp: 'abc',
346+
},
347+
});
348+
});
336349
});
337350

338351
describe('deserializeStructure', () => {
@@ -660,5 +673,27 @@ describe('StructureClone', () => {
660673

661674
expect(deserialized.data).not.toBe(deserialized.transferred[0]);
662675
});
676+
677+
it('should not error when given an object without hasOwnProperty', () => {
678+
const deserialized = deserializeStructure({
679+
root: ['$0'],
680+
refs: {
681+
$0: {
682+
root: {
683+
hasOwnProperty: null,
684+
myProp: 'abc',
685+
},
686+
},
687+
},
688+
});
689+
690+
expect(deserialized).toEqual({
691+
data: {
692+
hasOwnProperty: null,
693+
myProp: 'abc',
694+
},
695+
transferred: [],
696+
});
697+
});
663698
});
664699
});

src/StructureClone.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function _serializeObject(value: unknown, map: Map<any, MapRef>) {
228228
obj: ref,
229229
});
230230
for (let prop in value) {
231-
if (value.hasOwnProperty(prop)) {
231+
if (Object.hasOwnProperty.call(value, prop)) {
232232
root[prop] = _serializeObject((<any>value)[prop], map);
233233
}
234234
}
@@ -390,7 +390,7 @@ function _deserializeRef(
390390
let obj = {} as any;
391391
map.set(ref, obj);
392392
for (let prop in refData.root) {
393-
if (refData.root.hasOwnProperty(prop)) {
393+
if (Object.hasOwnProperty.call(refData.root, prop)) {
394394
const value = refData.root[prop];
395395
obj[prop] = Array.isArray(value)
396396
? _deserializeRef(structure, value[0], map, transfered)

0 commit comments

Comments
 (0)