MCPcopy Create free account
hub / github.com/loopbackio/loopback-next / sanitizeJsonParse

Function sanitizeJsonParse

packages/rest/src/parse-json.ts:24–52  ·  view source on GitHub ↗
(
  reviver?: (key: any, value: any) => any,
  prohibitedKeys?: string[],
)

Source from the content-addressed store, hash-verified

22 * @param prohibitedKeys - An array of keys to be rejected
23 */
24export function sanitizeJsonParse(
25 reviver?: (key: any, value: any) => any,
26 prohibitedKeys?: string[],
27) {
28 return (key: string, value: any) => {
29 if (key === '__proto__') {
30 // Reject `__proto__`
31 throw new Error(`JSON string cannot contain "${key}" key.`);
32 }
33 if (
34 key === 'constructor' &&
35 value != null &&
36 Object.keys(value).some(k => isMatched(k, 'prototype'))
37 ) {
38 // Reject `constructor/prototype.*`
39 throw new Error(
40 `JSON string cannot contain "constructor.prototype" key.`,
41 );
42 }
43 if (prohibitedKeys?.some(pattern => isMatched(key, pattern))) {
44 throw new Error(`JSON string cannot contain "${key}" key.`);
45 }
46 if (reviver) {
47 return reviver(key, value);
48 } else {
49 return value;
50 }
51 };
52}
53
54/**
55 * Parse a json string that rejects prohibited keys

Callers 4

parseJsonIfNeededFunction · 0.90
constructorMethod · 0.90
parse-json.unit.tsFile · 0.90
parseJsonFunction · 0.85

Calls 2

isMatchedFunction · 0.85
keysMethod · 0.65

Tested by

no test coverage detected