Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Get an AST with require statements resolved into respective ASTs
NOTE: This module uses synchronous I/O and should not be used at request time in a service.
readFile : (
filename: String,
options?: {
includeExternalDependencies?: Boolean(true)
}
) => Object
Passing includeExternalDependencies
will cause the ASTs of 3rd party modules to also be parsed. Note that this
is a recursive algorithm, so the dependencies of each depdendency will also be parsed. As a warning, setting this flag
to true can produce extremely large ASTs.
var deepAST = require('deep-ast'); var AST = deepAST('./myModule.js');
Take the file structure where module 'a' requires module 'b' like so:
b.js:
module.exports = 'panic';
a.js:
var b = require('./b');
module.exports = 'dont';
The ASTs of these two modules (generated by esprima would be):
b.js:
{
"type": "Program",
"body": [{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "Identifier",
"name": "module"
},
"property": {
"type": "Identifier",
"name": "exports"
}
},
"right": {
"type": "Literal",
"value": "panic",
"raw": "'panic'"
}
}
}]
}
a.js:
{
"type": "Program",
"body": [{
"type": "VariableDeclaration",
"declarations": [{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "b"
},
"init": {
"type": "CallExpression",
"callee": {
"type": "Identifier",
"name": "require"
},
"arguments": [{
"type": "Literal",
"value": "./b",
"raw": "'./b'"
}]
}
}],
"kind": "var"
},
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "MemberExpression",
"computed": false,
"object": {
"type": "Identifier",
"name": "module"
},
"property": {
"type": "Identifier",
"name": "exports"
}
},
"right": {
"type": "Literal",
"value": "dont",
"raw": "'dont'"
}
}
}]
}
Running deep-ast
on a.js
will result in the require statement for 'b' being replaced with the syntax tree which is
generated from running 'b' through deep-ast
. Or:
deep-ast of a (b) === deep-ast of b
Like so:
{
"type": "Program",
"body": [{
"type": "VariableDeclaration",
"declarations": [{
"type": "VariableDeclarator",
"id": {"type": "Identifier", name: "b"},
"init": {
"type": "Program",
"body": [{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "MemberExpression",
"computed": false,
"object": {"type": "Identifier", "name": "module"},
"property": {"type": "Identifier", "name": "exports"}
},
"right": {"type": "Literal", "value": "panic", "raw": "panic"}
}
}]
}
}],
"kind": "var"
},
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "=",
"left": {
"type": "MemberExpression",
"computed": false,
"object": {"type": "Identifier", "name": "module"},
"property": {"type": "Identifier", "name": "exports"}
},
"right": {"type": "Literal", "value": "dont", "raw": "dont"}
}
}]
}
FAQs
Get an AST with require statements resolved into respective ASTs
The npm package deep-ast receives a total of 4 weekly downloads. As such, deep-ast popularity was classified as not popular.
We found that deep-ast 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.