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.
Low footprint, fast source code parser, which allows you to find all code fragment occurrences with respect to all syntax rules that cannot be handled with plain regular expression search.
It aims at use cases where we don't need full AST tree, but instead we're interested in finding usages of given function, property etc. in syntactically valid code.
$ npm install esniff
Using main module you can configure sophisticated parser on your own. However, first, see preprared API utilities that may already address use cases you have.
code
- Code to parseexecutor
- A function to be executed immediately by the constructor, It receives an emitter
parameter.emitter
emits following events:
trigger:<char>
- When char is a code character approached in code, that is not a whitespaces, is not in a middle of identificator, is not part of a comment, string, template string or regular expression.Emitter passes to listener and accessor
object, which provides access to current parser state and allows to manipulate parsing process. accessor
exposes following methods:
skipCodePart(codePart)
- Skips forward through input codePart assuming parser index points start of given part. Returns true if given codePart
was found and index and skippedskipIdentifier
- Skips approached identifier (can be function name or property name), returns { name, start, end }
meta objectskipWhitespace
- Skips any whitespace and comments founds at current parsing indexcollectScope
- If at current index (
character is found, it registers given paranthesis scope for registrations (it's content will be returned as one of the results after finished parsing)stop
- Stops parsing processindex
- Returns currently parsed indexpreviousToken
- Previous non-whitespace characterscopeDepth
- Current scope depthshouldCollectComments
- Whether data about code comments should be collected in the resultParse all require(..)
calls:
var esniff = require("esniff");
var parseRequires = function (code) {
return esniff(code, function (emitter) {
emitter.on("trigger:r", function (accessor) {
if (accessor.previousToken === ".") return;
if (!accessor.skipCodePart("require")) return;
accessor.skipWhitespace();
accessor.collectScope();
});
});
};
console.log(parseRequires("var x = require('foo/bar')"));
[{ type: "scope", point: 17, column: 17, line: 1, raw: "'foo/bar'" }];
Returns function which allows us to find all accessed property names on given object name
var findProperties = require("esniff/accessed-properties");
var findContextProperties = findProperties("this");
var result = findContextProperties(
"var foo = \"0\"; this.bar = foo; this.someMethod(); otherFunction()"
);
console.log(result); // [ { name: 'bar', start: 20, end: 23 }, { name: 'someMethod', start: 36, end: 46 } ]
Returns function which allows us to find all occurrences of given function (or method) being invoked
Through options we can restrict cases which we're after:
asProperty
(default: false
), on true will allow x.name()
when we search for name
callsasPlain
(default: true
), on true it allows plain calls e.g. name()
when we search for name
. Should be set to false
if we're strictly about method calls.Setting both asProperty
and asPlain
to false, will always produce empty result
var findRequires = require("esniff/function")("require");
findRequires("var x = require('foo/bar')");
// [{ point: 17, column: 17, line: 1, raw: '\'foo/bar\'' }]
Resolves expressions separated with commas, with additional limit
you can specify after which number of arguments resolver should stop
var resolveArgs = require("esniff/resolve-arguments");
var result = resolveArgs("'raz', 'dwa', ['raz', 'dwa'], 'trzy'", 3);
console.log(result); // ['"raz"', ' "dwa"', ' [\'raz\', \'dwa\']']
x = { foo: 'bar' } / 14
, esniff in that case will assume that /
starts regular expression). Still there's not known use case where such code may make any sense, and many popular JS source code parsers share very same vulnerability.$ npm test
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
FAQs
Low footprint ECMAScript source code parser
The npm package esniff receives a total of 4,940,905 weekly downloads. As such, esniff popularity was classified as popular.
We found that esniff 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.
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.