@fntio/infra-client
Version:
Self-hosted AWS access portal: credential vending, target-account trust, and grant-driven private networking as CDK stacks and constructs.
30 lines (29 loc) • 1.01 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Utility to parse JSON safely
/**
* Check if a piece of JSON is a JSON object, and not e.g. a mere string or null
*
* @param j - the JSON
*/
export function isJsonObject(j) {
// It is not enough to check that `typeof j === "object"`
// because in JS `typeof null` is also "object", and so is `typeof []`.
// So we need to check that j is an object, and not null, and not an array
return typeof j === "object" && !Array.isArray(j) && j !== null;
}
/**
* Parse a string as JSON, while removing __proto__ and constructor, so JS prototype pollution is prevented
*
* @param s - the string to JSON parse
*/
export function safeJsonParse(s) {
return JSON.parse(s, (_, value) => {
if (typeof value === "object" && !Array.isArray(value) && value !== null) {
delete value.__proto__;
delete value.constructor;
}
return value;
});
}