Skip to content

Commit 8f31103

Browse files
committed
fix: 🐛 Security determines types not supported by the host
1 parent eac8529 commit 8f31103

3 files changed

Lines changed: 23 additions & 21 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-web3",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "BigInt-safe JSON serialization and deserialization for Web3 use cases.",
55
"keywords": [
66
"json",

src/utils.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ export const URL_TAG = '__@json.url__'
88
export const FUNCTION_TAG = '__@json.function__'
99
export const NUMBER_TAG = '__@json.number__'
1010

11-
export const undefined = void 0
12-
export const isUndefined = (value: any): value is undefined => typeof value === 'undefined'
11+
const UDF = 'undefined'
12+
export const undefined = void UDF
13+
1314
export const isString = (value: any): value is string => typeof value === 'string'
1415
export const hasOwnProperty = (obj: any, prop: string): boolean =>
1516
Object.prototype.hasOwnProperty.call(obj, prop)
@@ -22,28 +23,28 @@ export const isDate = (value: any): value is Date => value instanceof Date
2223
export const isMap = (value: any): value is Map<any, any> => value instanceof Map
2324
export const isSet = (value: any): value is Set<any> => value instanceof Set
2425
export const isRegExp = (value: any): value is RegExp => value instanceof RegExp
25-
export const isURL = (value: any): value is URL => !isUndefined(URL) && value instanceof URL
26+
export const isURL = (value: any): value is URL => typeof URL !== UDF && value instanceof URL
2627
export const isNonFiniteNumber = (value: any): value is number =>
2728
typeof value === 'number' && !Number.isFinite(value)
2829
export const isInteger = (value: any): value is number => Number.isInteger(value)
29-
const hasBuffer = (): boolean => typeof Buffer !== 'undefined'
30+
const hasBuffer = (): boolean => typeof Buffer !== UDF && isFunction(Buffer.isBuffer)
3031

3132
export const isBuffer = (value: any): value is Buffer =>
3233
hasBuffer() && isFunction(Buffer.isBuffer) && Buffer.isBuffer(value)
3334
export const isArrayBuffer = (value: any): value is ArrayBuffer =>
34-
!isUndefined(ArrayBuffer) && value instanceof ArrayBuffer
35+
typeof ArrayBuffer !== UDF && value instanceof ArrayBuffer
3536

3637
export const isTypedArray = (value: any): value is ArrayBufferView => {
37-
if (isUndefined(value)) return false
38-
if (!isUndefined(ArrayBuffer) && isFunction(ArrayBuffer.isView) && ArrayBuffer.isView(value)) {
38+
if (value == null) return false
39+
if (typeof ArrayBuffer !== UDF && isFunction(ArrayBuffer.isView) && ArrayBuffer.isView(value)) {
3940
return Object.prototype.toString.call(value) !== '[object DataView]'
4041
}
4142
const tag = Object.prototype.toString.call(value)
4243
return tag.endsWith('Array]') && tag !== '[object Array]'
4344
}
4445

4546
export const getTypedArrayName = (value: any): string | null => {
46-
if (isUndefined(value)) return null
47+
if (value == null) return null
4748
const ctor = (value as any).constructor
4849
if (ctor && isString(ctor.name)) return ctor.name
4950
const tag = Object.prototype.toString.call(value)
@@ -52,22 +53,22 @@ export const getTypedArrayName = (value: any): string | null => {
5253
}
5354

5455
const TYPEDARRAY_CTORS: Record<string, any> = {
55-
Int8Array: !isUndefined(Int8Array) ? Int8Array : undefined,
56-
Uint8Array: !isUndefined(Uint8Array) ? Uint8Array : undefined,
57-
Uint8ClampedArray: !isUndefined(Uint8ClampedArray) ? Uint8ClampedArray : undefined,
56+
Int8Array: typeof Int8Array !== UDF ? Int8Array : undefined,
57+
Uint8Array: typeof Uint8Array !== UDF ? Uint8Array : undefined,
58+
Uint8ClampedArray: typeof Uint8ClampedArray !== UDF ? Uint8ClampedArray : undefined,
5859

59-
Int16Array: !isUndefined(Int16Array) ? Int16Array : undefined,
60-
Uint16Array: !isUndefined(Uint16Array) ? Uint16Array : undefined,
61-
Float16Array: !isUndefined(Float16Array) ? Float16Array : undefined,
60+
Int16Array: typeof Int16Array !== UDF ? Int16Array : undefined,
61+
Uint16Array: typeof Uint16Array !== UDF ? Uint16Array : undefined,
62+
Float16Array: typeof Float16Array !== UDF ? Float16Array : undefined,
6263

63-
Int32Array: !isUndefined(Int32Array) ? Int32Array : undefined,
64-
Uint32Array: !isUndefined(Uint32Array) ? Uint32Array : undefined,
65-
Float32Array: !isUndefined(Float32Array) ? Float32Array : undefined,
64+
Int32Array: typeof Int32Array !== UDF ? Int32Array : undefined,
65+
Uint32Array: typeof Uint32Array !== UDF ? Uint32Array : undefined,
66+
Float32Array: typeof Float32Array !== UDF ? Float32Array : undefined,
6667

67-
Float64Array: !isUndefined(Float64Array) ? Float64Array : undefined,
68+
Float64Array: typeof Float64Array !== UDF ? Float64Array : undefined,
6869

69-
BigInt64Array: !isUndefined(BigInt64Array) ? BigInt64Array : undefined,
70-
BigUint64Array: !isUndefined(BigUint64Array) ? BigUint64Array : undefined,
70+
BigInt64Array: typeof BigInt64Array !== UDF ? BigInt64Array : undefined,
71+
BigUint64Array: typeof BigUint64Array !== UDF ? BigUint64Array : undefined,
7172
}
7273

7374
export const toHex = (value: Uint8Array): string => {

test/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const payload = {
44
decimals: 18n,
55
u8Array: new Uint8Array([1, 2, 3, 255]),
66
u16Array: new Uint16Array([1, 2, 3, 255]),
7+
Float16Array: new Float16Array([1.5, 2.5, 3.5, NaN]),
78
bigIntArray: new BigInt64Array([18446744073709551615n, 2n, 3n, 255n]),
89
createdAt: new Date('2020-01-02T03:04:05.006Z'),
910
ids: new Set([123, 456]),

0 commit comments

Comments
 (0)