lasik-agents 0.2.1 → 0.3.1
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
package/dist/js/html.js
CHANGED
|
@@ -3,15 +3,35 @@ import { healJs } from "./heal.js";
|
|
|
3
3
|
const SCRIPT = /<script(\s[^>]*)?>([\s\S]*?)<\/script\s*>/gi;
|
|
4
4
|
// The executable set BLA hardens (bla/src/engine/protector.ts:332-362, classifyHtmlScriptTag).
|
|
5
5
|
const JS_TYPE = /^\s*(?:text\/javascript|application\/javascript|module|text\/ecmascript|application\/ecmascript|javascript)?\s*$/i;
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
/** Data blocks: script types the browser never runs. */
|
|
7
|
+
const DATA_TYPE = /^\s*(?:application\/(?:ld\+)?json|importmap|speculationrules|text\/plain)\s*$/i;
|
|
8
|
+
const decodeEntities = (s) => s
|
|
9
|
+
.replace(/&#x([0-9a-f]+);?/gi, (_, h) => String.fromCodePoint(parseInt(h, 16)))
|
|
10
|
+
.replace(/&#(\d+);?/g, (_, d) => String.fromCodePoint(Number(d)))
|
|
11
|
+
.replace(/&(colon|lpar|rpar|period|lowbar|semi);/gi, (_, n) => ({ colon: ":", lpar: "(", rpar: ")", period: ".", lowbar: "_", semi: ";" })[n.toLowerCase()]);
|
|
12
|
+
/**
|
|
13
|
+
* Every identifier-like word in the page outside its scripts: event attributes in any
|
|
14
|
+
* syntax (quoted, unquoted, `/`-separated), `javascript:` URLs, entity-encoded ones. Any
|
|
15
|
+
* global they could name stays alive.
|
|
16
|
+
*/
|
|
17
|
+
function markupRoots(html) {
|
|
18
|
+
return new Set(decodeEntities(html.replace(SCRIPT, " ")).match(/[A-Za-z_$][\w$]*/g) ?? []);
|
|
19
|
+
}
|
|
20
|
+
/** Markup that lets code outside the page's inline scripts read their globals. */
|
|
21
|
+
function pageIsOpen(html, scripts) {
|
|
22
|
+
if (/<(?:iframe|frame|object|embed|portal)\b/i.test(html))
|
|
23
|
+
return true;
|
|
24
|
+
for (const { attrs } of scripts) {
|
|
25
|
+
if (/\b(?:src|href)\s*=/i.test(attrs))
|
|
26
|
+
return true;
|
|
27
|
+
const type = attrs.match(/\btype\s*=\s*["']?([^"'\s>]+)/i)?.[1] ?? "";
|
|
28
|
+
if (!JS_TYPE.test(type) && !DATA_TYPE.test(type))
|
|
29
|
+
return true; // text/babel and friends run JS we don't see
|
|
12
30
|
}
|
|
13
|
-
return
|
|
31
|
+
return false;
|
|
14
32
|
}
|
|
33
|
+
/** Surviving script code that can load more code into the page (imports, injected scripts, markup writes). */
|
|
34
|
+
const LOADS_CODE = /\bimport\s*(?:\(|["'{*\w])|createElement\s*\(|document\s*\.\s*write|\.(?:inner|outer)HTML\s*=|insertAdjacentHTML|createContextualFragment|new\s+Worker/;
|
|
15
35
|
/**
|
|
16
36
|
* BLA hardens every executable inline <script> independently (each gets its
|
|
17
37
|
* own seed, armor corpus and decoy suite) and injects a `void 0;` bootstrap
|
|
@@ -19,20 +39,50 @@ function attributeRoots(html) {
|
|
|
19
39
|
* drop scripts that end up empty.
|
|
20
40
|
*/
|
|
21
41
|
export function healHtml(html, opts = {}) {
|
|
42
|
+
const roots = markupRoots(html);
|
|
43
|
+
const all = [...html.matchAll(SCRIPT)].map((m) => ({ attrs: m[1] ?? "", body: m[2] ?? "" }));
|
|
44
|
+
const inline = all.filter((b) => !/\bsrc\s*=/i.test(b.attrs) && JS_TYPE.test(b.attrs.match(/\btype\s*=\s*["']?([^"'\s>]+)/i)?.[1] ?? ""));
|
|
45
|
+
// On a closed page every reader of a global is on this page: a script's globals stay only
|
|
46
|
+
// if the markup or another script's surviving code mentions them. Start from the unhealed
|
|
47
|
+
// scripts and re-heal until those mentions stop changing.
|
|
48
|
+
const healOne = (b, others, pageLocal) => {
|
|
49
|
+
const type = b.attrs.match(/\btype\s*=\s*["']?([^"'\s>]+)/i)?.[1] ?? "";
|
|
50
|
+
return healJs(b.body, { ...opts, filename: type === "module" ? "inline.mjs" : "inline.js", externalRoots: new Set([...roots, ...others]), pageLocal });
|
|
51
|
+
};
|
|
52
|
+
const wordsOf = (s) => new Set(s.match(/[A-Za-z_$][\w$]*/g) ?? []);
|
|
53
|
+
const healAll = (pageLocal) => {
|
|
54
|
+
let texts = inline.map((b) => b.body);
|
|
55
|
+
let results = [];
|
|
56
|
+
for (let round = 0; round < 4; round++) {
|
|
57
|
+
const words = texts.map(wordsOf);
|
|
58
|
+
results = inline.map((b, i) => healOne(b, pageLocal ? new Set(words.flatMap((w, j) => (j === i ? [] : [...w]))) : new Set(), pageLocal));
|
|
59
|
+
texts = results.map((r, i) => (r.changed ? r.code : inline[i].body));
|
|
60
|
+
const stable = texts.every((t, i) => {
|
|
61
|
+
const w = wordsOf(t);
|
|
62
|
+
return w.size === words[i].size && [...w].every((x) => words[i].has(x));
|
|
63
|
+
});
|
|
64
|
+
if (!pageLocal || stable)
|
|
65
|
+
return { results, settled: true };
|
|
66
|
+
}
|
|
67
|
+
return { results, settled: false };
|
|
68
|
+
};
|
|
69
|
+
// Assume the page is closed, then check what survived: removed code never runs, so only
|
|
70
|
+
// surviving code that loads more code can open it. Otherwise heal without the assumption.
|
|
71
|
+
let { results, settled } = healAll(!pageIsOpen(html, all));
|
|
72
|
+
if (!settled || results.some((r) => r.changed && LOADS_CODE.test(r.code)))
|
|
73
|
+
results = healAll(false).results;
|
|
22
74
|
const findings = new Findings();
|
|
23
75
|
const warnings = [];
|
|
24
|
-
const roots = attributeRoots(html);
|
|
25
76
|
let scripts = 0;
|
|
26
77
|
let changed = false;
|
|
27
|
-
const code = html.replace(SCRIPT, (whole, attrs
|
|
78
|
+
const code = html.replace(SCRIPT, (whole, attrs) => {
|
|
28
79
|
const a = attrs ?? "";
|
|
29
80
|
if (/\bsrc\s*=/i.test(a))
|
|
30
81
|
return whole;
|
|
31
82
|
const type = a.match(/\btype\s*=\s*["']?([^"'\s>]+)/i)?.[1] ?? "";
|
|
32
83
|
if (!JS_TYPE.test(type))
|
|
33
84
|
return whole;
|
|
34
|
-
scripts
|
|
35
|
-
const r = healJs(body, { ...opts, filename: type === "module" ? "inline.mjs" : "inline.js", externalRoots: roots });
|
|
85
|
+
const r = results[scripts++];
|
|
36
86
|
for (const x of r.findings.list())
|
|
37
87
|
findings.add(x.layer, x.technique, x.detail, x.count);
|
|
38
88
|
warnings.push(...r.warnings.map((w) => `<script #${scripts}>: ${w}`));
|
|
@@ -7,3 +7,8 @@ export declare function isHoneySourceMap(json: string): boolean;
|
|
|
7
7
|
* and inline honey source maps that point analysers at a fictional source tree.
|
|
8
8
|
*/
|
|
9
9
|
export declare function stripScrambledComments(ast: t.File, f: Findings): number;
|
|
10
|
+
/**
|
|
11
|
+
* BLA replaces every original comment, so in a file proven hardened the only comments
|
|
12
|
+
* with information are the ones tools read. Drop the rest, whatever their wording.
|
|
13
|
+
*/
|
|
14
|
+
export declare function stripHardenedComments(ast: t.File, f: Findings): number;
|
|
@@ -42,3 +42,31 @@ export function stripScrambledComments(ast, f) {
|
|
|
42
42
|
f.add("deceive", "honey-sourcemap", "removed inline honey source map (fake enclave source tree)", honey);
|
|
43
43
|
return scrambled + honey;
|
|
44
44
|
}
|
|
45
|
+
/** Comments that tools read: license banners, source maps, compiler/linter/bundler directives. */
|
|
46
|
+
const FUNCTIONAL_COMMENT = /^(?:!|\s*[#@]\s*source(?:Mapping)?URL=|\s*[@#]__(?:PURE|NO_SIDE_EFFECTS)__|\s*@(?:ts-|jsx|license|preserve|vite-ignore|flow|refresh|cc_on)|\s*(?:eslint|prettier-ignore|istanbul|c8|v8 ignore|biome-ignore|webpack[A-Z]|tslint|jshint|globals?\b|exported\b))/;
|
|
47
|
+
/**
|
|
48
|
+
* BLA replaces every original comment, so in a file proven hardened the only comments
|
|
49
|
+
* with information are the ones tools read. Drop the rest, whatever their wording.
|
|
50
|
+
*/
|
|
51
|
+
export function stripHardenedComments(ast, f) {
|
|
52
|
+
let n = 0;
|
|
53
|
+
const keep = (c) => {
|
|
54
|
+
if (FUNCTIONAL_COMMENT.test(c.value))
|
|
55
|
+
return true;
|
|
56
|
+
n++;
|
|
57
|
+
return false;
|
|
58
|
+
};
|
|
59
|
+
ast.comments = ast.comments?.filter(keep) ?? null;
|
|
60
|
+
n = 0;
|
|
61
|
+
t.traverseFast(ast, (node) => {
|
|
62
|
+
if (node.leadingComments)
|
|
63
|
+
node.leadingComments = node.leadingComments.filter(keep);
|
|
64
|
+
if (node.trailingComments)
|
|
65
|
+
node.trailingComments = node.trailingComments.filter(keep);
|
|
66
|
+
if (node.innerComments)
|
|
67
|
+
node.innerComments = node.innerComments.filter(keep);
|
|
68
|
+
});
|
|
69
|
+
if (n)
|
|
70
|
+
f.add("deceive", "hardened-comment", "removed comments from a proven BLA-hardened file (BLA replaces every original comment)", n);
|
|
71
|
+
return n;
|
|
72
|
+
}
|
|
@@ -4,13 +4,14 @@ interface Entry {
|
|
|
4
4
|
anchor: string;
|
|
5
5
|
payload: string;
|
|
6
6
|
}
|
|
7
|
-
/** BLA's corpus digest (cognitive-armor-trigger)
|
|
7
|
+
/** BLA 3.2.1's corpus digest (cognitive-armor-trigger). Reference for tests; the pass evaluates whatever digest the file carries. */
|
|
8
8
|
export declare function corpusDigest(entries: Entry[], seedInput: number, minLength: number): number;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* value it has in the unmodified program, which frees the corpus
|
|
10
|
+
* Anti-strip interlocks: real code reads `typeof D === "function" && D(s) === N`, where D
|
|
11
|
+
* is a pure function over a constant decoy corpus, so deleting the corpus flips the
|
|
12
|
+
* result. Evaluate D(s) statically (pure-eval walks the AST; nothing is executed) and fold
|
|
13
|
+
* the comparison to the value it has in the unmodified program, which frees the corpus.
|
|
14
|
+
* Keyed on that shape and on proof, not on BLA's names or its digest algorithm.
|
|
14
15
|
*/
|
|
15
16
|
export declare function cutCorpusInterlocks(ast: t.File, f: Findings): {
|
|
16
17
|
folded: number;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { t, traverse } from "../ast.js";
|
|
2
|
-
|
|
2
|
+
import { callPure, evalPure } from "../pure-eval.js";
|
|
3
|
+
/** BLA 3.2.1's corpus digest (cognitive-armor-trigger). Reference for tests; the pass evaluates whatever digest the file carries. */
|
|
3
4
|
export function corpusDigest(entries, seedInput, minLength) {
|
|
4
5
|
if (entries.length < minLength)
|
|
5
6
|
return 0;
|
|
@@ -35,87 +36,206 @@ function readCorpus(node) {
|
|
|
35
36
|
return undefined;
|
|
36
37
|
out.push({ anchor, payload });
|
|
37
38
|
}
|
|
38
|
-
return out;
|
|
39
|
+
return out.length ? out : undefined;
|
|
39
40
|
}
|
|
40
|
-
/**
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
41
|
+
/** Methods that return a primitive, so their result cannot alias the binding. */
|
|
42
|
+
const READ_METHODS = new Set(["charCodeAt", "codePointAt", "indexOf", "includes", "startsWith", "endsWith"]);
|
|
43
|
+
const GLOBAL_OBJECTS = new Set(["window", "globalThis", "self", "top", "parent", "frames"]);
|
|
44
|
+
/** Builtins pure-eval models; a program that declares or patches one gets no folds through it. */
|
|
45
|
+
const BUILTINS = new Set(["Math", "String", "Array", "Number", "Object", "NaN", "undefined", "Infinity"]);
|
|
46
|
+
/**
|
|
47
|
+
* True when this reference cannot change what the binding holds: `void x`, `typeof x`,
|
|
48
|
+
* `!x`, `Array.isArray(x)`, or a read chain like `x[i].a.length` used as an operand or
|
|
49
|
+
* a test. A `const` alias (`const e = x[i]`) is followed. Writes, deletes, escapes and
|
|
50
|
+
* unknown method calls are not read-only.
|
|
51
|
+
*/
|
|
52
|
+
function isReadOnlyUse(ref, depth = 0) {
|
|
53
|
+
let p = ref;
|
|
54
|
+
while (p.parentPath?.isMemberExpression() && p.parentPath.node.object === p.node)
|
|
55
|
+
p = p.parentPath;
|
|
56
|
+
const parent = p.parentPath;
|
|
57
|
+
if (!parent)
|
|
58
|
+
return false;
|
|
59
|
+
if (parent.isUnaryExpression())
|
|
60
|
+
return parent.node.operator !== "delete";
|
|
61
|
+
if (parent.isArrayExpression())
|
|
62
|
+
return parent.parentPath?.isUnaryExpression({ operator: "void" }) === true;
|
|
63
|
+
if (parent.isCallExpression()) {
|
|
64
|
+
if (parent.node.callee === p.node)
|
|
65
|
+
return p !== ref && t.isMemberExpression(p.node) && !p.node.computed && t.isIdentifier(p.node.property) && READ_METHODS.has(p.node.property.name);
|
|
66
|
+
const c = parent.node.callee;
|
|
67
|
+
return t.isMemberExpression(c) && t.isIdentifier(c.object, { name: "Array" }) && t.isIdentifier(c.property, { name: "isArray" }) && !parent.scope.getBinding("Array");
|
|
68
|
+
}
|
|
69
|
+
if (parent.isTemplateLiteral())
|
|
70
|
+
return !parent.parentPath?.isTaggedTemplateExpression();
|
|
71
|
+
if (parent.isBinaryExpression())
|
|
72
|
+
return true;
|
|
73
|
+
if ((parent.isIfStatement() || parent.isConditionalExpression() || parent.isWhileStatement() || parent.isForStatement()) && parent.node.test === p.node)
|
|
74
|
+
return true;
|
|
75
|
+
if (parent.isVariableDeclarator() && parent.node.init === p.node && t.isIdentifier(parent.node.id) && depth < 3) {
|
|
76
|
+
const decl = parent.parentPath;
|
|
77
|
+
const b = parent.scope.getBinding(parent.node.id.name);
|
|
78
|
+
return decl?.isVariableDeclaration({ kind: "const" }) === true && !!b && b.referencePaths.every((r) => isReadOnlyUse(r, depth + 1));
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
/** Name D in `typeof D === "function"` (either operand order), else undefined. */
|
|
83
|
+
function typeofFunctionOf(n) {
|
|
84
|
+
if (!t.isBinaryExpression(n) || (n.operator !== "===" && n.operator !== "=="))
|
|
85
|
+
return undefined;
|
|
86
|
+
const [a, b] = t.isUnaryExpression(n.left, { operator: "typeof" }) ? [n.left, n.right] : [n.right, n.left];
|
|
87
|
+
return t.isUnaryExpression(a, { operator: "typeof" }) && t.isIdentifier(a.argument) && t.isStringLiteral(b, { value: "function" }) ? a.argument.name : undefined;
|
|
62
88
|
}
|
|
63
89
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
* value it has in the unmodified program, which frees the corpus
|
|
90
|
+
* Anti-strip interlocks: real code reads `typeof D === "function" && D(s) === N`, where D
|
|
91
|
+
* is a pure function over a constant decoy corpus, so deleting the corpus flips the
|
|
92
|
+
* result. Evaluate D(s) statically (pure-eval walks the AST; nothing is executed) and fold
|
|
93
|
+
* the comparison to the value it has in the unmodified program, which frees the corpus.
|
|
94
|
+
* Keyed on that shape and on proof, not on BLA's names or its digest algorithm.
|
|
68
95
|
*/
|
|
69
96
|
export function cutCorpusInterlocks(ast, f) {
|
|
70
|
-
|
|
71
|
-
|
|
97
|
+
let program;
|
|
98
|
+
traverse(ast, { Program(p) { program = p; p.stop(); } });
|
|
99
|
+
const scope = program.scope;
|
|
100
|
+
const body = program.node.body;
|
|
101
|
+
const topIndex = (p) => {
|
|
102
|
+
let q = p;
|
|
103
|
+
while (q && !q.parentPath?.isProgram())
|
|
104
|
+
q = q.parentPath;
|
|
105
|
+
return q ? body.indexOf(q.node) : -1;
|
|
106
|
+
};
|
|
107
|
+
// Names the file writes through the global object, and whether it patches a builtin.
|
|
108
|
+
const globalWrites = new Set();
|
|
109
|
+
let anyGlobalWrite = false;
|
|
110
|
+
let builtinsPatched = false;
|
|
111
|
+
const rootOf = (n) => (t.isMemberExpression(n) ? rootOf(n.object) : n);
|
|
112
|
+
const noteWrite = (target, p) => {
|
|
113
|
+
if (!t.isMemberExpression(target))
|
|
114
|
+
return;
|
|
115
|
+
const root = rootOf(target);
|
|
116
|
+
if (t.isIdentifier(root) && BUILTINS.has(root.name) && !p.scope.getBinding(root.name))
|
|
117
|
+
builtinsPatched = true;
|
|
118
|
+
const obj = target.object;
|
|
119
|
+
const isGlobal = (t.isIdentifier(obj) && GLOBAL_OBJECTS.has(obj.name) && !p.scope.getBinding(obj.name)) || (t.isThisExpression(obj) && !p.getFunctionParent());
|
|
120
|
+
if (!isGlobal)
|
|
121
|
+
return;
|
|
122
|
+
const key = !target.computed && t.isIdentifier(target.property) ? target.property.name : t.isStringLiteral(target.property) ? target.property.value : undefined;
|
|
123
|
+
if (key === undefined)
|
|
124
|
+
anyGlobalWrite = true;
|
|
125
|
+
else
|
|
126
|
+
globalWrites.add(key);
|
|
127
|
+
};
|
|
72
128
|
traverse(ast, {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (!id || !/^_deriveContainmentCorpusDigest_[0-9a-f]{3,6}$/.test(id))
|
|
129
|
+
AssignmentExpression(p) { noteWrite(p.node.left, p); },
|
|
130
|
+
UpdateExpression(p) { noteWrite(p.node.argument, p); },
|
|
131
|
+
UnaryExpression(p) { if (p.node.operator === "delete")
|
|
132
|
+
noteWrite(p.node.argument, p); },
|
|
133
|
+
CallExpression(p) {
|
|
134
|
+
// Object.defineProperty(window, …), Reflect.set(String.prototype, …) and friends.
|
|
135
|
+
const c = p.node.callee;
|
|
136
|
+
const a = p.node.arguments[0];
|
|
137
|
+
if (!t.isMemberExpression(c) || !t.isIdentifier(c.object, { name: "Object" }) && !t.isIdentifier(c.object, { name: "Reflect" }) || !a)
|
|
83
138
|
return;
|
|
84
|
-
|
|
139
|
+
const root = rootOf(a);
|
|
140
|
+
if (t.isIdentifier(root) && BUILTINS.has(root.name))
|
|
141
|
+
builtinsPatched = true;
|
|
142
|
+
if (t.isIdentifier(root) && GLOBAL_OBJECTS.has(root.name))
|
|
143
|
+
anyGlobalWrite = true;
|
|
85
144
|
},
|
|
86
145
|
});
|
|
146
|
+
const shadowed = (name) => BUILTINS.has(name) && (builtinsPatched || !!scope.getBinding(name));
|
|
147
|
+
// Top-level bindings that provably always hold their literal initializer.
|
|
148
|
+
const constant = new Map();
|
|
149
|
+
const globals = (name) => {
|
|
150
|
+
if (constant.has(name))
|
|
151
|
+
return constant.get(name) ?? undefined;
|
|
152
|
+
const b = scope.getBinding(name);
|
|
153
|
+
let out;
|
|
154
|
+
// Only a top-level `const`: `var`/`let` can be rewritten from another script or through the global object.
|
|
155
|
+
if (b && b.kind === "const" && b.path.isVariableDeclarator() && b.path.parentPath?.parentPath?.isProgram() && b.path.node.init && t.isIdentifier(b.path.node.id) && b.referencePaths.every((r) => isReadOnlyUse(r))) {
|
|
156
|
+
const r = evalPure(b.path.node.init);
|
|
157
|
+
if (r.ok)
|
|
158
|
+
out = { value: r.value };
|
|
159
|
+
}
|
|
160
|
+
constant.set(name, out ?? null);
|
|
161
|
+
return out;
|
|
162
|
+
};
|
|
163
|
+
const topFunction = (name) => {
|
|
164
|
+
const b = scope.getBinding(name);
|
|
165
|
+
return b && b.kind === "hoisted" && b.constantViolations.length === 0 && b.path.isFunctionDeclaration() && b.path.parentPath?.isProgram() ? b.path.node : undefined;
|
|
166
|
+
};
|
|
87
167
|
let folded = 0;
|
|
88
168
|
let unresolved = 0;
|
|
169
|
+
const foldedFns = new Set();
|
|
170
|
+
const corpusInterlocks = new Set();
|
|
89
171
|
traverse(ast, {
|
|
90
|
-
|
|
91
|
-
const {
|
|
92
|
-
if (
|
|
172
|
+
BinaryExpression(p) {
|
|
173
|
+
const { operator, left, right } = p.node;
|
|
174
|
+
if (operator !== "===" && operator !== "!==" && operator !== "==" && operator !== "!=")
|
|
93
175
|
return;
|
|
94
|
-
const
|
|
95
|
-
|
|
176
|
+
const call = t.isCallExpression(left) ? left : t.isCallExpression(right) ? right : undefined;
|
|
177
|
+
const lit = call === left ? right : left;
|
|
178
|
+
if (!call || !(t.isNumericLiteral(lit) || t.isStringLiteral(lit)) || !t.isIdentifier(call.callee))
|
|
96
179
|
return;
|
|
97
|
-
const
|
|
98
|
-
if (
|
|
180
|
+
const name = call.callee.name;
|
|
181
|
+
if (p.scope.getBinding(name)?.scope !== scope)
|
|
99
182
|
return;
|
|
100
|
-
const
|
|
101
|
-
if (!
|
|
183
|
+
const fn = topFunction(name);
|
|
184
|
+
if (!fn)
|
|
102
185
|
return;
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
186
|
+
const interlock = t.isLogicalExpression(p.parent, { operator: "&&" }) && p.parent.right === p.node && typeofFunctionOf(p.parent.left) === name;
|
|
187
|
+
const args = [];
|
|
188
|
+
for (const a of call.arguments) {
|
|
189
|
+
const r = t.isExpression(a) ? evalPure(a) : { ok: false };
|
|
190
|
+
if (!r.ok) {
|
|
191
|
+
if (interlock)
|
|
192
|
+
unresolved++;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
args.push(r.value);
|
|
196
|
+
}
|
|
197
|
+
const reads = new Map();
|
|
198
|
+
const r = anyGlobalWrite || globalWrites.has(name)
|
|
199
|
+
? { ok: false }
|
|
200
|
+
: callPure(fn, args, (g) => { const v = globals(g); if (v)
|
|
201
|
+
reads.set(g, v.value); return v; }, { shadowed });
|
|
202
|
+
// Every constant read must be initialised before the statement that makes the call (no TDZ, no reorder),
|
|
203
|
+
// and none may be rewritten through the global object.
|
|
204
|
+
const site = topIndex(p);
|
|
205
|
+
const ordered = [...reads.keys()].every((g) => !globalWrites.has(g) && topIndex(scope.getBinding(g).path) < site);
|
|
206
|
+
if (!r.ok || !ordered || (typeof r.value === "object" && r.value !== null)) {
|
|
207
|
+
if (interlock)
|
|
208
|
+
unresolved++;
|
|
108
209
|
return;
|
|
109
210
|
}
|
|
110
|
-
|
|
111
|
-
const
|
|
112
|
-
|
|
211
|
+
const v = r.value;
|
|
212
|
+
const want = lit.value;
|
|
213
|
+
const holds = operator === "===" ? v === want : operator === "!==" ? v !== want : operator === "==" ? v == want : v != want;
|
|
214
|
+
p.replaceWith(t.booleanLiteral(holds));
|
|
215
|
+
if (interlock)
|
|
216
|
+
foldedFns.add(name);
|
|
217
|
+
// Evidence of an anti-strip interlock only when the check hashes a constant corpus.
|
|
218
|
+
if (interlock && [...reads.values()].some((v) => Array.isArray(v) && v.length > 0))
|
|
219
|
+
corpusInterlocks.add(name);
|
|
113
220
|
folded++;
|
|
114
221
|
},
|
|
115
222
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
223
|
+
// `typeof D === "function" && <folded>`: D is a constant top-level function, so the guard holds.
|
|
224
|
+
traverse(ast, {
|
|
225
|
+
LogicalExpression(p) {
|
|
226
|
+
if (p.node.operator !== "&&" || !t.isBooleanLiteral(p.node.right))
|
|
227
|
+
return;
|
|
228
|
+
const name = typeofFunctionOf(p.node.left);
|
|
229
|
+
if (name && foldedFns.has(name))
|
|
230
|
+
p.replaceWith(p.node.right);
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
let corpora = 0;
|
|
234
|
+
traverse(ast, { VariableDeclarator(p) { if (readCorpus(p.node.init))
|
|
235
|
+
corpora++; } });
|
|
236
|
+
if (corpusInterlocks.size)
|
|
237
|
+
f.add("entangle", "corpus-digest-interlock", "evaluated the anti-strip interlock statically and folded it to its unmodified value", corpusInterlocks.size);
|
|
238
|
+
if (corpora)
|
|
239
|
+
f.add("prompt", "interlock-safety-corpus", "LLM-directed decoy corpus (vectorId / classifierDomain / triggerPayload)", corpora);
|
|
120
240
|
return { folded, unresolved };
|
|
121
241
|
}
|
|
@@ -18,9 +18,13 @@ function isSealFunction(fn) {
|
|
|
18
18
|
if (t.isIdentifier(n, { name: "__blaSidecarDigest" }))
|
|
19
19
|
sidecar = true;
|
|
20
20
|
});
|
|
21
|
-
// A user's own integrity check has the first two; only BLA's has the markers too.
|
|
22
21
|
// The soft-fold sidecar preamble never throws (sidecar-entangle.ts:320-332) but is still BLA's.
|
|
23
|
-
|
|
22
|
+
if (hash && (err || sidecar) && marker)
|
|
23
|
+
return true;
|
|
24
|
+
// Without markers: a check that hashes the file's own bytes and throws on a mismatch fails on
|
|
25
|
+
// any healed output, and BLA's rewrite would already have broken a user's own such check. It
|
|
26
|
+
// goes only if it does nothing else (no writes outside itself, no side-effecting calls).
|
|
27
|
+
return hash && err && readsOwnFile(fn) && onlyChecks(fn);
|
|
24
28
|
}
|
|
25
29
|
/**
|
|
26
30
|
* BLA's self-seal / sidecar entanglement: the file hashes its own bytes and
|
|
@@ -96,3 +100,77 @@ function collectSiblings(fn, out) {
|
|
|
96
100
|
}
|
|
97
101
|
});
|
|
98
102
|
}
|
|
103
|
+
const unbound = (n, name) => t.isIdentifier(n, { name });
|
|
104
|
+
const isImportMetaUrl = (n) => t.isMemberExpression(n) && t.isMetaProperty(n.object) && t.isIdentifier(n.property, { name: "url" });
|
|
105
|
+
/** `__filename`, `import.meta.url`, `fileURLToPath(import.meta.url)`, or a guard choosing one of them or null. */
|
|
106
|
+
function isOwnPath(n) {
|
|
107
|
+
if (!n)
|
|
108
|
+
return false;
|
|
109
|
+
if (unbound(n, "__filename") || isImportMetaUrl(n))
|
|
110
|
+
return true;
|
|
111
|
+
if (t.isCallExpression(n) && n.arguments.length === 1 && isImportMetaUrl(n.arguments[0]))
|
|
112
|
+
return true;
|
|
113
|
+
if (t.isNewExpression(n) && t.isIdentifier(n.callee, { name: "URL" }) && n.arguments.length === 1 && isImportMetaUrl(n.arguments[0]))
|
|
114
|
+
return true;
|
|
115
|
+
if (t.isConditionalExpression(n))
|
|
116
|
+
return [n.consequent, n.alternate].every((b) => t.isNullLiteral(b) || isOwnPath(b));
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
/** Some `readFileSync` in the function reads the file's own path (directly or through a local initialised to it). */
|
|
120
|
+
function readsOwnFile(fn) {
|
|
121
|
+
const inits = new Map();
|
|
122
|
+
t.traverseFast(fn.body, (n) => { if (t.isVariableDeclarator(n) && t.isIdentifier(n.id))
|
|
123
|
+
inits.set(n.id.name, n.init); });
|
|
124
|
+
let own = false;
|
|
125
|
+
t.traverseFast(fn.body, (n) => {
|
|
126
|
+
if (!t.isCallExpression(n))
|
|
127
|
+
return;
|
|
128
|
+
const c = n.callee;
|
|
129
|
+
const name = t.isIdentifier(c) ? c.name : t.isMemberExpression(c) && t.isIdentifier(c.property) ? c.property.name : "";
|
|
130
|
+
if (name !== "readFileSync")
|
|
131
|
+
return;
|
|
132
|
+
const a = n.arguments[0];
|
|
133
|
+
if (isOwnPath(a) || (t.isIdentifier(a) && isOwnPath(inits.get(a.name))))
|
|
134
|
+
own = true;
|
|
135
|
+
});
|
|
136
|
+
return own;
|
|
137
|
+
}
|
|
138
|
+
/** Calls that act on the world rather than compute a digest. */
|
|
139
|
+
const EFFECT_CALL = /^(?:write|append|unlink|rm|mkdir|rename|copyFile|chmod|chown|symlink|send|postMessage|exit|abort|kill|spawn|exec|fork|fetch|request|connect|listen|setTimeout|setInterval|setImmediate|queueMicrotask|addEventListener|removeEventListener|dispatchEvent|emit|on|once|defineProperty|defineProperties|assign|setPrototypeOf|push|unshift|splice|log|warn|error|info|debug|open|close|eval|Function|require\.cache)/;
|
|
140
|
+
/** The function only computes: it writes nothing but its own locals and calls nothing with effects. */
|
|
141
|
+
function onlyChecks(fn) {
|
|
142
|
+
const locals = new Set();
|
|
143
|
+
const collect = (n) => t.traverseFast(n, (x) => { if (t.isIdentifier(x))
|
|
144
|
+
locals.add(x.name); });
|
|
145
|
+
for (const p of fn.params)
|
|
146
|
+
collect(p);
|
|
147
|
+
t.traverseFast(fn.body, (n) => {
|
|
148
|
+
if (t.isVariableDeclarator(n))
|
|
149
|
+
collect(n.id);
|
|
150
|
+
if (t.isCatchClause(n) && n.param)
|
|
151
|
+
collect(n.param);
|
|
152
|
+
if (t.isFunctionDeclaration(n) && n.id)
|
|
153
|
+
locals.add(n.id.name);
|
|
154
|
+
});
|
|
155
|
+
const root = (n) => (t.isMemberExpression(n) ? root(n.object) : n);
|
|
156
|
+
let ok = true;
|
|
157
|
+
t.traverseFast(fn.body, (n) => {
|
|
158
|
+
if (!ok)
|
|
159
|
+
return;
|
|
160
|
+
const target = t.isAssignmentExpression(n) ? n.left : t.isUpdateExpression(n) ? n.argument : t.isUnaryExpression(n, { operator: "delete" }) ? n.argument : undefined;
|
|
161
|
+
if (target) {
|
|
162
|
+
const r = root(target);
|
|
163
|
+
if (!t.isIdentifier(r) || !locals.has(r.name) || t.isMemberExpression(target))
|
|
164
|
+
ok = t.isIdentifier(target) && locals.has(target.name);
|
|
165
|
+
}
|
|
166
|
+
if (t.isCallExpression(n) || t.isNewExpression(n)) {
|
|
167
|
+
const c = n.callee;
|
|
168
|
+
const name = t.isIdentifier(c) ? c.name : t.isMemberExpression(c) && t.isIdentifier(c.property) ? c.property.name : "";
|
|
169
|
+
if (EFFECT_CALL.test(name))
|
|
170
|
+
ok = false;
|
|
171
|
+
}
|
|
172
|
+
if (t.isFunction(n) && n !== fn)
|
|
173
|
+
ok = false; // a callback could be registered and run later
|
|
174
|
+
});
|
|
175
|
+
return ok;
|
|
176
|
+
}
|
|
@@ -11,6 +11,12 @@ export interface ShakeOptions {
|
|
|
11
11
|
blaHardened?: boolean;
|
|
12
12
|
/** The program is a function body (a bundled module): its top-level names are closure locals. */
|
|
13
13
|
closure?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* The program is an inline script of a page with no external scripts: its globals
|
|
16
|
+
* are visible only to that page's own scripts and attributes, which the caller
|
|
17
|
+
* passes in `externalRoots`.
|
|
18
|
+
*/
|
|
19
|
+
pageLocal?: boolean;
|
|
14
20
|
}
|
|
15
21
|
/**
|
|
16
22
|
* Names code can reach through strings: `eval`, `new Function`, string timers.
|