
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
json-placeholder-replacer
Advanced tools
Javascript/Typescript library/cli to replace placeholders in an javascript object
Lightweight yet really powerful typescript library/cli to replace placeholders in an javascript object/JSON. By default, all you have to do is to use double curly brackets {{placeholderKey}} or angle brackets <<placeholderKey>>, interchangeably, to identify the placeholder. Don't worry, if you don't like these default placeholders you can create your own.
json-placeholder-replacer annotetad-json.json [...variableMaps]
$ json-placeholder-replacer annotated.json variable_map.json $ jpr variable_map.json < annotated.json $ cat annotated.json | jpr variable_map.json $ echo '{"curly": "{{key}}", "angle": "<>"}' | jpr variable_maps
cat replaceable.json
# {
# "curly": "{{key}}",
# "angle": "<<key>>"
# }
cat variable.map:
# {
# "key": 10,
# "not-mapped": 20
# }
json-placeholder-replacer replaceable.json variable.map
# {
# "curly": 10,
# "angle": 10,
# "not-mapped": 20
# }
import { JsonPlaceholderReplacer } from "json-placeholder-replacer";
const placeHolderReplacer = new JsonPlaceholderReplacer();
placeHolderReplacer.addVariableMap({
key: 100,
otherKey: 200,
});
const afterReplace = placeHolderReplacer.replace({
replaceable: "{{key}}",
otherReplaceableWithSameKey: "<<key>>",
otherReplaceable: "{{otherKey}}",
});
// afterReplace = {
// replaceable: 100,
// otherReplaceableWithSameKey: 100,
// otherReplaceable: 200
// }
[!NOTE] An object passed to
.replace()
is mutated in-place:
const beforeReplace = { some: "{{placeholder}}" }; const afterReplace = placeHolderReplacer.replace(beforeReplace); // beforeReplace === afterReplace
const placeHolderReplacer = new JsonPlaceholderReplacer({
delimiterTags: [{ begin: "@{{-", end: "-}}@" }],
});
placeHolderReplacer.addVariableMap({
key: "nice",
});
const afterReplace = placeHolderReplacer.replace({
replaceable: "@{{-key-}}@",
});
// afterReplace = {
// replaceable: "nice",
// }
placeHolderReplacer.addVariableMap({
firstMapKey: "1",
});
placeHolderReplacer.addVariableMap({
secondMapKey: 2,
});
const afterReplace = placeHolderReplacer.replace({
replaceable: "{{firstMapKey}}",
otherReplaceable: "<<secondMapKey>>",
});
// afterReplace = {
// replaceable: "1",
// otherReplaceable: 2
// }
placeHolderReplacer.addVariableMap({
id: "lowerPriority",
name: "Name",
});
placeHolderReplacer.addVariableMap({
id: "higherPriority",
name: undefined,
});
const afterReplace = placeHolderReplacer.replace({
id: "{{id}}",
name: "{{name}}",
});
// afterReplace = {
// id: "higherPriority"
// name: "Name"
// }
.setVariableMap()
placeHolderReplacer.addVariableMap({
id: "Id",
name: "Name",
});
placeHolderReplacer.setVariableMap({
// <- note setVariableMap() here
id: "New Id",
name: undefined,
});
const afterReplace = placeHolderReplacer.replace({
id: "{{id}}",
name: "{{name}}",
});
// afterReplace = {
// id: "New Id"
// name: "{{name}}"
// }
.replaceWith()
placeHolderReplacer.addVariableMap({
id: "Id",
name: "Name",
});
const afterReplace = placeHolderReplacer.replaceWith(
{
id: "{{id}}",
name: "{{name}}",
},
{ name: "New Name" },
);
// afterReplace = {
// id: "{{id}}"
// name: "New Name"
// }
If a variable in the map is boolean/string/number/object, it remains as boolean/string/number/object when it's replaced
placeHolderReplacer.addVariableMap({
booleanKey: true,
stringKey: "string",
numberKey: 10,
objectKey: {
inner: "inner",
},
});
const afterReplace = placeHolderReplacer.replace({
booleanReplaceable: "{{booleanKey}}",
stringReplaceable: "{{stringKey}}",
numberReplaceable: "{{numberKey}}",
objectReplaceable: "{{objectKey}}",
});
// afterReplace = {
// booleanReplaceable: true,
// stringReplaceable: "string",
// numberReplaceable: 10,
// objectReplaceable: {
// inner: "inner"
// }
// }
placeHolderReplacer.addVariableMap({
key: "someValue",
});
const afterReplace = placeHolderReplacer.replace({
"{{key}}": "value",
});
// afterReplace = {
// "{{key}}": "value"
// }
placeHolderReplacer.addVariableMap({
key: 987,
objectReplaceable: {
inner: "inner",
},
});
const afterReplace = placeHolderReplacer.replace({
array: ["string", "{{objectReplaceable}}", "{{key}}"],
});
// afterReplace = {
// array: ["string", { inner: "inner" }, 987]
// }
placeHolderReplacer.addVariableMap({
key: {
nested: "value",
},
});
const afterReplace: any = placeHolderReplacer.replace({
replaceable: "<<key.nested>>",
});
// afterReplace = {
// replaceable: "value"
// }
placeHolderReplacer.addVariableMap({
key: "value",
});
const afterReplace: any = placeHolderReplacer.replace({
replaceable: "<<not-found-key:default-value>>",
});
// afterReplace = {
// replaceable: "default-value"
// }
const placeHolderReplacer = new JsonPlaceholderReplacer({
defaultValueSeparator: ":=:",
});
placeHolderReplacer.addVariableMap({
key: "value",
});
const afterReplace: any = placeHolderReplacer.replace({
replaceable: "<<not-found-key:=:default-value>>", // Note the ':=:'
});
// afterReplace = {
// replaceable: "default-value"
// }
const placeHolderReplacer = new JsonPlaceholderReplacer();
const cyclicObject: any = {
key: "{{key1}}",
deep: {
nested: "{{key2}}",
},
};
cyclicObject.deep.circular = cyclicObject;
placeHolderReplacer.addVariableMap({ key1: "value1", key2: "value2" });
const afterReplace: any = placeHolderReplacer.replace(cyclicObject);
// afterReplace = {
// key: "value1",
// deep: {
// nested: "value2",
// circular: {
// key: "value1",
// deep: {
// nested: "value2",
// circular: [CYCLE...]
// }
// }
// }
// }
FAQs
Javascript/Typescript library/cli to replace placeholders in an javascript object
The npm package json-placeholder-replacer receives a total of 634 weekly downloads. As such, json-placeholder-replacer popularity was classified as not popular.
We found that json-placeholder-replacer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.