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.
Environment agnostic (Node.js) CJS modules resolver.
It implements a strict version of Node.js modules resolution logic, differences are as follows:
/
) are supported in require's path arguments (Background: even though Node.js internally seems to follow Windows path separator in Windows environment, it won't work in *nix environments, and even in Window env it's not reliable so by all means should be avoided)resolve(dir, 'fs')
will naturally result with null$ npm install ncjsm
For provided configuration, returns a CJS modules resolver:
['.js', '.json', '.node']
confirmFile(filepath)
function. Confirms whether there's a module at provided (not normalized, absolute) file path. Returns promise-like object which resolves with either normalized full path of a module or null (if there's no module for given path).resolvePackageMain(dirpath)
function. Returns value of package.json's main
property for given path. Returns promise-like object which resolves with either resolved value, or null, when either package.json
file was not found, or it didn't have main property.confirmFile
resolution can be synchronous.Node.js resolver
Asynchronously resolves module path against provided directory path. Returns promise. If module is found, then promise resolves with an object, containing two properties:
targetPath
- A path at which module was resolvedrealPath
- Real path of resolved module (if targetPath involves symlinks then realPath will be different)If no matching module was found, promise is rejected with MODULE_NOT_FOUND
error (unless silent: true
is passed with options (passed as third argument), then it resolves with null
)
const resolve = require("ncjsm/resolve");
// Asynchronously resolve path for 'foo' module against current path
resolve(__dirname, "foo").then(
function (pathData) {
// 'foo' module found at fooModulePath
},
function (error) {
if (error.code === "MODULE_NOT_FOUND") {
// 'foo' module doesn't exist
}
}
);
// `silent` option, prevents module not found rejections:
resolve(__dirname, "foo", { silent: true }).then(function (pathData) {
if (pathData) {
// 'foo' module found at fooModulePath
} else {
// 'foo' module doesn't exist
}
});
Node.js resolver
Synchronously resolves module path against provided directory path. Otherwise works same as resolve
const resolveSync = require("ncjsm/resolve/sync");
// Synchronously resolve path for 'foo' module against current path
let fooModulePathData;
try {
fooModulePathData = resolveSync(__dirname, "foo");
// 'foo' module found at fooModulePath
} catch (error) {
if (error.code === "MODULE_NOT_FOUND") {
// 'foo' module doesn't exist
}
}
fooModulePathData = resolveSync(__dirname, "foo", { silent: true });
if (fooModulePathData) {
// 'foo' module found
} else {
// 'foo' module doesn't exist
}
Create temporary environment where require
of specific modules will not resolved the eventually cached verions
var requireUncached = require("ncjsm/require-uncached");
const firstCopyOfModule1 = require("./module1");
var secondCopyOfModule2 = requireUnached([require.resolve("./module1")], function () {
return require("./module1");
});
console.log(firstCopyOfModule1 === secondCopyOfModule2); // false
Alternatively we may resolve callback in completely cleared require cache, for that moduleIds
argument should be skipped
var requireUncached = require("ncjsm/require-uncached");
const firstCopyOfModule1 = require("./module1");
var secondCopyOfModule2 = requireUnached(function () { return require("./module1"); });
console.log(firstCopyOfModule1 === secondCopyOfModule2); // false
Whether provided path is a root of a package
var isPackageRoot = require("ncjsm/is-package-root");
isPackageRoot(dirPath).done(function (isRoot) {
if (isRoot) {
// Provided path is package root
}
});
Resolve package root path for provided path. It is about resolution of first upper package root
var resolvePackageRoot = require("ncjsm/resolve-package-root");
resolvePackageRoot(dirPath).done(function (root) {
if (!root) {
// Provided path is not located in any package
}
});
Resolve project root path for provided path. It is about resolution of topmost package root for given path
var resolveProjectRoot = require("ncjsm/resolve-project-root");
resolveProjectRoot(dirPath).done(function (root) {
if (!root) {
// Provided path is not located in any project
}
});
Resolve all module dependencies. Returns promise that resolves with an array of paths, that includes path to input module and paths to all its dependencies (it includes deep dependencies, so also dependencies of the dependencies).
Paths to native Node.js modules are ignored.
var getDependencies = require("ncjsm/get-dependencies");
getDependencies(modulePath).done(function (deps) {
console.log(deps); // e.g. [pathToModulePath, pathToDep1, pathToDep2, ...pathToDepn]
});
options
ignoreMissing: false
If file for given module cannot be found then error is thrown. Set this to true
to simply ignore not found modules
ignoreExternal: false
By default all paths to all required modules are resolved. Resolution scope may be narrowed only to modules from same package (referenced via relative path), by settung this option to true
$ npm test
FAQs
CJS (Node.js) style modules resolver
The npm package ncjsm receives a total of 683,735 weekly downloads. As such, ncjsm popularity was classified as popular.
We found that ncjsm 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.