Skip to content

Commit 03bebe1

Browse files
committed
fix(search): reject inherited JSON serializers
1 parent 07b155c commit 03bebe1

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎apps/sim/lib/core/utils/bounded-json.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,20 @@ describe('bounded JSON', () => {
103103
expect(isJsonWithinByteLimit(value, bytes)).toBe(true)
104104
expect(isJsonWithinByteLimit(value, bytes - 1)).toBe(false)
105105
})
106+
107+
it.each(['method', 'accessor'] as const)(
108+
'rejects an inherited toJSON %s without invoking it or ignoring an own shadow',
109+
(kind) => {
110+
const serialize = vi.fn(() => 'x'.repeat(2048))
111+
const prototype = Object.create(Array.prototype, {
112+
toJSON: kind === 'method' ? { value: serialize } : { get: serialize },
113+
})
114+
const value = Object.setPrototypeOf([], Object.create(prototype))
115+
expect(isJsonWithinByteLimit(value, 1024)).toBe(false)
116+
expect(serialize).not.toHaveBeenCalled()
117+
Object.defineProperty(value, 'toJSON', { value: null })
118+
expect(isJsonWithinByteLimit(value, 2)).toBe(true)
119+
expect(serialize).not.toHaveBeenCalled()
120+
}
121+
)
106122
})

‎apps/sim/lib/core/utils/bounded-json.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@ export function isJsonWithinByteLimit(value: unknown, maxBytes: number): boolean
7676
const prototype = Object.getPrototypeOf(item)
7777
if (!Array.isArray(item) && prototype !== Object.prototype && prototype !== null)
7878
return false
79-
const serializer = Object.getOwnPropertyDescriptor(item, 'toJSON')
80-
if (serializer && (!('value' in serializer) || typeof serializer.value === 'function'))
81-
return false
79+
for (let owner: object | null = item; owner; owner = Object.getPrototypeOf(owner)) {
80+
const serializer = Object.getOwnPropertyDescriptor(owner, 'toJSON')
81+
if (!serializer) continue
82+
if (!('value' in serializer) || typeof serializer.value === 'function') return false
83+
break
84+
}
8285
bytes += 2
8386
ancestors.add(item)
8487
stack.push({ value: item, entries: entries(item), count: 0 })

0 commit comments

Comments
 (0)