Skip to content

Commit

Permalink
refactor: split utils (#209)
Browse files Browse the repository at this point in the history
* refactor: split utils to multiple files #208

* Resolve test errors
  • Loading branch information
mkarkkainen authored Jan 17, 2024
1 parent f04cfb1 commit 2f7ffaa
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 121 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import isMinified from "is-minified-code";
import { SourceFile } from "./src/SourceFile.js";
import { SourceParser } from "./src/SourceParser.js";
import { warnings } from "./src/warnings.js";
import { isOneLineExpressionExport } from "./src/utils.js";
import { isOneLineExpressionExport } from "./src/utils/index.js";

export function runASTAnalysis(
str,
Expand Down
2 changes: 1 addition & 1 deletion src/SourceFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Utils, Literal } from "@nodesecure/sec-literal";
import { VariableTracer } from "@nodesecure/estree-ast-utils";

// Import Internal Dependencies
import { rootLocation, toArrayLocation } from "./utils.js";
import { rootLocation, toArrayLocation } from "./utils/index.js";
import { generateWarning } from "./warnings.js";
import { isObfuscatedCode, hasTrojanSource } from "./obfuscators/index.js";
import { ProbeRunner } from "./ProbeRunner.js";
Expand Down
2 changes: 1 addition & 1 deletion src/obfuscators/jjencode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Require Internal Dependencies
import { notNullOrUndefined } from "../utils.js";
import { notNullOrUndefined } from "../utils/index.js";

// CONSTANTS
const kJJRegularSymbols = new Set(["$", "_"]);
Expand Down
2 changes: 1 addition & 1 deletion src/probes/isArrayExpression.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Internal Dependencies
import { extractNode } from "../utils.js";
import { extractNode } from "../utils/index.js";

// CONSTANTS
const kLiteralExtractor = extractNode("Literal");
Expand Down
2 changes: 1 addition & 1 deletion src/probes/isClassDeclaration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Internal Dependencies
import { extractNode } from "../utils.js";
import { extractNode } from "../utils/index.js";

// CONSTANTS
const kIdExtractor = extractNode("Identifier");
Expand Down
2 changes: 1 addition & 1 deletion src/probes/isFunction.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Internal Dependencies
import { extractNode } from "../utils.js";
import { extractNode } from "../utils/index.js";

// CONSTANTS
const kIdExtractor = extractNode("Identifier");
Expand Down
2 changes: 1 addition & 1 deletion src/probes/isMethodDefinition.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Internal Dependencies
import { extractNode } from "../utils.js";
import { extractNode } from "../utils/index.js";

// CONSTANTS
const kIdExtractor = extractNode("Identifier");
Expand Down
2 changes: 1 addition & 1 deletion src/probes/isUnsafeCallee.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Internal Dependencies
import { isUnsafeCallee } from "../utils.js";
import { isUnsafeCallee } from "../utils/index.js";
import { ProbeSignals } from "../ProbeRunner.js";

/**
Expand Down
111 changes: 0 additions & 111 deletions src/utils.js

This file was deleted.

40 changes: 40 additions & 0 deletions src/utils/exportAssignmentHasRequireLeave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Import Third-party Dependencies
import {
getCallExpressionIdentifier
} from "@nodesecure/estree-ast-utils";

export function exportAssignmentHasRequireLeave(expr) {
if (expr.type === "LogicalExpression") {
return atLeastOneBranchHasRequireLeave(expr.left, expr.right);
}

if (expr.type === "ConditionalExpression") {
return atLeastOneBranchHasRequireLeave(expr.consequent, expr.alternate);
}

if (expr.type === "CallExpression") {
return getCallExpressionIdentifier(expr) === "require";
}

if (expr.type === "MemberExpression") {
let rootMember = expr.object;
while (rootMember.type === "MemberExpression") {
rootMember = rootMember.object;
}

if (rootMember.type !== "CallExpression") {
return false;
}

return getCallExpressionIdentifier(rootMember) === "require";
}

return false;
}

function atLeastOneBranchHasRequireLeave(left, right) {
return [
exportAssignmentHasRequireLeave(left),
exportAssignmentHasRequireLeave(right)
].some((hasRequire) => hasRequire);
}
14 changes: 14 additions & 0 deletions src/utils/extractNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Import Internal Dependencies
import { notNullOrUndefined } from "./notNullOrUndefined.js";

export function extractNode(expectedType) {
return (callback, nodes) => {
const finalNodes = Array.isArray(nodes) ? nodes : [nodes];

for (const node of finalNodes) {
if (notNullOrUndefined(node) && node.type === expectedType) {
callback(node);
}
}
};
}
7 changes: 7 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from "./exportAssignmentHasRequireLeave.js";
export * from "./extractNode.js";
export * from "./isOneLineExpressionExport.js";
export * from "./isUnsafeCallee.js";
export * from "./notNullOrUndefined.js";
export * from "./rootLocation.js";
export * from "./toArrayLocation.js";
18 changes: 18 additions & 0 deletions src/utils/isOneLineExpressionExport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Import Internal Dependencies
import { exportAssignmentHasRequireLeave } from "./exportAssignmentHasRequireLeave.js";

export function isOneLineExpressionExport(body) {
if (body.length > 1) {
return false;
}

if (body[0].type !== "ExpressionStatement") {
return false;
}

if (body[0].expression.type !== "AssignmentExpression") {
return false;
}

return exportAssignmentHasRequireLeave(body[0].expression.right);
}
28 changes: 28 additions & 0 deletions src/utils/isUnsafeCallee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Import Third-party Dependencies
import { getCallExpressionIdentifier } from "@nodesecure/estree-ast-utils";

function isEvalCallee(node) {
const identifier = getCallExpressionIdentifier(node, {
resolveCallExpression: false
});

return identifier === "eval";
}

function isFunctionCallee(node) {
const identifier = getCallExpressionIdentifier(node);

return identifier === "Function" && node.callee.type === "CallExpression";
}

export function isUnsafeCallee(node) {
if (isEvalCallee(node)) {
return [true, "eval"];
}

if (isFunctionCallee(node)) {
return [true, "Function"];
}

return [false, null];
}
3 changes: 3 additions & 0 deletions src/utils/notNullOrUndefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function notNullOrUndefined(value) {
return value !== null && value !== void 0;
}
3 changes: 3 additions & 0 deletions src/utils/rootLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function rootLocation() {
return { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } };
}
11 changes: 11 additions & 0 deletions src/utils/toArrayLocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Import Internal Dependencies
import { rootLocation } from "./rootLocation.js";

export function toArrayLocation(location = rootLocation()) {
const { start, end = start } = location;

return [
[start.line || 0, start.column || 0],
[end.line || 0, end.column || 0]
];
}
2 changes: 1 addition & 1 deletion src/warnings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Internal Dependencies
import * as utils from "./utils.js";
import * as utils from "./utils/index.js";

export const warnings = Object.freeze({
"parsing-error": {
Expand Down
2 changes: 1 addition & 1 deletion test/warnings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import { rootLocation } from "../src/utils.js";
import { rootLocation } from "../src/utils/index.js";
import { generateWarning } from "../src/warnings.js";

test("Given an encoded-literal kind it should generate a warning with deep location array", () => {
Expand Down

0 comments on commit 2f7ffaa

Please sign in to comment.