fix: harden Protobuf Deserialization and Refactor Utilities (#1820)
* refactor: reorganize browser utility functions and improve buffer handling * fix: harden protobuf deserialization and clean up code * refactor: simplify data handling in GroupCipher and SenderKey classes * fix: Ensure consistent Buffer hydration and remove redundant code * refactor: update decodeAndHydrate calls to use proto types for improved type safety
This commit is contained in:
committed by
GitHub
parent
202c39f3fe
commit
3b46d43beb
@@ -0,0 +1,70 @@
|
||||
import { BufferJSON } from '../../Utils/generics'
|
||||
|
||||
describe('BufferJSON', () => {
|
||||
const originalObject = {
|
||||
id: 1,
|
||||
key: Buffer.from([1, 2, 3, 4, 5]),
|
||||
nested: {
|
||||
data: Buffer.from([6, 7, 8])
|
||||
}
|
||||
}
|
||||
|
||||
const legacyJsonString = '{"id":1,"key":{"0":1,"1":2,"2":3,"3":4,"4":5},"nested":{"data":{"0":6,"1":7,"2":8}}}'
|
||||
|
||||
it('should correctly perform a round trip (stringify with replacer, parse with reviver)', () => {
|
||||
const serialized = JSON.stringify(originalObject, BufferJSON.replacer)
|
||||
|
||||
expect(serialized).toContain('"type":"Buffer"')
|
||||
expect(serialized).not.toContain('"data":[1,2,3,4,5]')
|
||||
|
||||
const revived = JSON.parse(serialized, BufferJSON.reviver)
|
||||
|
||||
expect(Buffer.isBuffer(revived.key)).toBe(true)
|
||||
expect(Buffer.isBuffer(revived.nested.data)).toBe(true)
|
||||
expect(revived.key).toEqual(originalObject.key)
|
||||
expect(revived).toEqual(originalObject)
|
||||
})
|
||||
|
||||
it('should correctly revive a legacy JSON string with object-like buffers', () => {
|
||||
const revived = JSON.parse(legacyJsonString, BufferJSON.reviver)
|
||||
|
||||
expect(Buffer.isBuffer(revived.key)).toBe(true)
|
||||
expect(Buffer.isBuffer(revived.nested.data)).toBe(true)
|
||||
expect(revived.key).toEqual(originalObject.key)
|
||||
expect(revived.nested.data).toEqual(originalObject.nested.data)
|
||||
expect(revived.id).toEqual(originalObject.id)
|
||||
})
|
||||
|
||||
it('should not corrupt legitimate objects that are not buffers', () => {
|
||||
const legitimateProtoObject = {
|
||||
'0': 'some-value',
|
||||
'1': 'another-value'
|
||||
}
|
||||
const jsonString = JSON.stringify(legitimateProtoObject)
|
||||
const revived = JSON.parse(jsonString, BufferJSON.reviver)
|
||||
|
||||
expect(Buffer.isBuffer(revived)).toBe(false)
|
||||
expect(revived).toEqual(legitimateProtoObject)
|
||||
})
|
||||
|
||||
it('should not convert other objects or null values', () => {
|
||||
const otherObject = {
|
||||
a: 1,
|
||||
b: 2,
|
||||
c: null,
|
||||
d: { '0': 1, foo: 'bar' }
|
||||
}
|
||||
const jsonString = JSON.stringify(otherObject)
|
||||
const revived = JSON.parse(jsonString, BufferJSON.reviver)
|
||||
|
||||
expect(Buffer.isBuffer(revived.a)).toBe(false)
|
||||
expect(revived.c).toBeNull()
|
||||
expect(Buffer.isBuffer(revived.d)).toBe(false)
|
||||
expect(revived).toEqual(otherObject)
|
||||
})
|
||||
|
||||
it('should correctly handle an empty object', () => {
|
||||
const revived = JSON.parse('{}', BufferJSON.reviver)
|
||||
expect(revived).toEqual({})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user