Registry Mirror
Skip to content

File tree

‎packages/client/src/rpc-json-serializer.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,54 @@ describe('rpcJsonSerializer: deserialize', () => {
580580
})
581581
expect(JSON.stringify(payload)).toBe(snapshot)
582582
})
583+
584+
it('rejects walking into a container shared by several slots', () => {
585+
const shared = { x: '1', y: '2', f: null }
586+
587+
expect(() =>
588+
serializer.deserializeJson({
589+
json: [shared, shared],
590+
meta: [
591+
['bigint', 0, 'x'],
592+
['bigint', 1, 'y'],
593+
],
594+
}),
595+
).toThrow('Invalid RPC serialized data: segment "1" cannot be walked into.')
596+
597+
expect(() =>
598+
serializer.deserializeJson({
599+
json: [shared, shared],
600+
maps: [
601+
[0, 'f'],
602+
[1, 'f'],
603+
],
604+
blobs: [new Blob(['a']), new Blob(['b'])],
605+
}),
606+
).toThrow('Invalid RPC serialized data: segment "1" cannot be walked into.')
607+
608+
expect(shared).toEqual({ x: '1', y: '2', f: null })
609+
})
610+
611+
it('rejects walking into a cycle', () => {
612+
const cyclic: any = { id: '1' }
613+
cyclic.self = cyclic
614+
615+
expect(() =>
616+
serializer.deserializeJson({ json: cyclic, meta: [['bigint', 'self', 'id']] }),
617+
).toThrow('Invalid RPC serialized data: segment "self" cannot be walked into.')
618+
})
619+
620+
it('still restores through one slot of a shared container', () => {
621+
const shared = { x: '1' }
622+
623+
const result = serializer.deserializeJson({
624+
json: [shared, shared],
625+
meta: [['bigint', 0, 'x']],
626+
}) as any[]
627+
628+
expect(result).toEqual([{ x: 1n }, { x: '1' }])
629+
expect(shared).toEqual({ x: '1' })
630+
})
583631
})
584632

585633
describe('rpcJsonSerializer: security', () => {

‎packages/client/src/rpc-json-serializer.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,29 @@ function assertSerializedType(ok: boolean, type: string, expected: string): void
5151
}
5252
}
5353

54-
function walkInto(parent: any, key: Segment, original: unknown, restored?: Set<unknown>): any {
54+
function walkInto(
55+
parent: any,
56+
key: Segment,
57+
original: unknown,
58+
copied: Set<unknown>,
59+
restored?: Set<unknown>,
60+
): any {
5561
const value = parent[key]
5662

5763
if (!isPlainObjectOrArray(value) || restored?.has(value)) {
5864
throw invalidSerializedData(`segment "${key}" cannot be walked into.`)
5965
}
6066

61-
return value === original ? copyOnWrite(parent, key, original) : value
67+
if (value !== original) {
68+
return value
69+
}
70+
71+
if (copied.has(original)) {
72+
throw invalidSerializedData(`segment "${key}" cannot be walked into.`)
73+
}
74+
75+
copied.add(original)
76+
return copyOnWrite(parent, key, original)
6277
}
6378

6479
const DEFAULT_RPC_JSON_SERIALIZER_HANDLERS: Record<string, RPCJsonSerializerHandler> = {
@@ -466,6 +481,7 @@ export class RPCJsonSerializer {
466481

467482
deserializeJson(serialized: RPCJsonSerialization): unknown {
468483
const ref = { json: serialized.json }
484+
const copied = new Set<unknown>()
469485

470486
if (serialized.blobs?.length) {
471487
for (let i = 0; i < serialized.maps.length; i++) {
@@ -483,7 +499,7 @@ export class RPCJsonSerializer {
483499

484500
for (let j = 0; j < segments.length; j++) {
485501
original = original[preSegment]
486-
currentRef = walkInto(currentRef, preSegment, original)
502+
currentRef = walkInto(currentRef, preSegment, original, copied)
487503
preSegment = segments[j]!
488504

489505
if (!Object.hasOwn(currentRef, preSegment)) {
@@ -539,7 +555,7 @@ export class RPCJsonSerializer {
539555

540556
while (d < depth) {
541557
original = original[preSegment]
542-
currentRef = walkInto(currentRef, preSegment, original, restored)
558+
currentRef = walkInto(currentRef, preSegment, original, copied, restored)
543559

544560
if (chain !== undefined) {
545561
chain[3 * d] = preSegment

0 commit comments

Comments
 (0)