Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
object-rewrite
Advanced tools
Rewrite Object(s) in place using plugins.
This library is used for doing complex in-memory modifications of data. It allows to define use cases in a dynamic way that allows for powerful abstraction.
npm i --save object-rewrite
const {
injectPlugin,
filterPlugin,
sortPlugin,
rewriter
} = require('object-rewrite');
const queryDataStore = (fields) => { /* ... */ };
const inject = injectPlugin({
target: 'idNeg',
requires: ['id'],
fn: ({ value }) => -value.id
});
const filter = filterPlugin({
target: '*',
requires: ['idNeg'],
fn: ({ value }) => [-2, -1].includes(value.idNeg)
});
const sort = sortPlugin({
target: '*',
requires: ['idNeg'],
fn: ({ value }) => value.idNeg
});
const rew = rewriter({ '': [inject, filter, sort] }, ['id']);
const desiredFields = ['id'];
const rewInstance = rew.init(desiredFields);
const data = queryDataStore(rewInstance.fieldsToRequest);
// data => [{ id: 0 }, { id: 1 }, { id: 2 }]
rewInstance.rewrite(data);
// data => [{ id: 2 }, { id: 1 }]
Please see the tests for more in-depth examples on how to use this library.
There are three types of plugins INJECT
, FILTER
and SORT
.
All plugins require:
target
String: target field relative to the plugin path.required
Array: required fields relative to the plugin path. Will influence fieldsToRequest
.fn
Function: result of this function is used by the plugin. Signature is fn({ key, value, parents, context })
.Used to inject data
target
: field that is created or overwrittenrequires
: See abovefn
: return value is used for target. Relative to prefixUsed to filter arrays
target
: array that should be filteredrequired
: See abovefn
: target is removed iff function returns false
. Similar to
Array.filter(). Relative to targetUsed to sort arrays
target
: array that should be sortedrequired
: See abovefn
: called for each object in array. Final array is sorted using the result. Relative to targetOnly one sort plugin can be specified per target.
Allows for complex sort comparisons and uses sort-fn.js
under the hood (see source code).
rewriter(pluginMap: Object, dataStoreFields: Array)
Used to combine multiple plugins. Plugins can be re-used in different rewriters. Rewriters are then used to modify input data.
Constructor takes in an object that maps absolute paths to plugins and the available dataStoreFields
.
Could for example re-use a plugin as
const { injectPlugin, rewriter } = require('object-rewrite');
const plugin = injectPlugin(/* ... */);
rewriter({
'': [plugin],
nodes: [plugin]
}, [/* data store fields */]);
allowedFields: Array
Fields that are allowed to be requested.
init(fields: Array)
Initialize the rewriter for a specific set of fields.
fieldsToRequest
Exposes fields which should be requested from data store. Dynamically computed fields are excluded since they would not be present in the data store.
rewrite(data: Object/Array, context: Object = {})
Pass in object that should be rewritten. The context allows for additional data to be made available for all plugins.
Under the hood this library uses object-scan. Please refer to the docs for what key pattern are supported.
Plugins are executed in the order INJECT
, FILTER
and then SORT
.
Plugins within the same type are evaluated bottom-up. While this is less performant, it allows plugins to rely on previous executed plugins of the same type.
Plugins of the same type that operate on the same target are executed in order.
FAQs
Rewrite Object(s) in place using plugins.
We found that object-rewrite demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.