-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.ts
100 lines (89 loc) · 2.52 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// https://github.com/fastify/secure-json-parse
// https://github.com/hapijs/bourne
const suspectProtoRx =
/"(?:_|\\u0{2}5[Ff]){2}(?:p|\\u0{2}70)(?:r|\\u0{2}72)(?:o|\\u0{2}6[Ff])(?:t|\\u0{2}74)(?:o|\\u0{2}6[Ff])(?:_|\\u0{2}5[Ff]){2}"\s*:/;
const suspectConstructorRx =
/"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
const JsonSigRx = /^\s*["[{]|^\s*-?\d{1,16}(\.\d{1,17})?([Ee][+-]?\d+)?\s*$/;
function jsonParseTransform(key: string, value: any): any {
if (
key === "__proto__" ||
(key === "constructor" &&
value &&
typeof value === "object" &&
"prototype" in value)
) {
warnKeyDropped(key);
return;
}
return value;
}
function warnKeyDropped(key: string): void {
console.warn(`[destr] Dropping "${key}" key to prevent prototype pollution.`);
}
export type Options = {
strict?: boolean;
};
export function destr<T = unknown>(value: any, options: Options = {}): T {
if (typeof value !== "string") {
return value;
}
const _value = value.trim();
if (
// eslint-disable-next-line unicorn/prefer-at
value[0] === '"' &&
value.at(-1) === '"' &&
!value.includes("\\")
) {
return _value.slice(1, -1) as T;
}
if (_value.length <= 9) {
const _lval = _value.toLowerCase();
if (_lval === "true") {
return true as T;
}
if (_lval === "false") {
return false as T;
}
if (_lval === "undefined") {
return undefined as T;
}
if (_lval === "null") {
// eslint-disable-next-line unicorn/no-null
return null as T;
}
if (_lval === "nan") {
return Number.NaN as T;
}
if (_lval === "infinity") {
return Number.POSITIVE_INFINITY as T;
}
if (_lval === "-infinity") {
return Number.NEGATIVE_INFINITY as T;
}
}
if (!JsonSigRx.test(value)) {
if (options.strict) {
throw new SyntaxError("[destr] Invalid JSON");
}
return value as T;
}
try {
if (suspectProtoRx.test(value) || suspectConstructorRx.test(value)) {
if (options.strict) {
throw new Error("[destr] Possible prototype pollution");
}
return JSON.parse(value, jsonParseTransform);
}
return JSON.parse(value);
} catch (error) {
if (options.strict) {
throw error;
}
return value as T;
}
}
export function safeDestr<T = unknown>(value: any, options: Options = {}): T {
return destr<T>(value, { ...options, strict: true });
}
export default destr;