-
Notifications
You must be signed in to change notification settings - Fork 39
/
resolveMinimalAlphabet.js
36 lines (35 loc) · 1.46 KB
/
resolveMinimalAlphabet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import {badValue} from '../config.js';
import {evalInVm} from '../utils/evalInVm.js';
import {doesDescendantMatchCondition} from '../utils/doesDescendantMatchCondition.js';
/**
* Resolve unary expressions on values which aren't numbers such as +true, +[], +[...], etc,
* as well as binary expressions around the + operator. These usually resolve to string values,
* which can be used to obfuscate code in schemes such as JSFuck.
* @param {Arborist} arb
* @param {Function} candidateFilter (optional) a filter to apply on the candidates list
* @return {Arborist}
*/
export default function resolveMinimalAlphabet(arb, candidateFilter = () => true) {
const relevantNodes = [
...(arb.ast[0].typeMap.UnaryExpression || []),
...(arb.ast[0].typeMap.BinaryExpression || []),
];
for (let i = 0; i < relevantNodes.length; i++) {
const n = relevantNodes[i];
if ((n.type === 'UnaryExpression' &&
((n.argument.type === 'Literal' && /^\D/.test(n.argument.raw[0])) ||
n.argument.type === 'ArrayExpression')) ||
(n.type === 'BinaryExpression' &&
n.operator === '+' &&
(n.left.type !== 'MemberExpression' && Number.isNaN(parseFloat(n.left?.value))) &&
![n.left?.type, n.right?.type].includes('ThisExpression')) &&
candidateFilter(n)) {
if (doesDescendantMatchCondition(n, n => n.type === 'ThisExpression')) continue;
const replacementNode = evalInVm(n.src);
if (replacementNode !== badValue) {
arb.markNode(n, replacementNode);
}
}
}
return arb;
}