Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { warnings } from "./src/warnings.js";
const kMeriyahDefaultOptions = {
next: true,
loc: true,
raw: true
raw: true,
jsx: true
};

export function runASTAnalysis(str, options = Object.create(null)) {
Expand Down Expand Up @@ -97,7 +98,10 @@ function parseScriptExtended(strToAnalyze, isEcmaScriptModule) {
return body;
}
catch (error) {
if (error.name === "SyntaxError" && error.description.includes("The import keyword")) {
if (error.name === "SyntaxError" && (
error.description.includes("The import keyword") ||
error.description.includes("The export keyword")
)) {
const { body } = meriyah.parseScript(strToAnalyze, {
...kMeriyahDefaultOptions, module: true
});
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/NodeSecure/js-x-ray#readme",
"dependencies": {
"@nodesecure/estree-ast-utils": "^1.3.0",
"@nodesecure/estree-ast-utils": "^1.3.1",
"@nodesecure/sec-literal": "^1.2.0",
"estree-walker": "^3.0.1",
"is-minified-code": "^2.0.0",
Expand Down
6 changes: 5 additions & 1 deletion src/obfuscators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ export function isObfuscatedCode(analysis) {
}
else {
// TODO: also implement Dictionnary checkup
const identifiers = analysis.identifiersName
.map((value) => value?.name ?? null)
.filter((name) => typeof name === "string");

const { prefix, oneTimeOccurence } = Patterns.commonHexadecimalPrefix(
analysis.identifiersName.map((value) => value.name)
identifiers
);
const uPrefixNames = new Set(Object.keys(prefix));

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/regress/jsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Dropzone = forwardRef(({ children, ...params }, ref) => {
const { open, ...props } = useDropzone(params);

useImperativeHandle(ref, () => ({ open }), [open]);

// TODO: Figure out why react-styleguidist cannot create docs if we don't return a jsx element
return <Fragment>{children({ ...props, open })}</Fragment>;
});
10 changes: 9 additions & 1 deletion test/module.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import test from "tape";
// Import Internal Dependencies
import { runASTAnalysis } from "../index.js";

test("it should not crash even if module 'false' is provided", (tape) => {
test("it should not crash even if module 'false' is provided (import keyword)", (tape) => {
runASTAnalysis("import * as foo from \"foo\";", {
module: false
});

tape.end();
});

test("it should not crash even if module 'false' is provided (export keyword)", (tape) => {
runASTAnalysis("export const foo = 5;", {
module: false
});

tape.end();
});

test("it should be capable to extract dependencies name for ECMAScript Modules (ESM)", (tape) => {
const { dependencies, warnings } = runASTAnalysis(`
import * as http from "http";
Expand Down
1 change: 0 additions & 1 deletion test/obfuscated.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ test("should detect 'freejsobfuscator' obfuscation", (tape) => {
const trycatch = readFileSync(new URL("freejsobfuscator.js", FIXTURE_URL), "utf-8");
const { warnings } = runASTAnalysis(trycatch);

tape.strictEqual(warnings.length, 3);
tape.deepEqual(getWarningKind(warnings), [
"encoded-literal", "encoded-literal", "obfuscated-code"
].sort());
Expand Down
8 changes: 8 additions & 0 deletions test/regress.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ const FIXTURE_URL = new URL("fixtures/regress/", import.meta.url);
test("it should not crash for prop-types", (tape) => {
const propTypes = readFileSync(new URL("prop-types.min.js", FIXTURE_URL), "utf-8");
runASTAnalysis(propTypes);

tape.end();
});

test("it should not crash for JSX", (tape) => {
const propTypes = readFileSync(new URL("jsx.js", FIXTURE_URL), "utf-8");
runASTAnalysis(propTypes);

tape.end();
});