-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: consider Function("return this") as safe (#211)
- Loading branch information
Showing
10 changed files
with
213 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Import Node.js Dependencies | ||
import { test } from "node:test"; | ||
import assert from "node:assert"; | ||
|
||
// Import Internal Dependencies | ||
import { runASTAnalysis } from "../../index.js"; | ||
|
||
/** | ||
* @see https://github.com/NodeSecure/js-x-ray/issues/180 | ||
*/ | ||
test("should detect required core 'http' with a LogicalExpr containing Function('return this')()", () => { | ||
const { warnings, dependencies } = runASTAnalysis(` | ||
var root = freeGlobal || freeSelf || Function('return this')(); | ||
const foo = root.require; | ||
foo("http"); | ||
`); | ||
|
||
assert.strictEqual(warnings.length, 0); | ||
assert.strictEqual(dependencies.size, 1); | ||
assert.ok(dependencies.has("http")); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
workspaces/estree-ast-utils/src/extractLogicalExpression.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
export function* extractLogicalExpression( | ||
node | ||
) { | ||
if (node.type !== "LogicalExpression") { | ||
return; | ||
} | ||
|
||
if (node.left.type === "LogicalExpression") { | ||
yield* extractLogicalExpression(node.left); | ||
} | ||
else { | ||
yield { operator: node.operator, node: node.left }; | ||
} | ||
|
||
if (node.right.type === "LogicalExpression") { | ||
yield* extractLogicalExpression(node.right); | ||
} | ||
else { | ||
yield { operator: node.operator, node: node.right }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
workspaces/estree-ast-utils/test/extractLogicalExpression.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Import Third-party Dependencies | ||
import test from "tape"; | ||
|
||
// Import Internal Dependencies | ||
import { extractLogicalExpression } from "../src/index.js"; | ||
import { codeToAst, getExpressionFromStatement } from "./utils.js"; | ||
|
||
test("it should extract two Nodes from a LogicalExpression with two operands", (tape) => { | ||
const [astNode] = codeToAst("5 || 10"); | ||
const iter = extractLogicalExpression( | ||
getExpressionFromStatement(astNode) | ||
); | ||
|
||
const iterResult = [...iter]; | ||
tape.strictEqual(iterResult.length, 2); | ||
|
||
tape.strictEqual(iterResult[0].operator, "||"); | ||
tape.strictEqual(iterResult[0].node.type, "Literal"); | ||
tape.strictEqual(iterResult[0].node.value, 5); | ||
|
||
tape.strictEqual(iterResult[1].operator, "||"); | ||
tape.strictEqual(iterResult[1].node.type, "Literal"); | ||
tape.strictEqual(iterResult[1].node.value, 10); | ||
|
||
tape.end(); | ||
}); | ||
|
||
test("it should extract all nodes and add up all Literal values", (tape) => { | ||
const [astNode] = codeToAst("5 || 10 || 15 || 20"); | ||
const iter = extractLogicalExpression( | ||
getExpressionFromStatement(astNode) | ||
); | ||
|
||
const total = [...iter] | ||
.reduce((previous, { node }) => previous + node.value, 0); | ||
tape.strictEqual(total, 50); | ||
|
||
tape.end(); | ||
}); | ||
|
||
test("it should extract all Nodes but with different operators and a LogicalExpr on the right", (tape) => { | ||
const [astNode] = codeToAst("5 || 10 && 55"); | ||
const iter = extractLogicalExpression( | ||
getExpressionFromStatement(astNode) | ||
); | ||
|
||
const operators = new Set( | ||
[...iter].map(({ operator }) => operator) | ||
); | ||
tape.deepEqual([...operators], ["||", "&&"]); | ||
|
||
tape.end(); | ||
}); |