lasik-agents 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -40
- package/dist/formats/asar.js +119 -8
- package/dist/formats/wasm.js +62 -16
- package/dist/identity.d.ts +1 -1
- package/dist/identity.js +1 -1
- package/dist/js/heal.d.ts +8 -0
- package/dist/js/heal.js +16 -6
- package/dist/js/html.js +61 -11
- package/dist/js/passes/comments.d.ts +5 -0
- package/dist/js/passes/comments.js +28 -0
- package/dist/js/passes/interlock.d.ts +6 -5
- package/dist/js/passes/interlock.js +182 -62
- package/dist/js/passes/self-seal.js +80 -2
- package/dist/js/passes/tree-shake.d.ts +6 -0
- package/dist/js/passes/tree-shake.js +68 -1
- package/dist/js/pure-eval.d.ts +35 -0
- package/dist/js/pure-eval.js +393 -0
- package/package.json +2 -1
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
import { DECOY_DECL } from "../fingerprints.js";
|
|
2
2
|
import { parseJs, t, traverse } from "../ast.js";
|
|
3
|
+
const GLOBAL_OBJECTS = new Set(["window", "globalThis", "self", "top", "parent", "frames"]);
|
|
4
|
+
/**
|
|
5
|
+
* A global could be read under a name built at runtime: `window[k]`, `this[k]` (sloppy
|
|
6
|
+
* functions see the global object as `this`), or the global object escaping as a value
|
|
7
|
+
* (`var w = window; w[k]`, `f(globalThis)`).
|
|
8
|
+
*/
|
|
9
|
+
function readsGlobalsDynamically(paths) {
|
|
10
|
+
let dyn = false;
|
|
11
|
+
const literalKey = (n) => !n.computed || t.isStringLiteral(n.property) || t.isNumericLiteral(n.property);
|
|
12
|
+
const thisKey = (p) => {
|
|
13
|
+
if (!literalKey(p.node) && t.isThisExpression(p.node.object)) {
|
|
14
|
+
dyn = true;
|
|
15
|
+
p.stop();
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const visitor = {
|
|
19
|
+
MemberExpression: thisKey,
|
|
20
|
+
OptionalMemberExpression: thisKey,
|
|
21
|
+
Identifier(p) {
|
|
22
|
+
if (!GLOBAL_OBJECTS.has(p.node.name) || p.scope.getBinding(p.node.name) || !p.isReferencedIdentifier())
|
|
23
|
+
return;
|
|
24
|
+
const parent = p.parentPath;
|
|
25
|
+
const plainRead = (parent?.isMemberExpression() || parent?.isOptionalMemberExpression()) && parent.node.object === p.node && literalKey(parent.node);
|
|
26
|
+
if (!plainRead && !parent?.isUnaryExpression({ operator: "typeof" })) {
|
|
27
|
+
dyn = true;
|
|
28
|
+
p.stop();
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
for (const p of paths) {
|
|
33
|
+
if (dyn)
|
|
34
|
+
break;
|
|
35
|
+
p.traverse(visitor);
|
|
36
|
+
}
|
|
37
|
+
return dyn;
|
|
38
|
+
}
|
|
3
39
|
/** BLA's generated names: camelCase stem (`verbAdjNoun`) + `_` + 3–4 hex. Not `show_404` or `cache_add`. */
|
|
4
40
|
const BLA_TAG = /^_?[a-z][a-z0-9]*[A-Z][A-Za-z0-9]*_[0-9a-f]{3,4}$/;
|
|
5
41
|
/** True when the file is a module (ESM/CJS), so unreferenced top-level code can't be used from elsewhere. */
|
|
@@ -234,6 +270,12 @@ export function shakeUnreachable(ast, f, opts = {}) {
|
|
|
234
270
|
}
|
|
235
271
|
const moduleLike = opts.closure === true || isModuleLike(ast);
|
|
236
272
|
const external = opts.externalRoots ?? new Set();
|
|
273
|
+
// Page-local globals can also be read as `window.x` or `window["x"]`: a property name or
|
|
274
|
+
// string word in live code keeps a global alive (see markRefs). A computed key built at
|
|
275
|
+
// runtime could name anything, so then globals stay.
|
|
276
|
+
// Assumed, then checked against the code that stays live: removed code never runs, so only
|
|
277
|
+
// live code reading globals by a runtime-built name can break the assumption.
|
|
278
|
+
let pageLocal = opts.pageLocal === true && !moduleLike;
|
|
237
279
|
for (let round = 0; round < 8; round++) {
|
|
238
280
|
pp = programPath();
|
|
239
281
|
pp.scope.crawl();
|
|
@@ -264,13 +306,33 @@ export function shakeUnreachable(ast, f, opts = {}) {
|
|
|
264
306
|
// In a plain script every global may be used by another script; only fingerprinted decoys are fair game.
|
|
265
307
|
for (const [name, d] of decls) {
|
|
266
308
|
const injected = DECOY_DECL.test(name) || (opts.blaHardened === true && BLA_TAG.test(name));
|
|
267
|
-
|
|
309
|
+
// `function onload(){}` / `var onbeforeunload = …` on a page: the browser calls those itself.
|
|
310
|
+
const browserHandler = pageLocal && /^on[a-z]+$/.test(name);
|
|
311
|
+
if (external.has(name) || evalRoots.has(name) || browserHandler || (!moduleLike && !pageLocal && !injected))
|
|
268
312
|
roots.push(d);
|
|
269
313
|
}
|
|
270
314
|
const live = new Set();
|
|
271
315
|
const queue = [...roots];
|
|
272
316
|
const programScope = pp.scope;
|
|
317
|
+
const mark = (name) => {
|
|
318
|
+
if (live.has(name) || !decls.has(name))
|
|
319
|
+
return;
|
|
320
|
+
live.add(name);
|
|
321
|
+
queue.push(decls.get(name));
|
|
322
|
+
};
|
|
323
|
+
const markWords = (s) => { for (const w of s.match(/[A-Za-z_$][\w$]*/g) ?? [])
|
|
324
|
+
mark(w); };
|
|
273
325
|
const markRefs = (p) => {
|
|
326
|
+
if (pageLocal) {
|
|
327
|
+
p.traverse({
|
|
328
|
+
"MemberExpression|OptionalMemberExpression"(mp) {
|
|
329
|
+
if (!mp.node.computed && t.isIdentifier(mp.node.property))
|
|
330
|
+
mark(mp.node.property.name);
|
|
331
|
+
},
|
|
332
|
+
StringLiteral(sp) { markWords(sp.node.value); },
|
|
333
|
+
TemplateElement(tp) { markWords(tp.node.value.raw); },
|
|
334
|
+
});
|
|
335
|
+
}
|
|
274
336
|
p.traverse({
|
|
275
337
|
JSXIdentifier(jp) {
|
|
276
338
|
// <Button/> references the Button binding.
|
|
@@ -303,6 +365,11 @@ export function shakeUnreachable(ast, f, opts = {}) {
|
|
|
303
365
|
continue;
|
|
304
366
|
markRefs(p);
|
|
305
367
|
}
|
|
368
|
+
if (pageLocal && readsGlobalsDynamically([...roots, ...[...live].map((n) => decls.get(n))])) {
|
|
369
|
+
pageLocal = false;
|
|
370
|
+
round--;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
306
373
|
let thisRound = 0;
|
|
307
374
|
for (const [name, d] of decls) {
|
|
308
375
|
if (live.has(name))
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { t } from "./ast.js";
|
|
2
|
+
/**
|
|
3
|
+
* Evaluator for a closed, side-effect-free subset of JS: numbers, strings, plain
|
|
4
|
+
* arrays/objects, local variables, loops, and a few whitelisted builtins. It walks the
|
|
5
|
+
* AST itself and never runs target code; anything outside the subset (calls to unknown
|
|
6
|
+
* functions, property writes, getters, prototype lookups) makes it give up.
|
|
7
|
+
*/
|
|
8
|
+
export type Val = number | string | boolean | null | undefined | Val[] | {
|
|
9
|
+
[k: string]: Val;
|
|
10
|
+
};
|
|
11
|
+
/** Resolves a free identifier (a top-level binding proven constant) or returns undefined to refuse. */
|
|
12
|
+
export type Globals = (name: string) => {
|
|
13
|
+
value: Val;
|
|
14
|
+
} | undefined;
|
|
15
|
+
/** Evaluate `fn(...args)`; undefined result means "could not prove a value" (use `ok`). */
|
|
16
|
+
/**
|
|
17
|
+
* Evaluate `fn(...args)`. `shadowed(name)` must say whether the program declares or
|
|
18
|
+
* patches `name` (`Math`, `String`, `NaN` …): then the real builtin is not what runs.
|
|
19
|
+
*/
|
|
20
|
+
export declare function callPure(fn: t.Function, args: Val[], globals: Globals, opts?: {
|
|
21
|
+
shadowed?: (name: string) => boolean;
|
|
22
|
+
fuel?: number;
|
|
23
|
+
}): {
|
|
24
|
+
ok: true;
|
|
25
|
+
value: Val;
|
|
26
|
+
} | {
|
|
27
|
+
ok: false;
|
|
28
|
+
};
|
|
29
|
+
/** Evaluate a closed expression (a literal initializer) with the same rules. */
|
|
30
|
+
export declare function evalPure(node: t.Expression, globals?: Globals): {
|
|
31
|
+
ok: true;
|
|
32
|
+
value: Val;
|
|
33
|
+
} | {
|
|
34
|
+
ok: false;
|
|
35
|
+
};
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { t } from "./ast.js";
|
|
2
|
+
class Refuse extends Error {
|
|
3
|
+
}
|
|
4
|
+
const refuse = () => { throw new Refuse(); };
|
|
5
|
+
class Return {
|
|
6
|
+
value;
|
|
7
|
+
constructor(value) {
|
|
8
|
+
this.value = value;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const BREAK = Symbol("break");
|
|
12
|
+
const CONTINUE = Symbol("continue");
|
|
13
|
+
class Scope {
|
|
14
|
+
parent;
|
|
15
|
+
vars = new Map();
|
|
16
|
+
constructor(parent) {
|
|
17
|
+
this.parent = parent;
|
|
18
|
+
}
|
|
19
|
+
declare(name, value, mutable) { this.vars.set(name, { value, mutable }); }
|
|
20
|
+
find(name) { return this.vars.get(name) ?? this.parent?.find(name); }
|
|
21
|
+
}
|
|
22
|
+
const isPrim = (v) => v === null || typeof v !== "object";
|
|
23
|
+
const isRecord = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
24
|
+
const OBJECT_PROTO = new Set(Object.getOwnPropertyNames(Object.prototype));
|
|
25
|
+
/** Evaluate `fn(...args)`; undefined result means "could not prove a value" (use `ok`). */
|
|
26
|
+
/**
|
|
27
|
+
* Evaluate `fn(...args)`. `shadowed(name)` must say whether the program declares or
|
|
28
|
+
* patches `name` (`Math`, `String`, `NaN` …): then the real builtin is not what runs.
|
|
29
|
+
*/
|
|
30
|
+
export function callPure(fn, args, globals, opts = {}) {
|
|
31
|
+
const fuel = opts.fuel ?? 5_000_000;
|
|
32
|
+
const shadowed = opts.shadowed ?? (() => false);
|
|
33
|
+
// Every name the function declares: a read that misses the scope chain but hits one of
|
|
34
|
+
// these is a hoisting or TDZ case (`return K; var K`, `(a = b, b)`), not a global.
|
|
35
|
+
const locals = new Set();
|
|
36
|
+
let steps = 0;
|
|
37
|
+
const tick = () => { if (++steps > fuel)
|
|
38
|
+
refuse(); };
|
|
39
|
+
const prim = (v) => (isPrim(v) ? v : refuse());
|
|
40
|
+
const lookup = (s, name) => {
|
|
41
|
+
const v = s.find(name);
|
|
42
|
+
if (v)
|
|
43
|
+
return v.value;
|
|
44
|
+
if (locals.has(name) || shadowed(name))
|
|
45
|
+
return refuse();
|
|
46
|
+
if (name === "undefined")
|
|
47
|
+
return undefined;
|
|
48
|
+
if (name === "NaN")
|
|
49
|
+
return NaN;
|
|
50
|
+
if (name === "Infinity")
|
|
51
|
+
return Infinity;
|
|
52
|
+
const g = globals(name);
|
|
53
|
+
return g ? g.value : refuse();
|
|
54
|
+
};
|
|
55
|
+
const assign = (s, name, value) => {
|
|
56
|
+
const v = s.find(name);
|
|
57
|
+
if (!v || !v.mutable)
|
|
58
|
+
refuse();
|
|
59
|
+
v.value = value;
|
|
60
|
+
};
|
|
61
|
+
const member = (obj, key) => {
|
|
62
|
+
if (typeof obj === "string") {
|
|
63
|
+
if (key === "length")
|
|
64
|
+
return obj.length;
|
|
65
|
+
if (typeof key === "number")
|
|
66
|
+
return Number.isInteger(key) && key >= 0 ? obj[key] : undefined;
|
|
67
|
+
return refuse();
|
|
68
|
+
}
|
|
69
|
+
if (Array.isArray(obj)) {
|
|
70
|
+
if (key === "length")
|
|
71
|
+
return obj.length;
|
|
72
|
+
if (typeof key === "number")
|
|
73
|
+
return Number.isInteger(key) && key >= 0 && key < obj.length ? obj[key] : undefined;
|
|
74
|
+
return refuse();
|
|
75
|
+
}
|
|
76
|
+
if (isRecord(obj)) {
|
|
77
|
+
const k = String(prim(key));
|
|
78
|
+
if (Object.prototype.hasOwnProperty.call(obj, k))
|
|
79
|
+
return obj[k];
|
|
80
|
+
return OBJECT_PROTO.has(k) ? refuse() : undefined;
|
|
81
|
+
}
|
|
82
|
+
return refuse();
|
|
83
|
+
};
|
|
84
|
+
const call = (node, s) => {
|
|
85
|
+
const c = node.callee;
|
|
86
|
+
const argsOf = () => node.arguments.map((a) => (t.isExpression(a) ? expr(a, s) : refuse()));
|
|
87
|
+
if (t.isMemberExpression(c) && !c.computed && t.isIdentifier(c.property)) {
|
|
88
|
+
const m = c.property.name;
|
|
89
|
+
if (t.isIdentifier(c.object) && !s.find(c.object.name) && !locals.has(c.object.name) && !shadowed(c.object.name) && !globals(c.object.name)) {
|
|
90
|
+
const args = argsOf();
|
|
91
|
+
const o = c.object.name;
|
|
92
|
+
if (o === "Array" && m === "isArray")
|
|
93
|
+
return Array.isArray(args[0]);
|
|
94
|
+
const n = args.map((a) => prim(a));
|
|
95
|
+
if (o === "Math" && m === "imul")
|
|
96
|
+
return Math.imul(Number(n[0]), Number(n[1]));
|
|
97
|
+
if (o === "Math" && (m === "abs" || m === "floor" || m === "ceil" || m === "trunc" || m === "sign"))
|
|
98
|
+
return Math[m](Number(n[0]));
|
|
99
|
+
if (o === "Math" && (m === "min" || m === "max"))
|
|
100
|
+
return Math[m](...n.map(Number));
|
|
101
|
+
if (o === "Array" && m === "isArray")
|
|
102
|
+
return Array.isArray(args[0]);
|
|
103
|
+
if (o === "String" && m === "fromCharCode")
|
|
104
|
+
return String.fromCharCode(...n.map(Number));
|
|
105
|
+
return refuse();
|
|
106
|
+
}
|
|
107
|
+
// The receiver is evaluated before the arguments, as in JS.
|
|
108
|
+
const self = t.isExpression(c.object) ? expr(c.object, s) : refuse();
|
|
109
|
+
const args = argsOf();
|
|
110
|
+
if (typeof self === "string" && m === "charCodeAt")
|
|
111
|
+
return self.charCodeAt(Number(prim(args[0] ?? 0)));
|
|
112
|
+
return refuse();
|
|
113
|
+
}
|
|
114
|
+
return refuse();
|
|
115
|
+
};
|
|
116
|
+
const binary = (op, a, b) => {
|
|
117
|
+
if (op === "===")
|
|
118
|
+
return a === b;
|
|
119
|
+
if (op === "!==")
|
|
120
|
+
return a !== b;
|
|
121
|
+
const x = prim(a);
|
|
122
|
+
const y = prim(b);
|
|
123
|
+
switch (op) {
|
|
124
|
+
case "==": return x == y;
|
|
125
|
+
case "!=": return x != y;
|
|
126
|
+
case "+": return typeof x === "string" || typeof y === "string" ? String(x) + String(y) : Number(x) + Number(y);
|
|
127
|
+
case "-": return Number(x) - Number(y);
|
|
128
|
+
case "*": return Number(x) * Number(y);
|
|
129
|
+
case "/": return Number(x) / Number(y);
|
|
130
|
+
case "%": return Number(x) % Number(y);
|
|
131
|
+
case "**": return Number(x) ** Number(y);
|
|
132
|
+
case "|": return Number(x) | Number(y);
|
|
133
|
+
case "&": return Number(x) & Number(y);
|
|
134
|
+
case "^": return Number(x) ^ Number(y);
|
|
135
|
+
case "<<": return Number(x) << Number(y);
|
|
136
|
+
case ">>": return Number(x) >> Number(y);
|
|
137
|
+
case ">>>": return Number(x) >>> Number(y);
|
|
138
|
+
case "<": return x < y;
|
|
139
|
+
case "<=": return x <= y;
|
|
140
|
+
case ">": return x > y;
|
|
141
|
+
case ">=": return x >= y;
|
|
142
|
+
}
|
|
143
|
+
return refuse();
|
|
144
|
+
};
|
|
145
|
+
const expr = (n, s) => {
|
|
146
|
+
tick();
|
|
147
|
+
if (t.isNumericLiteral(n) || t.isStringLiteral(n) || t.isBooleanLiteral(n))
|
|
148
|
+
return n.value;
|
|
149
|
+
if (t.isNullLiteral(n))
|
|
150
|
+
return null;
|
|
151
|
+
if (t.isParenthesizedExpression(n))
|
|
152
|
+
return expr(n.expression, s);
|
|
153
|
+
if (t.isIdentifier(n))
|
|
154
|
+
return lookup(s, n.name);
|
|
155
|
+
if (t.isTemplateLiteral(n)) {
|
|
156
|
+
let out = n.quasis[0].value.cooked ?? refuse();
|
|
157
|
+
n.expressions.forEach((e, i) => { out += String(prim(expr(e, s))) + (n.quasis[i + 1].value.cooked ?? refuse()); });
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
if (t.isArrayExpression(n))
|
|
161
|
+
return n.elements.map((e) => (e && t.isExpression(e) ? expr(e, s) : refuse()));
|
|
162
|
+
if (t.isObjectExpression(n)) {
|
|
163
|
+
const o = {};
|
|
164
|
+
for (const p of n.properties) {
|
|
165
|
+
if (!t.isObjectProperty(p) || p.computed || !t.isExpression(p.value))
|
|
166
|
+
return refuse();
|
|
167
|
+
const k = t.isIdentifier(p.key) ? p.key.name : t.isStringLiteral(p.key) || t.isNumericLiteral(p.key) ? String(p.key.value) : refuse();
|
|
168
|
+
if (k === "__proto__")
|
|
169
|
+
return refuse();
|
|
170
|
+
o[k] = expr(p.value, s);
|
|
171
|
+
}
|
|
172
|
+
return o;
|
|
173
|
+
}
|
|
174
|
+
if (t.isMemberExpression(n)) {
|
|
175
|
+
const obj = t.isExpression(n.object) ? expr(n.object, s) : refuse();
|
|
176
|
+
const key = n.computed ? expr(n.property, s) : t.isIdentifier(n.property) ? n.property.name : refuse();
|
|
177
|
+
return member(obj, key);
|
|
178
|
+
}
|
|
179
|
+
if (t.isCallExpression(n))
|
|
180
|
+
return call(n, s);
|
|
181
|
+
if (t.isUnaryExpression(n)) {
|
|
182
|
+
if (n.operator === "typeof") {
|
|
183
|
+
if (t.isIdentifier(n.argument) && !s.find(n.argument.name) && !globals(n.argument.name))
|
|
184
|
+
return refuse();
|
|
185
|
+
const v = expr(n.argument, s);
|
|
186
|
+
return v === null ? "object" : typeof v;
|
|
187
|
+
}
|
|
188
|
+
const v = expr(n.argument, s);
|
|
189
|
+
if (n.operator === "void")
|
|
190
|
+
return undefined;
|
|
191
|
+
if (n.operator === "!")
|
|
192
|
+
return !v;
|
|
193
|
+
const x = prim(v);
|
|
194
|
+
if (n.operator === "-")
|
|
195
|
+
return -Number(x);
|
|
196
|
+
if (n.operator === "+")
|
|
197
|
+
return Number(x);
|
|
198
|
+
if (n.operator === "~")
|
|
199
|
+
return ~Number(x);
|
|
200
|
+
return refuse();
|
|
201
|
+
}
|
|
202
|
+
if (t.isBinaryExpression(n)) {
|
|
203
|
+
if (!t.isExpression(n.left))
|
|
204
|
+
return refuse();
|
|
205
|
+
return binary(n.operator, expr(n.left, s), expr(n.right, s));
|
|
206
|
+
}
|
|
207
|
+
if (t.isLogicalExpression(n)) {
|
|
208
|
+
const a = expr(n.left, s);
|
|
209
|
+
if (n.operator === "&&")
|
|
210
|
+
return a ? expr(n.right, s) : a;
|
|
211
|
+
if (n.operator === "||")
|
|
212
|
+
return a ? a : expr(n.right, s);
|
|
213
|
+
return a ?? expr(n.right, s);
|
|
214
|
+
}
|
|
215
|
+
if (t.isConditionalExpression(n))
|
|
216
|
+
return expr(n.test, s) ? expr(n.consequent, s) : expr(n.alternate, s);
|
|
217
|
+
if (t.isSequenceExpression(n)) {
|
|
218
|
+
let v;
|
|
219
|
+
for (const e of n.expressions)
|
|
220
|
+
v = expr(e, s);
|
|
221
|
+
return v;
|
|
222
|
+
}
|
|
223
|
+
if (t.isAssignmentExpression(n)) {
|
|
224
|
+
if (!t.isIdentifier(n.left))
|
|
225
|
+
return refuse();
|
|
226
|
+
const name = n.left.name;
|
|
227
|
+
// `x op= y` reads x before evaluating y.
|
|
228
|
+
const old = n.operator === "=" ? undefined : lookup(s, name);
|
|
229
|
+
const rhs = expr(n.right, s);
|
|
230
|
+
const v = n.operator === "=" ? rhs : binary(n.operator.slice(0, -1), old, rhs);
|
|
231
|
+
assign(s, name, v);
|
|
232
|
+
return v;
|
|
233
|
+
}
|
|
234
|
+
if (t.isUpdateExpression(n)) {
|
|
235
|
+
if (!t.isIdentifier(n.argument))
|
|
236
|
+
return refuse();
|
|
237
|
+
const old = Number(prim(lookup(s, n.argument.name)));
|
|
238
|
+
const v = n.operator === "++" ? old + 1 : old - 1;
|
|
239
|
+
assign(s, n.argument.name, v);
|
|
240
|
+
return n.prefix ? v : old;
|
|
241
|
+
}
|
|
242
|
+
return refuse();
|
|
243
|
+
};
|
|
244
|
+
const declare = (d, s, values) => {
|
|
245
|
+
d.declarations.forEach((x, i) => {
|
|
246
|
+
if (!t.isIdentifier(x.id))
|
|
247
|
+
refuse();
|
|
248
|
+
const v = values ? values[i] : x.init ? expr(x.init, s) : undefined;
|
|
249
|
+
s.declare(x.id.name, v, d.kind !== "const");
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
const loopBody = (body, s) => {
|
|
253
|
+
const f = stmt(body, new Scope(s));
|
|
254
|
+
if (f instanceof Return)
|
|
255
|
+
return f;
|
|
256
|
+
if (f === BREAK)
|
|
257
|
+
return undefined;
|
|
258
|
+
return "next";
|
|
259
|
+
};
|
|
260
|
+
const stmt = (n, s) => {
|
|
261
|
+
tick();
|
|
262
|
+
if (t.isBlockStatement(n)) {
|
|
263
|
+
const inner = new Scope(s);
|
|
264
|
+
for (const x of n.body) {
|
|
265
|
+
const f = stmt(x, inner);
|
|
266
|
+
if (f)
|
|
267
|
+
return f;
|
|
268
|
+
}
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
if (t.isEmptyStatement(n))
|
|
272
|
+
return undefined;
|
|
273
|
+
if (t.isExpressionStatement(n)) {
|
|
274
|
+
expr(n.expression, s);
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
if (t.isVariableDeclaration(n)) {
|
|
278
|
+
if (n.kind === "var")
|
|
279
|
+
refuse();
|
|
280
|
+
declare(n, s);
|
|
281
|
+
return undefined;
|
|
282
|
+
}
|
|
283
|
+
if (t.isReturnStatement(n))
|
|
284
|
+
return new Return(n.argument ? expr(n.argument, s) : undefined);
|
|
285
|
+
if (t.isIfStatement(n))
|
|
286
|
+
return expr(n.test, s) ? stmt(n.consequent, s) : n.alternate ? stmt(n.alternate, s) : undefined;
|
|
287
|
+
if (t.isBreakStatement(n))
|
|
288
|
+
return n.label ? refuse() : BREAK;
|
|
289
|
+
if (t.isContinueStatement(n))
|
|
290
|
+
return n.label ? refuse() : CONTINUE;
|
|
291
|
+
if (t.isForStatement(n)) {
|
|
292
|
+
const ls = new Scope(s);
|
|
293
|
+
if (t.isVariableDeclaration(n.init)) {
|
|
294
|
+
if (n.init.kind === "var")
|
|
295
|
+
refuse();
|
|
296
|
+
declare(n.init, ls);
|
|
297
|
+
}
|
|
298
|
+
else if (n.init)
|
|
299
|
+
expr(n.init, ls);
|
|
300
|
+
for (;;) {
|
|
301
|
+
tick();
|
|
302
|
+
if (n.test && !expr(n.test, ls))
|
|
303
|
+
return undefined;
|
|
304
|
+
// A fresh copy per iteration matches let-per-iteration semantics for closures; we have none.
|
|
305
|
+
const r = loopBody(n.body, ls);
|
|
306
|
+
if (r !== "next")
|
|
307
|
+
return r;
|
|
308
|
+
if (n.update)
|
|
309
|
+
expr(n.update, ls);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (t.isWhileStatement(n)) {
|
|
313
|
+
for (;;) {
|
|
314
|
+
tick();
|
|
315
|
+
if (!expr(n.test, s))
|
|
316
|
+
return undefined;
|
|
317
|
+
const r = loopBody(n.body, s);
|
|
318
|
+
if (r !== "next")
|
|
319
|
+
return r;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (t.isForOfStatement(n)) {
|
|
323
|
+
if (n.await || !t.isVariableDeclaration(n.left) || n.left.kind === "var")
|
|
324
|
+
return refuse();
|
|
325
|
+
const it = expr(n.right, s);
|
|
326
|
+
const items = typeof it === "string" ? [...it] : Array.isArray(it) ? it : refuse();
|
|
327
|
+
for (const v of items) {
|
|
328
|
+
const ls = new Scope(s);
|
|
329
|
+
declare(n.left, ls, [v]);
|
|
330
|
+
const r = loopBody(n.body, ls);
|
|
331
|
+
if (r !== "next")
|
|
332
|
+
return r;
|
|
333
|
+
}
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
return refuse();
|
|
337
|
+
};
|
|
338
|
+
try {
|
|
339
|
+
if (fn.async || fn.generator)
|
|
340
|
+
return { ok: false };
|
|
341
|
+
// `var` and nested declarations hoist; refuse them rather than model hoisting.
|
|
342
|
+
for (const p of fn.params)
|
|
343
|
+
t.traverseFast(p, (x) => { if (t.isIdentifier(x))
|
|
344
|
+
locals.add(x.name); });
|
|
345
|
+
let hoists = false;
|
|
346
|
+
const scan = (n, top) => {
|
|
347
|
+
if (!top && (t.isFunction(n) || t.isClass(n))) {
|
|
348
|
+
if (t.isFunctionDeclaration(n) || t.isClassDeclaration(n))
|
|
349
|
+
hoists = true;
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (t.isVariableDeclaration(n)) {
|
|
353
|
+
if (n.kind !== "let" && n.kind !== "const")
|
|
354
|
+
hoists = true;
|
|
355
|
+
for (const d of n.declarations)
|
|
356
|
+
t.traverseFast(d.id, (x) => { if (t.isIdentifier(x))
|
|
357
|
+
locals.add(x.name); });
|
|
358
|
+
}
|
|
359
|
+
for (const key of t.VISITOR_KEYS[n.type] ?? []) {
|
|
360
|
+
const child = n[key];
|
|
361
|
+
if (Array.isArray(child))
|
|
362
|
+
child.forEach((c) => c && typeof c === "object" && scan(c, false));
|
|
363
|
+
else if (child && typeof child === "object")
|
|
364
|
+
scan(child, false);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
scan(fn.body, true);
|
|
368
|
+
if (hoists)
|
|
369
|
+
return { ok: false };
|
|
370
|
+
const s = new Scope();
|
|
371
|
+
fn.params.forEach((p, i) => {
|
|
372
|
+
if (t.isIdentifier(p))
|
|
373
|
+
s.declare(p.name, args[i], true);
|
|
374
|
+
else if (t.isAssignmentPattern(p) && t.isIdentifier(p.left))
|
|
375
|
+
s.declare(p.left.name, args[i] === undefined ? expr(p.right, s) : args[i], true);
|
|
376
|
+
else
|
|
377
|
+
refuse();
|
|
378
|
+
});
|
|
379
|
+
if (!t.isBlockStatement(fn.body))
|
|
380
|
+
return { ok: true, value: expr(fn.body, s) };
|
|
381
|
+
const f = stmt(fn.body, s);
|
|
382
|
+
return { ok: true, value: f instanceof Return ? f.value : undefined };
|
|
383
|
+
}
|
|
384
|
+
catch (e) {
|
|
385
|
+
if (e instanceof Refuse)
|
|
386
|
+
return { ok: false };
|
|
387
|
+
throw e;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/** Evaluate a closed expression (a literal initializer) with the same rules. */
|
|
391
|
+
export function evalPure(node, globals = () => undefined) {
|
|
392
|
+
return callPure(t.arrowFunctionExpression([], node), [], globals, { shadowed: () => true });
|
|
393
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lasik-agents",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "LASIK (LLM Agent Sight In Kode): restores the sight of AI reverse-engineering agents blinded by BLA. Statically undoes every BLA shield (JS bundles, Electron/ASAR, WebAssembly, Python/.pyc, native ELF/PE/Mach-O, SQLite, models, APK/DEX, JVM, .NET, BEAM, EVM, PCAP and more) and prompt-firewalls the decoy text so REA can read them again.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lasik",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"typecheck": "tsc --noEmit",
|
|
60
60
|
"test": "vitest run",
|
|
61
61
|
"bench": "node dist/cli.js bench",
|
|
62
|
+
"bench:renamed": "node scripts/bench-renamed.mjs",
|
|
62
63
|
"prepublishOnly": "npm run build && npm test",
|
|
63
64
|
"coverage:docs": "node scripts/bla-coverage.mjs docs",
|
|
64
65
|
"coverage:check": "node scripts/bla-coverage.mjs check",
|