unpeek 4.1.0 → 4.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 +7 -7
- package/README.tr.md +7 -7
- package/dist/adapters/esbuild.cjs +5 -5
- package/dist/adapters/esbuild.js +3 -3
- package/dist/adapters/next/adapter.cjs +5 -5
- package/dist/adapters/next/adapter.js +4 -4
- package/dist/adapters/next/index.cjs +5 -5
- package/dist/adapters/next/index.js +4 -4
- package/dist/adapters/rollup.cjs +5 -5
- package/dist/adapters/rollup.js +3 -3
- package/dist/adapters/rspack.cjs +5 -5
- package/dist/adapters/rspack.js +3 -3
- package/dist/adapters/vite.cjs +5 -5
- package/dist/adapters/vite.js +3 -3
- package/dist/adapters/webpack.cjs +5 -5
- package/dist/adapters/webpack.js +3 -3
- package/dist/browser.cjs +2 -2
- package/dist/browser.d.cts +3 -2
- package/dist/browser.d.ts +3 -2
- package/dist/browser.js +1 -1
- package/dist/{chunk-Q4DFRMAV.cjs → chunk-4IRIMLT3.cjs} +16 -16
- package/dist/{chunk-AD6UFBPA.cjs → chunk-4XSFPX2Z.cjs} +367 -8
- package/dist/{chunk-WYW5UOBE.cjs → chunk-7F75S3GQ.cjs} +60 -36
- package/dist/{chunk-HV47WJIR.js → chunk-EASD7C2B.js} +3 -3
- package/dist/{chunk-STTUFUDC.js → chunk-GKWTP4Z5.js} +30 -6
- package/dist/{chunk-VLL4THTG.cjs → chunk-OR5M3SN2.cjs} +2432 -636
- package/dist/{chunk-32GLSJAW.js → chunk-RDP4MTHK.js} +359 -0
- package/dist/{chunk-6KLUNEKE.js → chunk-RM3V7KJA.js} +2298 -502
- package/dist/{chunk-FLZ4FSP5.cjs → chunk-SBRU7GSX.cjs} +8 -8
- package/dist/{chunk-TOUYQ4N5.js → chunk-XEYMZUP5.js} +2 -2
- package/dist/cli/index.js +2781 -469
- package/dist/index.cjs +169 -29
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +151 -11
- package/dist/plugins/unplugin.cjs +4 -4
- package/dist/plugins/unplugin.js +3 -3
- package/dist/{types-BZqvBhBI.d.ts → types-BDpFfokR.d.ts} +2 -1
- package/dist/{types-E4ugA4Oz.d.cts → types-WIoud53X.d.cts} +2 -1
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -11,10 +11,18 @@ import path2 from "path";
|
|
|
11
11
|
import fs from "fs";
|
|
12
12
|
import path from "path";
|
|
13
13
|
var SUPPORTED_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".mjs", ".cjs"]);
|
|
14
|
+
var MAX_HTML_FILES = 64;
|
|
15
|
+
var MAX_SCAN_BYTES = 512 * 1024;
|
|
16
|
+
var MAX_JS_GRAPH_FILES = 1024;
|
|
14
17
|
var EXCLUDED_DIR_NAMES = /* @__PURE__ */ new Set([
|
|
15
18
|
"node_modules",
|
|
16
19
|
".unpeek-maps"
|
|
17
20
|
]);
|
|
21
|
+
var HTML_WALK_EXCLUDED_DIR_NAMES = /* @__PURE__ */ new Set([
|
|
22
|
+
"node_modules",
|
|
23
|
+
".unpeek-maps",
|
|
24
|
+
".git"
|
|
25
|
+
]);
|
|
18
26
|
var WORKER_OR_SW_BASENAME_REGEX = /^(?:sw|service-worker|workbox-[^/]+)\.(?:js|mjs|cjs)$|\.worker(?:[.-][^/]+)*\.(?:js|mjs|cjs)$/i;
|
|
19
27
|
function isWorkerOrServiceWorkerFile(filePath, rootDir) {
|
|
20
28
|
const normalized = filePath.replace(/\\/g, "/").split("?")[0];
|
|
@@ -377,6 +385,356 @@ function scanClientBundles(outDir, config) {
|
|
|
377
385
|
walk9(resolvedOutDir);
|
|
378
386
|
return results.sort();
|
|
379
387
|
}
|
|
388
|
+
var SCRIPT_TAG_REGEX = /<script\b([^>]*)>([\s\S]*?)<\/script>/gi;
|
|
389
|
+
var LINK_TAG_REGEX = /<link\b([^>]*)>/gi;
|
|
390
|
+
var HTML_COMMENT_REGEX = /<!--[\s\S]*?-->/g;
|
|
391
|
+
var ATTR_SRC_REGEX = /\bsrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
|
|
392
|
+
var ATTR_HREF_REGEX = /\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
|
|
393
|
+
var ATTR_REL_REGEX = /\brel\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
|
|
394
|
+
var ATTR_AS_REGEX = /\bas\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
|
|
395
|
+
var ATTR_TYPE_REGEX = /\btype\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i;
|
|
396
|
+
var STATIC_FROM_IMPORT_REGEX = /\bfrom\s*(['"`])([^'"`\r\n${}]+)\1/g;
|
|
397
|
+
var SIDE_EFFECT_IMPORT_REGEX = /(?:^|[;}\n])\s*import\s*(['"`])([^'"`\r\n${}]+)\1/g;
|
|
398
|
+
var DYNAMIC_IMPORT_REGEX = /\bimport\s*\(\s*(?:\/\*[^*]{0,120}\*\/\s*)?(['"`])([^'"`\r\n${}]+)\1/g;
|
|
399
|
+
var WORKER_META_URL_REGEX = /\bnew\s+(?:(?:window|self|globalThis)\s*\.\s*)?(?:Shared)?Worker\s*\(\s*new\s+URL\s*\(\s*(['"`])([^'"`\r\n${}]+)\1\s*,\s*import\.meta\.url\s*\)/g;
|
|
400
|
+
var WORKER_DIRECT_URL_REGEX = /\bnew\s+(?:(?:window|self|globalThis)\s*\.\s*)?(?:Shared)?Worker\s*\(\s*(['"`])([^'"`\r\n${}]+)\1/g;
|
|
401
|
+
function getHtmlAttr(attrs, regex) {
|
|
402
|
+
const match = attrs.match(regex);
|
|
403
|
+
if (!match) return void 0;
|
|
404
|
+
return (match[1] ?? match[2] ?? match[3] ?? "").trim();
|
|
405
|
+
}
|
|
406
|
+
function readBoundedText(filePath, maxBytes) {
|
|
407
|
+
let fd;
|
|
408
|
+
try {
|
|
409
|
+
fd = fs.openSync(filePath, "r");
|
|
410
|
+
const stat = fs.fstatSync(fd);
|
|
411
|
+
const toRead = Math.min(stat.size, maxBytes);
|
|
412
|
+
if (toRead <= 0) return "";
|
|
413
|
+
const buf = Buffer.allocUnsafe(toRead);
|
|
414
|
+
let offset = 0;
|
|
415
|
+
while (offset < toRead) {
|
|
416
|
+
const bytesRead = fs.readSync(fd, buf, offset, toRead - offset, offset);
|
|
417
|
+
if (bytesRead <= 0) break;
|
|
418
|
+
offset += bytesRead;
|
|
419
|
+
}
|
|
420
|
+
return buf.subarray(0, offset).toString("utf8");
|
|
421
|
+
} catch {
|
|
422
|
+
return "";
|
|
423
|
+
} finally {
|
|
424
|
+
if (fd !== void 0) {
|
|
425
|
+
try {
|
|
426
|
+
fs.closeSync(fd);
|
|
427
|
+
} catch {
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
function buildServerDirPrefixes(config) {
|
|
433
|
+
const prefixes = [];
|
|
434
|
+
if (config?.next?.distDir) {
|
|
435
|
+
prefixes.push(
|
|
436
|
+
path.join(config.next.distDir, "server"),
|
|
437
|
+
path.join(config.next.distDir, "ssr")
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
if (config?.vite?.outDir) {
|
|
441
|
+
prefixes.push(path.join(config.vite.outDir, "server"));
|
|
442
|
+
}
|
|
443
|
+
return prefixes;
|
|
444
|
+
}
|
|
445
|
+
function getAllowedExtensions(config) {
|
|
446
|
+
return config?.extensions && config.extensions.length > 0 ? new Set(
|
|
447
|
+
config.extensions.map((e) => e.startsWith(".") ? e.toLowerCase() : `.${e.toLowerCase()}`)
|
|
448
|
+
) : SUPPORTED_EXTENSIONS;
|
|
449
|
+
}
|
|
450
|
+
function isExcludedPathSegment(fullPath) {
|
|
451
|
+
const normalized = fullPath.replace(/\\/g, "/");
|
|
452
|
+
return normalized.includes("/.git/") || normalized.endsWith("/.git") || normalized.includes("/node_modules/") || normalized.endsWith("/node_modules") || normalized.includes("/.unpeek-maps/") || normalized.endsWith("/.unpeek-maps");
|
|
453
|
+
}
|
|
454
|
+
function resolveOutDirJsTarget(rawSpecifier, baseDir, resolvedOutDir, allowedExtensions, serverDirPrefixes, fallbackRootDir) {
|
|
455
|
+
const trimmed = rawSpecifier.trim();
|
|
456
|
+
if (!trimmed || trimmed.startsWith("#") || /^(?:https?:|data:|blob:|javascript:|mailto:|\/\/)/i.test(trimmed)) {
|
|
457
|
+
return void 0;
|
|
458
|
+
}
|
|
459
|
+
const withoutQuery = trimmed.split("?")[0].split("#")[0];
|
|
460
|
+
let cleanSpec = withoutQuery;
|
|
461
|
+
try {
|
|
462
|
+
cleanSpec = decodeURIComponent(withoutQuery);
|
|
463
|
+
} catch {
|
|
464
|
+
}
|
|
465
|
+
if (!cleanSpec) return void 0;
|
|
466
|
+
const candidates = [];
|
|
467
|
+
if (cleanSpec.startsWith("/")) {
|
|
468
|
+
const stripped = cleanSpec.replace(/^\/+/, "");
|
|
469
|
+
candidates.push(path.resolve(resolvedOutDir, stripped));
|
|
470
|
+
if (fallbackRootDir && fallbackRootDir !== resolvedOutDir) {
|
|
471
|
+
candidates.push(path.resolve(fallbackRootDir, stripped));
|
|
472
|
+
}
|
|
473
|
+
} else {
|
|
474
|
+
candidates.push(path.resolve(baseDir, cleanSpec));
|
|
475
|
+
}
|
|
476
|
+
for (const baseCandidate of candidates) {
|
|
477
|
+
const ext = path.extname(baseCandidate).toLowerCase();
|
|
478
|
+
const withExts = ext ? [baseCandidate] : Array.from(allowedExtensions).map((e) => `${baseCandidate}${e}`);
|
|
479
|
+
for (const candidate of withExts) {
|
|
480
|
+
const candExt = path.extname(candidate).toLowerCase();
|
|
481
|
+
if (!allowedExtensions.has(candExt)) continue;
|
|
482
|
+
if (isExcludedPathSegment(candidate)) continue;
|
|
483
|
+
if (hasNextServerSegment(candidate, resolvedOutDir, serverDirPrefixes)) continue;
|
|
484
|
+
const rel = path.relative(resolvedOutDir, candidate).replace(/\\/g, "/");
|
|
485
|
+
if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) continue;
|
|
486
|
+
try {
|
|
487
|
+
if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
|
|
488
|
+
return { absPath: candidate, relPath: rel };
|
|
489
|
+
}
|
|
490
|
+
} catch {
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return void 0;
|
|
495
|
+
}
|
|
496
|
+
function extractRelativeSpecifiers(jsText, allowBareRelativeFromRoot) {
|
|
497
|
+
const imports = [];
|
|
498
|
+
const workers = [];
|
|
499
|
+
const collectMatches = (regex, target, requireDotPrefix) => {
|
|
500
|
+
regex.lastIndex = 0;
|
|
501
|
+
let m;
|
|
502
|
+
while ((m = regex.exec(jsText)) !== null) {
|
|
503
|
+
const spec = m[2];
|
|
504
|
+
if (!spec) continue;
|
|
505
|
+
if (spec.startsWith("./") || spec.startsWith("../") || !requireDotPrefix && !/^(?:https?:|data:|blob:)/i.test(spec)) {
|
|
506
|
+
target.push(spec);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
collectMatches(STATIC_FROM_IMPORT_REGEX, imports, true);
|
|
511
|
+
collectMatches(SIDE_EFFECT_IMPORT_REGEX, imports, true);
|
|
512
|
+
collectMatches(DYNAMIC_IMPORT_REGEX, imports, true);
|
|
513
|
+
collectMatches(WORKER_META_URL_REGEX, workers, !allowBareRelativeFromRoot);
|
|
514
|
+
collectMatches(WORKER_DIRECT_URL_REGEX, workers, true);
|
|
515
|
+
return { imports, workers };
|
|
516
|
+
}
|
|
517
|
+
function discoverHtmlEntryGraph(outDir, config, extraHtmlDirs = []) {
|
|
518
|
+
const htmlEntryFiles = /* @__PURE__ */ new Set();
|
|
519
|
+
const preloadedFiles = /* @__PURE__ */ new Set();
|
|
520
|
+
const workerEntryFiles = /* @__PURE__ */ new Set();
|
|
521
|
+
const reachableFiles = /* @__PURE__ */ new Set();
|
|
522
|
+
const emptyResult = () => ({
|
|
523
|
+
htmlEntryFiles,
|
|
524
|
+
preloadedFiles,
|
|
525
|
+
workerEntryFiles,
|
|
526
|
+
reachableFiles,
|
|
527
|
+
entryFiles: /* @__PURE__ */ new Set(),
|
|
528
|
+
scannedHtmlFiles: 0,
|
|
529
|
+
scannedJsFiles: 0
|
|
530
|
+
});
|
|
531
|
+
const resolvedOutDir = path.resolve(outDir);
|
|
532
|
+
if (!fs.existsSync(resolvedOutDir)) {
|
|
533
|
+
return emptyResult();
|
|
534
|
+
}
|
|
535
|
+
const serverDirPrefixes = buildServerDirPrefixes(config);
|
|
536
|
+
const allowedExtensions = getAllowedExtensions(config);
|
|
537
|
+
const htmlFiles = [];
|
|
538
|
+
function walkHtml(currentDir) {
|
|
539
|
+
if (htmlFiles.length >= MAX_HTML_FILES) return;
|
|
540
|
+
if (hasNextServerSegment(currentDir, resolvedOutDir, serverDirPrefixes)) return;
|
|
541
|
+
let entries;
|
|
542
|
+
try {
|
|
543
|
+
entries = fs.readdirSync(currentDir, { withFileTypes: true });
|
|
544
|
+
} catch {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
548
|
+
for (const entry of entries) {
|
|
549
|
+
if (htmlFiles.length >= MAX_HTML_FILES) break;
|
|
550
|
+
const fullPath = path.join(currentDir, entry.name);
|
|
551
|
+
if (entry.isDirectory()) {
|
|
552
|
+
if (HTML_WALK_EXCLUDED_DIR_NAMES.has(entry.name.toLowerCase())) continue;
|
|
553
|
+
if (hasNextServerSegment(fullPath, resolvedOutDir, serverDirPrefixes)) continue;
|
|
554
|
+
walkHtml(fullPath);
|
|
555
|
+
} else if (entry.isFile()) {
|
|
556
|
+
if (hasNextServerSegment(fullPath, resolvedOutDir, serverDirPrefixes)) continue;
|
|
557
|
+
if (path.extname(entry.name).toLowerCase() === ".html") {
|
|
558
|
+
htmlFiles.push({ filePath: fullPath, rootDir: resolvedOutDir });
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
walkHtml(resolvedOutDir);
|
|
564
|
+
if (htmlFiles.length === 0 && extraHtmlDirs.length > 0) {
|
|
565
|
+
for (const extraDir of extraHtmlDirs) {
|
|
566
|
+
if (htmlFiles.length >= MAX_HTML_FILES) break;
|
|
567
|
+
const resolvedExtra = path.resolve(extraDir);
|
|
568
|
+
if (!fs.existsSync(resolvedExtra) || isExcludedPathSegment(resolvedExtra) || hasNextServerSegment(resolvedExtra, resolvedExtra, serverDirPrefixes)) {
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
let entries;
|
|
572
|
+
try {
|
|
573
|
+
entries = fs.readdirSync(resolvedExtra, { withFileTypes: true });
|
|
574
|
+
} catch {
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
578
|
+
for (const entry of entries) {
|
|
579
|
+
if (htmlFiles.length >= MAX_HTML_FILES) break;
|
|
580
|
+
if (entry.isFile() && path.extname(entry.name).toLowerCase() === ".html") {
|
|
581
|
+
htmlFiles.push({ filePath: path.join(resolvedExtra, entry.name), rootDir: resolvedExtra });
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
const jsQueue = [];
|
|
587
|
+
const queuedOrVisitedJs = /* @__PURE__ */ new Set();
|
|
588
|
+
const enqueueJs = (resolved) => {
|
|
589
|
+
if (!resolved) return;
|
|
590
|
+
reachableFiles.add(resolved.relPath);
|
|
591
|
+
if (!queuedOrVisitedJs.has(resolved.relPath) && queuedOrVisitedJs.size < MAX_JS_GRAPH_FILES) {
|
|
592
|
+
queuedOrVisitedJs.add(resolved.relPath);
|
|
593
|
+
jsQueue.push(resolved);
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
for (const { filePath: htmlPath, rootDir: htmlRootDir } of htmlFiles) {
|
|
597
|
+
const rawHtml = readBoundedText(htmlPath, MAX_SCAN_BYTES);
|
|
598
|
+
if (!rawHtml) continue;
|
|
599
|
+
const htmlText = rawHtml.replace(HTML_COMMENT_REGEX, "");
|
|
600
|
+
const htmlDir = path.dirname(htmlPath);
|
|
601
|
+
SCRIPT_TAG_REGEX.lastIndex = 0;
|
|
602
|
+
let scriptMatch;
|
|
603
|
+
while ((scriptMatch = SCRIPT_TAG_REGEX.exec(htmlText)) !== null) {
|
|
604
|
+
const attrs = scriptMatch[1] ?? "";
|
|
605
|
+
const body = scriptMatch[2] ?? "";
|
|
606
|
+
const scriptType = (getHtmlAttr(attrs, ATTR_TYPE_REGEX) ?? "").toLowerCase();
|
|
607
|
+
const isJsScript = !scriptType || scriptType === "module" || scriptType.includes("javascript") || scriptType.includes("ecmascript");
|
|
608
|
+
if (!isJsScript) continue;
|
|
609
|
+
const src = getHtmlAttr(attrs, ATTR_SRC_REGEX);
|
|
610
|
+
if (src) {
|
|
611
|
+
const resolved = resolveOutDirJsTarget(
|
|
612
|
+
src,
|
|
613
|
+
htmlDir,
|
|
614
|
+
resolvedOutDir,
|
|
615
|
+
allowedExtensions,
|
|
616
|
+
serverDirPrefixes,
|
|
617
|
+
htmlRootDir
|
|
618
|
+
);
|
|
619
|
+
if (resolved) {
|
|
620
|
+
htmlEntryFiles.add(resolved.relPath);
|
|
621
|
+
enqueueJs(resolved);
|
|
622
|
+
}
|
|
623
|
+
} else if (body.trim()) {
|
|
624
|
+
const { imports, workers } = extractRelativeSpecifiers(body, false);
|
|
625
|
+
for (const imp of imports) {
|
|
626
|
+
const resolved = resolveOutDirJsTarget(
|
|
627
|
+
imp,
|
|
628
|
+
htmlDir,
|
|
629
|
+
resolvedOutDir,
|
|
630
|
+
allowedExtensions,
|
|
631
|
+
serverDirPrefixes,
|
|
632
|
+
htmlRootDir
|
|
633
|
+
);
|
|
634
|
+
if (resolved) {
|
|
635
|
+
htmlEntryFiles.add(resolved.relPath);
|
|
636
|
+
enqueueJs(resolved);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
for (const workerSpec of workers) {
|
|
640
|
+
const resolved = resolveOutDirJsTarget(
|
|
641
|
+
workerSpec,
|
|
642
|
+
htmlDir,
|
|
643
|
+
resolvedOutDir,
|
|
644
|
+
allowedExtensions,
|
|
645
|
+
serverDirPrefixes,
|
|
646
|
+
htmlRootDir
|
|
647
|
+
);
|
|
648
|
+
if (resolved) {
|
|
649
|
+
workerEntryFiles.add(resolved.relPath);
|
|
650
|
+
enqueueJs(resolved);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
LINK_TAG_REGEX.lastIndex = 0;
|
|
656
|
+
let linkMatch;
|
|
657
|
+
while ((linkMatch = LINK_TAG_REGEX.exec(htmlText)) !== null) {
|
|
658
|
+
const attrs = linkMatch[1] ?? "";
|
|
659
|
+
const relAttr = (getHtmlAttr(attrs, ATTR_REL_REGEX) ?? "").toLowerCase();
|
|
660
|
+
if (!relAttr) continue;
|
|
661
|
+
const relTokens = relAttr.split(/\s+/);
|
|
662
|
+
const isModulePreload = relTokens.includes("modulepreload");
|
|
663
|
+
const isPreload = relTokens.includes("preload");
|
|
664
|
+
if (!isModulePreload && !isPreload) continue;
|
|
665
|
+
if (isPreload && !isModulePreload) {
|
|
666
|
+
const asAttr = (getHtmlAttr(attrs, ATTR_AS_REGEX) ?? "").toLowerCase();
|
|
667
|
+
if (asAttr && asAttr !== "script" && asAttr !== "worker") {
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
const href = getHtmlAttr(attrs, ATTR_HREF_REGEX);
|
|
672
|
+
if (!href) continue;
|
|
673
|
+
const resolved = resolveOutDirJsTarget(
|
|
674
|
+
href,
|
|
675
|
+
htmlDir,
|
|
676
|
+
resolvedOutDir,
|
|
677
|
+
allowedExtensions,
|
|
678
|
+
serverDirPrefixes,
|
|
679
|
+
htmlRootDir
|
|
680
|
+
);
|
|
681
|
+
if (resolved) {
|
|
682
|
+
preloadedFiles.add(resolved.relPath);
|
|
683
|
+
enqueueJs(resolved);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
let scannedJsFiles = 0;
|
|
688
|
+
let head = 0;
|
|
689
|
+
while (head < jsQueue.length && scannedJsFiles < MAX_JS_GRAPH_FILES) {
|
|
690
|
+
const current = jsQueue[head++];
|
|
691
|
+
const jsText = readBoundedText(current.absPath, MAX_SCAN_BYTES);
|
|
692
|
+
scannedJsFiles++;
|
|
693
|
+
if (!jsText) continue;
|
|
694
|
+
const currentDir = path.dirname(current.absPath);
|
|
695
|
+
const { imports, workers } = extractRelativeSpecifiers(jsText, true);
|
|
696
|
+
for (const imp of imports) {
|
|
697
|
+
const resolved = resolveOutDirJsTarget(
|
|
698
|
+
imp,
|
|
699
|
+
currentDir,
|
|
700
|
+
resolvedOutDir,
|
|
701
|
+
allowedExtensions,
|
|
702
|
+
serverDirPrefixes
|
|
703
|
+
);
|
|
704
|
+
if (resolved) {
|
|
705
|
+
enqueueJs(resolved);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
for (const workerSpec of workers) {
|
|
709
|
+
const resolved = resolveOutDirJsTarget(
|
|
710
|
+
workerSpec,
|
|
711
|
+
currentDir,
|
|
712
|
+
resolvedOutDir,
|
|
713
|
+
allowedExtensions,
|
|
714
|
+
serverDirPrefixes
|
|
715
|
+
);
|
|
716
|
+
if (resolved) {
|
|
717
|
+
workerEntryFiles.add(resolved.relPath);
|
|
718
|
+
enqueueJs(resolved);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
const entryFiles = /* @__PURE__ */ new Set([...htmlEntryFiles, ...workerEntryFiles]);
|
|
723
|
+
if (htmlEntryFiles.size === 0 && preloadedFiles.size > 0) {
|
|
724
|
+
for (const preloaded of preloadedFiles) {
|
|
725
|
+
entryFiles.add(preloaded);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
return {
|
|
729
|
+
htmlEntryFiles,
|
|
730
|
+
preloadedFiles,
|
|
731
|
+
workerEntryFiles,
|
|
732
|
+
reachableFiles,
|
|
733
|
+
entryFiles,
|
|
734
|
+
scannedHtmlFiles: htmlFiles.length,
|
|
735
|
+
scannedJsFiles
|
|
736
|
+
};
|
|
737
|
+
}
|
|
380
738
|
|
|
381
739
|
// src/shared/errors.ts
|
|
382
740
|
var UNPEEK_ERROR_BRAND = /* @__PURE__ */ Symbol.for("unpeek.UnpeekError");
|
|
@@ -3844,8 +4202,119 @@ var compactGenerator = Object.assign({}, baseGenerator, {
|
|
|
3844
4202
|
this[node.options.type](node.options, state);
|
|
3845
4203
|
}
|
|
3846
4204
|
state.write(")");
|
|
4205
|
+
},
|
|
4206
|
+
ImportSpecifier(node, state) {
|
|
4207
|
+
writeModuleBindingName(this, node.imported, state);
|
|
4208
|
+
const sameIdent = node.imported?.type === "Identifier" && node.local?.type === "Identifier" && node.imported.name === node.local.name;
|
|
4209
|
+
if (!sameIdent && node.local?.name) {
|
|
4210
|
+
state.write(" as " + node.local.name);
|
|
4211
|
+
}
|
|
4212
|
+
},
|
|
4213
|
+
ExportSpecifier(node, state) {
|
|
4214
|
+
writeModuleBindingName(this, node.local, state);
|
|
4215
|
+
const sameIdent = node.local?.type === "Identifier" && node.exported?.type === "Identifier" && node.local.name === node.exported.name;
|
|
4216
|
+
if (!sameIdent && node.exported) {
|
|
4217
|
+
state.write(" as ");
|
|
4218
|
+
writeModuleBindingName(this, node.exported, state);
|
|
4219
|
+
}
|
|
4220
|
+
},
|
|
4221
|
+
ImportDeclaration(node, state) {
|
|
4222
|
+
state.write("import ");
|
|
4223
|
+
const specifiers = node.specifiers || [];
|
|
4224
|
+
const len = specifiers.length;
|
|
4225
|
+
let i = 0;
|
|
4226
|
+
if (len > 0) {
|
|
4227
|
+
while (i < len) {
|
|
4228
|
+
if (i > 0) state.write(", ");
|
|
4229
|
+
const spec = specifiers[i];
|
|
4230
|
+
const tag = spec.type[6];
|
|
4231
|
+
if (tag === "D") {
|
|
4232
|
+
state.write(spec.local.name, spec);
|
|
4233
|
+
i++;
|
|
4234
|
+
} else if (tag === "N") {
|
|
4235
|
+
state.write("* as " + spec.local.name, spec);
|
|
4236
|
+
i++;
|
|
4237
|
+
} else {
|
|
4238
|
+
break;
|
|
4239
|
+
}
|
|
4240
|
+
}
|
|
4241
|
+
if (i < len) {
|
|
4242
|
+
state.write("{");
|
|
4243
|
+
for (; ; ) {
|
|
4244
|
+
this.ImportSpecifier(specifiers[i], state);
|
|
4245
|
+
if (++i < len) {
|
|
4246
|
+
state.write(", ");
|
|
4247
|
+
} else {
|
|
4248
|
+
break;
|
|
4249
|
+
}
|
|
4250
|
+
}
|
|
4251
|
+
state.write("}");
|
|
4252
|
+
}
|
|
4253
|
+
state.write(" from ");
|
|
4254
|
+
}
|
|
4255
|
+
this.Literal(node.source, state);
|
|
4256
|
+
writeImportAttributes(this, node.attributes, state);
|
|
4257
|
+
state.write(";");
|
|
4258
|
+
},
|
|
4259
|
+
ExportNamedDeclaration(node, state) {
|
|
4260
|
+
state.write("export ");
|
|
4261
|
+
if (node.declaration) {
|
|
4262
|
+
this[node.declaration.type](node.declaration, state);
|
|
4263
|
+
return;
|
|
4264
|
+
}
|
|
4265
|
+
state.write("{");
|
|
4266
|
+
const specifiers = node.specifiers || [];
|
|
4267
|
+
const len = specifiers.length;
|
|
4268
|
+
if (len > 0) {
|
|
4269
|
+
for (let i = 0; ; ) {
|
|
4270
|
+
this.ExportSpecifier(specifiers[i], state);
|
|
4271
|
+
if (++i < len) {
|
|
4272
|
+
state.write(", ");
|
|
4273
|
+
} else {
|
|
4274
|
+
break;
|
|
4275
|
+
}
|
|
4276
|
+
}
|
|
4277
|
+
}
|
|
4278
|
+
state.write("}");
|
|
4279
|
+
if (node.source) {
|
|
4280
|
+
state.write(" from ");
|
|
4281
|
+
this.Literal(node.source, state);
|
|
4282
|
+
}
|
|
4283
|
+
writeImportAttributes(this, node.attributes, state);
|
|
4284
|
+
state.write(";");
|
|
4285
|
+
},
|
|
4286
|
+
ExportAllDeclaration(node, state) {
|
|
4287
|
+
if (node.exported == null) {
|
|
4288
|
+
state.write("export * from ");
|
|
4289
|
+
} else {
|
|
4290
|
+
state.write("export * as ");
|
|
4291
|
+
writeModuleBindingName(this, node.exported, state);
|
|
4292
|
+
state.write(" from ");
|
|
4293
|
+
}
|
|
4294
|
+
this.Literal(node.source, state);
|
|
4295
|
+
writeImportAttributes(this, node.attributes, state);
|
|
4296
|
+
state.write(";");
|
|
3847
4297
|
}
|
|
3848
4298
|
});
|
|
4299
|
+
function writeModuleBindingName(generator, node, state) {
|
|
4300
|
+
if (!node) return;
|
|
4301
|
+
if (node.type === "Literal") {
|
|
4302
|
+
generator.Literal(node, state);
|
|
4303
|
+
} else {
|
|
4304
|
+
state.write(node.name, node);
|
|
4305
|
+
}
|
|
4306
|
+
}
|
|
4307
|
+
function writeImportAttributes(generator, attributes, state) {
|
|
4308
|
+
if (!attributes || attributes.length === 0) return;
|
|
4309
|
+
state.write(" with { ");
|
|
4310
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
4311
|
+
generator.ImportAttribute(attributes[i], state);
|
|
4312
|
+
if (i < attributes.length - 1) {
|
|
4313
|
+
state.write(", ");
|
|
4314
|
+
}
|
|
4315
|
+
}
|
|
4316
|
+
state.write(" }");
|
|
4317
|
+
}
|
|
3849
4318
|
var VLQ_BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
3850
4319
|
function encodeVlq2(value) {
|
|
3851
4320
|
let vlq = value < 0 ? (-value << 1) + 1 : value << 1;
|
|
@@ -3991,7 +4460,7 @@ function createDeterrentSnippet(config) {
|
|
|
3991
4460
|
const consoleSilence = deterrentMode === "gradual" ? "for(var k in console)console[k]=()=>0;" : "";
|
|
3992
4461
|
const symLit = JSON.stringify(sym);
|
|
3993
4462
|
const headlessDecl = detectHeadless ? ",n=w.navigator||0" : "";
|
|
3994
|
-
const headlessCond = detectHeadless ? "||n.webdriver||/HeadlessChrome/.test(n.userAgent)" : "";
|
|
4463
|
+
const headlessCond = detectHeadless ? "||n.webdriver||/HeadlessChrome/.test(n.userAgent)||['__playwright','__puppeteer_evaluation_script__','__nightmare','frida','_frida_'].some(k=>w[k]||G[k])" : "";
|
|
3995
4464
|
const k1Mul = (seed | 1) >>> 0;
|
|
3996
4465
|
return `(function ${fnName}(w){var G=globalThis||w;if(w){var O=Object,P=O.getOwnPropertyDescriptor,RF=G.Reflect,R=RF&&RF.apply,FP=Function.prototype,SP=String.prototype,ER=Error,EP=ER.prototype,Q='__unpeek_',y=${symLit},z=y+'_s',U='__unpeek_tampered',K=Q+'seed',ST='stack',PS='prepareStackTrace',A='\\n at ',T=(o,k)=>{try{for(var r;o;o=O.getPrototypeOf(o))if((r=P(o,k))&&(r.set||!r.writable))return 1;}catch(e){return 1;}},D=k=>T(w,k)|T(G,k),B=(k,v,c,W)=>{W=o=>{try{if(!c||!T(o,k))o[k]=v;}catch(e){}};W(G);W(w);},s=FP.toString${headlessDecl},src='',t=+((G[z]||w[z])?.[0]==2),sd=P(EP,ST),ep=ER[PS],M=/^function (?:get )?(\\w*)\\(\\)\\s*\\x7b\\s*\\[native code\\]\\s*\\}$/;try{var ps,rf=P(new ER(),ST),Z=()=>{if(ER[PS]!==void 0){if(typeof process>'u')t=1;ER[PS]=void 0;}},F=(f,n,l)=>typeof f!='function'||'prototype'in f||(l>=0&&(f.name!=n||f.length!=l))||M.exec(R(R,null,[s,f,[]]))?.[1]!=n,E=(f,a,b)=>{try{R(f,a,b);t=1;}catch(e){Z();var ed=P(e,ST),st=sd&&sd.get?R(sd.get,e,[]):ed&&ed.get?R(ed.get,e,[]):e[ST];if(ed&&(!rf||F(ed.get,'',0)))t=1;t|=e?.name!='TypeError'||!st||st.indexOf(A)<0||/(?:Object|Proxy)\\.apply\\b|\\n at (?!.* \\(<anonymous>\\)$)/m.test(st.split(A+'E (')[0]);}};Z();try{E(P,null,[]);E(s,{toString(){}},[]);E(R,s,[null,s,[]]);}finally{if(ep!==void 0)ER[PS]=ep;}src=R(s,${fnName},[]);t|=(sd&&F(sd.get,ST,-1))|F(R,'apply',3)|F(s,'toString',0)|s.call(T)!==(ps=R(s,T,[]))|FP.call.call(s,T)!==ps|/\\n/.test(src)${headlessCond};}catch(e){t=1;}t|=D(y)|D(z)|D(U)|T(SP,'_u')|T(FP,'toString')|T(FP,'call')|T(RF,'apply')|T(ER,PS)|(sd?T(O.getPrototypeOf(EP),ST):T(EP,ST));for(var h=2166136261,i=0;i<src.length;)h=Math.imul(h^src.charCodeAt(i++),16777619);var d=h&65535,pd=t?d^((h>>>16^23131)||1):d;B(z,[t+1,h^${seed}^t,h^${k1Mul}^t,1,d^h>>>16],t);B(y,pd,t);if(t){B(y+'_t',!0);B(Q+'delta',pd);try{SP._u=14;}catch(e){}B(K,((w[K]||${seed})^1)>>>0);B(U,!0);try{${consoleSilence}w.dispatchEvent?.(new CustomEvent('unpeek:tamper'));}catch(e){}}}})(typeof window!=='undefined'?window:typeof self<'u'?self:typeof globalThis<'u'?globalThis:0);`;
|
|
3997
4466
|
}
|
|
@@ -4092,7 +4561,88 @@ function deriveWasmHostParams(seed) {
|
|
|
4092
4561
|
const pcMask = computeFnv1aHash("wasm-pcmask", seed) | 1 | 0;
|
|
4093
4562
|
return { gArr, sOff, rA0, rA1, rA2, rA3, gMask, pcMask };
|
|
4094
4563
|
}
|
|
4095
|
-
|
|
4564
|
+
var ABI_START_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
|
|
4565
|
+
var ABI_CONT_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
|
|
4566
|
+
var FORBIDDEN_ABI_NAMES = /* @__PURE__ */ new Set([
|
|
4567
|
+
"env",
|
|
4568
|
+
"g",
|
|
4569
|
+
"m",
|
|
4570
|
+
"kdf",
|
|
4571
|
+
"bulkmix",
|
|
4572
|
+
"mem",
|
|
4573
|
+
"as",
|
|
4574
|
+
"do",
|
|
4575
|
+
"if",
|
|
4576
|
+
"in",
|
|
4577
|
+
"of",
|
|
4578
|
+
"for",
|
|
4579
|
+
"int",
|
|
4580
|
+
"let",
|
|
4581
|
+
"new",
|
|
4582
|
+
"try",
|
|
4583
|
+
"var",
|
|
4584
|
+
"nan",
|
|
4585
|
+
"map",
|
|
4586
|
+
"set",
|
|
4587
|
+
"url",
|
|
4588
|
+
"top",
|
|
4589
|
+
"get",
|
|
4590
|
+
"set"
|
|
4591
|
+
]);
|
|
4592
|
+
function avalancheMix32(h) {
|
|
4593
|
+
let x = h >>> 0;
|
|
4594
|
+
x = Math.imul(x ^ x >>> 16, 2246822507) >>> 0;
|
|
4595
|
+
x = Math.imul(x ^ x >>> 13, 3266489909) >>> 0;
|
|
4596
|
+
return (x ^ x >>> 16) >>> 0;
|
|
4597
|
+
}
|
|
4598
|
+
function encodeWasmName(name) {
|
|
4599
|
+
const utf8 = new TextEncoder().encode(name);
|
|
4600
|
+
return [...encodeUnsignedLeb128(utf8.length), ...utf8];
|
|
4601
|
+
}
|
|
4602
|
+
function deriveWasmAbiNames(seed) {
|
|
4603
|
+
const s = seed >>> 0;
|
|
4604
|
+
const used = new Set(FORBIDDEN_ABI_NAMES);
|
|
4605
|
+
const pickName = (role) => {
|
|
4606
|
+
for (let attempt = 0; attempt < 64; attempt++) {
|
|
4607
|
+
const h = avalancheMix32(computeFnv1aHash(`wasm-abi:${role}:${attempt}`, s));
|
|
4608
|
+
const len = h >>> 18 & 1 ? 3 : 2;
|
|
4609
|
+
const c0 = ABI_START_CHARS[h % ABI_START_CHARS.length];
|
|
4610
|
+
const c1 = ABI_CONT_CHARS[(h >>> 6) % ABI_CONT_CHARS.length];
|
|
4611
|
+
const c2 = len === 3 ? ABI_CONT_CHARS[(h >>> 12) % ABI_CONT_CHARS.length] : "";
|
|
4612
|
+
const candidate = `${c0}${c1}${c2}`;
|
|
4613
|
+
const lower = candidate.toLowerCase();
|
|
4614
|
+
if (!candidate.startsWith("__") && !used.has(lower)) {
|
|
4615
|
+
used.add(lower);
|
|
4616
|
+
return candidate;
|
|
4617
|
+
}
|
|
4618
|
+
}
|
|
4619
|
+
const fallback = `w${s.toString(36).slice(0, 2).padStart(2, "0")}`;
|
|
4620
|
+
used.add(fallback.toLowerCase());
|
|
4621
|
+
return fallback;
|
|
4622
|
+
};
|
|
4623
|
+
const envMod = pickName("envMod");
|
|
4624
|
+
const gImport = pickName("gImport");
|
|
4625
|
+
const mImport = pickName("mImport");
|
|
4626
|
+
const kdfExport = pickName("kdfExport");
|
|
4627
|
+
const bulkMixExport = pickName("bulkMixExport");
|
|
4628
|
+
const memExport = pickName("memExport");
|
|
4629
|
+
const exportOrder = [0, 1, 2];
|
|
4630
|
+
const orderHash = avalancheMix32(computeFnv1aHash("wasm-abi:order", s));
|
|
4631
|
+
const j2 = orderHash % 3;
|
|
4632
|
+
[exportOrder[2], exportOrder[j2]] = [exportOrder[j2], exportOrder[2]];
|
|
4633
|
+
const j1 = (orderHash >>> 8) % 2;
|
|
4634
|
+
[exportOrder[1], exportOrder[j1]] = [exportOrder[j1], exportOrder[1]];
|
|
4635
|
+
return {
|
|
4636
|
+
envMod,
|
|
4637
|
+
gImport,
|
|
4638
|
+
mImport,
|
|
4639
|
+
kdfExport,
|
|
4640
|
+
bulkMixExport,
|
|
4641
|
+
memExport,
|
|
4642
|
+
exportOrder
|
|
4643
|
+
};
|
|
4644
|
+
}
|
|
4645
|
+
function generateWasmKdfBinary(seed, options) {
|
|
4096
4646
|
const { sVal, c1, c2, k0, r1, r2 } = deriveKdfParams(seed);
|
|
4097
4647
|
const { gMask, pcMask } = deriveWasmHostParams(seed);
|
|
4098
4648
|
const memBase = (seed >>> 0 & 127) * 4;
|
|
@@ -4618,27 +5168,23 @@ function generateWasmKdfBinary(seed) {
|
|
|
4618
5168
|
...encodeUnsignedLeb128(typePayload.length),
|
|
4619
5169
|
...typePayload
|
|
4620
5170
|
];
|
|
5171
|
+
const abi = options?.polymorphicAbi ? deriveWasmAbiNames(seed) : null;
|
|
5172
|
+
const envModName = abi ? abi.envMod : "env";
|
|
5173
|
+
const gImportName = abi ? abi.gImport : "g";
|
|
5174
|
+
const mImportName = abi ? abi.mImport : "m";
|
|
4621
5175
|
const importPayload = [
|
|
4622
5176
|
2,
|
|
4623
5177
|
// 2 imports
|
|
4624
|
-
|
|
4625
|
-
|
|
4626
|
-
110,
|
|
4627
|
-
118,
|
|
4628
|
-
1,
|
|
4629
|
-
103,
|
|
5178
|
+
...encodeWasmName(envModName),
|
|
5179
|
+
...encodeWasmName(gImportName),
|
|
4630
5180
|
0,
|
|
4631
5181
|
0,
|
|
4632
|
-
//
|
|
4633
|
-
|
|
4634
|
-
|
|
4635
|
-
110,
|
|
4636
|
-
118,
|
|
4637
|
-
1,
|
|
4638
|
-
109,
|
|
5182
|
+
// g: func type 0
|
|
5183
|
+
...encodeWasmName(envModName),
|
|
5184
|
+
...encodeWasmName(mImportName),
|
|
4639
5185
|
0,
|
|
4640
5186
|
1
|
|
4641
|
-
//
|
|
5187
|
+
// m: func type 1
|
|
4642
5188
|
];
|
|
4643
5189
|
const importSection = [
|
|
4644
5190
|
2,
|
|
@@ -4652,35 +5198,21 @@ function generateWasmKdfBinary(seed) {
|
|
|
4652
5198
|
...funcPayload
|
|
4653
5199
|
];
|
|
4654
5200
|
const memSection = [5, 3, 1, 0, 1];
|
|
4655
|
-
const
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
0
|
|
4663
|
-
2,
|
|
4664
|
-
// "kdf" -> func 2
|
|
4665
|
-
7,
|
|
4666
|
-
98,
|
|
4667
|
-
117,
|
|
4668
|
-
108,
|
|
4669
|
-
107,
|
|
4670
|
-
77,
|
|
4671
|
-
105,
|
|
4672
|
-
120,
|
|
4673
|
-
0,
|
|
4674
|
-
3,
|
|
4675
|
-
// "bulkMix" -> func 3
|
|
4676
|
-
3,
|
|
4677
|
-
109,
|
|
4678
|
-
101,
|
|
4679
|
-
109,
|
|
4680
|
-
2,
|
|
4681
|
-
0
|
|
4682
|
-
// "mem" -> memory 0
|
|
5201
|
+
const exportSpecs = abi ? [
|
|
5202
|
+
[abi.kdfExport, 0, 2],
|
|
5203
|
+
[abi.bulkMixExport, 0, 3],
|
|
5204
|
+
[abi.memExport, 2, 0]
|
|
5205
|
+
] : [
|
|
5206
|
+
["kdf", 0, 2],
|
|
5207
|
+
["bulkMix", 0, 3],
|
|
5208
|
+
["mem", 2, 0]
|
|
4683
5209
|
];
|
|
5210
|
+
const exportOrder = abi ? abi.exportOrder : [0, 1, 2];
|
|
5211
|
+
const exportPayload = [3];
|
|
5212
|
+
for (const idx of exportOrder) {
|
|
5213
|
+
const [name, kind, targetIdx] = exportSpecs[idx];
|
|
5214
|
+
exportPayload.push(...encodeWasmName(name), kind, targetIdx);
|
|
5215
|
+
}
|
|
4684
5216
|
const exportSection = [
|
|
4685
5217
|
7,
|
|
4686
5218
|
...encodeUnsignedLeb128(exportPayload.length),
|
|
@@ -4778,12 +5310,13 @@ function injectWasmVault(ast, seed = 2654435769) {
|
|
|
4778
5310
|
const id = existing?.declarations[0]?.id?.name ?? kdfFnName;
|
|
4779
5311
|
return {
|
|
4780
5312
|
kdfFnName: id,
|
|
4781
|
-
wasmBytes: generateWasmKdfBinary(seed),
|
|
5313
|
+
wasmBytes: generateWasmKdfBinary(seed, { polymorphicAbi: true }),
|
|
4782
5314
|
wasmVarName,
|
|
4783
5315
|
kdfParityVerified: true
|
|
4784
5316
|
};
|
|
4785
5317
|
}
|
|
4786
|
-
const
|
|
5318
|
+
const abi = deriveWasmAbiNames(seed);
|
|
5319
|
+
const wasmBytes = generateWasmKdfBinary(seed, { polymorphicAbi: true });
|
|
4787
5320
|
const byteArrayLiteral = Array.from(wasmBytes).join(",");
|
|
4788
5321
|
const { sVal, c1, c2, k0, r1, r2 } = deriveKdfParams(seed);
|
|
4789
5322
|
const { gArr, sOff, rA0, rA1, rA2, rA3, gMask, pcMask } = deriveWasmHostParams(seed);
|
|
@@ -4795,7 +5328,8 @@ function injectWasmVault(ast, seed = 2654435769) {
|
|
|
4795
5328
|
const mChars = [105 ^ strKey, 109 ^ strKey, 117 ^ strKey, 108 ^ strKey].join(",");
|
|
4796
5329
|
const helperSnippet = `var ${kdfFnName};
|
|
4797
5330
|
(function(){
|
|
4798
|
-
var _G = typeof globalThis < 'u' ? globalThis : typeof self < 'u' ? self : 0;
|
|
5331
|
+
var _G = typeof globalThis < 'u' ? globalThis : typeof self < 'u' ? self : typeof window < 'u' ? window : 0;
|
|
5332
|
+
var _M = (_G && _G.Math) || Math;
|
|
4799
5333
|
var _reg = _G ? (_G.__unpeek_wasm_kdf || (_G.__unpeek_wasm_kdf = {})) : 0;
|
|
4800
5334
|
if (_reg && _reg['${suffix}']) {
|
|
4801
5335
|
${kdfFnName} = _reg['${suffix}'];
|
|
@@ -4803,10 +5337,10 @@ function injectWasmVault(ast, seed = 2654435769) {
|
|
|
4803
5337
|
}
|
|
4804
5338
|
var _g = [${gArr.join(",")}];
|
|
4805
5339
|
var _evR = function(x) {
|
|
4806
|
-
return (${rA0} +
|
|
5340
|
+
return (${rA0} + _M.imul(x, (${rA1} + _M.imul(x, (${rA2} + _M.imul(${rA3}, x)) | 0)) | 0)) | 0;
|
|
4807
5341
|
};
|
|
4808
5342
|
var _sb = function(b) {
|
|
4809
|
-
return (
|
|
5343
|
+
return (_M.imul(167, b & 255) + ${sOff}) & 255;
|
|
4810
5344
|
};
|
|
4811
5345
|
var _tk = ['__','unp','eek_t','ampered'].join('');
|
|
4812
5346
|
var _eG = function(sl) {
|
|
@@ -4820,8 +5354,8 @@ function injectWasmVault(ast, seed = 2654435769) {
|
|
|
4820
5354
|
try {
|
|
4821
5355
|
var ${wasmVarName} = new Uint8Array([${byteArrayLiteral}]);
|
|
4822
5356
|
var _wm${suffix} = new WebAssembly.Module(${wasmVarName});
|
|
4823
|
-
var _wi${suffix} = new WebAssembly.Instance(_wm${suffix}, {
|
|
4824
|
-
${kdfFnName} = _wi${suffix}.exports.
|
|
5357
|
+
var _wi${suffix} = new WebAssembly.Instance(_wm${suffix}, { ${abi.envMod}: { ${abi.gImport}: _eG, ${abi.mImport}: _eM } });
|
|
5358
|
+
${kdfFnName} = _wi${suffix}.exports[${JSON.stringify(abi.kdfExport)}];
|
|
4825
5359
|
} catch (e) {
|
|
4826
5360
|
var _kx = [${rivEncodedTargets.join(",")}];
|
|
4827
5361
|
var _k = [];
|
|
@@ -4833,7 +5367,7 @@ function injectWasmVault(ast, seed = 2654435769) {
|
|
|
4833
5367
|
for (var _i = 0; _i < _c.length; _i++) {
|
|
4834
5368
|
_p += String.fromCharCode(_c[_i] ^ ${strKey});
|
|
4835
5369
|
}
|
|
4836
|
-
var _m =
|
|
5370
|
+
var _m = _M[_p];
|
|
4837
5371
|
${kdfFnName} = function(f, s, h) {
|
|
4838
5372
|
var _x = (f ^ _k[2] ^ _k[3] ^ (_eG(0) ^ ${gMask})) | 0;
|
|
4839
5373
|
_x = (_x ^ (_x >>> 16)) | 0;
|
|
@@ -4980,6 +5514,61 @@ function unwrapAssignedHostMember(node, matcher, depth = 0) {
|
|
|
4980
5514
|
return false;
|
|
4981
5515
|
}
|
|
4982
5516
|
}
|
|
5517
|
+
function isImportMetaMember(node, propName) {
|
|
5518
|
+
if (!node || node.type !== "MemberExpression") return false;
|
|
5519
|
+
const obj = node.object;
|
|
5520
|
+
if (obj?.type !== "MetaProperty" || obj.meta?.type !== "Identifier" || obj.meta.name !== "import" || obj.property?.type !== "Identifier" || obj.property.name !== "meta") {
|
|
5521
|
+
return false;
|
|
5522
|
+
}
|
|
5523
|
+
if (!node.computed && node.property?.type === "Identifier") {
|
|
5524
|
+
return node.property.name === propName;
|
|
5525
|
+
}
|
|
5526
|
+
return node.computed && node.property?.type === "Literal" && node.property.value === propName;
|
|
5527
|
+
}
|
|
5528
|
+
function isRequireResolveCallee(callee) {
|
|
5529
|
+
if (!callee || callee.type !== "MemberExpression") return false;
|
|
5530
|
+
if (callee.object?.type !== "Identifier" || callee.object.name !== "require") {
|
|
5531
|
+
return false;
|
|
5532
|
+
}
|
|
5533
|
+
if (!callee.computed && callee.property?.type === "Identifier") {
|
|
5534
|
+
return callee.property.name === "resolve";
|
|
5535
|
+
}
|
|
5536
|
+
return callee.computed && callee.property?.type === "Literal" && callee.property.value === "resolve";
|
|
5537
|
+
}
|
|
5538
|
+
function isStaticModuleOrAssetPosition(node, parent) {
|
|
5539
|
+
if (!node || !parent) return false;
|
|
5540
|
+
if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration") {
|
|
5541
|
+
return parent.source === node;
|
|
5542
|
+
}
|
|
5543
|
+
if (parent.type === "ExportAllDeclaration") {
|
|
5544
|
+
return parent.source === node || parent.exported === node;
|
|
5545
|
+
}
|
|
5546
|
+
if (parent.type === "ImportExpression") {
|
|
5547
|
+
return parent.source === node;
|
|
5548
|
+
}
|
|
5549
|
+
if (parent.type === "ImportSpecifier") {
|
|
5550
|
+
return parent.imported === node;
|
|
5551
|
+
}
|
|
5552
|
+
if (parent.type === "ExportSpecifier") {
|
|
5553
|
+
return parent.local === node || parent.exported === node;
|
|
5554
|
+
}
|
|
5555
|
+
if (parent.type === "ImportAttribute") {
|
|
5556
|
+
return true;
|
|
5557
|
+
}
|
|
5558
|
+
if (parent.type === "NewExpression") {
|
|
5559
|
+
return parent.callee?.type === "Identifier" && parent.callee.name === "URL" && Array.isArray(parent.arguments) && parent.arguments.length >= 2 && parent.arguments[0] === node && isImportMetaMember(parent.arguments[1], "url");
|
|
5560
|
+
}
|
|
5561
|
+
if (parent.type === "CallExpression") {
|
|
5562
|
+
if (!Array.isArray(parent.arguments) || parent.arguments[0] !== node) {
|
|
5563
|
+
return false;
|
|
5564
|
+
}
|
|
5565
|
+
const callee = parent.callee;
|
|
5566
|
+
if (isImportMetaMember(callee, "resolve")) return true;
|
|
5567
|
+
if (isRequireResolveCallee(callee)) return true;
|
|
5568
|
+
if (callee?.type === "Identifier" && callee.name === "require") return true;
|
|
5569
|
+
}
|
|
5570
|
+
return false;
|
|
5571
|
+
}
|
|
4983
5572
|
function classifyChunkBoundary(ast) {
|
|
4984
5573
|
const exemptSubtreeRoots = /* @__PURE__ */ new WeakSet();
|
|
4985
5574
|
const importSourceNodes = /* @__PURE__ */ new WeakSet();
|
|
@@ -4989,6 +5578,10 @@ function classifyChunkBoundary(ast) {
|
|
|
4989
5578
|
let hasPreloadManifest = false;
|
|
4990
5579
|
walk2.simple(ast, {
|
|
4991
5580
|
CallExpression(node) {
|
|
5581
|
+
const firstArg = Array.isArray(node.arguments) ? node.arguments[0] : void 0;
|
|
5582
|
+
if (firstArg && (firstArg.type === "Literal" || firstArg.type === "TemplateLiteral") && isStaticModuleOrAssetPosition(firstArg, node)) {
|
|
5583
|
+
exemptSubtreeRoots.add(firstArg);
|
|
5584
|
+
}
|
|
4992
5585
|
const callee = node.callee;
|
|
4993
5586
|
const isPushCall = callee?.type === "MemberExpression" && (!callee.computed && callee.property?.type === "Identifier" && callee.property.name === "push" || callee.computed && callee.property?.type === "Literal" && callee.property.value === "push");
|
|
4994
5587
|
if (isPushCall) {
|
|
@@ -5052,6 +5645,49 @@ function classifyChunkBoundary(ast) {
|
|
|
5052
5645
|
exemptSubtreeRoots.add(node);
|
|
5053
5646
|
}
|
|
5054
5647
|
},
|
|
5648
|
+
NewExpression(node) {
|
|
5649
|
+
const firstArg = Array.isArray(node.arguments) ? node.arguments[0] : void 0;
|
|
5650
|
+
if (firstArg && (firstArg.type === "Literal" || firstArg.type === "TemplateLiteral") && isStaticModuleOrAssetPosition(firstArg, node)) {
|
|
5651
|
+
exemptSubtreeRoots.add(firstArg);
|
|
5652
|
+
}
|
|
5653
|
+
},
|
|
5654
|
+
ImportDeclaration(node) {
|
|
5655
|
+
if (node.source && typeof node.source === "object") {
|
|
5656
|
+
exemptSubtreeRoots.add(node.source);
|
|
5657
|
+
}
|
|
5658
|
+
if (Array.isArray(node.specifiers)) {
|
|
5659
|
+
for (const spec of node.specifiers) {
|
|
5660
|
+
if (spec?.type === "ImportSpecifier" && spec.imported?.type === "Literal") {
|
|
5661
|
+
exemptSubtreeRoots.add(spec.imported);
|
|
5662
|
+
}
|
|
5663
|
+
}
|
|
5664
|
+
}
|
|
5665
|
+
},
|
|
5666
|
+
ExportNamedDeclaration(node) {
|
|
5667
|
+
if (node.source && typeof node.source === "object") {
|
|
5668
|
+
exemptSubtreeRoots.add(node.source);
|
|
5669
|
+
}
|
|
5670
|
+
if (Array.isArray(node.specifiers)) {
|
|
5671
|
+
for (const spec of node.specifiers) {
|
|
5672
|
+
if (spec?.type === "ExportSpecifier") {
|
|
5673
|
+
if (spec.local?.type === "Literal") {
|
|
5674
|
+
exemptSubtreeRoots.add(spec.local);
|
|
5675
|
+
}
|
|
5676
|
+
if (spec.exported?.type === "Literal") {
|
|
5677
|
+
exemptSubtreeRoots.add(spec.exported);
|
|
5678
|
+
}
|
|
5679
|
+
}
|
|
5680
|
+
}
|
|
5681
|
+
}
|
|
5682
|
+
},
|
|
5683
|
+
ExportAllDeclaration(node) {
|
|
5684
|
+
if (node.source && typeof node.source === "object") {
|
|
5685
|
+
exemptSubtreeRoots.add(node.source);
|
|
5686
|
+
}
|
|
5687
|
+
if (node.exported && typeof node.exported === "object") {
|
|
5688
|
+
exemptSubtreeRoots.add(node.exported);
|
|
5689
|
+
}
|
|
5690
|
+
},
|
|
5055
5691
|
ImportExpression(node) {
|
|
5056
5692
|
importExpressions.push(node);
|
|
5057
5693
|
if (node.source && typeof node.source === "object") {
|
|
@@ -5121,22 +5757,36 @@ function buildRivestDeltaExpr(seed, expectedDelta, varName = "_ed") {
|
|
|
5121
5757
|
const xMasked = xEnc ^ xMask | 0;
|
|
5122
5758
|
return `var _rx=(${xMasked}^${xMask | 0})|0,${varName}=(${rA0}+Math.imul(_rx,(${rA1}+Math.imul(_rx,(${rA2}+Math.imul(${rA3},_rx))|0))|0))&65535;`;
|
|
5123
5759
|
}
|
|
5124
|
-
function
|
|
5760
|
+
function buildLatchInitSnippet(seed, latchCoupling) {
|
|
5761
|
+
if (!latchCoupling || typeof latchCoupling.latchFnName !== "string" || !/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(latchCoupling.latchFnName)) {
|
|
5762
|
+
return { hasLatch: false, latchDecl: "" };
|
|
5763
|
+
}
|
|
5764
|
+
const tamperByte = seed >>> 8 & 127 | 53;
|
|
5765
|
+
const expectedLatch = latchCoupling.expectedLatchByte & 255;
|
|
5766
|
+
return {
|
|
5767
|
+
hasLatch: true,
|
|
5768
|
+
latchDecl: `var _ld=typeof ${latchCoupling.latchFnName}==="function"?((${latchCoupling.latchFnName}()^${expectedLatch})&255):${tamperByte};`
|
|
5769
|
+
};
|
|
5770
|
+
}
|
|
5771
|
+
function createSaltedDecryptorFnAst(seed, pChars, expectedDelta, windowKeySymbol, safeAlpha = SAFE_ALPHA, shieldCoupled = true, _browserOnly = false, latchCoupling) {
|
|
5125
5772
|
const propAccess = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(windowKeySymbol) ? `.${windowKeySymbol}` : `[${JSON.stringify(windowKeySymbol)}]`;
|
|
5126
5773
|
const stateSym = `${windowKeySymbol}_s`;
|
|
5127
5774
|
const stateAccess = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(stateSym) ? `.${stateSym}` : `[${JSON.stringify(stateSym)}]`;
|
|
5128
5775
|
const flagSym = `${windowKeySymbol}_t`;
|
|
5129
5776
|
const flagAccess = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(flagSym) ? `.${flagSym}` : `[${JSON.stringify(flagSym)}]`;
|
|
5130
5777
|
const tkExpr = `['__','unp','eek_t','ampered'].join('')`;
|
|
5778
|
+
const { hasLatch, latchDecl } = buildLatchInitSnippet(seed, latchCoupling);
|
|
5779
|
+
const charEmit = hasLatch ? `o+=String.fromCharCode((k<0?ch:A[(A.indexOf(P[(k-(t&65535)%92+92)%92])-(t>>>16)%92+92)%92]).charCodeAt(0)^_ld);` : `o+=k<0?ch:A[(A.indexOf(P[(k-(t&65535)%92+92)%92])-(t>>>16)%92+92)%92];`;
|
|
5780
|
+
const cacheGuardSuffix = hasLatch ? "&&!_ld" : "";
|
|
5131
5781
|
let fBody;
|
|
5132
5782
|
if (!shieldCoupled) {
|
|
5133
|
-
fBody = `var m=[],c=Function.prototype.toString.call(f),_tk=${tkExpr};for(var h=0x811c9dc5,j=0;j<c.length;j++)h=Math.imul(h^c.charCodeAt(j),0x01000193)>>>0;var A="${safeAlpha}",P="${pChars}";return function(i,s){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var w=typeof window<"u"?window:typeof self<"u"?self:0;var tm=(G&&(G[_tk]===!0||G${flagAccess}===!0))||(w&&(w[_tk]===!0||w${flagAccess}===!0))||((""._u||a._u||0)!==0);if(!tm&&(i in m))return m[i]
|
|
5783
|
+
fBody = `var m=[],c=Function.prototype.toString.call(f),_tk=${tkExpr};for(var h=0x811c9dc5,j=0;j<c.length;j++)h=Math.imul(h^c.charCodeAt(j),0x01000193)>>>0;var A="${safeAlpha}",P="${pChars}";return function(i,s){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var w=typeof window<"u"?window:typeof self<"u"?self:0;var tm=(G&&(G[_tk]===!0||G${flagAccess}===!0))||(w&&(w[_tk]===!0||w${flagAccess}===!0))||((""._u||a._u||0)!==0);if(!tm&&(i in m))return m[i];` + latchDecl + `var d=tm?(((Math.imul(i+1,0x9e3779b1)^0x5a5b)&65535)|1):0;var q=(h^(""._u||a._u||0)^d)>>>0,p=a[i],o="";if(p)for(var t=q^s^Math.imul(i+1,0x01000193),j=0;j<p.length;j++){t=Math.imul(t^j+1,0x01000193)^0x811c9dc5;var ch=p[j],k=A.indexOf(ch);` + charEmit + `}if(!tm${cacheGuardSuffix})m[i]=o;return o;};`;
|
|
5134
5784
|
} else {
|
|
5135
5785
|
const edInit = buildRivestDeltaExpr(seed, expectedDelta, "_ed");
|
|
5136
5786
|
const hasWindowKey = Boolean(windowKeySymbol);
|
|
5137
5787
|
const brDecl = hasWindowKey ? NARROWED_ASYNC_BR_DECL : "";
|
|
5138
5788
|
const svCheck = hasWindowKey ? `(sv===void 0?!_br:sv!==_ed)` : `(sv!==void 0&&sv!==_ed)`;
|
|
5139
|
-
fBody = `var m=[],c=Function.prototype.toString.call(f),_tk=${tkExpr};for(var h=0x811c9dc5,j=0;j<c.length;j++)h=Math.imul(h^c.charCodeAt(j),0x01000193)>>>0;var A="${safeAlpha}",P="${pChars}";${edInit}return function(i,s){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var w=typeof window<"u"?window:typeof self<"u"?self:0;` + brDecl + `var sv=G&&G${propAccess}!==void 0?G${propAccess}:w&&w${propAccess}!==void 0?w${propAccess}:void 0;var sr=G&&G${stateAccess}?G${stateAccess}:w&&w${stateAccess}?w${stateAccess}:void 0;var tm=(G&&(G[_tk]===!0||G${flagAccess}===!0))||(w&&(w[_tk]===!0||w${flagAccess}===!0))||(sr&&sr[0]===2)||((""._u||a._u||0)!==0)||${svCheck};if(!tm&&sv===_ed&&(i in m))return m[i]
|
|
5789
|
+
fBody = `var m=[],c=Function.prototype.toString.call(f),_tk=${tkExpr};for(var h=0x811c9dc5,j=0;j<c.length;j++)h=Math.imul(h^c.charCodeAt(j),0x01000193)>>>0;var A="${safeAlpha}",P="${pChars}";${edInit}return function(i,s){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var w=typeof window<"u"?window:typeof self<"u"?self:0;` + brDecl + `var sv=G&&G${propAccess}!==void 0?G${propAccess}:w&&w${propAccess}!==void 0?w${propAccess}:void 0;var sr=G&&G${stateAccess}?G${stateAccess}:w&&w${stateAccess}?w${stateAccess}:void 0;var tm=(G&&(G[_tk]===!0||G${flagAccess}===!0))||(w&&(w[_tk]===!0||w${flagAccess}===!0))||(sr&&sr[0]===2)||((""._u||a._u||0)!==0)||${svCheck};if(!tm&&sv===_ed&&(i in m))return m[i];` + latchDecl + `var d=tm?((_ed^(((Math.imul(i+1,0x9e3779b1)^0x5a5b)&65535)|1))&65535):_ed;var q=(h^(""._u||a._u||0)^d)>>>0,p=a[i],o="";if(p)for(var t=q^s^Math.imul(i+1,0x01000193),j=0;j<p.length;j++){t=Math.imul(t^j+1,0x01000193)^0x811c9dc5;var ch=p[j],k=A.indexOf(ch);` + charEmit + `}if(!tm&&sv===_ed${cacheGuardSuffix})m[i]=o;return o;};`;
|
|
5140
5790
|
}
|
|
5141
5791
|
return acorn3.parse(`(function f(a){${fBody}})`, {
|
|
5142
5792
|
ecmaVersion: "latest",
|
|
@@ -5210,7 +5860,7 @@ function replaceNodeInParent(parent, oldNode, newNode) {
|
|
|
5210
5860
|
}
|
|
5211
5861
|
return false;
|
|
5212
5862
|
}
|
|
5213
|
-
function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, config, windowKeySymbol, expectedDelta, deterrentMode, shieldCoupled = true, detectHeadless = false, exemptSubtreeRoots) {
|
|
5863
|
+
function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, config, windowKeySymbol, expectedDelta, deterrentMode, shieldCoupled = true, detectHeadless = false, exemptSubtreeRoots, latchCoupling) {
|
|
5214
5864
|
if (isAstAlreadySalted(ast)) {
|
|
5215
5865
|
return {
|
|
5216
5866
|
encryptedStringsCount: 0,
|
|
@@ -5233,16 +5883,39 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
5233
5883
|
const shaderToIndex = /* @__PURE__ */ new Map();
|
|
5234
5884
|
const shaderPayloads = [];
|
|
5235
5885
|
const shaderTargets = [];
|
|
5236
|
-
|
|
5886
|
+
const interpolatedTemplates = [];
|
|
5887
|
+
let regExpShadowedScopes;
|
|
5888
|
+
const getRegExpShadowedScopes = () => {
|
|
5889
|
+
regExpShadowedScopes ??= collectIdentifierShadowedScopes(ast, "RegExp");
|
|
5890
|
+
return regExpShadowedScopes;
|
|
5891
|
+
};
|
|
5892
|
+
function pushSaltedTarget(rawString, node, parent, extra) {
|
|
5893
|
+
const callSiteIdx = targetsToReplace.length;
|
|
5894
|
+
const [saltLo, saltHi] = computeCallSiteSalt64(
|
|
5895
|
+
seed >>> 0,
|
|
5896
|
+
callSiteIdx >>> 0,
|
|
5897
|
+
seed >>> 0
|
|
5898
|
+
);
|
|
5899
|
+
const localSalt = ((saltLo ^ saltHi) & 2147483647 | 1) >>> 0;
|
|
5900
|
+
targetsToReplace.push({
|
|
5901
|
+
node,
|
|
5902
|
+
parent,
|
|
5903
|
+
callSiteIdx,
|
|
5904
|
+
localSalt,
|
|
5905
|
+
rawString,
|
|
5906
|
+
...extra
|
|
5907
|
+
});
|
|
5908
|
+
}
|
|
5909
|
+
function processSaltedCandidate(val, node, parent, ancestors, onReplace) {
|
|
5237
5910
|
if (isNodeInExemptSubtree(ancestors, exemptSubtreeRoots)) {
|
|
5238
|
-
return;
|
|
5911
|
+
return false;
|
|
5239
5912
|
}
|
|
5240
5913
|
if (parent.type === "ExpressionStatement" && (Boolean(parent.directive) || activeDirectives.has(val))) {
|
|
5241
|
-
return;
|
|
5914
|
+
return false;
|
|
5242
5915
|
}
|
|
5243
5916
|
for (let i = ancestors.length - 2; i >= 0; i--) {
|
|
5244
5917
|
if (ancestors[i].type === "ExpressionStatement" && Boolean(ancestors[i].directive)) {
|
|
5245
|
-
return;
|
|
5918
|
+
return false;
|
|
5246
5919
|
}
|
|
5247
5920
|
}
|
|
5248
5921
|
let isHotExempt = false;
|
|
@@ -5269,11 +5942,11 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
5269
5942
|
shaderToIndex.set(val, idx);
|
|
5270
5943
|
shaderPayloads.push(val);
|
|
5271
5944
|
}
|
|
5272
|
-
shaderTargets.push({ node, parent, idx });
|
|
5273
|
-
return;
|
|
5945
|
+
shaderTargets.push({ node, parent, idx, ...onReplace ? { onReplace } : {} });
|
|
5946
|
+
return true;
|
|
5274
5947
|
}
|
|
5275
5948
|
if (isHotExempt) {
|
|
5276
|
-
return;
|
|
5949
|
+
return false;
|
|
5277
5950
|
}
|
|
5278
5951
|
if (!shouldEncryptString(
|
|
5279
5952
|
val,
|
|
@@ -5282,60 +5955,94 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
5282
5955
|
activeDirectives,
|
|
5283
5956
|
customReserved
|
|
5284
5957
|
)) {
|
|
5285
|
-
return;
|
|
5958
|
+
return false;
|
|
5286
5959
|
}
|
|
5287
|
-
|
|
5288
|
-
|
|
5289
|
-
seed >>> 0,
|
|
5290
|
-
callSiteIdx >>> 0,
|
|
5291
|
-
seed >>> 0
|
|
5292
|
-
);
|
|
5293
|
-
const localSalt = ((saltLo ^ saltHi) & 2147483647 | 1) >>> 0;
|
|
5294
|
-
targetsToReplace.push({
|
|
5295
|
-
node,
|
|
5296
|
-
parent,
|
|
5297
|
-
callSiteIdx,
|
|
5298
|
-
localSalt,
|
|
5299
|
-
rawString: val
|
|
5300
|
-
});
|
|
5960
|
+
pushSaltedTarget(val, node, parent, onReplace ? { onReplace } : void 0);
|
|
5961
|
+
return true;
|
|
5301
5962
|
}
|
|
5302
|
-
walk3.ancestor(
|
|
5303
|
-
|
|
5304
|
-
|
|
5305
|
-
|
|
5306
|
-
|
|
5307
|
-
|
|
5308
|
-
|
|
5309
|
-
|
|
5310
|
-
|
|
5311
|
-
|
|
5312
|
-
|
|
5313
|
-
|
|
5314
|
-
|
|
5315
|
-
|
|
5316
|
-
|
|
5317
|
-
|
|
5318
|
-
|
|
5319
|
-
|
|
5963
|
+
walk3.ancestor(
|
|
5964
|
+
ast,
|
|
5965
|
+
{
|
|
5966
|
+
Literal(node, _state, ancestors) {
|
|
5967
|
+
if (node.regex) {
|
|
5968
|
+
if (isEligibleSecretRegExp(
|
|
5969
|
+
node,
|
|
5970
|
+
ancestors,
|
|
5971
|
+
hotExemptNodes,
|
|
5972
|
+
exemptSubtreeRoots,
|
|
5973
|
+
getRegExpShadowedScopes
|
|
5974
|
+
)) {
|
|
5975
|
+
pushSaltedTarget(
|
|
5976
|
+
node.regex.pattern,
|
|
5977
|
+
node,
|
|
5978
|
+
ancestors[ancestors.length - 2],
|
|
5979
|
+
{
|
|
5980
|
+
replaceMode: "regexp",
|
|
5981
|
+
flags: node.regex.flags ?? ""
|
|
5982
|
+
}
|
|
5983
|
+
);
|
|
5984
|
+
}
|
|
5985
|
+
return;
|
|
5986
|
+
}
|
|
5987
|
+
if (typeof node.value !== "string") return;
|
|
5988
|
+
const val = node.value;
|
|
5989
|
+
if (activeDirectives.has(val)) return;
|
|
5990
|
+
const parent = ancestors[ancestors.length - 2];
|
|
5991
|
+
if (!parent) return;
|
|
5992
|
+
if (isStaticModuleOrAssetPosition(node, parent)) {
|
|
5993
|
+
return;
|
|
5994
|
+
}
|
|
5995
|
+
if (parent.type === "Property" && parent.key === node && !parent.computed) {
|
|
5996
|
+
if (!isEligibleObjectPropertyKey(val, node, parent, ancestors)) {
|
|
5997
|
+
return;
|
|
5998
|
+
}
|
|
5999
|
+
}
|
|
6000
|
+
if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
6001
|
+
return;
|
|
6002
|
+
}
|
|
6003
|
+
processSaltedCandidate(val, node, parent, ancestors);
|
|
6004
|
+
},
|
|
6005
|
+
TemplateLiteral(node, _state, ancestors) {
|
|
6006
|
+
if (!Array.isArray(node.quasis) || node.quasis.length === 0) return;
|
|
6007
|
+
const parent = ancestors[ancestors.length - 2];
|
|
6008
|
+
if (isExemptTemplateLiteral(node, parent)) return;
|
|
6009
|
+
const exprCount = Array.isArray(node.expressions) ? node.expressions.length : 0;
|
|
6010
|
+
if (exprCount === 0) {
|
|
6011
|
+
if (node.quasis.length !== 1) return;
|
|
6012
|
+
const val = node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
|
|
6013
|
+
if (typeof val !== "string" || activeDirectives.has(val)) return;
|
|
6014
|
+
processSaltedCandidate(val, node, parent, ancestors);
|
|
6015
|
+
return;
|
|
6016
|
+
}
|
|
6017
|
+
if (node.quasis.length !== exprCount + 1) return;
|
|
6018
|
+
if (node.quasis.some((q) => typeof q?.value?.cooked !== "string")) {
|
|
6019
|
+
return;
|
|
6020
|
+
}
|
|
6021
|
+
const replacements = /* @__PURE__ */ new Map();
|
|
6022
|
+
let queuedCount = 0;
|
|
6023
|
+
for (let qIdx = 0; qIdx < node.quasis.length; qIdx++) {
|
|
6024
|
+
const val = node.quasis[qIdx].value.cooked;
|
|
6025
|
+
if (activeDirectives.has(val)) continue;
|
|
6026
|
+
const queued = processSaltedCandidate(
|
|
6027
|
+
val,
|
|
6028
|
+
node,
|
|
6029
|
+
parent,
|
|
6030
|
+
ancestors,
|
|
6031
|
+
(exprAst) => {
|
|
6032
|
+
replacements.set(qIdx, exprAst);
|
|
6033
|
+
}
|
|
6034
|
+
);
|
|
6035
|
+
if (queued) {
|
|
6036
|
+
queuedCount++;
|
|
6037
|
+
}
|
|
6038
|
+
}
|
|
6039
|
+
if (queuedCount > 0) {
|
|
6040
|
+
interpolatedTemplates.push({ node, replacements });
|
|
6041
|
+
}
|
|
5320
6042
|
}
|
|
5321
|
-
processSaltedCandidate(val, node, parent, ancestors);
|
|
5322
6043
|
},
|
|
5323
|
-
|
|
5324
|
-
|
|
5325
|
-
if (node.expressions && node.expressions.length !== 0) return;
|
|
5326
|
-
const val = node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
|
|
5327
|
-
if (typeof val !== "string" || activeDirectives.has(val)) return;
|
|
5328
|
-
const parent = ancestors[ancestors.length - 2];
|
|
5329
|
-
if (!parent) return;
|
|
5330
|
-
if (parent.type === "TaggedTemplateExpression" && parent.quasi === node) {
|
|
5331
|
-
return;
|
|
5332
|
-
}
|
|
5333
|
-
if (parent.type === "ImportExpression" && parent.source === node) {
|
|
5334
|
-
return;
|
|
5335
|
-
}
|
|
5336
|
-
processSaltedCandidate(val, node, parent, ancestors);
|
|
5337
|
-
}
|
|
5338
|
-
});
|
|
6044
|
+
VAULT_WALK_BASE
|
|
6045
|
+
);
|
|
5339
6046
|
if (targetsToReplace.length === 0 && shaderTargets.length === 0) {
|
|
5340
6047
|
return {
|
|
5341
6048
|
encryptedStringsCount: 0,
|
|
@@ -5374,15 +6081,18 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
5374
6081
|
const flagAccess = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(flagSym) ? `.${flagSym}` : `[${JSON.stringify(flagSym)}]`;
|
|
5375
6082
|
const tkExpr = `['__','unp','eek_t','ampered'].join('')`;
|
|
5376
6083
|
const dkExpr = `['__','unp','eek_d','elta'].join('')`;
|
|
6084
|
+
const { hasLatch, latchDecl } = buildLatchInitSnippet(seed, latchCoupling);
|
|
6085
|
+
const charMaskXor = hasLatch ? "^_ld" : "";
|
|
6086
|
+
const cacheGuardSuffix = hasLatch ? "&&!_ld" : "";
|
|
5377
6087
|
let helperFnSnippet;
|
|
5378
6088
|
if (!shieldCoupled) {
|
|
5379
|
-
helperFnSnippet = `function ${decryptFnName}(i,s){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var w=typeof window<"u"?window:typeof self<"u"?self:0,_tk=${tkExpr},_dk=${dkExpr};var dv=(G&&G[_dk]!=null?G[_dk]:w&&w[_dk]!=null?w[_dk]:0)|0;var tm=(G&&(G[_tk]===!0||G${flagAccess}===!0))||(w&&(w[_tk]===!0||w${flagAccess}===!0))||((""._u||0)!==0)||(dv!==0);if(!tm&&${cacheVarName}[i]!==void 0)return ${cacheVarName}[i]
|
|
6089
|
+
helperFnSnippet = `function ${decryptFnName}(i,s){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var w=typeof window<"u"?window:typeof self<"u"?self:0,_tk=${tkExpr},_dk=${dkExpr};var dv=(G&&G[_dk]!=null?G[_dk]:w&&w[_dk]!=null?w[_dk]:0)|0;var tm=(G&&(G[_tk]===!0||G${flagAccess}===!0))||(w&&(w[_tk]===!0||w${flagAccess}===!0))||((""._u||0)!==0)||(dv!==0);if(!tm&&${cacheVarName}[i]!==void 0)return ${cacheVarName}[i];` + latchDecl + `if(${dCacheVarName}===void 0){var c=Function.prototype.toString.call(${decryptFnName}),h=0x811c9dc5;for(var l=0;l<c.length;l++)h=Math.imul(h^c.charCodeAt(l),0x01000193)>>>0;${dCacheVarName}=h|0;}var effD=tm?((${dCacheVarName}^(((Math.imul(i+1,0x9e3779b1)^0x5a5b)&65535)|1))|0):${dCacheVarName};var k=${kdfFnName}(i,s,effD);var p=${vaultVarName}[i],o="",rk=k|0;for(var j=0;j<p.length;j++){rk=(Math.imul(rk^(j+1),0x45d9f3b)+0x9e3779b9)|0;o+=String.fromCharCode(p.charCodeAt(j)^(((rk>>>16)&127)|1)${charMaskXor});}if(!tm${cacheGuardSuffix})${cacheVarName}[i]=o;return o;}`;
|
|
5380
6090
|
} else {
|
|
5381
6091
|
const edInit = buildRivestDeltaExpr(seed, (expDelta ?? 0) & 65535, "_ed");
|
|
5382
6092
|
const hasWindowKey = Boolean(sym);
|
|
5383
6093
|
const brDecl = hasWindowKey ? NARROWED_ASYNC_BR_DECL : "";
|
|
5384
6094
|
const svCheck = hasWindowKey ? `(sv===void 0?!_br:sv!==_ed)` : `(sv!==void 0&&sv!==_ed)`;
|
|
5385
|
-
helperFnSnippet = `function ${decryptFnName}(i,s){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var w=typeof window<"u"?window:typeof self<"u"?self:0,_tk=${tkExpr},_dk=${dkExpr};${edInit}` + brDecl + `var sv=G&&G${propAccess}!==void 0?G${propAccess}:w&&w${propAccess}!==void 0?w${propAccess}:void 0;var sr=G&&G${stateAccess}?G${stateAccess}:w&&w${stateAccess}?w${stateAccess}:void 0;var dv=(G&&G[_dk]!=null?G[_dk]:w&&w[_dk]!=null?w[_dk]:0)|0;var tm=(G&&(G[_tk]===!0||G${flagAccess}===!0))||(w&&(w[_tk]===!0||w${flagAccess}===!0))||(sr&&sr[0]===2)||((""._u||0)!==0)||(dv!==0)||${svCheck};if(!tm&&sv===_ed&&${cacheVarName}[i]!==void 0)return ${cacheVarName}[i]
|
|
6095
|
+
helperFnSnippet = `function ${decryptFnName}(i,s){var G=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof self<"u"?self:this;var w=typeof window<"u"?window:typeof self<"u"?self:0,_tk=${tkExpr},_dk=${dkExpr};${edInit}` + brDecl + `var sv=G&&G${propAccess}!==void 0?G${propAccess}:w&&w${propAccess}!==void 0?w${propAccess}:void 0;var sr=G&&G${stateAccess}?G${stateAccess}:w&&w${stateAccess}?w${stateAccess}:void 0;var dv=(G&&G[_dk]!=null?G[_dk]:w&&w[_dk]!=null?w[_dk]:0)|0;var tm=(G&&(G[_tk]===!0||G${flagAccess}===!0))||(w&&(w[_tk]===!0||w${flagAccess}===!0))||(sr&&sr[0]===2)||((""._u||0)!==0)||(dv!==0)||${svCheck};if(!tm&&sv===_ed&&${cacheVarName}[i]!==void 0)return ${cacheVarName}[i];` + latchDecl + `if(${dCacheVarName}===void 0){var c=Function.prototype.toString.call(${decryptFnName}),h=0x811c9dc5;for(var l=0;l<c.length;l++)h=Math.imul(h^c.charCodeAt(l),0x01000193)>>>0;${dCacheVarName}=((h|0)^_ed)|0;}var effD=tm?((${dCacheVarName}^(((Math.imul(i+1,0x9e3779b1)^0x5a5b)&65535)|1))|0):${dCacheVarName};var k=${kdfFnName}(i,s,effD);var p=${vaultVarName}[i],o="",rk=k|0;for(var j=0;j<p.length;j++){rk=(Math.imul(rk^(j+1),0x45d9f3b)+0x9e3779b9)|0;o+=String.fromCharCode(p.charCodeAt(j)^(((rk>>>16)&127)|1)${charMaskXor});}if(!tm&&sv===_ed${cacheGuardSuffix})${cacheVarName}[i]=o;return o;}`;
|
|
5386
6096
|
}
|
|
5387
6097
|
const parsedFn = acorn3.parse(helperFnSnippet, {
|
|
5388
6098
|
ecmaVersion: "latest",
|
|
@@ -5414,7 +6124,20 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
5414
6124
|
],
|
|
5415
6125
|
optional: false
|
|
5416
6126
|
};
|
|
5417
|
-
|
|
6127
|
+
if (target.onReplace) {
|
|
6128
|
+
target.onReplace(callExpr);
|
|
6129
|
+
} else if (target.replaceMode === "regexp") {
|
|
6130
|
+
replaceNodeInParent(
|
|
6131
|
+
target.parent,
|
|
6132
|
+
target.node,
|
|
6133
|
+
createNewRegExpAst(callExpr, target.flags ?? "")
|
|
6134
|
+
);
|
|
6135
|
+
} else {
|
|
6136
|
+
if (target.parent?.type === "Property" && target.parent.key === target.node) {
|
|
6137
|
+
target.parent.computed = true;
|
|
6138
|
+
}
|
|
6139
|
+
replaceNodeInParent(target.parent, target.node, callExpr);
|
|
6140
|
+
}
|
|
5418
6141
|
}
|
|
5419
6142
|
const serializedTable = JSON.stringify(encryptedPayloads);
|
|
5420
6143
|
integrityToken = computeFnv1aHash(serializedTable, seed);
|
|
@@ -5444,7 +6167,11 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
5444
6167
|
],
|
|
5445
6168
|
optional: false
|
|
5446
6169
|
};
|
|
5447
|
-
|
|
6170
|
+
if (target.onReplace) {
|
|
6171
|
+
target.onReplace(callExpr);
|
|
6172
|
+
} else {
|
|
6173
|
+
replaceNodeInParent(target.parent, target.node, callExpr);
|
|
6174
|
+
}
|
|
5448
6175
|
}
|
|
5449
6176
|
const shaderHelperStmt = createShaderVaultAst(
|
|
5450
6177
|
shaderVarName,
|
|
@@ -5460,6 +6187,7 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
5460
6187
|
integrityToken = computeFnv1aHash(JSON.stringify(shaderPayloads), seed);
|
|
5461
6188
|
}
|
|
5462
6189
|
}
|
|
6190
|
+
rebuildInterpolatedTemplates(interpolatedTemplates);
|
|
5463
6191
|
return {
|
|
5464
6192
|
encryptedStringsCount: targetsToReplace.length,
|
|
5465
6193
|
integrityToken,
|
|
@@ -5469,6 +6197,133 @@ function applySaltedStringVault(ast, seed = 2654435769, minStringLength = 4, hot
|
|
|
5469
6197
|
};
|
|
5470
6198
|
}
|
|
5471
6199
|
|
|
6200
|
+
// src/ast/secret-patterns.ts
|
|
6201
|
+
var PEM_HEADER_RE = /-----BEGIN\s+[A-Z0-9 ]+-----/;
|
|
6202
|
+
var PEM_REGEX_HEADER_RE = /-----BEGIN(?:\s|\\s[+*?]?)+(?:[A-Z0-9 ]|\\s[+*?]?)+-----/;
|
|
6203
|
+
var TOKEN_PREFIX_RE = /\b(?:sk_live_|sk_test_|pk_live_|ghp_|gho_|github_pat_|xox[baprs]-|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{35}|FLAG\{[^}]{4,}\}|eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,})/;
|
|
6204
|
+
var ESCAPED_FLAG_REGEX_RE = /\bFLAG\\\{[^}]{4,}\\\}/;
|
|
6205
|
+
var HEX_RE = /^[0-9a-fA-F]+$/;
|
|
6206
|
+
var BASE64_RE = /^[A-Za-z0-9+/]+=*$/;
|
|
6207
|
+
var TOKEN_CHARSET_RE = /^[A-Za-z0-9_\-+/=.~:@!#$%^&*]+$/;
|
|
6208
|
+
var PATH_LIKE_RE = /^(?:\.{1,2}\/|@[\w.-]+\/)|\.(?:[cm]?[jt]sx?|json|css|map|wasm|html?|node|svg|png|jpe?g|gif|woff2?|ttf|md|txt|ya?ml|toml|lock)$/i;
|
|
6209
|
+
var REGEX_CHAR_CLASS_RE = /\[(?:\\[\s\S]|[^\]\\])*\]/g;
|
|
6210
|
+
var REGEX_META_SPLIT_RE = /\(\?(?:<[=!]|<[^>]+>|[:=!])?|\\[\s\S]|[\s^$.*+?()[\]{}|/]+/;
|
|
6211
|
+
var COMMON_BIGRAMS = new Set(
|
|
6212
|
+
"th he in er an re on at en nd ti es or te of ed is it al ar st to nt ng se ha as ou io le ve co me de hi ri ro ic ne ea ra ce li ch ll be ma si om ur ca el ta la ns ge ec ct pe ol un il us ss ac em tr pr po fi ad lo ut id pa so mo wi no di ee ul et ai rs oo ex".split(" ")
|
|
6213
|
+
);
|
|
6214
|
+
var COMMON_BIGRAM_CHANCE = COMMON_BIGRAMS.size / 676;
|
|
6215
|
+
var NATURAL_TEXT_Z = 3.5;
|
|
6216
|
+
function isAsciiLetter(code) {
|
|
6217
|
+
return code >= 65 && code <= 90 || code >= 97 && code <= 122;
|
|
6218
|
+
}
|
|
6219
|
+
function isBinaryOrWasmPrefix(value) {
|
|
6220
|
+
return value.startsWith("AGFzbQE") || /^data:/i.test(value);
|
|
6221
|
+
}
|
|
6222
|
+
function countBase64MarkerRatio(value) {
|
|
6223
|
+
let hits = 0;
|
|
6224
|
+
for (let i = 0; i < value.length; i++) {
|
|
6225
|
+
const code = value.charCodeAt(i);
|
|
6226
|
+
if (code >= 48 && code <= 57 || // 0-9
|
|
6227
|
+
code === 43 || // +
|
|
6228
|
+
code === 47 || // /
|
|
6229
|
+
code === 61) {
|
|
6230
|
+
hits += 1;
|
|
6231
|
+
}
|
|
6232
|
+
}
|
|
6233
|
+
return hits / value.length;
|
|
6234
|
+
}
|
|
6235
|
+
function shannonEntropy(value) {
|
|
6236
|
+
if (value.length === 0) return 0;
|
|
6237
|
+
const counts = /* @__PURE__ */ new Map();
|
|
6238
|
+
for (let i = 0; i < value.length; i++) {
|
|
6239
|
+
const code = value.charCodeAt(i);
|
|
6240
|
+
counts.set(code, (counts.get(code) ?? 0) + 1);
|
|
6241
|
+
}
|
|
6242
|
+
let h = 0;
|
|
6243
|
+
for (const count of counts.values()) {
|
|
6244
|
+
const p = count / value.length;
|
|
6245
|
+
h -= p * Math.log2(p);
|
|
6246
|
+
}
|
|
6247
|
+
return h;
|
|
6248
|
+
}
|
|
6249
|
+
function isSequentialAlphabet(value) {
|
|
6250
|
+
if (value.length < 8) return false;
|
|
6251
|
+
let ascending = 0;
|
|
6252
|
+
for (let i = 1; i < value.length; i++) {
|
|
6253
|
+
if (value.charCodeAt(i) - value.charCodeAt(i - 1) === 1) {
|
|
6254
|
+
ascending += 1;
|
|
6255
|
+
}
|
|
6256
|
+
}
|
|
6257
|
+
return ascending / (value.length - 1) >= 0.6;
|
|
6258
|
+
}
|
|
6259
|
+
function looksLikeNaturalText(value) {
|
|
6260
|
+
let pairs = 0;
|
|
6261
|
+
let hits = 0;
|
|
6262
|
+
const lower = value.toLowerCase();
|
|
6263
|
+
for (let i = 1; i < value.length; i++) {
|
|
6264
|
+
const a = value.charCodeAt(i - 1);
|
|
6265
|
+
const b = value.charCodeAt(i);
|
|
6266
|
+
if (!isAsciiLetter(a) || !isAsciiLetter(b)) continue;
|
|
6267
|
+
pairs += 1;
|
|
6268
|
+
if (a >= 97 && b <= 90) continue;
|
|
6269
|
+
if (COMMON_BIGRAMS.has(lower[i - 1] + lower[i])) {
|
|
6270
|
+
hits += 1;
|
|
6271
|
+
}
|
|
6272
|
+
}
|
|
6273
|
+
if (pairs < 4) return false;
|
|
6274
|
+
const expected = COMMON_BIGRAM_CHANCE * pairs;
|
|
6275
|
+
const deviation = Math.sqrt(
|
|
6276
|
+
pairs * COMMON_BIGRAM_CHANCE * (1 - COMMON_BIGRAM_CHANCE)
|
|
6277
|
+
);
|
|
6278
|
+
return (hits - expected) / deviation >= NATURAL_TEXT_Z;
|
|
6279
|
+
}
|
|
6280
|
+
function classifyPlaintextSecret(value) {
|
|
6281
|
+
if (typeof value !== "string" || value.length < 8) return null;
|
|
6282
|
+
if (isBinaryOrWasmPrefix(value)) return null;
|
|
6283
|
+
if (PATH_LIKE_RE.test(value)) return null;
|
|
6284
|
+
const entropy = shannonEntropy(value);
|
|
6285
|
+
if (PEM_HEADER_RE.test(value)) {
|
|
6286
|
+
return { category: "pem-header", entropy };
|
|
6287
|
+
}
|
|
6288
|
+
if (TOKEN_PREFIX_RE.test(value)) {
|
|
6289
|
+
return { category: "token-prefix", entropy };
|
|
6290
|
+
}
|
|
6291
|
+
if (isSequentialAlphabet(value)) {
|
|
6292
|
+
return null;
|
|
6293
|
+
}
|
|
6294
|
+
const isHex = value.length >= 24 && value.length % 2 === 0 && HEX_RE.test(value) && entropy >= 3.2;
|
|
6295
|
+
if (isHex) {
|
|
6296
|
+
return { category: "hex-or-base64", entropy };
|
|
6297
|
+
}
|
|
6298
|
+
const isBase64Like = value.length >= 28 && BASE64_RE.test(value) && countBase64MarkerRatio(value) >= 0.15 && !looksLikeNaturalText(value) && entropy >= 4;
|
|
6299
|
+
if (isBase64Like) {
|
|
6300
|
+
return { category: "hex-or-base64", entropy };
|
|
6301
|
+
}
|
|
6302
|
+
if (value.length >= 20 && value.length <= 512 && TOKEN_CHARSET_RE.test(value) && !looksLikeNaturalText(value) && entropy >= 4.1) {
|
|
6303
|
+
return { category: "high-entropy", entropy };
|
|
6304
|
+
}
|
|
6305
|
+
return null;
|
|
6306
|
+
}
|
|
6307
|
+
function isLikelySecretLiteral(value) {
|
|
6308
|
+
return classifyPlaintextSecret(value) !== null;
|
|
6309
|
+
}
|
|
6310
|
+
function hasRegexSecretRun(pattern) {
|
|
6311
|
+
if (typeof pattern !== "string" || pattern.length === 0 || pattern.length > 4096) {
|
|
6312
|
+
return false;
|
|
6313
|
+
}
|
|
6314
|
+
if (PEM_HEADER_RE.test(pattern) || PEM_REGEX_HEADER_RE.test(pattern) || TOKEN_PREFIX_RE.test(pattern) || ESCAPED_FLAG_REGEX_RE.test(pattern)) {
|
|
6315
|
+
return true;
|
|
6316
|
+
}
|
|
6317
|
+
const outsideClasses = pattern.replace(REGEX_CHAR_CLASS_RE, " ");
|
|
6318
|
+
const parts = outsideClasses.split(REGEX_META_SPLIT_RE);
|
|
6319
|
+
for (const part of parts) {
|
|
6320
|
+
if (part.length >= 20 && isLikelySecretLiteral(part)) {
|
|
6321
|
+
return true;
|
|
6322
|
+
}
|
|
6323
|
+
}
|
|
6324
|
+
return false;
|
|
6325
|
+
}
|
|
6326
|
+
|
|
5472
6327
|
// src/ast/string-vault.ts
|
|
5473
6328
|
var SAFE_ALPHA = " !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~";
|
|
5474
6329
|
var DIRECTIVE_STRINGS = /* @__PURE__ */ new Set([
|
|
@@ -5653,13 +6508,10 @@ function isEligibleHotLiteral(val, node, parent, ancestors, minLen, seed = 26544
|
|
|
5653
6508
|
return false;
|
|
5654
6509
|
}
|
|
5655
6510
|
if (parent) {
|
|
5656
|
-
if (
|
|
5657
|
-
if (parent.source === node) return false;
|
|
5658
|
-
}
|
|
5659
|
-
if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
6511
|
+
if (isStaticModuleOrAssetPosition(node, parent)) {
|
|
5660
6512
|
return false;
|
|
5661
6513
|
}
|
|
5662
|
-
if (parent.type === "
|
|
6514
|
+
if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
5663
6515
|
return false;
|
|
5664
6516
|
}
|
|
5665
6517
|
}
|
|
@@ -5722,6 +6574,68 @@ function shouldEncryptString(val, minLen, parent, activeDirectives = DIRECTIVE_S
|
|
|
5722
6574
|
if (isTailwindNoise(val)) return false;
|
|
5723
6575
|
return true;
|
|
5724
6576
|
}
|
|
6577
|
+
var VAULT_WALK_BASE = {
|
|
6578
|
+
...walk4.base,
|
|
6579
|
+
Property(node, st, c) {
|
|
6580
|
+
c(node.key, st);
|
|
6581
|
+
if (node.value) c(node.value, st);
|
|
6582
|
+
}
|
|
6583
|
+
};
|
|
6584
|
+
function isCjsExportsTarget(lhs) {
|
|
6585
|
+
if (!lhs || typeof lhs !== "object") return false;
|
|
6586
|
+
if (lhs.type === "ParenthesizedExpression") {
|
|
6587
|
+
return isCjsExportsTarget(lhs.expression);
|
|
6588
|
+
}
|
|
6589
|
+
if (lhs.type === "Identifier") {
|
|
6590
|
+
return lhs.name === "exports";
|
|
6591
|
+
}
|
|
6592
|
+
if (lhs.type === "MemberExpression") {
|
|
6593
|
+
if (lhs.object?.type === "Identifier" && lhs.object.name === "module" && (!lhs.computed && lhs.property?.type === "Identifier" && lhs.property.name === "exports" || lhs.computed && lhs.property?.type === "Literal" && lhs.property.value === "exports")) {
|
|
6594
|
+
return true;
|
|
6595
|
+
}
|
|
6596
|
+
return isCjsExportsTarget(lhs.object);
|
|
6597
|
+
}
|
|
6598
|
+
return false;
|
|
6599
|
+
}
|
|
6600
|
+
function isInsideCjsExportsObject(ancestors, objIdx) {
|
|
6601
|
+
if (objIdx < 1) return false;
|
|
6602
|
+
let cur = ancestors[objIdx];
|
|
6603
|
+
for (let i = objIdx - 1; i >= 0; i--) {
|
|
6604
|
+
const anc = ancestors[i];
|
|
6605
|
+
if (!anc) break;
|
|
6606
|
+
if (anc.type === "ParenthesizedExpression") {
|
|
6607
|
+
cur = anc;
|
|
6608
|
+
continue;
|
|
6609
|
+
}
|
|
6610
|
+
if (anc.type === "AssignmentExpression" && anc.right === cur) {
|
|
6611
|
+
if (isCjsExportsTarget(anc.left)) {
|
|
6612
|
+
return true;
|
|
6613
|
+
}
|
|
6614
|
+
cur = anc;
|
|
6615
|
+
continue;
|
|
6616
|
+
}
|
|
6617
|
+
if (anc.type === "CallExpression" && anc.callee?.type === "MemberExpression" && !anc.callee.computed && anc.callee.object?.type === "Identifier" && anc.callee.object.name === "Object" && anc.callee.property?.type === "Identifier" && anc.callee.property.name === "assign" && Array.isArray(anc.arguments) && anc.arguments.slice(1).includes(cur) && isCjsExportsTarget(anc.arguments[0])) {
|
|
6618
|
+
return true;
|
|
6619
|
+
}
|
|
6620
|
+
break;
|
|
6621
|
+
}
|
|
6622
|
+
return false;
|
|
6623
|
+
}
|
|
6624
|
+
function isEligibleObjectPropertyKey(val, node, parent, ancestors) {
|
|
6625
|
+
if (!parent || parent.type !== "Property" || parent.key !== node || parent.computed) {
|
|
6626
|
+
return false;
|
|
6627
|
+
}
|
|
6628
|
+
if (val === "__proto__") return false;
|
|
6629
|
+
if (parent.kind !== "init" || parent.shorthand) return false;
|
|
6630
|
+
if (!isLikelySecretLiteral(val)) return false;
|
|
6631
|
+
const containerIdx = ancestors.length - 3;
|
|
6632
|
+
const container = containerIdx >= 0 ? ancestors[containerIdx] : void 0;
|
|
6633
|
+
if (!container || container.type === "ObjectPattern") return false;
|
|
6634
|
+
if (container.type === "ObjectExpression" && isInsideCjsExportsObject(ancestors, containerIdx)) {
|
|
6635
|
+
return false;
|
|
6636
|
+
}
|
|
6637
|
+
return true;
|
|
6638
|
+
}
|
|
5725
6639
|
function createPermutationTable(seed) {
|
|
5726
6640
|
let s = (seed || 305419896) >>> 0;
|
|
5727
6641
|
function xorshift() {
|
|
@@ -5856,7 +6770,223 @@ function findLowestCommonScope(ast, chains) {
|
|
|
5856
6770
|
}
|
|
5857
6771
|
return lca[lca.length - 1] ?? ast;
|
|
5858
6772
|
}
|
|
5859
|
-
function
|
|
6773
|
+
function isExemptTemplateLiteral(node, parent) {
|
|
6774
|
+
if (!parent) return true;
|
|
6775
|
+
if (parent.type === "TaggedTemplateExpression" && parent.quasi === node) {
|
|
6776
|
+
return true;
|
|
6777
|
+
}
|
|
6778
|
+
if (isStaticModuleOrAssetPosition(node, parent)) {
|
|
6779
|
+
return true;
|
|
6780
|
+
}
|
|
6781
|
+
return false;
|
|
6782
|
+
}
|
|
6783
|
+
function rebuildInterpolatedTemplates(plans) {
|
|
6784
|
+
for (const { node, replacements } of plans) {
|
|
6785
|
+
if (replacements.size === 0) continue;
|
|
6786
|
+
const origQuasis = node.quasis;
|
|
6787
|
+
const origExprs = node.expressions;
|
|
6788
|
+
const nextQuasis = [];
|
|
6789
|
+
const nextExprs = [];
|
|
6790
|
+
let pendingRaw = "";
|
|
6791
|
+
let pendingCooked = "";
|
|
6792
|
+
const flushQuasi = (tail) => {
|
|
6793
|
+
nextQuasis.push({
|
|
6794
|
+
type: "TemplateElement",
|
|
6795
|
+
value: { raw: pendingRaw, cooked: pendingCooked },
|
|
6796
|
+
tail
|
|
6797
|
+
});
|
|
6798
|
+
pendingRaw = "";
|
|
6799
|
+
pendingCooked = "";
|
|
6800
|
+
};
|
|
6801
|
+
for (let i = 0; i < origQuasis.length; i++) {
|
|
6802
|
+
const quasi = origQuasis[i];
|
|
6803
|
+
const replacement = replacements.get(i);
|
|
6804
|
+
if (replacement !== void 0) {
|
|
6805
|
+
flushQuasi(false);
|
|
6806
|
+
nextExprs.push(replacement);
|
|
6807
|
+
} else {
|
|
6808
|
+
pendingRaw += quasi.value?.raw ?? "";
|
|
6809
|
+
pendingCooked += quasi.value?.cooked ?? "";
|
|
6810
|
+
}
|
|
6811
|
+
if (i < origExprs.length) {
|
|
6812
|
+
flushQuasi(false);
|
|
6813
|
+
nextExprs.push(origExprs[i]);
|
|
6814
|
+
}
|
|
6815
|
+
}
|
|
6816
|
+
flushQuasi(true);
|
|
6817
|
+
node.quasis = nextQuasis;
|
|
6818
|
+
node.expressions = nextExprs;
|
|
6819
|
+
}
|
|
6820
|
+
}
|
|
6821
|
+
function createNewRegExpAst(sourceExpr, flags) {
|
|
6822
|
+
return {
|
|
6823
|
+
type: "NewExpression",
|
|
6824
|
+
callee: { type: "Identifier", name: "RegExp" },
|
|
6825
|
+
arguments: flags.length > 0 ? [
|
|
6826
|
+
sourceExpr,
|
|
6827
|
+
{
|
|
6828
|
+
type: "Literal",
|
|
6829
|
+
value: flags,
|
|
6830
|
+
raw: JSON.stringify(flags)
|
|
6831
|
+
}
|
|
6832
|
+
] : [sourceExpr]
|
|
6833
|
+
};
|
|
6834
|
+
}
|
|
6835
|
+
function mutateNodeToNewRegExp(node, sourceExpr, flags) {
|
|
6836
|
+
const newExpr = createNewRegExpAst(sourceExpr, flags);
|
|
6837
|
+
node.type = "NewExpression";
|
|
6838
|
+
node.callee = newExpr.callee;
|
|
6839
|
+
node.arguments = newExpr.arguments;
|
|
6840
|
+
delete node.value;
|
|
6841
|
+
delete node.raw;
|
|
6842
|
+
delete node.regex;
|
|
6843
|
+
delete node.optional;
|
|
6844
|
+
}
|
|
6845
|
+
var SCOPE_FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
6846
|
+
"FunctionDeclaration",
|
|
6847
|
+
"FunctionExpression",
|
|
6848
|
+
"ArrowFunctionExpression"
|
|
6849
|
+
]);
|
|
6850
|
+
var SCOPE_LEXICAL_TYPES = /* @__PURE__ */ new Set([
|
|
6851
|
+
"Program",
|
|
6852
|
+
"FunctionDeclaration",
|
|
6853
|
+
"FunctionExpression",
|
|
6854
|
+
"ArrowFunctionExpression",
|
|
6855
|
+
"BlockStatement",
|
|
6856
|
+
"ForStatement",
|
|
6857
|
+
"ForInStatement",
|
|
6858
|
+
"ForOfStatement",
|
|
6859
|
+
"SwitchStatement",
|
|
6860
|
+
"CatchClause",
|
|
6861
|
+
"ClassDeclaration",
|
|
6862
|
+
"ClassExpression",
|
|
6863
|
+
"StaticBlock"
|
|
6864
|
+
]);
|
|
6865
|
+
function patternContainsIdentifier(pattern, targetName) {
|
|
6866
|
+
if (!pattern || typeof pattern !== "object") return false;
|
|
6867
|
+
switch (pattern.type) {
|
|
6868
|
+
case "Identifier":
|
|
6869
|
+
return pattern.name === targetName;
|
|
6870
|
+
case "AssignmentPattern":
|
|
6871
|
+
return patternContainsIdentifier(pattern.left, targetName);
|
|
6872
|
+
case "RestElement":
|
|
6873
|
+
return patternContainsIdentifier(pattern.argument, targetName);
|
|
6874
|
+
case "ArrayPattern":
|
|
6875
|
+
return (pattern.elements || []).some(
|
|
6876
|
+
(el) => patternContainsIdentifier(el, targetName)
|
|
6877
|
+
);
|
|
6878
|
+
case "ObjectPattern":
|
|
6879
|
+
return (pattern.properties || []).some(
|
|
6880
|
+
(prop) => prop?.type === "Property" ? patternContainsIdentifier(prop.value, targetName) : prop?.type === "RestElement" ? patternContainsIdentifier(prop.argument, targetName) : false
|
|
6881
|
+
);
|
|
6882
|
+
default:
|
|
6883
|
+
return false;
|
|
6884
|
+
}
|
|
6885
|
+
}
|
|
6886
|
+
function findEnclosingScope(ancestors, scopeSet, fallback) {
|
|
6887
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
6888
|
+
if (scopeSet.has(ancestors[i].type)) {
|
|
6889
|
+
return ancestors[i];
|
|
6890
|
+
}
|
|
6891
|
+
}
|
|
6892
|
+
return fallback;
|
|
6893
|
+
}
|
|
6894
|
+
function collectIdentifierShadowedScopes(ast, identName = "RegExp") {
|
|
6895
|
+
const shadowed = /* @__PURE__ */ new WeakSet();
|
|
6896
|
+
walk4.ancestor(ast, {
|
|
6897
|
+
FunctionDeclaration(node, _state, ancestors) {
|
|
6898
|
+
if (node.id?.name === identName) {
|
|
6899
|
+
shadowed.add(
|
|
6900
|
+
findEnclosingScope(ancestors.slice(0, -1), SCOPE_LEXICAL_TYPES, ast)
|
|
6901
|
+
);
|
|
6902
|
+
shadowed.add(node);
|
|
6903
|
+
}
|
|
6904
|
+
if (Array.isArray(node.params) && node.params.some((p) => patternContainsIdentifier(p, identName))) {
|
|
6905
|
+
shadowed.add(node);
|
|
6906
|
+
}
|
|
6907
|
+
},
|
|
6908
|
+
FunctionExpression(node) {
|
|
6909
|
+
if (node.id?.name === identName || Array.isArray(node.params) && node.params.some((p) => patternContainsIdentifier(p, identName))) {
|
|
6910
|
+
shadowed.add(node);
|
|
6911
|
+
}
|
|
6912
|
+
},
|
|
6913
|
+
ArrowFunctionExpression(node) {
|
|
6914
|
+
if (Array.isArray(node.params) && node.params.some((p) => patternContainsIdentifier(p, identName))) {
|
|
6915
|
+
shadowed.add(node);
|
|
6916
|
+
}
|
|
6917
|
+
},
|
|
6918
|
+
ClassDeclaration(node, _state, ancestors) {
|
|
6919
|
+
if (node.id?.name === identName) {
|
|
6920
|
+
shadowed.add(
|
|
6921
|
+
findEnclosingScope(ancestors.slice(0, -1), SCOPE_LEXICAL_TYPES, ast)
|
|
6922
|
+
);
|
|
6923
|
+
shadowed.add(node);
|
|
6924
|
+
}
|
|
6925
|
+
},
|
|
6926
|
+
ClassExpression(node) {
|
|
6927
|
+
if (node.id?.name === identName) {
|
|
6928
|
+
shadowed.add(node);
|
|
6929
|
+
}
|
|
6930
|
+
},
|
|
6931
|
+
CatchClause(node) {
|
|
6932
|
+
if (node.param && patternContainsIdentifier(node.param, identName)) {
|
|
6933
|
+
shadowed.add(node);
|
|
6934
|
+
}
|
|
6935
|
+
},
|
|
6936
|
+
WithStatement(node) {
|
|
6937
|
+
shadowed.add(node);
|
|
6938
|
+
},
|
|
6939
|
+
ImportDeclaration(node) {
|
|
6940
|
+
if (Array.isArray(node.specifiers) && node.specifiers.some((s) => s?.local?.name === identName)) {
|
|
6941
|
+
shadowed.add(ast);
|
|
6942
|
+
}
|
|
6943
|
+
},
|
|
6944
|
+
VariableDeclaration(node, _state, ancestors) {
|
|
6945
|
+
if (!Array.isArray(node.declarations) || !node.declarations.some(
|
|
6946
|
+
(d) => patternContainsIdentifier(d?.id, identName)
|
|
6947
|
+
)) {
|
|
6948
|
+
return;
|
|
6949
|
+
}
|
|
6950
|
+
if (node.kind === "var") {
|
|
6951
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
6952
|
+
const a = ancestors[i];
|
|
6953
|
+
if (SCOPE_FUNCTION_TYPES.has(a.type) || a.type === "StaticBlock" || a.type === "Program") {
|
|
6954
|
+
shadowed.add(a);
|
|
6955
|
+
break;
|
|
6956
|
+
}
|
|
6957
|
+
}
|
|
6958
|
+
} else {
|
|
6959
|
+
shadowed.add(
|
|
6960
|
+
findEnclosingScope(ancestors.slice(0, -1), SCOPE_LEXICAL_TYPES, ast)
|
|
6961
|
+
);
|
|
6962
|
+
}
|
|
6963
|
+
}
|
|
6964
|
+
});
|
|
6965
|
+
return shadowed;
|
|
6966
|
+
}
|
|
6967
|
+
function isEligibleSecretRegExp(node, ancestors, hotExemptNodes, exemptSubtreeRoots, getShadowedScopes) {
|
|
6968
|
+
const pattern = node?.regex?.pattern;
|
|
6969
|
+
if (typeof pattern !== "string" || pattern.length === 0 || pattern.length > 4096) {
|
|
6970
|
+
return false;
|
|
6971
|
+
}
|
|
6972
|
+
if (!hasRegexSecretRun(pattern)) {
|
|
6973
|
+
return false;
|
|
6974
|
+
}
|
|
6975
|
+
if (isNodeInExemptSubtree(ancestors, exemptSubtreeRoots)) {
|
|
6976
|
+
return false;
|
|
6977
|
+
}
|
|
6978
|
+
if (isInsideLoopOrSerialized(ancestors)) {
|
|
6979
|
+
return false;
|
|
6980
|
+
}
|
|
6981
|
+
if (isNodeInExemptSubtree(ancestors, hotExemptNodes)) {
|
|
6982
|
+
return false;
|
|
6983
|
+
}
|
|
6984
|
+
if (getShadowedScopes && isNodeInExemptSubtree(ancestors, getShadowedScopes())) {
|
|
6985
|
+
return false;
|
|
6986
|
+
}
|
|
6987
|
+
return true;
|
|
6988
|
+
}
|
|
6989
|
+
function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExemptNodes, windowKeySymbol, expectedDelta, deterrentMode, config, shieldCoupled = true, detectHeadless = false, exemptSubtreeRoots, latchCoupling) {
|
|
5860
6990
|
const minLen = config?.vault?.minStringLength ?? minStringLength;
|
|
5861
6991
|
const activeDirectives = new Set(DIRECTIVE_STRINGS);
|
|
5862
6992
|
if (config?.vault?.customDirectives) {
|
|
@@ -5905,7 +7035,8 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
5905
7035
|
sym,
|
|
5906
7036
|
SAFE_ALPHA,
|
|
5907
7037
|
shieldCoupled,
|
|
5908
|
-
browserOnly
|
|
7038
|
+
browserOnly,
|
|
7039
|
+
latchCoupling
|
|
5909
7040
|
);
|
|
5910
7041
|
const compactFnStr = generateCompactJs(fnAst);
|
|
5911
7042
|
const buildH = computeIntegrityHash(compactFnStr, 0);
|
|
@@ -5917,23 +7048,22 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
5917
7048
|
const shaderPayloads = [];
|
|
5918
7049
|
const targetsToReplace = [];
|
|
5919
7050
|
const hotTargetsToReplace = [];
|
|
5920
|
-
const
|
|
5921
|
-
|
|
5922
|
-
|
|
5923
|
-
|
|
5924
|
-
|
|
5925
|
-
}
|
|
7051
|
+
const interpolatedTemplates = [];
|
|
7052
|
+
let regExpShadowedScopes;
|
|
7053
|
+
const getRegExpShadowedScopes = () => {
|
|
7054
|
+
regExpShadowedScopes ??= collectIdentifierShadowedScopes(ast, "RegExp");
|
|
7055
|
+
return regExpShadowedScopes;
|
|
5926
7056
|
};
|
|
5927
|
-
function processVaultCandidate(val, node, parent, ancestors) {
|
|
7057
|
+
function processVaultCandidate(val, node, parent, ancestors, onReplace) {
|
|
5928
7058
|
if (isNodeInExemptSubtree(ancestors, exemptSubtreeRoots)) {
|
|
5929
|
-
return;
|
|
7059
|
+
return false;
|
|
5930
7060
|
}
|
|
5931
7061
|
if (parent.type === "ExpressionStatement" && (Boolean(parent.directive) || activeDirectives.has(val))) {
|
|
5932
|
-
return;
|
|
7062
|
+
return false;
|
|
5933
7063
|
}
|
|
5934
7064
|
for (let i = ancestors.length - 2; i >= 0; i--) {
|
|
5935
7065
|
if (ancestors[i].type === "ExpressionStatement" && Boolean(ancestors[i].directive)) {
|
|
5936
|
-
return;
|
|
7066
|
+
return false;
|
|
5937
7067
|
}
|
|
5938
7068
|
}
|
|
5939
7069
|
let isHotExempt = false;
|
|
@@ -5965,9 +7095,10 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
5965
7095
|
node,
|
|
5966
7096
|
parent,
|
|
5967
7097
|
idx: idx2,
|
|
5968
|
-
scopeChain: extractScopeChain(ast, ancestors)
|
|
7098
|
+
scopeChain: extractScopeChain(ast, ancestors),
|
|
7099
|
+
...onReplace ? { onReplace } : {}
|
|
5969
7100
|
});
|
|
5970
|
-
return;
|
|
7101
|
+
return true;
|
|
5971
7102
|
}
|
|
5972
7103
|
if (isHotExempt) {
|
|
5973
7104
|
if (isEligibleHotLiteral(
|
|
@@ -5986,10 +7117,12 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
5986
7117
|
node,
|
|
5987
7118
|
parent,
|
|
5988
7119
|
val,
|
|
5989
|
-
idx: hotTargetsToReplace.length
|
|
7120
|
+
idx: hotTargetsToReplace.length,
|
|
7121
|
+
...onReplace ? { onReplace } : {}
|
|
5990
7122
|
});
|
|
7123
|
+
return true;
|
|
5991
7124
|
}
|
|
5992
|
-
return;
|
|
7125
|
+
return false;
|
|
5993
7126
|
}
|
|
5994
7127
|
if (!shouldEncryptString(
|
|
5995
7128
|
val,
|
|
@@ -5998,7 +7131,7 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
5998
7131
|
activeDirectives,
|
|
5999
7132
|
customReserved
|
|
6000
7133
|
)) {
|
|
6001
|
-
return;
|
|
7134
|
+
return false;
|
|
6002
7135
|
}
|
|
6003
7136
|
let idx = stringToIndex.get(val);
|
|
6004
7137
|
if (idx === void 0) {
|
|
@@ -6011,46 +7144,102 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
6011
7144
|
node,
|
|
6012
7145
|
parent,
|
|
6013
7146
|
idx,
|
|
6014
|
-
scopeChain: extractScopeChain(ast, ancestors)
|
|
7147
|
+
scopeChain: extractScopeChain(ast, ancestors),
|
|
7148
|
+
...onReplace ? { onReplace } : {}
|
|
6015
7149
|
});
|
|
7150
|
+
return true;
|
|
6016
7151
|
}
|
|
6017
7152
|
walk4.ancestor(
|
|
6018
7153
|
ast,
|
|
6019
7154
|
{
|
|
6020
7155
|
Literal(node, _state, ancestors) {
|
|
7156
|
+
if (node.regex) {
|
|
7157
|
+
if (isEligibleSecretRegExp(
|
|
7158
|
+
node,
|
|
7159
|
+
ancestors,
|
|
7160
|
+
hotExemptNodes,
|
|
7161
|
+
exemptSubtreeRoots,
|
|
7162
|
+
getRegExpShadowedScopes
|
|
7163
|
+
)) {
|
|
7164
|
+
const pattern = node.regex.pattern;
|
|
7165
|
+
const flags = node.regex.flags ?? "";
|
|
7166
|
+
const parent2 = ancestors[ancestors.length - 2];
|
|
7167
|
+
let idx = stringToIndex.get(pattern);
|
|
7168
|
+
if (idx === void 0) {
|
|
7169
|
+
idx = uniqueStrings.length;
|
|
7170
|
+
stringToIndex.set(pattern, idx);
|
|
7171
|
+
uniqueStrings.push(pattern);
|
|
7172
|
+
}
|
|
7173
|
+
targetsToReplace.push({
|
|
7174
|
+
type: "string",
|
|
7175
|
+
node,
|
|
7176
|
+
parent: parent2,
|
|
7177
|
+
idx,
|
|
7178
|
+
scopeChain: extractScopeChain(ast, ancestors),
|
|
7179
|
+
replaceMode: "regexp",
|
|
7180
|
+
flags
|
|
7181
|
+
});
|
|
7182
|
+
}
|
|
7183
|
+
return;
|
|
7184
|
+
}
|
|
6021
7185
|
if (typeof node.value !== "string") return;
|
|
6022
7186
|
const val = node.value;
|
|
6023
7187
|
if (activeDirectives.has(val)) return;
|
|
6024
7188
|
const parent = ancestors[ancestors.length - 2];
|
|
6025
7189
|
if (!parent) return;
|
|
6026
|
-
if (
|
|
6027
|
-
|
|
7190
|
+
if (isStaticModuleOrAssetPosition(node, parent)) {
|
|
7191
|
+
return;
|
|
6028
7192
|
}
|
|
6029
7193
|
if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
|
|
6030
7194
|
return;
|
|
6031
7195
|
}
|
|
6032
|
-
if (parent.type === "
|
|
6033
|
-
|
|
7196
|
+
if (parent.type === "Property" && parent.key === node && !parent.computed) {
|
|
7197
|
+
if (!isEligibleObjectPropertyKey(val, node, parent, ancestors)) {
|
|
7198
|
+
return;
|
|
7199
|
+
}
|
|
6034
7200
|
}
|
|
6035
7201
|
processVaultCandidate(val, node, parent, ancestors);
|
|
6036
7202
|
},
|
|
6037
7203
|
TemplateLiteral(node, _state, ancestors) {
|
|
6038
|
-
if (!node.quasis || node.quasis.length
|
|
6039
|
-
if (node.expressions && node.expressions.length !== 0) return;
|
|
6040
|
-
const val = node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
|
|
6041
|
-
if (typeof val !== "string" || activeDirectives.has(val)) return;
|
|
7204
|
+
if (!Array.isArray(node.quasis) || node.quasis.length === 0) return;
|
|
6042
7205
|
const parent = ancestors[ancestors.length - 2];
|
|
6043
|
-
if (
|
|
6044
|
-
|
|
7206
|
+
if (isExemptTemplateLiteral(node, parent)) return;
|
|
7207
|
+
const exprCount = Array.isArray(node.expressions) ? node.expressions.length : 0;
|
|
7208
|
+
if (exprCount === 0) {
|
|
7209
|
+
if (node.quasis.length !== 1) return;
|
|
7210
|
+
const val = node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
|
|
7211
|
+
if (typeof val !== "string" || activeDirectives.has(val)) return;
|
|
7212
|
+
processVaultCandidate(val, node, parent, ancestors);
|
|
6045
7213
|
return;
|
|
6046
7214
|
}
|
|
6047
|
-
if (
|
|
7215
|
+
if (node.quasis.length !== exprCount + 1) return;
|
|
7216
|
+
if (node.quasis.some((q) => typeof q?.value?.cooked !== "string")) {
|
|
6048
7217
|
return;
|
|
6049
7218
|
}
|
|
6050
|
-
|
|
7219
|
+
const replacements = /* @__PURE__ */ new Map();
|
|
7220
|
+
let queuedCount = 0;
|
|
7221
|
+
for (let qIdx = 0; qIdx < node.quasis.length; qIdx++) {
|
|
7222
|
+
const val = node.quasis[qIdx].value.cooked;
|
|
7223
|
+
if (activeDirectives.has(val)) continue;
|
|
7224
|
+
const queued = processVaultCandidate(
|
|
7225
|
+
val,
|
|
7226
|
+
node,
|
|
7227
|
+
parent,
|
|
7228
|
+
ancestors,
|
|
7229
|
+
(exprAst) => {
|
|
7230
|
+
replacements.set(qIdx, exprAst);
|
|
7231
|
+
}
|
|
7232
|
+
);
|
|
7233
|
+
if (queued) {
|
|
7234
|
+
queuedCount++;
|
|
7235
|
+
}
|
|
7236
|
+
}
|
|
7237
|
+
if (queuedCount > 0) {
|
|
7238
|
+
interpolatedTemplates.push({ node, replacements });
|
|
7239
|
+
}
|
|
6051
7240
|
}
|
|
6052
7241
|
},
|
|
6053
|
-
|
|
7242
|
+
VAULT_WALK_BASE
|
|
6054
7243
|
);
|
|
6055
7244
|
if (targetsToReplace.length === 0 && hotTargetsToReplace.length === 0) {
|
|
6056
7245
|
return {
|
|
@@ -6059,38 +7248,69 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
6059
7248
|
shaderBuffersCount: 0
|
|
6060
7249
|
};
|
|
6061
7250
|
}
|
|
6062
|
-
for (const { node, parent, val, idx } of hotTargetsToReplace) {
|
|
7251
|
+
for (const { node, parent, val, idx, onReplace } of hotTargetsToReplace) {
|
|
6063
7252
|
const key = idx % 3 + 1;
|
|
6064
7253
|
let encodedStr = "";
|
|
6065
7254
|
for (let i = 0; i < val.length; i++) {
|
|
6066
7255
|
encodedStr += String.fromCharCode(val.charCodeAt(i) ^ key);
|
|
6067
7256
|
}
|
|
7257
|
+
const callExpr = {
|
|
7258
|
+
type: "CallExpression",
|
|
7259
|
+
callee: {
|
|
7260
|
+
type: "Identifier",
|
|
7261
|
+
name: "_hX"
|
|
7262
|
+
},
|
|
7263
|
+
arguments: [
|
|
7264
|
+
{
|
|
7265
|
+
type: "Literal",
|
|
7266
|
+
value: encodedStr,
|
|
7267
|
+
raw: JSON.stringify(encodedStr)
|
|
7268
|
+
},
|
|
7269
|
+
{
|
|
7270
|
+
type: "Literal",
|
|
7271
|
+
value: key,
|
|
7272
|
+
raw: String(key)
|
|
7273
|
+
}
|
|
7274
|
+
],
|
|
7275
|
+
optional: false
|
|
7276
|
+
};
|
|
7277
|
+
if (onReplace) {
|
|
7278
|
+
onReplace(callExpr);
|
|
7279
|
+
continue;
|
|
7280
|
+
}
|
|
6068
7281
|
if (parent?.type === "Property" && parent.key === node) {
|
|
6069
7282
|
parent.computed = true;
|
|
6070
7283
|
}
|
|
6071
7284
|
node.type = "CallExpression";
|
|
6072
|
-
node.callee =
|
|
6073
|
-
|
|
6074
|
-
name: "_hX"
|
|
6075
|
-
};
|
|
6076
|
-
node.arguments = [
|
|
6077
|
-
{
|
|
6078
|
-
type: "Literal",
|
|
6079
|
-
value: encodedStr,
|
|
6080
|
-
raw: JSON.stringify(encodedStr)
|
|
6081
|
-
},
|
|
6082
|
-
{
|
|
6083
|
-
type: "Literal",
|
|
6084
|
-
value: key,
|
|
6085
|
-
raw: String(key)
|
|
6086
|
-
}
|
|
6087
|
-
];
|
|
7285
|
+
node.callee = callExpr.callee;
|
|
7286
|
+
node.arguments = callExpr.arguments;
|
|
6088
7287
|
node.optional = false;
|
|
6089
7288
|
delete node.value;
|
|
6090
7289
|
delete node.raw;
|
|
6091
7290
|
delete node.quasis;
|
|
6092
7291
|
delete node.expressions;
|
|
6093
7292
|
}
|
|
7293
|
+
function applyVaultTargetReplacement(target, callExpr) {
|
|
7294
|
+
if (target.onReplace) {
|
|
7295
|
+
target.onReplace(callExpr);
|
|
7296
|
+
return;
|
|
7297
|
+
}
|
|
7298
|
+
if (target.replaceMode === "regexp") {
|
|
7299
|
+
mutateNodeToNewRegExp(target.node, callExpr, target.flags ?? "");
|
|
7300
|
+
return;
|
|
7301
|
+
}
|
|
7302
|
+
if (target.parent?.type === "Property" && target.parent.key === target.node) {
|
|
7303
|
+
target.parent.computed = true;
|
|
7304
|
+
}
|
|
7305
|
+
target.node.type = "CallExpression";
|
|
7306
|
+
target.node.callee = callExpr.callee;
|
|
7307
|
+
target.node.arguments = callExpr.arguments;
|
|
7308
|
+
target.node.optional = false;
|
|
7309
|
+
delete target.node.value;
|
|
7310
|
+
delete target.node.raw;
|
|
7311
|
+
delete target.node.quasis;
|
|
7312
|
+
delete target.node.expressions;
|
|
7313
|
+
}
|
|
6094
7314
|
let insertIdx = findVaultInsertIndex(ast, activeDirectives);
|
|
6095
7315
|
const wantFragmented = Boolean(config?.vault?.fragmented);
|
|
6096
7316
|
let fragCount = Math.max(2, Math.min(8, Math.floor(config?.vault?.fragmentCount ?? 3)));
|
|
@@ -6107,28 +7327,28 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
6107
7327
|
);
|
|
6108
7328
|
}
|
|
6109
7329
|
integrityToken = computeFnv1aHash(JSON.stringify(encryptedPayloads), seed);
|
|
6110
|
-
for (const
|
|
6111
|
-
|
|
6112
|
-
|
|
6113
|
-
|
|
6114
|
-
|
|
6115
|
-
|
|
6116
|
-
|
|
6117
|
-
|
|
7330
|
+
for (const target of targetsToReplace) {
|
|
7331
|
+
let callExpr;
|
|
7332
|
+
if (target.type === "shader") {
|
|
7333
|
+
callExpr = {
|
|
7334
|
+
type: "CallExpression",
|
|
7335
|
+
callee: { type: "Identifier", name: shaderVarName },
|
|
7336
|
+
arguments: [{ type: "Literal", value: target.idx, raw: String(target.idx) }],
|
|
7337
|
+
optional: false
|
|
7338
|
+
};
|
|
6118
7339
|
} else {
|
|
6119
|
-
const salt = computeCallSiteSalt(seed, idx);
|
|
6120
|
-
|
|
6121
|
-
|
|
6122
|
-
|
|
6123
|
-
|
|
6124
|
-
|
|
6125
|
-
|
|
6126
|
-
|
|
6127
|
-
|
|
6128
|
-
|
|
6129
|
-
|
|
6130
|
-
|
|
6131
|
-
delete node.expressions;
|
|
7340
|
+
const salt = computeCallSiteSalt(seed, target.idx);
|
|
7341
|
+
callExpr = {
|
|
7342
|
+
type: "CallExpression",
|
|
7343
|
+
callee: { type: "Identifier", name: decryptFnName },
|
|
7344
|
+
arguments: [
|
|
7345
|
+
{ type: "Literal", value: target.idx, raw: String(target.idx) },
|
|
7346
|
+
{ type: "Literal", value: salt, raw: String(salt) }
|
|
7347
|
+
],
|
|
7348
|
+
optional: false
|
|
7349
|
+
};
|
|
7350
|
+
}
|
|
7351
|
+
applyVaultTargetReplacement(target, callExpr);
|
|
6132
7352
|
}
|
|
6133
7353
|
} else {
|
|
6134
7354
|
const perm = [];
|
|
@@ -6171,7 +7391,8 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
6171
7391
|
sym,
|
|
6172
7392
|
SAFE_ALPHA,
|
|
6173
7393
|
shieldCoupled,
|
|
6174
|
-
browserOnly
|
|
7394
|
+
browserOnly,
|
|
7395
|
+
latchCoupling
|
|
6175
7396
|
);
|
|
6176
7397
|
const buildHF = computeIntegrityHash(generateCompactJs(fnAstF), 0);
|
|
6177
7398
|
const expectedKeyF = (buildHF ^ 0 ^ (shieldCoupled ? (expDelta ?? 0) & 65535 : 0)) >>> 0;
|
|
@@ -6196,31 +7417,31 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
6196
7417
|
chains: []
|
|
6197
7418
|
});
|
|
6198
7419
|
}
|
|
6199
|
-
for (const
|
|
6200
|
-
|
|
6201
|
-
|
|
6202
|
-
|
|
6203
|
-
|
|
6204
|
-
|
|
6205
|
-
|
|
6206
|
-
|
|
7420
|
+
for (const target of targetsToReplace) {
|
|
7421
|
+
let callExpr;
|
|
7422
|
+
if (target.type === "shader") {
|
|
7423
|
+
callExpr = {
|
|
7424
|
+
type: "CallExpression",
|
|
7425
|
+
callee: { type: "Identifier", name: shaderVarName },
|
|
7426
|
+
arguments: [{ type: "Literal", value: target.idx, raw: String(target.idx) }],
|
|
7427
|
+
optional: false
|
|
7428
|
+
};
|
|
6207
7429
|
} else {
|
|
6208
|
-
const { fragId, localIdx } = stringFragAssign[idx];
|
|
7430
|
+
const { fragId, localIdx } = stringFragAssign[target.idx];
|
|
6209
7431
|
const meta = fragMeta[fragId];
|
|
6210
|
-
meta.chains.push(scopeChain);
|
|
7432
|
+
meta.chains.push(target.scopeChain);
|
|
6211
7433
|
const salt = computeCallSiteSalt(meta.fragSeed, localIdx);
|
|
6212
|
-
|
|
6213
|
-
|
|
6214
|
-
|
|
6215
|
-
|
|
6216
|
-
|
|
6217
|
-
|
|
6218
|
-
|
|
6219
|
-
|
|
6220
|
-
|
|
6221
|
-
|
|
6222
|
-
|
|
6223
|
-
delete node.expressions;
|
|
7434
|
+
callExpr = {
|
|
7435
|
+
type: "CallExpression",
|
|
7436
|
+
callee: { type: "Identifier", name: meta.fnName },
|
|
7437
|
+
arguments: [
|
|
7438
|
+
{ type: "Literal", value: localIdx, raw: String(localIdx) },
|
|
7439
|
+
{ type: "Literal", value: salt, raw: String(salt) }
|
|
7440
|
+
],
|
|
7441
|
+
optional: false
|
|
7442
|
+
};
|
|
7443
|
+
}
|
|
7444
|
+
applyVaultTargetReplacement(target, callExpr);
|
|
6224
7445
|
}
|
|
6225
7446
|
const allPayloads = [];
|
|
6226
7447
|
for (const meta of fragMeta) {
|
|
@@ -6243,6 +7464,7 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
|
|
|
6243
7464
|
}
|
|
6244
7465
|
integrityToken = computeFnv1aHash(JSON.stringify(allPayloads), seed);
|
|
6245
7466
|
}
|
|
7467
|
+
rebuildInterpolatedTemplates(interpolatedTemplates);
|
|
6246
7468
|
if (hotTargetsToReplace.length > 0) {
|
|
6247
7469
|
const alreadyHasHx = ast.body.some(
|
|
6248
7470
|
(stmt) => stmt.type === "FunctionDeclaration" && stmt.id?.name === "_hX"
|
|
@@ -6464,14 +7686,55 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
6464
7686
|
}
|
|
6465
7687
|
const manglerConfig = config?.mangler;
|
|
6466
7688
|
const scopeRecords = /* @__PURE__ */ new Map();
|
|
7689
|
+
const protectedScopeNodes = /* @__PURE__ */ new Set();
|
|
6467
7690
|
const barrierOrFreeIds = /* @__PURE__ */ new Set();
|
|
6468
7691
|
const renamedTargetNodes = /* @__PURE__ */ new WeakSet();
|
|
6469
7692
|
let counter = 0;
|
|
7693
|
+
function hasStrictDirective(stmts) {
|
|
7694
|
+
if (!Array.isArray(stmts)) return false;
|
|
7695
|
+
for (const stmt of stmts) {
|
|
7696
|
+
if (stmt?.type !== "ExpressionStatement") break;
|
|
7697
|
+
const isStrLit = stmt.expression?.type === "Literal" && typeof stmt.expression.value === "string";
|
|
7698
|
+
if (!stmt.directive && !isStrLit) break;
|
|
7699
|
+
if (stmt.directive === "use strict" || stmt.expression?.value === "use strict") {
|
|
7700
|
+
return true;
|
|
7701
|
+
}
|
|
7702
|
+
}
|
|
7703
|
+
return false;
|
|
7704
|
+
}
|
|
7705
|
+
function isSloppyFunctionNode(fnNode, ancestors) {
|
|
7706
|
+
if (fnNode.type === "ArrowFunctionExpression") return false;
|
|
7707
|
+
if (ast.sourceType === "module") return false;
|
|
7708
|
+
const parent = ancestors.length >= 2 ? ancestors[ancestors.length - 2] : null;
|
|
7709
|
+
if (parent && (parent.type === "MethodDefinition" || parent.type === "Property" && (parent.method || parent.kind === "get" || parent.kind === "set"))) {
|
|
7710
|
+
return false;
|
|
7711
|
+
}
|
|
7712
|
+
for (let i = 0; i < ancestors.length - 1; i++) {
|
|
7713
|
+
const anc = ancestors[i];
|
|
7714
|
+
if (anc.type === "ClassDeclaration" || anc.type === "ClassExpression") {
|
|
7715
|
+
return false;
|
|
7716
|
+
}
|
|
7717
|
+
if (anc.type === "Program" && hasStrictDirective(anc.body)) {
|
|
7718
|
+
return false;
|
|
7719
|
+
}
|
|
7720
|
+
if (FUNCTION_TYPES.has(anc.type) && anc.body?.type === "BlockStatement" && hasStrictDirective(anc.body.body)) {
|
|
7721
|
+
return false;
|
|
7722
|
+
}
|
|
7723
|
+
}
|
|
7724
|
+
if (fnNode.body?.type === "BlockStatement" && hasStrictDirective(fnNode.body.body)) {
|
|
7725
|
+
return false;
|
|
7726
|
+
}
|
|
7727
|
+
return true;
|
|
7728
|
+
}
|
|
6470
7729
|
function ensureScopeChain(ancestors) {
|
|
7730
|
+
const insideWith = ancestors.some((a) => a.type === "WithStatement");
|
|
6471
7731
|
let prevScope = null;
|
|
6472
7732
|
for (let i = 0; i < ancestors.length; i++) {
|
|
6473
7733
|
const node = ancestors[i];
|
|
6474
7734
|
if (!LEXICAL_SCOPE_TYPES.has(node.type)) continue;
|
|
7735
|
+
if (insideWith) {
|
|
7736
|
+
protectedScopeNodes.add(node);
|
|
7737
|
+
}
|
|
6475
7738
|
let record = scopeRecords.get(node);
|
|
6476
7739
|
if (!record) {
|
|
6477
7740
|
const isFn = FUNCTION_TYPES.has(node.type);
|
|
@@ -6558,16 +7821,45 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
6558
7821
|
}
|
|
6559
7822
|
function registerFunctionParams(fnNode, ancestors) {
|
|
6560
7823
|
ensureScopeChain(ancestors);
|
|
6561
|
-
|
|
7824
|
+
const params = fnNode.params || [];
|
|
7825
|
+
const duplicateParamNames = /* @__PURE__ */ new Set();
|
|
7826
|
+
const allSimpleParams = params.every((p) => p?.type === "Identifier");
|
|
7827
|
+
if (allSimpleParams && params.length > 1 && isSloppyFunctionNode(fnNode, ancestors)) {
|
|
7828
|
+
const seen = /* @__PURE__ */ new Set();
|
|
7829
|
+
for (const p of params) {
|
|
7830
|
+
if (seen.has(p.name)) {
|
|
7831
|
+
duplicateParamNames.add(p.name);
|
|
7832
|
+
}
|
|
7833
|
+
seen.add(p.name);
|
|
7834
|
+
}
|
|
7835
|
+
}
|
|
7836
|
+
for (const param of params) {
|
|
6562
7837
|
const targets = [];
|
|
6563
7838
|
collectPatternTargets(param, targets);
|
|
6564
7839
|
for (const target of targets) {
|
|
6565
|
-
|
|
7840
|
+
const allowMangle = !duplicateParamNames.has(target.idNode.name);
|
|
7841
|
+
registerBindingTarget(fnNode, target, allowMangle, fnNode);
|
|
6566
7842
|
}
|
|
6567
7843
|
}
|
|
6568
7844
|
}
|
|
6569
7845
|
ensureScopeChain([ast]);
|
|
6570
7846
|
walk5.ancestor(ast, {
|
|
7847
|
+
CallExpression(node, _state, ancestors) {
|
|
7848
|
+
if (!node.optional && node.callee?.type === "Identifier" && node.callee.name === "eval") {
|
|
7849
|
+
for (const anc of ancestors) {
|
|
7850
|
+
if (LEXICAL_SCOPE_TYPES.has(anc.type)) {
|
|
7851
|
+
protectedScopeNodes.add(anc);
|
|
7852
|
+
}
|
|
7853
|
+
}
|
|
7854
|
+
}
|
|
7855
|
+
},
|
|
7856
|
+
WithStatement(_node, _state, ancestors) {
|
|
7857
|
+
for (const anc of ancestors) {
|
|
7858
|
+
if (LEXICAL_SCOPE_TYPES.has(anc.type)) {
|
|
7859
|
+
protectedScopeNodes.add(anc);
|
|
7860
|
+
}
|
|
7861
|
+
}
|
|
7862
|
+
},
|
|
6571
7863
|
BlockStatement(_node, _state, ancestors) {
|
|
6572
7864
|
ensureScopeChain(ancestors);
|
|
6573
7865
|
},
|
|
@@ -6680,6 +7972,14 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
6680
7972
|
}
|
|
6681
7973
|
}
|
|
6682
7974
|
});
|
|
7975
|
+
for (const scopeNode of protectedScopeNodes) {
|
|
7976
|
+
const scope = scopeRecords.get(scopeNode);
|
|
7977
|
+
if (!scope) continue;
|
|
7978
|
+
for (const entry of scope.bindings.values()) {
|
|
7979
|
+
entry.shouldMangle = false;
|
|
7980
|
+
barrierOrFreeIds.add(entry.origName);
|
|
7981
|
+
}
|
|
7982
|
+
}
|
|
6683
7983
|
function resolveBinding(name, ancestors) {
|
|
6684
7984
|
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
6685
7985
|
const scope = scopeRecords.get(ancestors[i]);
|
|
@@ -6846,6 +8146,35 @@ function applyPolymorphicMangler(ast, seed = 2654435769, serializedFunctionNodes
|
|
|
6846
8146
|
// src/ast/cold-flow-guard.ts
|
|
6847
8147
|
import * as acorn5 from "acorn";
|
|
6848
8148
|
import * as walk6 from "acorn-walk";
|
|
8149
|
+
function deriveColdFlowSuffix(seed) {
|
|
8150
|
+
return (seed >>> 0).toString(36).slice(0, 4);
|
|
8151
|
+
}
|
|
8152
|
+
function deriveColdAliasInitWords(seed) {
|
|
8153
|
+
const words = [
|
|
8154
|
+
mixSeed32(seed, 101),
|
|
8155
|
+
mixSeed32(seed, 103),
|
|
8156
|
+
mixSeed32(seed, 107),
|
|
8157
|
+
mixSeed32(seed, 109),
|
|
8158
|
+
mixSeed32(seed, 113),
|
|
8159
|
+
mixSeed32(seed, 127),
|
|
8160
|
+
mixSeed32(seed, 131),
|
|
8161
|
+
mixSeed32(seed, 137)
|
|
8162
|
+
];
|
|
8163
|
+
const latchByte = (words[0] ^ words[1]) & 255;
|
|
8164
|
+
if (latchByte === 0 || latchByte === 255) {
|
|
8165
|
+
words[1] = words[1] ^ 90 | 0;
|
|
8166
|
+
}
|
|
8167
|
+
return words;
|
|
8168
|
+
}
|
|
8169
|
+
function deriveColdLatchCoupling(seed) {
|
|
8170
|
+
const sfx = deriveColdFlowSuffix(seed);
|
|
8171
|
+
const initWords = deriveColdAliasInitWords(seed);
|
|
8172
|
+
const expectedLatchByte = (initWords[0] ^ initWords[1]) & 255;
|
|
8173
|
+
return {
|
|
8174
|
+
latchFnName: `_uLr${sfx}`,
|
|
8175
|
+
expectedLatchByte
|
|
8176
|
+
};
|
|
8177
|
+
}
|
|
6849
8178
|
var NEUTRAL_DECOY_KEYS = [
|
|
6850
8179
|
"telemetryOk",
|
|
6851
8180
|
"retryBudget",
|
|
@@ -6869,6 +8198,26 @@ function mixSeed32(seed, salt) {
|
|
|
6869
8198
|
h ^= h >>> 16;
|
|
6870
8199
|
return h | 0;
|
|
6871
8200
|
}
|
|
8201
|
+
function modInverseUint32(oddValue) {
|
|
8202
|
+
const m = (oddValue | 1) >>> 0;
|
|
8203
|
+
let inv = 1;
|
|
8204
|
+
for (let i = 0; i < 5; i++) {
|
|
8205
|
+
inv = Math.imul(inv, 2 - Math.imul(m, inv) | 0) >>> 0;
|
|
8206
|
+
}
|
|
8207
|
+
return inv >>> 0;
|
|
8208
|
+
}
|
|
8209
|
+
function deriveAffineMbaParams(seed, salt) {
|
|
8210
|
+
let m = (mixSeed32(seed, salt) | 1) >>> 0;
|
|
8211
|
+
if (m === 1 || m === 4294967295 || m === 2654435769) {
|
|
8212
|
+
m = (m ^ 1540483477 | 1) >>> 0;
|
|
8213
|
+
}
|
|
8214
|
+
const m2 = Math.imul(m, 2) >>> 0;
|
|
8215
|
+
const mInv = modInverseUint32(m);
|
|
8216
|
+
let c = mixSeed32(seed, salt + 40503) >>> 0;
|
|
8217
|
+
if (c === 0) c = 668265263;
|
|
8218
|
+
const d = -Math.imul(mInv, c) >>> 0;
|
|
8219
|
+
return { m, m2, mInv, c, d };
|
|
8220
|
+
}
|
|
6872
8221
|
function cloneNode(node) {
|
|
6873
8222
|
return JSON.parse(JSON.stringify(node));
|
|
6874
8223
|
}
|
|
@@ -7416,6 +8765,14 @@ function makePropAccess(objExpr, propName) {
|
|
|
7416
8765
|
property: makeLiteral(propName)
|
|
7417
8766
|
};
|
|
7418
8767
|
}
|
|
8768
|
+
function makeSafeAliasRead(aliasIdent, slot) {
|
|
8769
|
+
return {
|
|
8770
|
+
type: "ConditionalExpression",
|
|
8771
|
+
test: makeBinary("<", makeUnary("typeof", { type: "Identifier", name: aliasIdent }), makeLiteral("u")),
|
|
8772
|
+
consequent: makeMemberIndex(aliasIdent, makeLiteral(slot)),
|
|
8773
|
+
alternate: makeLiteral(0)
|
|
8774
|
+
};
|
|
8775
|
+
}
|
|
7419
8776
|
function buildQnrBitExpr(qnrValRef, bitKind) {
|
|
7420
8777
|
const shiftAmt = bitKind === "xor1" ? 1 : 2;
|
|
7421
8778
|
const combOp = bitKind === "xor1" ? "^" : "|";
|
|
@@ -7435,8 +8792,8 @@ function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArro
|
|
|
7435
8792
|
const mba4 = buildMba4Ast(seed, fnIndex, x1, x2, x3, x4);
|
|
7436
8793
|
const qnrFamilyIdx = ((seed ^ Math.imul(fnIndex + 1, 2654435761)) >>> 0) % QNR_RESIDUE_FAMILIES.length;
|
|
7437
8794
|
const qnrFamily = QNR_RESIDUE_FAMILIES[qnrFamilyIdx];
|
|
7438
|
-
const slotA = fnIndex + (seed &
|
|
7439
|
-
const slotB = slotA
|
|
8795
|
+
const slotA = 2 + (fnIndex + (seed & 1) & 1);
|
|
8796
|
+
const slotB = slotA ^ 1;
|
|
7440
8797
|
const hasAlias = Boolean(ctxOpts?.aliasIdentA);
|
|
7441
8798
|
const aliasReadIdent = ctxOpts?.aliasIdentB ?? ctxOpts?.aliasIdentA ?? "";
|
|
7442
8799
|
let qnrLeft;
|
|
@@ -7617,7 +8974,8 @@ function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArro
|
|
|
7617
8974
|
zRefForDecoy
|
|
7618
8975
|
);
|
|
7619
8976
|
const hasShieldCoupling = Boolean(ctxOpts?.shieldCoupled && ctxOpts?.windowKeySymbol);
|
|
7620
|
-
const
|
|
8977
|
+
const predOffset = hasShieldCoupling ? 1 : (seed >>> 2) % 6;
|
|
8978
|
+
const predForm = ((fnIndex + predOffset) % 6 + 6) % 6;
|
|
7621
8979
|
let testExpr;
|
|
7622
8980
|
if (predForm === 0) {
|
|
7623
8981
|
let pathACore = qnrValForForm0;
|
|
@@ -7628,7 +8986,7 @@ function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArro
|
|
|
7628
8986
|
pathACore = makeBinary(
|
|
7629
8987
|
"^",
|
|
7630
8988
|
pathACore,
|
|
7631
|
-
makeBinary("&",
|
|
8989
|
+
makeBinary("&", makeSafeAliasRead(aliasReadIdent, slotB), makeLiteral(1))
|
|
7632
8990
|
);
|
|
7633
8991
|
}
|
|
7634
8992
|
const pathA = ctxOpts?.scratchVarIdent ? {
|
|
@@ -7638,7 +8996,7 @@ function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArro
|
|
|
7638
8996
|
const pathB = hasAlias ? makeBinary(
|
|
7639
8997
|
"^",
|
|
7640
8998
|
makeLiteral(qnrFamily.expectedBit),
|
|
7641
|
-
makeBinary("&",
|
|
8999
|
+
makeBinary("&", makeSafeAliasRead(ctxOpts.aliasIdentA, slotB), makeLiteral(1))
|
|
7642
9000
|
) : makeLiteral(qnrFamily.expectedBit);
|
|
7643
9001
|
testExpr = makeBinary("!==", makeBinary("^", pathA, pathB), makeLiteral(0));
|
|
7644
9002
|
} else if (predForm === 1) {
|
|
@@ -7685,21 +9043,92 @@ function createSotaMbaPredicate(seed, fnIndex = 0, paramNode, decoyIdent, isArro
|
|
|
7685
9043
|
right: tamperCheck
|
|
7686
9044
|
};
|
|
7687
9045
|
} else if (predForm === 2) {
|
|
7688
|
-
const slotReadB = hasAlias ? makeBinary("&",
|
|
7689
|
-
const slotWriteB = hasAlias ? makeBinary("&",
|
|
9046
|
+
const slotReadB = hasAlias ? makeBinary("&", makeSafeAliasRead(aliasReadIdent, slotB), makeLiteral(7)) : makeBinary("&", makeTypeofGlobalLen(), makeLiteral(7));
|
|
9047
|
+
const slotWriteB = hasAlias ? makeBinary("&", makeSafeAliasRead(ctxOpts.aliasIdentA, slotB), makeLiteral(7)) : makeBinary("&", makeTypeofGlobalLen(), makeLiteral(7));
|
|
7690
9048
|
let lhsExpr = makeBinary("+", qnrLeft, slotReadB);
|
|
7691
9049
|
if (aliasInvZero) {
|
|
7692
9050
|
lhsExpr = makeBinary("+", lhsExpr, makeBinary("&", aliasInvZero, makeLiteral(3)));
|
|
7693
9051
|
}
|
|
7694
9052
|
const rhsExpr = makeBinary("+", makeLiteral(qnrFamily.outBoundVal), slotWriteB);
|
|
7695
9053
|
testExpr = makeBinary(qnrFamily.outBoundOp, lhsExpr, rhsExpr);
|
|
7696
|
-
} else {
|
|
7697
|
-
const tagLhs = hasAlias ? makeBinary("&",
|
|
7698
|
-
const tagRhsBase = hasAlias ? makeBinary("&",
|
|
9054
|
+
} else if (predForm === 3) {
|
|
9055
|
+
const tagLhs = hasAlias ? makeBinary("&", makeSafeAliasRead(ctxOpts.aliasIdentA, slotB), makeLiteral(240)) : makeBinary("&", makeBinary("<<", makeTypeofGlobalLen(), makeLiteral(4)), makeLiteral(240));
|
|
9056
|
+
const tagRhsBase = hasAlias ? makeBinary("&", makeSafeAliasRead(aliasReadIdent, slotB), makeLiteral(240)) : makeBinary("&", makeBinary("<<", makeTypeofGlobalLen(), makeLiteral(4)), makeLiteral(240));
|
|
7699
9057
|
const tagRhs = aliasInvZero ? makeBinary("^", tagRhsBase, makeBinary("&", aliasInvZero, makeLiteral(15))) : tagRhsBase;
|
|
7700
9058
|
const lhsExpr = makeBinary("|", makeBinary("^", qnrLeft, tagLhs), makeLiteral(0));
|
|
7701
9059
|
const rhsExpr = makeBinary("|", makeBinary("^", makeLiteral(qnrFamily.target), tagRhs), makeLiteral(0));
|
|
7702
9060
|
testExpr = makeBinary("===", lhsExpr, rhsExpr);
|
|
9061
|
+
} else if (predForm === 4) {
|
|
9062
|
+
const k4 = mixSeed32(seed, fnIndex * 29 + 13) >>> 0 || 1;
|
|
9063
|
+
if (ctxOpts?.scratchVarIdent) {
|
|
9064
|
+
const scratchId = { type: "Identifier", name: ctxOpts.scratchVarIdent };
|
|
9065
|
+
const preU = aliasInvZero ? makeBinary("^", cloneNode(scratchId), aliasInvZero) : cloneNode(scratchId);
|
|
9066
|
+
const uAssign = {
|
|
9067
|
+
type: "AssignmentExpression",
|
|
9068
|
+
operator: "=",
|
|
9069
|
+
left: cloneNode(scratchId),
|
|
9070
|
+
right: makeBinary(">>>", makeBinary("^", preU, makeLiteral(k4)), makeLiteral(0))
|
|
9071
|
+
};
|
|
9072
|
+
const uMinus1Lhs = makeBinary(">>>", makeBinary("-", cloneNode(scratchId), makeLiteral(1)), makeLiteral(0));
|
|
9073
|
+
const uMinus1Rhs = makeBinary(">>>", makeBinary("-", cloneNode(scratchId), makeLiteral(1)), makeLiteral(0));
|
|
9074
|
+
const orU = makeBinary(">>>", makeBinary("|", cloneNode(scratchId), uMinus1Lhs), makeLiteral(0));
|
|
9075
|
+
const xorU = makeBinary(">>>", makeBinary("^", cloneNode(scratchId), uMinus1Rhs), makeLiteral(0));
|
|
9076
|
+
const lhsSeq = {
|
|
9077
|
+
type: "SequenceExpression",
|
|
9078
|
+
expressions: [...qnrLeft.expressions.slice(0, -1), uAssign, orU]
|
|
9079
|
+
};
|
|
9080
|
+
testExpr = makeBinary("<", lhsSeq, xorU);
|
|
9081
|
+
} else {
|
|
9082
|
+
const uExpr = makeBinary(">>>", makeBinary("^", cloneNode(zRefForDecoy), makeLiteral(k4)), makeLiteral(0));
|
|
9083
|
+
const uMinus1Lhs = makeBinary(">>>", makeBinary("-", cloneNode(uExpr), makeLiteral(1)), makeLiteral(0));
|
|
9084
|
+
const uMinus1Rhs = makeBinary(">>>", makeBinary("-", cloneNode(uExpr), makeLiteral(1)), makeLiteral(0));
|
|
9085
|
+
const orU = makeBinary(">>>", makeBinary("|", cloneNode(uExpr), uMinus1Lhs), makeLiteral(0));
|
|
9086
|
+
const xorU = makeBinary(">>>", makeBinary("^", cloneNode(uExpr), uMinus1Rhs), makeLiteral(0));
|
|
9087
|
+
testExpr = makeBinary("<", orU, xorU);
|
|
9088
|
+
}
|
|
9089
|
+
} else {
|
|
9090
|
+
const k5 = mixSeed32(seed, fnIndex * 31 + 17) >>> 0 || 3;
|
|
9091
|
+
const { m, m2, mInv, c, d } = deriveAffineMbaParams(seed, fnIndex * 37 + 23);
|
|
9092
|
+
const buildAffineLhs = (valNode) => {
|
|
9093
|
+
const inner = makeBinary(
|
|
9094
|
+
"|",
|
|
9095
|
+
makeBinary(
|
|
9096
|
+
"+",
|
|
9097
|
+
makeBinary(
|
|
9098
|
+
"+",
|
|
9099
|
+
makeImul(makeLiteral(m), makeBinary("^", cloneNode(valNode), makeLiteral(k5))),
|
|
9100
|
+
makeImul(makeLiteral(m2), makeBinary("&", cloneNode(valNode), makeLiteral(k5)))
|
|
9101
|
+
),
|
|
9102
|
+
makeLiteral(c)
|
|
9103
|
+
),
|
|
9104
|
+
makeLiteral(0)
|
|
9105
|
+
);
|
|
9106
|
+
return makeBinary(
|
|
9107
|
+
"|",
|
|
9108
|
+
makeBinary("+", makeImul(makeLiteral(mInv), inner), makeLiteral(d)),
|
|
9109
|
+
makeLiteral(0)
|
|
9110
|
+
);
|
|
9111
|
+
};
|
|
9112
|
+
const buildAffineRhs = (valNode) => makeBinary("|", makeBinary("+", cloneNode(valNode), makeLiteral(k5)), makeLiteral(0));
|
|
9113
|
+
if (ctxOpts?.scratchVarIdent) {
|
|
9114
|
+
const scratchId = { type: "Identifier", name: ctxOpts.scratchVarIdent };
|
|
9115
|
+
const preSeqs = [...qnrLeft.expressions.slice(0, -1)];
|
|
9116
|
+
if (aliasInvZero) {
|
|
9117
|
+
preSeqs.push({
|
|
9118
|
+
type: "AssignmentExpression",
|
|
9119
|
+
operator: "=",
|
|
9120
|
+
left: cloneNode(scratchId),
|
|
9121
|
+
right: makeBinary("|", makeBinary("^", cloneNode(scratchId), aliasInvZero), makeLiteral(0))
|
|
9122
|
+
});
|
|
9123
|
+
}
|
|
9124
|
+
const lhsSeq = {
|
|
9125
|
+
type: "SequenceExpression",
|
|
9126
|
+
expressions: [...preSeqs, buildAffineLhs(scratchId)]
|
|
9127
|
+
};
|
|
9128
|
+
testExpr = makeBinary("!==", lhsSeq, buildAffineRhs(scratchId));
|
|
9129
|
+
} else {
|
|
9130
|
+
testExpr = makeBinary("!==", buildAffineLhs(zRefForDecoy), buildAffineRhs(zRefForDecoy));
|
|
9131
|
+
}
|
|
7703
9132
|
}
|
|
7704
9133
|
return {
|
|
7705
9134
|
type: "IfStatement",
|
|
@@ -7746,290 +9175,998 @@ function inferFunctionReturnDomain(fnNode) {
|
|
|
7746
9175
|
}
|
|
7747
9176
|
return;
|
|
7748
9177
|
}
|
|
7749
|
-
for (const key of Object.keys(node)) {
|
|
7750
|
-
if (key === "loc" || key === "range") continue;
|
|
7751
|
-
const val = node[key];
|
|
7752
|
-
if (val && typeof val === "object") {
|
|
7753
|
-
scanStmts(val);
|
|
7754
|
-
}
|
|
9178
|
+
for (const key of Object.keys(node)) {
|
|
9179
|
+
if (key === "loc" || key === "range") continue;
|
|
9180
|
+
const val = node[key];
|
|
9181
|
+
if (val && typeof val === "object") {
|
|
9182
|
+
scanStmts(val);
|
|
9183
|
+
}
|
|
9184
|
+
}
|
|
9185
|
+
}
|
|
9186
|
+
scanStmts(fnNode.body);
|
|
9187
|
+
if (!hasReturnArg && fnNode.body?.type === "BlockStatement") return "void";
|
|
9188
|
+
if (seenObject) return "object";
|
|
9189
|
+
if (seenBoolean && !seenNumber && !seenString) return "boolean";
|
|
9190
|
+
if (seenNumber && !seenBoolean && !seenString) return "number";
|
|
9191
|
+
if (seenString && !seenNumber && !seenBoolean) return "string";
|
|
9192
|
+
return "object";
|
|
9193
|
+
}
|
|
9194
|
+
function buildSboxInitExpression(seed) {
|
|
9195
|
+
return acorn5.parseExpressionAt(
|
|
9196
|
+
`(function(){var a=[],i,s=${seed | 0},j,t;for(i=0;i<256;i++)a[i]=i;for(i=255;i>0;i--){s=(Math.imul(s,1664525)+1013904223)|0;j=(s>>>0)%(i+1);t=a[i];a[i]=a[j];a[j]=t;}return a;})()`,
|
|
9197
|
+
0,
|
|
9198
|
+
{ ecmaVersion: "latest" }
|
|
9199
|
+
);
|
|
9200
|
+
}
|
|
9201
|
+
function createColdFlowRuntimeDeclarations(sboxIdent, aliasIdentA, aliasIdentB, scratchVarIdent, seed, latchFnName, mxIdent) {
|
|
9202
|
+
const sboxInit = buildSboxInitExpression(seed);
|
|
9203
|
+
const initWords = deriveColdAliasInitWords(seed);
|
|
9204
|
+
const wordsArrayExpr = {
|
|
9205
|
+
type: "ArrayExpression",
|
|
9206
|
+
elements: initWords.map((v) => makeInt32Literal(v))
|
|
9207
|
+
};
|
|
9208
|
+
const newArrayBuffer32 = {
|
|
9209
|
+
type: "NewExpression",
|
|
9210
|
+
callee: { type: "Identifier", name: "ArrayBuffer" },
|
|
9211
|
+
arguments: [makeLiteral(32)]
|
|
9212
|
+
};
|
|
9213
|
+
const declarations = [
|
|
9214
|
+
{
|
|
9215
|
+
type: "VariableDeclarator",
|
|
9216
|
+
id: { type: "Identifier", name: sboxIdent },
|
|
9217
|
+
init: sboxInit
|
|
9218
|
+
},
|
|
9219
|
+
{
|
|
9220
|
+
type: "VariableDeclarator",
|
|
9221
|
+
id: { type: "Identifier", name: aliasIdentA },
|
|
9222
|
+
init: {
|
|
9223
|
+
type: "NewExpression",
|
|
9224
|
+
callee: { type: "Identifier", name: "Uint32Array" },
|
|
9225
|
+
arguments: [newArrayBuffer32]
|
|
9226
|
+
}
|
|
9227
|
+
},
|
|
9228
|
+
{
|
|
9229
|
+
type: "VariableDeclarator",
|
|
9230
|
+
id: { type: "Identifier", name: aliasIdentB },
|
|
9231
|
+
init: {
|
|
9232
|
+
type: "NewExpression",
|
|
9233
|
+
callee: { type: "Identifier", name: "Int32Array" },
|
|
9234
|
+
arguments: [
|
|
9235
|
+
{
|
|
9236
|
+
type: "MemberExpression",
|
|
9237
|
+
computed: false,
|
|
9238
|
+
object: { type: "Identifier", name: aliasIdentA },
|
|
9239
|
+
property: { type: "Identifier", name: "buffer" }
|
|
9240
|
+
}
|
|
9241
|
+
]
|
|
9242
|
+
}
|
|
9243
|
+
},
|
|
9244
|
+
{
|
|
9245
|
+
type: "VariableDeclarator",
|
|
9246
|
+
id: { type: "Identifier", name: scratchVarIdent },
|
|
9247
|
+
init: makeLiteral(0)
|
|
9248
|
+
}
|
|
9249
|
+
];
|
|
9250
|
+
const varDecl = {
|
|
9251
|
+
type: "VariableDeclaration",
|
|
9252
|
+
kind: "var",
|
|
9253
|
+
declarations
|
|
9254
|
+
};
|
|
9255
|
+
const aliasInitStmt = {
|
|
9256
|
+
type: "ExpressionStatement",
|
|
9257
|
+
expression: {
|
|
9258
|
+
type: "CallExpression",
|
|
9259
|
+
callee: {
|
|
9260
|
+
type: "MemberExpression",
|
|
9261
|
+
computed: false,
|
|
9262
|
+
object: { type: "Identifier", name: aliasIdentB },
|
|
9263
|
+
property: { type: "Identifier", name: "set" }
|
|
9264
|
+
},
|
|
9265
|
+
arguments: [cloneNode(wordsArrayExpr)],
|
|
9266
|
+
optional: false
|
|
9267
|
+
}
|
|
9268
|
+
};
|
|
9269
|
+
const wordsList = initWords.map((w) => w | 0).join(",");
|
|
9270
|
+
const latchFnParsed = acorn5.parse(
|
|
9271
|
+
`function ${latchFnName}(){try{return(${aliasIdentA}!==void 0&&${aliasIdentB}!==void 0)||(${aliasIdentA}=new Uint32Array(new ArrayBuffer(32)),${aliasIdentB}=new Int32Array(${aliasIdentA}.buffer),${aliasIdentB}.set([${wordsList}])),((${aliasIdentA}[0]^${aliasIdentB}[1])&255)}catch(e){return -1}}`,
|
|
9272
|
+
{ ecmaVersion: "latest", sourceType: "script" }
|
|
9273
|
+
);
|
|
9274
|
+
const latchFnDecl = latchFnParsed.body[0];
|
|
9275
|
+
let mxFnDecl;
|
|
9276
|
+
if (mxIdent) {
|
|
9277
|
+
const mbaAst = buildMba4Ast(
|
|
9278
|
+
seed,
|
|
9279
|
+
0,
|
|
9280
|
+
{ type: "Identifier", name: "$u" },
|
|
9281
|
+
{ type: "Identifier", name: "$v" },
|
|
9282
|
+
{ type: "Identifier", name: "$p" },
|
|
9283
|
+
{ type: "Identifier", name: "$q" }
|
|
9284
|
+
);
|
|
9285
|
+
const slotExpr = makeBinary("+", makeLiteral(2), makeBinary("&", { type: "Identifier", name: "$c" }, makeLiteral(1)));
|
|
9286
|
+
const lazyAliasGuard = {
|
|
9287
|
+
type: "ExpressionStatement",
|
|
9288
|
+
expression: {
|
|
9289
|
+
type: "LogicalExpression",
|
|
9290
|
+
operator: "||",
|
|
9291
|
+
left: {
|
|
9292
|
+
type: "LogicalExpression",
|
|
9293
|
+
operator: "&&",
|
|
9294
|
+
left: makeBinary("!==", { type: "Identifier", name: aliasIdentA }, makeUnary("void", makeLiteral(0))),
|
|
9295
|
+
right: makeBinary("!==", { type: "Identifier", name: aliasIdentB }, makeUnary("void", makeLiteral(0)))
|
|
9296
|
+
},
|
|
9297
|
+
right: {
|
|
9298
|
+
type: "LogicalExpression",
|
|
9299
|
+
operator: "||",
|
|
9300
|
+
left: makeBinary("!==", makeUnary("typeof", { type: "Identifier", name: latchFnName }), makeLiteral("function")),
|
|
9301
|
+
right: {
|
|
9302
|
+
type: "CallExpression",
|
|
9303
|
+
callee: { type: "Identifier", name: latchFnName },
|
|
9304
|
+
arguments: [],
|
|
9305
|
+
optional: false
|
|
9306
|
+
}
|
|
9307
|
+
}
|
|
9308
|
+
}
|
|
9309
|
+
};
|
|
9310
|
+
const lazySboxGuard = {
|
|
9311
|
+
type: "ExpressionStatement",
|
|
9312
|
+
expression: {
|
|
9313
|
+
type: "LogicalExpression",
|
|
9314
|
+
operator: "||",
|
|
9315
|
+
left: { type: "Identifier", name: sboxIdent },
|
|
9316
|
+
right: {
|
|
9317
|
+
type: "AssignmentExpression",
|
|
9318
|
+
operator: "=",
|
|
9319
|
+
left: { type: "Identifier", name: sboxIdent },
|
|
9320
|
+
right: buildSboxInitExpression(seed)
|
|
9321
|
+
}
|
|
9322
|
+
}
|
|
9323
|
+
};
|
|
9324
|
+
mxFnDecl = {
|
|
9325
|
+
type: "FunctionDeclaration",
|
|
9326
|
+
id: { type: "Identifier", name: mxIdent },
|
|
9327
|
+
params: [
|
|
9328
|
+
{ type: "Identifier", name: "$a" },
|
|
9329
|
+
{ type: "Identifier", name: "$b" },
|
|
9330
|
+
{ type: "Identifier", name: "$c" },
|
|
9331
|
+
{ type: "Identifier", name: "$d" },
|
|
9332
|
+
{ type: "Identifier", name: "$e" },
|
|
9333
|
+
{ type: "Identifier", name: "$f" },
|
|
9334
|
+
{ type: "Identifier", name: "$g" },
|
|
9335
|
+
{ type: "Identifier", name: "$h" },
|
|
9336
|
+
{ type: "Identifier", name: "$i" }
|
|
9337
|
+
],
|
|
9338
|
+
body: {
|
|
9339
|
+
type: "BlockStatement",
|
|
9340
|
+
body: [
|
|
9341
|
+
{
|
|
9342
|
+
type: "TryStatement",
|
|
9343
|
+
block: {
|
|
9344
|
+
type: "BlockStatement",
|
|
9345
|
+
body: [
|
|
9346
|
+
lazyAliasGuard,
|
|
9347
|
+
lazySboxGuard,
|
|
9348
|
+
{
|
|
9349
|
+
type: "VariableDeclaration",
|
|
9350
|
+
kind: "var",
|
|
9351
|
+
declarations: [
|
|
9352
|
+
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$s" }, init: slotExpr },
|
|
9353
|
+
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$u" }, init: makeBinary("|", { type: "Identifier", name: "$a" }, makeLiteral(0)) },
|
|
9354
|
+
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$v" }, init: makeBinary("|", { type: "Identifier", name: "$b" }, makeLiteral(0)) },
|
|
9355
|
+
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$p" }, init: makeBinary("|", makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$s" }), makeLiteral(0)) },
|
|
9356
|
+
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$q" }, init: makeBinary("|", makeMemberIndex(aliasIdentB, { type: "Identifier", name: "$d" }), makeLiteral(0)) },
|
|
9357
|
+
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$z" }, init: mbaAst }
|
|
9358
|
+
]
|
|
9359
|
+
},
|
|
9360
|
+
{
|
|
9361
|
+
type: "ExpressionStatement",
|
|
9362
|
+
expression: {
|
|
9363
|
+
type: "AssignmentExpression",
|
|
9364
|
+
operator: "=",
|
|
9365
|
+
left: { type: "Identifier", name: "$z" },
|
|
9366
|
+
right: makeBinary(
|
|
9367
|
+
"|",
|
|
9368
|
+
makeBinary(
|
|
9369
|
+
"+",
|
|
9370
|
+
{ type: "Identifier", name: "$f" },
|
|
9371
|
+
makeImul(
|
|
9372
|
+
{ type: "Identifier", name: "$z" },
|
|
9373
|
+
makeBinary(
|
|
9374
|
+
"+",
|
|
9375
|
+
{ type: "Identifier", name: "$g" },
|
|
9376
|
+
makeImul(
|
|
9377
|
+
{ type: "Identifier", name: "$z" },
|
|
9378
|
+
makeBinary(
|
|
9379
|
+
"+",
|
|
9380
|
+
{ type: "Identifier", name: "$h" },
|
|
9381
|
+
makeImul({ type: "Identifier", name: "$z" }, { type: "Identifier", name: "$i" })
|
|
9382
|
+
)
|
|
9383
|
+
)
|
|
9384
|
+
)
|
|
9385
|
+
)
|
|
9386
|
+
),
|
|
9387
|
+
makeLiteral(0)
|
|
9388
|
+
)
|
|
9389
|
+
}
|
|
9390
|
+
},
|
|
9391
|
+
{
|
|
9392
|
+
type: "ExpressionStatement",
|
|
9393
|
+
expression: {
|
|
9394
|
+
type: "AssignmentExpression",
|
|
9395
|
+
operator: "=",
|
|
9396
|
+
left: { type: "Identifier", name: "$z" },
|
|
9397
|
+
right: buildSboxMixAst(sboxIdent, "$z")
|
|
9398
|
+
}
|
|
9399
|
+
},
|
|
9400
|
+
{
|
|
9401
|
+
type: "VariableDeclaration",
|
|
9402
|
+
kind: "var",
|
|
9403
|
+
declarations: [
|
|
9404
|
+
{
|
|
9405
|
+
type: "VariableDeclarator",
|
|
9406
|
+
id: { type: "Identifier", name: "$m" },
|
|
9407
|
+
init: makeBinary("^", makeBinary("<<", { type: "Identifier", name: "$z" }, makeLiteral(3)), { type: "Identifier", name: "$e" })
|
|
9408
|
+
}
|
|
9409
|
+
]
|
|
9410
|
+
},
|
|
9411
|
+
{
|
|
9412
|
+
type: "ExpressionStatement",
|
|
9413
|
+
expression: {
|
|
9414
|
+
type: "AssignmentExpression",
|
|
9415
|
+
operator: "=",
|
|
9416
|
+
left: makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$s" }),
|
|
9417
|
+
right: makeBinary("|", makeBinary("^", makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$s" }), { type: "Identifier", name: "$m" }), makeLiteral(0))
|
|
9418
|
+
}
|
|
9419
|
+
},
|
|
9420
|
+
{
|
|
9421
|
+
type: "VariableDeclaration",
|
|
9422
|
+
kind: "var",
|
|
9423
|
+
declarations: [
|
|
9424
|
+
{
|
|
9425
|
+
type: "VariableDeclarator",
|
|
9426
|
+
id: { type: "Identifier", name: "$w" },
|
|
9427
|
+
init: makeBinary(
|
|
9428
|
+
"^",
|
|
9429
|
+
makeBinary("|", makeBinary("^", makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$s" }), { type: "Identifier", name: "$m" }), makeLiteral(0)),
|
|
9430
|
+
{
|
|
9431
|
+
type: "AssignmentExpression",
|
|
9432
|
+
operator: "=",
|
|
9433
|
+
left: makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$s" }),
|
|
9434
|
+
right: makeBinary("|", makeBinary("^", makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$s" }), { type: "Identifier", name: "$m" }), makeLiteral(0))
|
|
9435
|
+
}
|
|
9436
|
+
)
|
|
9437
|
+
}
|
|
9438
|
+
]
|
|
9439
|
+
},
|
|
9440
|
+
{
|
|
9441
|
+
type: "ReturnStatement",
|
|
9442
|
+
argument: {
|
|
9443
|
+
type: "AssignmentExpression",
|
|
9444
|
+
operator: "=",
|
|
9445
|
+
left: { type: "Identifier", name: scratchVarIdent },
|
|
9446
|
+
right: makeBinary("|", makeBinary("^", { type: "Identifier", name: "$z" }, { type: "Identifier", name: "$w" }), makeLiteral(0))
|
|
9447
|
+
}
|
|
9448
|
+
}
|
|
9449
|
+
]
|
|
9450
|
+
},
|
|
9451
|
+
handler: {
|
|
9452
|
+
type: "CatchClause",
|
|
9453
|
+
param: { type: "Identifier", name: "$e" },
|
|
9454
|
+
body: {
|
|
9455
|
+
type: "BlockStatement",
|
|
9456
|
+
body: [{ type: "ReturnStatement", argument: makeLiteral(0) }]
|
|
9457
|
+
}
|
|
9458
|
+
},
|
|
9459
|
+
finalizer: null
|
|
9460
|
+
}
|
|
9461
|
+
]
|
|
9462
|
+
},
|
|
9463
|
+
generator: false,
|
|
9464
|
+
async: false,
|
|
9465
|
+
expression: false
|
|
9466
|
+
};
|
|
9467
|
+
}
|
|
9468
|
+
return {
|
|
9469
|
+
varDecl,
|
|
9470
|
+
aliasInitStmt,
|
|
9471
|
+
latchFnDecl,
|
|
9472
|
+
...mxFnDecl ? { mxFnDecl } : {}
|
|
9473
|
+
};
|
|
9474
|
+
}
|
|
9475
|
+
var MAX_COLD_GUARDS_PER_CHUNK = 12;
|
|
9476
|
+
var MAX_COLD_BITWISE_MBA_PER_CHUNK = 8;
|
|
9477
|
+
var BITWISE_MBA_OPS = /* @__PURE__ */ new Set(["^", "|", "&"]);
|
|
9478
|
+
var BITWISE_LOOP_NODE_TYPES = /* @__PURE__ */ new Set([
|
|
9479
|
+
"ForStatement",
|
|
9480
|
+
"WhileStatement",
|
|
9481
|
+
"DoWhileStatement",
|
|
9482
|
+
"ForInStatement",
|
|
9483
|
+
"ForOfStatement"
|
|
9484
|
+
]);
|
|
9485
|
+
var BITWISE_FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
9486
|
+
"FunctionDeclaration",
|
|
9487
|
+
"FunctionExpression",
|
|
9488
|
+
"ArrowFunctionExpression"
|
|
9489
|
+
]);
|
|
9490
|
+
var BITWISE_LEXICAL_SCOPE_TYPES = /* @__PURE__ */ new Set([
|
|
9491
|
+
"Program",
|
|
9492
|
+
"FunctionDeclaration",
|
|
9493
|
+
"FunctionExpression",
|
|
9494
|
+
"ArrowFunctionExpression",
|
|
9495
|
+
"BlockStatement",
|
|
9496
|
+
"ForStatement",
|
|
9497
|
+
"ForInStatement",
|
|
9498
|
+
"ForOfStatement",
|
|
9499
|
+
"SwitchStatement",
|
|
9500
|
+
"CatchClause",
|
|
9501
|
+
"StaticBlock"
|
|
9502
|
+
]);
|
|
9503
|
+
var FORBIDDEN_BITWISE_IDENTIFIERS = /* @__PURE__ */ new Set([
|
|
9504
|
+
"arguments",
|
|
9505
|
+
"eval",
|
|
9506
|
+
"undefined",
|
|
9507
|
+
"NaN",
|
|
9508
|
+
"Infinity",
|
|
9509
|
+
"globalThis",
|
|
9510
|
+
"window",
|
|
9511
|
+
"self",
|
|
9512
|
+
"Math",
|
|
9513
|
+
"BigInt"
|
|
9514
|
+
]);
|
|
9515
|
+
function collectBoundPatternNames(pattern, out) {
|
|
9516
|
+
if (!pattern || typeof pattern !== "object") return;
|
|
9517
|
+
switch (pattern.type) {
|
|
9518
|
+
case "Identifier":
|
|
9519
|
+
out.push(pattern.name);
|
|
9520
|
+
break;
|
|
9521
|
+
case "AssignmentPattern":
|
|
9522
|
+
collectBoundPatternNames(pattern.left, out);
|
|
9523
|
+
break;
|
|
9524
|
+
case "RestElement":
|
|
9525
|
+
collectBoundPatternNames(pattern.argument, out);
|
|
9526
|
+
break;
|
|
9527
|
+
case "ArrayPattern":
|
|
9528
|
+
for (const el of pattern.elements || []) {
|
|
9529
|
+
if (el) collectBoundPatternNames(el, out);
|
|
9530
|
+
}
|
|
9531
|
+
break;
|
|
9532
|
+
case "ObjectPattern":
|
|
9533
|
+
for (const prop of pattern.properties || []) {
|
|
9534
|
+
if (prop.type === "Property") {
|
|
9535
|
+
collectBoundPatternNames(prop.value, out);
|
|
9536
|
+
} else if (prop.type === "RestElement") {
|
|
9537
|
+
collectBoundPatternNames(prop.argument, out);
|
|
9538
|
+
}
|
|
9539
|
+
}
|
|
9540
|
+
break;
|
|
9541
|
+
}
|
|
9542
|
+
}
|
|
9543
|
+
function analyzeBitwiseMbaScopes(ast) {
|
|
9544
|
+
const scopeBindings = /* @__PURE__ */ new Map();
|
|
9545
|
+
let unsafe = false;
|
|
9546
|
+
const addBinding = (scopeNode, name) => {
|
|
9547
|
+
if (name === "Math") {
|
|
9548
|
+
unsafe = true;
|
|
9549
|
+
return;
|
|
9550
|
+
}
|
|
9551
|
+
let set = scopeBindings.get(scopeNode);
|
|
9552
|
+
if (!set) {
|
|
9553
|
+
set = /* @__PURE__ */ new Set();
|
|
9554
|
+
scopeBindings.set(scopeNode, set);
|
|
9555
|
+
}
|
|
9556
|
+
set.add(name);
|
|
9557
|
+
};
|
|
9558
|
+
const scopeStack = [];
|
|
9559
|
+
const varTargetStack = [];
|
|
9560
|
+
const getNearestLexicalScope = () => scopeStack[scopeStack.length - 1] ?? ast;
|
|
9561
|
+
const getNearestVarScope = () => varTargetStack[varTargetStack.length - 1] ?? ast;
|
|
9562
|
+
function scan(node) {
|
|
9563
|
+
if (unsafe || !node || typeof node !== "object" || typeof node.type !== "string") {
|
|
9564
|
+
return;
|
|
9565
|
+
}
|
|
9566
|
+
if (node.type === "WithStatement") {
|
|
9567
|
+
unsafe = true;
|
|
9568
|
+
return;
|
|
9569
|
+
}
|
|
9570
|
+
if (node.type === "Literal" && (typeof node.value === "bigint" || typeof node.bigint === "string") || node.type === "BigIntLiteral") {
|
|
9571
|
+
unsafe = true;
|
|
9572
|
+
return;
|
|
9573
|
+
}
|
|
9574
|
+
if (node.type === "Identifier" && node.name === "BigInt") {
|
|
9575
|
+
unsafe = true;
|
|
9576
|
+
return;
|
|
9577
|
+
}
|
|
9578
|
+
if (node.type === "ImportSpecifier" || node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") {
|
|
9579
|
+
if (node.local?.type === "Identifier") {
|
|
9580
|
+
addBinding(ast, node.local.name);
|
|
9581
|
+
}
|
|
9582
|
+
} else if (node.type === "FunctionDeclaration") {
|
|
9583
|
+
if (node.id?.type === "Identifier") {
|
|
9584
|
+
addBinding(getNearestLexicalScope(), node.id.name);
|
|
9585
|
+
addBinding(getNearestVarScope(), node.id.name);
|
|
9586
|
+
}
|
|
9587
|
+
} else if (node.type === "ClassDeclaration") {
|
|
9588
|
+
if (node.id?.type === "Identifier") {
|
|
9589
|
+
addBinding(getNearestLexicalScope(), node.id.name);
|
|
9590
|
+
}
|
|
9591
|
+
} else if (node.type === "VariableDeclaration") {
|
|
9592
|
+
const targetScope = node.kind === "var" ? getNearestVarScope() : getNearestLexicalScope();
|
|
9593
|
+
for (const decl of node.declarations || []) {
|
|
9594
|
+
const names = [];
|
|
9595
|
+
collectBoundPatternNames(decl.id, names);
|
|
9596
|
+
for (const name of names) {
|
|
9597
|
+
addBinding(targetScope, name);
|
|
9598
|
+
}
|
|
9599
|
+
}
|
|
9600
|
+
}
|
|
9601
|
+
if (unsafe) return;
|
|
9602
|
+
const pushesLexical = BITWISE_LEXICAL_SCOPE_TYPES.has(node.type);
|
|
9603
|
+
const isFn = BITWISE_FUNCTION_TYPES.has(node.type);
|
|
9604
|
+
const isStaticBlock = node.type === "StaticBlock";
|
|
9605
|
+
if (pushesLexical) {
|
|
9606
|
+
scopeStack.push(node);
|
|
9607
|
+
if (!scopeBindings.has(node)) {
|
|
9608
|
+
scopeBindings.set(node, /* @__PURE__ */ new Set());
|
|
9609
|
+
}
|
|
9610
|
+
}
|
|
9611
|
+
if (isFn) {
|
|
9612
|
+
if (node.id?.type === "Identifier") {
|
|
9613
|
+
addBinding(node, node.id.name);
|
|
9614
|
+
}
|
|
9615
|
+
for (const param of node.params || []) {
|
|
9616
|
+
const names = [];
|
|
9617
|
+
collectBoundPatternNames(param, names);
|
|
9618
|
+
for (const name of names) {
|
|
9619
|
+
addBinding(node, name);
|
|
9620
|
+
}
|
|
9621
|
+
}
|
|
9622
|
+
const bodyScope = node.body?.type === "BlockStatement" ? node.body : node;
|
|
9623
|
+
varTargetStack.push(bodyScope);
|
|
9624
|
+
} else if (node.type === "Program" || isStaticBlock) {
|
|
9625
|
+
varTargetStack.push(node);
|
|
9626
|
+
} else if (node.type === "CatchClause" && node.param) {
|
|
9627
|
+
const names = [];
|
|
9628
|
+
collectBoundPatternNames(node.param, names);
|
|
9629
|
+
for (const name of names) {
|
|
9630
|
+
addBinding(node, name);
|
|
9631
|
+
}
|
|
9632
|
+
} else if (node.type === "ClassExpression" && node.id?.type === "Identifier") {
|
|
9633
|
+
addBinding(node, node.id.name);
|
|
9634
|
+
}
|
|
9635
|
+
try {
|
|
9636
|
+
for (const key of Object.keys(node)) {
|
|
9637
|
+
if (key === "loc" || key === "range" || key === "start" || key === "end" || key === "comments" || key === "tokens") {
|
|
9638
|
+
continue;
|
|
9639
|
+
}
|
|
9640
|
+
const child = node[key];
|
|
9641
|
+
if (Array.isArray(child)) {
|
|
9642
|
+
for (const item of child) {
|
|
9643
|
+
scan(item);
|
|
9644
|
+
if (unsafe) return;
|
|
9645
|
+
}
|
|
9646
|
+
} else if (child && typeof child === "object") {
|
|
9647
|
+
scan(child);
|
|
9648
|
+
if (unsafe) return;
|
|
9649
|
+
}
|
|
9650
|
+
}
|
|
9651
|
+
} finally {
|
|
9652
|
+
if (isFn || node.type === "Program" || isStaticBlock) {
|
|
9653
|
+
varTargetStack.pop();
|
|
9654
|
+
}
|
|
9655
|
+
if (pushesLexical) {
|
|
9656
|
+
scopeStack.pop();
|
|
9657
|
+
}
|
|
9658
|
+
}
|
|
9659
|
+
}
|
|
9660
|
+
scan(ast);
|
|
9661
|
+
return { unsafe, scopeBindings };
|
|
9662
|
+
}
|
|
9663
|
+
function isSafeAtomicIntegerLiteral(node) {
|
|
9664
|
+
return Boolean(
|
|
9665
|
+
node && node.type === "Literal" && typeof node.value === "number" && Number.isInteger(node.value) && node.value >= 0 && node.value <= 4294967295
|
|
9666
|
+
);
|
|
9667
|
+
}
|
|
9668
|
+
function isZeroNumericLiteral(node) {
|
|
9669
|
+
return Boolean(node && node.type === "Literal" && typeof node.value === "number" && node.value === 0);
|
|
9670
|
+
}
|
|
9671
|
+
function isLocalBoundIdentifier(node, scopeStack, scopeBindings, programNode) {
|
|
9672
|
+
if (!node || node.type !== "Identifier") return false;
|
|
9673
|
+
if (FORBIDDEN_BITWISE_IDENTIFIERS.has(node.name)) return false;
|
|
9674
|
+
for (let i = scopeStack.length - 1; i >= 0; i--) {
|
|
9675
|
+
const scopeNode = scopeStack[i];
|
|
9676
|
+
if (scopeNode === programNode) return false;
|
|
9677
|
+
const bindings = scopeBindings.get(scopeNode);
|
|
9678
|
+
if (bindings && bindings.has(node.name)) {
|
|
9679
|
+
return true;
|
|
9680
|
+
}
|
|
9681
|
+
}
|
|
9682
|
+
return false;
|
|
9683
|
+
}
|
|
9684
|
+
function buildColdBitwiseConditional(op, left, right, seed, subIndex, hotExemptNodes) {
|
|
9685
|
+
const { m, mInv, c, d } = deriveAffineMbaParams(seed, 16640 + subIndex * 43);
|
|
9686
|
+
const a1 = cloneNode(left);
|
|
9687
|
+
const b1 = cloneNode(right);
|
|
9688
|
+
const a2 = cloneNode(left);
|
|
9689
|
+
const b2 = cloneNode(right);
|
|
9690
|
+
let affineComb;
|
|
9691
|
+
if (op === "^") {
|
|
9692
|
+
affineComb = makeBinary(
|
|
9693
|
+
"-",
|
|
9694
|
+
makeImul(makeLiteral(m), makeBinary("|", a1, b1)),
|
|
9695
|
+
makeImul(makeLiteral(m), makeBinary("&", a2, b2))
|
|
9696
|
+
);
|
|
9697
|
+
} else if (op === "|") {
|
|
9698
|
+
affineComb = makeBinary(
|
|
9699
|
+
"+",
|
|
9700
|
+
makeImul(makeLiteral(m), makeBinary("&", a1, b1)),
|
|
9701
|
+
makeImul(makeLiteral(m), makeBinary("^", a2, b2))
|
|
9702
|
+
);
|
|
9703
|
+
} else {
|
|
9704
|
+
affineComb = makeBinary(
|
|
9705
|
+
"-",
|
|
9706
|
+
makeImul(makeLiteral(m), makeBinary("|", a1, b1)),
|
|
9707
|
+
makeImul(makeLiteral(m), makeBinary("^", a2, b2))
|
|
9708
|
+
);
|
|
9709
|
+
}
|
|
9710
|
+
const innerWithC = makeBinary("|", makeBinary("+", affineComb, makeLiteral(c)), makeLiteral(0));
|
|
9711
|
+
const consequent = makeBinary(
|
|
9712
|
+
"|",
|
|
9713
|
+
makeBinary("+", makeImul(makeLiteral(mInv), innerWithC), makeLiteral(d)),
|
|
9714
|
+
makeLiteral(0)
|
|
9715
|
+
);
|
|
9716
|
+
const alternate = makeBinary(op, cloneNode(left), cloneNode(right));
|
|
9717
|
+
const exemptNodes = [];
|
|
9718
|
+
const uniqueIdents = [];
|
|
9719
|
+
const seenNames = /* @__PURE__ */ new Set();
|
|
9720
|
+
for (const operand of [left, right]) {
|
|
9721
|
+
if (operand?.type === "Identifier" && !seenNames.has(operand.name)) {
|
|
9722
|
+
seenNames.add(operand.name);
|
|
9723
|
+
uniqueIdents.push(operand);
|
|
9724
|
+
}
|
|
9725
|
+
}
|
|
9726
|
+
const makeIsNumberExpr = (ident) => {
|
|
9727
|
+
const numLit = makeLiteral("number");
|
|
9728
|
+
const eqNode = makeBinary("===", makeUnary("typeof", { type: "Identifier", name: ident.name }), numLit);
|
|
9729
|
+
exemptNodes.push(numLit, eqNode);
|
|
9730
|
+
return eqNode;
|
|
9731
|
+
};
|
|
9732
|
+
const testExpr = uniqueIdents.length === 2 ? {
|
|
9733
|
+
type: "LogicalExpression",
|
|
9734
|
+
operator: "&&",
|
|
9735
|
+
left: makeIsNumberExpr(uniqueIdents[0]),
|
|
9736
|
+
right: makeIsNumberExpr(uniqueIdents[1])
|
|
9737
|
+
} : makeIsNumberExpr(uniqueIdents[0]);
|
|
9738
|
+
const replacement = {
|
|
9739
|
+
type: "ConditionalExpression",
|
|
9740
|
+
test: testExpr,
|
|
9741
|
+
consequent,
|
|
9742
|
+
alternate,
|
|
9743
|
+
_unpeekGuardSubtree: true
|
|
9744
|
+
};
|
|
9745
|
+
if (hotExemptNodes) {
|
|
9746
|
+
hotExemptNodes.add(replacement);
|
|
9747
|
+
hotExemptNodes.add(testExpr);
|
|
9748
|
+
for (const nodeToExempt of exemptNodes) {
|
|
9749
|
+
hotExemptNodes.add(nodeToExempt);
|
|
9750
|
+
}
|
|
9751
|
+
}
|
|
9752
|
+
return replacement;
|
|
9753
|
+
}
|
|
9754
|
+
function applyColdBitwiseMba(ast, coldFunctionNodes, seed = 2654435769, hotExemptNodes, guardSubtreeNodes) {
|
|
9755
|
+
const { unsafe, scopeBindings } = analyzeBitwiseMbaScopes(ast);
|
|
9756
|
+
if (unsafe) return 0;
|
|
9757
|
+
let mbaCount = 0;
|
|
9758
|
+
const scopeStack = [];
|
|
9759
|
+
function walkTransform(node, setInParent, inColdFn) {
|
|
9760
|
+
if (mbaCount >= MAX_COLD_BITWISE_MBA_PER_CHUNK) return;
|
|
9761
|
+
if (!node || typeof node !== "object" || typeof node.type !== "string") return;
|
|
9762
|
+
if (hotExemptNodes?.has(node) || guardSubtreeNodes?.has(node)) return;
|
|
9763
|
+
if (node._unpeekGuardSubtree === true) return;
|
|
9764
|
+
if (node._unpeekSerialized || node._unpeekVirtualized === true) return;
|
|
9765
|
+
if (BITWISE_LOOP_NODE_TYPES.has(node.type)) return;
|
|
9766
|
+
const pushedScope = scopeBindings.has(node);
|
|
9767
|
+
if (pushedScope) scopeStack.push(node);
|
|
9768
|
+
try {
|
|
9769
|
+
const childInColdFn = BITWISE_FUNCTION_TYPES.has(node.type) ? coldFunctionNodes.has(node) : inColdFn;
|
|
9770
|
+
if (childInColdFn && node.type === "BinaryExpression" && BITWISE_MBA_OPS.has(node.operator)) {
|
|
9771
|
+
const op = node.operator;
|
|
9772
|
+
const { left, right } = node;
|
|
9773
|
+
const isCoercionOrZero = op === "|" && isZeroNumericLiteral(right);
|
|
9774
|
+
if (!isCoercionOrZero) {
|
|
9775
|
+
const leftAtomic = isSafeAtomicIntegerLiteral(left) || isLocalBoundIdentifier(left, scopeStack, scopeBindings, ast);
|
|
9776
|
+
const rightAtomic = isSafeAtomicIntegerLiteral(right) || isLocalBoundIdentifier(right, scopeStack, scopeBindings, ast);
|
|
9777
|
+
const hasIdent = left?.type === "Identifier" || right?.type === "Identifier";
|
|
9778
|
+
if (leftAtomic && rightAtomic && hasIdent) {
|
|
9779
|
+
const replacement = buildColdBitwiseConditional(
|
|
9780
|
+
op,
|
|
9781
|
+
left,
|
|
9782
|
+
right,
|
|
9783
|
+
seed,
|
|
9784
|
+
mbaCount,
|
|
9785
|
+
hotExemptNodes
|
|
9786
|
+
);
|
|
9787
|
+
mbaCount++;
|
|
9788
|
+
setInParent(replacement);
|
|
9789
|
+
return;
|
|
9790
|
+
}
|
|
9791
|
+
}
|
|
9792
|
+
}
|
|
9793
|
+
for (const key of Object.keys(node)) {
|
|
9794
|
+
if (mbaCount >= MAX_COLD_BITWISE_MBA_PER_CHUNK) break;
|
|
9795
|
+
if (key === "loc" || key === "range" || key === "start" || key === "end" || key === "comments" || key === "tokens") {
|
|
9796
|
+
continue;
|
|
9797
|
+
}
|
|
9798
|
+
const child = node[key];
|
|
9799
|
+
if (Array.isArray(child)) {
|
|
9800
|
+
for (let i = 0; i < child.length; i++) {
|
|
9801
|
+
if (mbaCount >= MAX_COLD_BITWISE_MBA_PER_CHUNK) break;
|
|
9802
|
+
const item = child[i];
|
|
9803
|
+
if (item && typeof item === "object" && typeof item.type === "string") {
|
|
9804
|
+
walkTransform(
|
|
9805
|
+
item,
|
|
9806
|
+
(rep) => {
|
|
9807
|
+
child[i] = rep;
|
|
9808
|
+
},
|
|
9809
|
+
childInColdFn
|
|
9810
|
+
);
|
|
9811
|
+
}
|
|
9812
|
+
}
|
|
9813
|
+
} else if (child && typeof child === "object" && typeof child.type === "string") {
|
|
9814
|
+
walkTransform(
|
|
9815
|
+
child,
|
|
9816
|
+
(rep) => {
|
|
9817
|
+
node[key] = rep;
|
|
9818
|
+
},
|
|
9819
|
+
childInColdFn
|
|
9820
|
+
);
|
|
9821
|
+
}
|
|
9822
|
+
}
|
|
9823
|
+
} finally {
|
|
9824
|
+
if (pushedScope) scopeStack.pop();
|
|
9825
|
+
}
|
|
9826
|
+
}
|
|
9827
|
+
walkTransform(ast, () => {
|
|
9828
|
+
}, false);
|
|
9829
|
+
return mbaCount;
|
|
9830
|
+
}
|
|
9831
|
+
var UNSAFE_EXPRESSION_NODE_TYPES = /* @__PURE__ */ new Set(["Super", "WithStatement"]);
|
|
9832
|
+
var UNSAFE_IF_NODE_TYPES = /* @__PURE__ */ new Set([
|
|
9833
|
+
"Super",
|
|
9834
|
+
"WithStatement",
|
|
9835
|
+
"FunctionDeclaration",
|
|
9836
|
+
"ClassDeclaration"
|
|
9837
|
+
]);
|
|
9838
|
+
function containsAstNodeType(node, types) {
|
|
9839
|
+
if (!node || typeof node !== "object") return false;
|
|
9840
|
+
if (Array.isArray(node)) {
|
|
9841
|
+
for (const item of node) {
|
|
9842
|
+
if (containsAstNodeType(item, types)) return true;
|
|
9843
|
+
}
|
|
9844
|
+
return false;
|
|
9845
|
+
}
|
|
9846
|
+
if (typeof node.type === "string" && types.has(node.type)) {
|
|
9847
|
+
return true;
|
|
9848
|
+
}
|
|
9849
|
+
for (const key of Object.keys(node)) {
|
|
9850
|
+
if (key === "loc" || key === "range" || key === "start" || key === "end" || key === "comments" || key === "tokens") {
|
|
9851
|
+
continue;
|
|
9852
|
+
}
|
|
9853
|
+
const val = node[key];
|
|
9854
|
+
if (val && typeof val === "object" && containsAstNodeType(val, types)) {
|
|
9855
|
+
return true;
|
|
7755
9856
|
}
|
|
7756
9857
|
}
|
|
7757
|
-
|
|
7758
|
-
if (!hasReturnArg && fnNode.body?.type === "BlockStatement") return "void";
|
|
7759
|
-
if (seenObject) return "object";
|
|
7760
|
-
if (seenBoolean && !seenNumber && !seenString) return "boolean";
|
|
7761
|
-
if (seenNumber && !seenBoolean && !seenString) return "number";
|
|
7762
|
-
if (seenString && !seenNumber && !seenBoolean) return "string";
|
|
7763
|
-
return "object";
|
|
9858
|
+
return false;
|
|
7764
9859
|
}
|
|
7765
|
-
function
|
|
7766
|
-
const
|
|
7767
|
-
|
|
7768
|
-
0
|
|
7769
|
-
|
|
7770
|
-
|
|
7771
|
-
|
|
7772
|
-
|
|
7773
|
-
|
|
7774
|
-
|
|
7775
|
-
|
|
7776
|
-
|
|
7777
|
-
|
|
7778
|
-
{
|
|
7779
|
-
|
|
7780
|
-
|
|
7781
|
-
|
|
9860
|
+
function collectMathUnsafeScopes(ast) {
|
|
9861
|
+
const unsafe = /* @__PURE__ */ new WeakSet();
|
|
9862
|
+
const findEnclosing = (ancestors) => {
|
|
9863
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
9864
|
+
if (BITWISE_LEXICAL_SCOPE_TYPES.has(ancestors[i].type)) {
|
|
9865
|
+
return ancestors[i];
|
|
9866
|
+
}
|
|
9867
|
+
}
|
|
9868
|
+
return ast;
|
|
9869
|
+
};
|
|
9870
|
+
const markScopeAndEnclosingFn = (ancestors, scopeNode) => {
|
|
9871
|
+
unsafe.add(scopeNode);
|
|
9872
|
+
const parent = ancestors.length >= 2 ? ancestors[ancestors.length - 2] : null;
|
|
9873
|
+
if (parent && BITWISE_FUNCTION_TYPES.has(parent.type) && parent.body === scopeNode) {
|
|
9874
|
+
unsafe.add(parent);
|
|
9875
|
+
}
|
|
9876
|
+
};
|
|
9877
|
+
const patternHasMath = (pat) => {
|
|
9878
|
+
const names = [];
|
|
9879
|
+
collectBoundPatternNames(pat, names);
|
|
9880
|
+
return names.includes("Math");
|
|
9881
|
+
};
|
|
9882
|
+
walk6.ancestor(ast, {
|
|
9883
|
+
WithStatement(node, _st, ancestors) {
|
|
9884
|
+
unsafe.add(node);
|
|
9885
|
+
for (let i = ancestors.length - 2; i >= 0; i--) {
|
|
9886
|
+
const a = ancestors[i];
|
|
9887
|
+
if (BITWISE_FUNCTION_TYPES.has(a.type)) {
|
|
9888
|
+
unsafe.add(a);
|
|
9889
|
+
break;
|
|
9890
|
+
}
|
|
9891
|
+
if (a.type === "Program") break;
|
|
9892
|
+
}
|
|
7782
9893
|
},
|
|
7783
|
-
{
|
|
7784
|
-
|
|
7785
|
-
|
|
7786
|
-
|
|
7787
|
-
|
|
7788
|
-
|
|
9894
|
+
ImportSpecifier(node) {
|
|
9895
|
+
if (node.local?.name === "Math") unsafe.add(ast);
|
|
9896
|
+
},
|
|
9897
|
+
ImportDefaultSpecifier(node) {
|
|
9898
|
+
if (node.local?.name === "Math") unsafe.add(ast);
|
|
9899
|
+
},
|
|
9900
|
+
ImportNamespaceSpecifier(node) {
|
|
9901
|
+
if (node.local?.name === "Math") unsafe.add(ast);
|
|
9902
|
+
},
|
|
9903
|
+
FunctionDeclaration(node, _st, ancestors) {
|
|
9904
|
+
if (node.id?.name === "Math") {
|
|
9905
|
+
const prefix = ancestors.slice(0, -1);
|
|
9906
|
+
markScopeAndEnclosingFn(prefix, findEnclosing(prefix));
|
|
9907
|
+
unsafe.add(node);
|
|
9908
|
+
}
|
|
9909
|
+
if (Array.isArray(node.params) && node.params.some(patternHasMath)) {
|
|
9910
|
+
unsafe.add(node);
|
|
7789
9911
|
}
|
|
7790
9912
|
},
|
|
7791
|
-
{
|
|
7792
|
-
|
|
7793
|
-
|
|
7794
|
-
|
|
9913
|
+
FunctionExpression(node) {
|
|
9914
|
+
if (node.id?.name === "Math" || Array.isArray(node.params) && node.params.some(patternHasMath)) {
|
|
9915
|
+
unsafe.add(node);
|
|
9916
|
+
}
|
|
7795
9917
|
},
|
|
7796
|
-
{
|
|
7797
|
-
|
|
7798
|
-
|
|
7799
|
-
|
|
7800
|
-
}
|
|
7801
|
-
|
|
7802
|
-
|
|
7803
|
-
|
|
7804
|
-
|
|
7805
|
-
|
|
7806
|
-
|
|
7807
|
-
|
|
7808
|
-
|
|
7809
|
-
|
|
7810
|
-
|
|
7811
|
-
|
|
7812
|
-
|
|
7813
|
-
|
|
7814
|
-
|
|
7815
|
-
|
|
7816
|
-
|
|
7817
|
-
|
|
7818
|
-
|
|
7819
|
-
|
|
7820
|
-
|
|
7821
|
-
|
|
7822
|
-
|
|
7823
|
-
|
|
7824
|
-
|
|
7825
|
-
|
|
7826
|
-
|
|
7827
|
-
|
|
7828
|
-
{
|
|
7829
|
-
type: "VariableDeclaration",
|
|
7830
|
-
kind: "var",
|
|
7831
|
-
declarations: [
|
|
7832
|
-
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$u" }, init: makeBinary("|", { type: "Identifier", name: "$a" }, makeLiteral(0)) },
|
|
7833
|
-
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$v" }, init: makeBinary("|", { type: "Identifier", name: "$b" }, makeLiteral(0)) },
|
|
7834
|
-
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$p" }, init: makeBinary("|", makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$c" }), makeLiteral(0)) },
|
|
7835
|
-
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$q" }, init: makeBinary("|", makeMemberIndex(aliasIdentB, { type: "Identifier", name: "$d" }), makeLiteral(0)) },
|
|
7836
|
-
{ type: "VariableDeclarator", id: { type: "Identifier", name: "$z" }, init: mbaAst }
|
|
7837
|
-
]
|
|
7838
|
-
},
|
|
7839
|
-
{
|
|
7840
|
-
type: "ExpressionStatement",
|
|
7841
|
-
expression: {
|
|
7842
|
-
type: "AssignmentExpression",
|
|
7843
|
-
operator: "=",
|
|
7844
|
-
left: { type: "Identifier", name: "$z" },
|
|
7845
|
-
right: makeBinary(
|
|
7846
|
-
"|",
|
|
7847
|
-
makeBinary(
|
|
7848
|
-
"+",
|
|
7849
|
-
{ type: "Identifier", name: "$f" },
|
|
7850
|
-
makeImul(
|
|
7851
|
-
{ type: "Identifier", name: "$z" },
|
|
7852
|
-
makeBinary(
|
|
7853
|
-
"+",
|
|
7854
|
-
{ type: "Identifier", name: "$g" },
|
|
7855
|
-
makeImul(
|
|
7856
|
-
{ type: "Identifier", name: "$z" },
|
|
7857
|
-
makeBinary(
|
|
7858
|
-
"+",
|
|
7859
|
-
{ type: "Identifier", name: "$h" },
|
|
7860
|
-
makeImul({ type: "Identifier", name: "$z" }, { type: "Identifier", name: "$i" })
|
|
7861
|
-
)
|
|
7862
|
-
)
|
|
7863
|
-
)
|
|
7864
|
-
)
|
|
7865
|
-
),
|
|
7866
|
-
makeLiteral(0)
|
|
7867
|
-
)
|
|
7868
|
-
}
|
|
7869
|
-
},
|
|
7870
|
-
{
|
|
7871
|
-
type: "ExpressionStatement",
|
|
7872
|
-
expression: {
|
|
7873
|
-
type: "AssignmentExpression",
|
|
7874
|
-
operator: "=",
|
|
7875
|
-
left: { type: "Identifier", name: "$z" },
|
|
7876
|
-
right: buildSboxMixAst(sboxIdent, "$z")
|
|
7877
|
-
}
|
|
7878
|
-
},
|
|
7879
|
-
{
|
|
7880
|
-
type: "VariableDeclaration",
|
|
7881
|
-
kind: "var",
|
|
7882
|
-
declarations: [
|
|
7883
|
-
{
|
|
7884
|
-
type: "VariableDeclarator",
|
|
7885
|
-
id: { type: "Identifier", name: "$m" },
|
|
7886
|
-
init: makeBinary("^", makeBinary("<<", { type: "Identifier", name: "$z" }, makeLiteral(3)), { type: "Identifier", name: "$e" })
|
|
7887
|
-
}
|
|
7888
|
-
]
|
|
7889
|
-
},
|
|
7890
|
-
{
|
|
7891
|
-
type: "ExpressionStatement",
|
|
7892
|
-
expression: {
|
|
7893
|
-
type: "AssignmentExpression",
|
|
7894
|
-
operator: "=",
|
|
7895
|
-
left: makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$c" }),
|
|
7896
|
-
right: makeBinary("|", makeBinary("^", makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$c" }), { type: "Identifier", name: "$m" }), makeLiteral(0))
|
|
7897
|
-
}
|
|
7898
|
-
},
|
|
7899
|
-
{
|
|
7900
|
-
type: "VariableDeclaration",
|
|
7901
|
-
kind: "var",
|
|
7902
|
-
declarations: [
|
|
7903
|
-
{
|
|
7904
|
-
type: "VariableDeclarator",
|
|
7905
|
-
id: { type: "Identifier", name: "$w" },
|
|
7906
|
-
init: makeBinary(
|
|
7907
|
-
"^",
|
|
7908
|
-
makeBinary("|", makeBinary("^", makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$c" }), { type: "Identifier", name: "$m" }), makeLiteral(0)),
|
|
7909
|
-
{
|
|
7910
|
-
type: "AssignmentExpression",
|
|
7911
|
-
operator: "=",
|
|
7912
|
-
left: makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$c" }),
|
|
7913
|
-
right: makeBinary("|", makeBinary("^", makeMemberIndex(aliasIdentA, { type: "Identifier", name: "$c" }), { type: "Identifier", name: "$m" }), makeLiteral(0))
|
|
7914
|
-
}
|
|
7915
|
-
)
|
|
7916
|
-
}
|
|
7917
|
-
]
|
|
7918
|
-
},
|
|
7919
|
-
{
|
|
7920
|
-
type: "ReturnStatement",
|
|
7921
|
-
argument: {
|
|
7922
|
-
type: "AssignmentExpression",
|
|
7923
|
-
operator: "=",
|
|
7924
|
-
left: { type: "Identifier", name: scratchVarIdent },
|
|
7925
|
-
right: makeBinary("|", makeBinary("^", { type: "Identifier", name: "$z" }, { type: "Identifier", name: "$w" }), makeLiteral(0))
|
|
7926
|
-
}
|
|
9918
|
+
ArrowFunctionExpression(node) {
|
|
9919
|
+
if (Array.isArray(node.params) && node.params.some(patternHasMath)) {
|
|
9920
|
+
unsafe.add(node);
|
|
9921
|
+
}
|
|
9922
|
+
},
|
|
9923
|
+
ClassDeclaration(node, _st, ancestors) {
|
|
9924
|
+
if (node.id?.name === "Math") {
|
|
9925
|
+
const prefix = ancestors.slice(0, -1);
|
|
9926
|
+
markScopeAndEnclosingFn(prefix, findEnclosing(prefix));
|
|
9927
|
+
unsafe.add(node);
|
|
9928
|
+
}
|
|
9929
|
+
},
|
|
9930
|
+
ClassExpression(node) {
|
|
9931
|
+
if (node.id?.name === "Math") {
|
|
9932
|
+
unsafe.add(node);
|
|
9933
|
+
}
|
|
9934
|
+
},
|
|
9935
|
+
CatchClause(node) {
|
|
9936
|
+
if (node.param && patternHasMath(node.param)) {
|
|
9937
|
+
unsafe.add(node);
|
|
9938
|
+
}
|
|
9939
|
+
},
|
|
9940
|
+
VariableDeclaration(node, _st, ancestors) {
|
|
9941
|
+
if (!Array.isArray(node.declarations) || !node.declarations.some((d) => patternHasMath(d?.id))) {
|
|
9942
|
+
return;
|
|
9943
|
+
}
|
|
9944
|
+
if (node.kind === "var") {
|
|
9945
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
9946
|
+
const a = ancestors[i];
|
|
9947
|
+
if (BITWISE_FUNCTION_TYPES.has(a.type) || a.type === "StaticBlock" || a.type === "Program") {
|
|
9948
|
+
unsafe.add(a);
|
|
9949
|
+
break;
|
|
7927
9950
|
}
|
|
7928
|
-
|
|
7929
|
-
}
|
|
7930
|
-
|
|
7931
|
-
|
|
7932
|
-
|
|
7933
|
-
}
|
|
7934
|
-
|
|
7935
|
-
|
|
7936
|
-
|
|
7937
|
-
|
|
7938
|
-
|
|
9951
|
+
}
|
|
9952
|
+
} else {
|
|
9953
|
+
const prefix = ancestors.slice(0, -1);
|
|
9954
|
+
markScopeAndEnclosingFn(prefix, findEnclosing(prefix));
|
|
9955
|
+
}
|
|
9956
|
+
}
|
|
9957
|
+
});
|
|
9958
|
+
return unsafe;
|
|
9959
|
+
}
|
|
9960
|
+
function isAnyAncestorInSet(ancestors, set) {
|
|
9961
|
+
if (!set) return false;
|
|
9962
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
9963
|
+
if (set.has(ancestors[i])) return true;
|
|
7939
9964
|
}
|
|
7940
|
-
return
|
|
7941
|
-
type: "VariableDeclaration",
|
|
7942
|
-
kind: "var",
|
|
7943
|
-
declarations
|
|
7944
|
-
};
|
|
9965
|
+
return false;
|
|
7945
9966
|
}
|
|
7946
|
-
|
|
7947
|
-
|
|
7948
|
-
|
|
9967
|
+
function isDirectiveStatement(stmt) {
|
|
9968
|
+
if (!stmt || stmt.type !== "ExpressionStatement") return false;
|
|
9969
|
+
if (stmt.directive) return true;
|
|
9970
|
+
return stmt.expression?.type === "Literal" && typeof stmt.expression.value === "string" && /^(?:use\s+[a-zA-Z0-9_-]+)/.test(stmt.expression.value.trim());
|
|
9971
|
+
}
|
|
9972
|
+
function isSafeC9Statement(stmt, hotExemptNodes) {
|
|
9973
|
+
if (!stmt || typeof stmt !== "object") return false;
|
|
9974
|
+
if (stmt._unpeekGuardSubtree === true) return false;
|
|
9975
|
+
if (hotExemptNodes?.has(stmt)) return false;
|
|
9976
|
+
if (stmt.type === "ExpressionStatement") {
|
|
9977
|
+
if (isDirectiveStatement(stmt)) return false;
|
|
9978
|
+
return !containsAstNodeType(stmt, UNSAFE_EXPRESSION_NODE_TYPES);
|
|
9979
|
+
}
|
|
9980
|
+
if (stmt.type === "IfStatement") {
|
|
9981
|
+
return !containsAstNodeType(stmt, UNSAFE_IF_NODE_TYPES);
|
|
9982
|
+
}
|
|
9983
|
+
return false;
|
|
9984
|
+
}
|
|
9985
|
+
function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExemptNodes, windowKeySymbol, expectedDelta, shieldCoupled = false, options) {
|
|
9986
|
+
const sfx = deriveColdFlowSuffix(seed);
|
|
7949
9987
|
const decoyIdent = `_uDec${sfx}`;
|
|
7950
9988
|
const sboxIdent = `_uSb${sfx}`;
|
|
7951
9989
|
const aliasIdentA = `_uAl${sfx}`;
|
|
7952
9990
|
const aliasIdentB = `_uAm${sfx}`;
|
|
7953
9991
|
const scratchVarIdent = `_uZt${sfx}`;
|
|
7954
9992
|
const mxIdent = `_uMx${sfx}`;
|
|
9993
|
+
const latchFnName = `_uLr${sfx}`;
|
|
7955
9994
|
if (Array.isArray(ast.body)) {
|
|
7956
9995
|
const alreadyGuarded = ast.body.some(
|
|
7957
9996
|
(stmt) => stmt.type === "VariableDeclaration" && stmt.declarations?.some(
|
|
7958
9997
|
(d) => d.id?.type === "Identifier" && (d.id.name === decoyIdent || d.id.name === sboxIdent || d.id.name === mxIdent)
|
|
7959
|
-
)
|
|
9998
|
+
) || stmt.type === "FunctionDeclaration" && stmt.id?.type === "Identifier" && (stmt.id.name === mxIdent || stmt.id.name === latchFnName)
|
|
7960
9999
|
);
|
|
7961
10000
|
if (alreadyGuarded) return 0;
|
|
7962
10001
|
}
|
|
7963
|
-
|
|
7964
|
-
|
|
7965
|
-
|
|
10002
|
+
if (options?.enableBitwiseMba) {
|
|
10003
|
+
applyColdBitwiseMba(ast, coldFunctionNodes, seed, hotExemptNodes);
|
|
10004
|
+
}
|
|
10005
|
+
const mathUnsafeScopes = collectMathUnsafeScopes(ast);
|
|
10006
|
+
const C9_RESERVED_SLOTS = 4;
|
|
10007
|
+
const eligibleColdFns = [];
|
|
10008
|
+
function collectEligibleColdFn(node, _state, ancestors) {
|
|
10009
|
+
if (eligibleColdFns.length >= MAX_COLD_GUARDS_PER_CHUNK) return;
|
|
7966
10010
|
if (!coldFunctionNodes.has(node)) return;
|
|
7967
|
-
if (
|
|
10011
|
+
if (isAnyAncestorInSet(ancestors, hotExemptNodes)) return;
|
|
10012
|
+
if (isAnyAncestorInSet(ancestors, mathUnsafeScopes)) return;
|
|
10013
|
+
if (node._unpeekSerialized) return;
|
|
7968
10014
|
if (node._unpeekVirtualized === true) return;
|
|
7969
10015
|
if (!node.body || node.body.type !== "BlockStatement") return;
|
|
7970
10016
|
const paramNames = [];
|
|
7971
10017
|
for (const p of node.params || []) {
|
|
7972
10018
|
extractParamNames(p, paramNames);
|
|
7973
10019
|
}
|
|
7974
|
-
|
|
7975
|
-
|
|
7976
|
-
|
|
10020
|
+
eligibleColdFns.push({
|
|
10021
|
+
node,
|
|
10022
|
+
paramNames,
|
|
10023
|
+
firstParam: node.params?.[0],
|
|
10024
|
+
isArrow: node.type === "ArrowFunctionExpression",
|
|
10025
|
+
returnDomain: inferFunctionReturnDomain(node)
|
|
10026
|
+
});
|
|
10027
|
+
}
|
|
10028
|
+
walk6.ancestor(ast, {
|
|
10029
|
+
FunctionDeclaration: collectEligibleColdFn,
|
|
10030
|
+
FunctionExpression: collectEligibleColdFn,
|
|
10031
|
+
ArrowFunctionExpression: collectEligibleColdFn
|
|
10032
|
+
});
|
|
10033
|
+
let availableC9Slots = 0;
|
|
10034
|
+
for (const fnInfo of eligibleColdFns) {
|
|
10035
|
+
if (availableC9Slots >= C9_RESERVED_SLOTS) break;
|
|
10036
|
+
const bodyStmts = fnInfo.node.body.body;
|
|
10037
|
+
let idx = 0;
|
|
10038
|
+
while (idx < bodyStmts.length && isDirectiveStatement(bodyStmts[idx])) {
|
|
10039
|
+
idx++;
|
|
10040
|
+
}
|
|
10041
|
+
for (; idx < bodyStmts.length; idx++) {
|
|
10042
|
+
if (isSafeC9Statement(bodyStmts[idx], hotExemptNodes)) {
|
|
10043
|
+
availableC9Slots++;
|
|
10044
|
+
if (availableC9Slots >= C9_RESERVED_SLOTS) break;
|
|
10045
|
+
}
|
|
10046
|
+
}
|
|
10047
|
+
}
|
|
10048
|
+
const opaqueFalseLimit = MAX_COLD_GUARDS_PER_CHUNK - availableC9Slots;
|
|
10049
|
+
let opaqueFalseCount = 0;
|
|
10050
|
+
for (let i = 0; i < eligibleColdFns.length && opaqueFalseCount < opaqueFalseLimit; i++) {
|
|
10051
|
+
const fnInfo = eligibleColdFns[i];
|
|
7977
10052
|
const guardStmt = createSotaMbaPredicate(
|
|
7978
10053
|
seed,
|
|
7979
|
-
|
|
7980
|
-
firstParam,
|
|
10054
|
+
opaqueFalseCount,
|
|
10055
|
+
fnInfo.firstParam,
|
|
7981
10056
|
decoyIdent,
|
|
7982
|
-
isArrow,
|
|
10057
|
+
fnInfo.isArrow,
|
|
7983
10058
|
{
|
|
7984
|
-
liveVars: paramNames,
|
|
10059
|
+
liveVars: fnInfo.paramNames,
|
|
7985
10060
|
sboxIdent,
|
|
7986
10061
|
aliasIdentA,
|
|
7987
10062
|
aliasIdentB,
|
|
7988
10063
|
scratchVarIdent,
|
|
7989
10064
|
mxIdent,
|
|
7990
|
-
returnDomain,
|
|
10065
|
+
returnDomain: fnInfo.returnDomain,
|
|
7991
10066
|
windowKeySymbol,
|
|
7992
10067
|
expectedDelta,
|
|
7993
10068
|
shieldCoupled
|
|
7994
10069
|
}
|
|
7995
10070
|
);
|
|
7996
|
-
|
|
10071
|
+
opaqueFalseCount++;
|
|
10072
|
+
guardStmt._unpeekGuardSubtree = true;
|
|
7997
10073
|
if (hotExemptNodes) {
|
|
7998
10074
|
hotExemptNodes.add(guardStmt);
|
|
7999
10075
|
hotExemptNodes.add(guardStmt.consequent);
|
|
8000
10076
|
}
|
|
8001
10077
|
let insertIdx = 0;
|
|
8002
|
-
while (insertIdx < node.body.body.length &&
|
|
10078
|
+
while (insertIdx < fnInfo.node.body.body.length && isDirectiveStatement(fnInfo.node.body.body[insertIdx])) {
|
|
8003
10079
|
insertIdx++;
|
|
8004
10080
|
}
|
|
8005
|
-
node.body.body.splice(insertIdx, 0, guardStmt);
|
|
10081
|
+
fnInfo.node.body.body.splice(insertIdx, 0, guardStmt);
|
|
8006
10082
|
}
|
|
8007
|
-
|
|
8008
|
-
|
|
8009
|
-
|
|
8010
|
-
|
|
8011
|
-
|
|
8012
|
-
|
|
10083
|
+
let c9WrappedCount = 0;
|
|
10084
|
+
if (eligibleColdFns.length > 0 && opaqueFalseCount < MAX_COLD_GUARDS_PER_CHUNK) {
|
|
10085
|
+
const fnCursors = eligibleColdFns.map((fnInfo) => {
|
|
10086
|
+
const bodyStmts = fnInfo.node.body.body;
|
|
10087
|
+
let startIdx = 0;
|
|
10088
|
+
while (startIdx < bodyStmts.length && isDirectiveStatement(bodyStmts[startIdx])) {
|
|
10089
|
+
startIdx++;
|
|
10090
|
+
}
|
|
10091
|
+
return { fnInfo, bodyStmts, idx: startIdx };
|
|
10092
|
+
});
|
|
10093
|
+
let progress = true;
|
|
10094
|
+
while (progress && opaqueFalseCount + c9WrappedCount < MAX_COLD_GUARDS_PER_CHUNK) {
|
|
10095
|
+
progress = false;
|
|
10096
|
+
for (const cursor of fnCursors) {
|
|
10097
|
+
if (opaqueFalseCount + c9WrappedCount >= MAX_COLD_GUARDS_PER_CHUNK) break;
|
|
10098
|
+
const { fnInfo, bodyStmts } = cursor;
|
|
10099
|
+
while (cursor.idx < bodyStmts.length) {
|
|
10100
|
+
const stmt = bodyStmts[cursor.idx];
|
|
10101
|
+
const curIdx = cursor.idx++;
|
|
10102
|
+
if (!isSafeC9Statement(stmt, hotExemptNodes)) continue;
|
|
10103
|
+
const pred = createSotaMbaPredicate(
|
|
10104
|
+
seed,
|
|
10105
|
+
opaqueFalseCount + c9WrappedCount,
|
|
10106
|
+
fnInfo.firstParam,
|
|
10107
|
+
decoyIdent,
|
|
10108
|
+
fnInfo.isArrow,
|
|
10109
|
+
{
|
|
10110
|
+
liveVars: fnInfo.paramNames,
|
|
10111
|
+
sboxIdent,
|
|
10112
|
+
aliasIdentA,
|
|
10113
|
+
aliasIdentB,
|
|
10114
|
+
scratchVarIdent,
|
|
10115
|
+
mxIdent,
|
|
10116
|
+
returnDomain: "void",
|
|
10117
|
+
windowKeySymbol,
|
|
10118
|
+
expectedDelta,
|
|
10119
|
+
shieldCoupled
|
|
10120
|
+
}
|
|
10121
|
+
);
|
|
10122
|
+
const negatedTest = makeUnary("!", pred.test);
|
|
10123
|
+
negatedTest._unpeekGuardSubtree = true;
|
|
10124
|
+
if (hotExemptNodes) {
|
|
10125
|
+
hotExemptNodes.add(negatedTest);
|
|
10126
|
+
}
|
|
10127
|
+
bodyStmts[curIdx] = {
|
|
10128
|
+
type: "IfStatement",
|
|
10129
|
+
test: negatedTest,
|
|
10130
|
+
consequent: {
|
|
10131
|
+
type: "BlockStatement",
|
|
10132
|
+
body: [stmt]
|
|
10133
|
+
},
|
|
10134
|
+
alternate: null
|
|
10135
|
+
};
|
|
10136
|
+
c9WrappedCount++;
|
|
10137
|
+
progress = true;
|
|
10138
|
+
break;
|
|
10139
|
+
}
|
|
10140
|
+
}
|
|
10141
|
+
}
|
|
10142
|
+
}
|
|
10143
|
+
if ((opaqueFalseCount > 0 || c9WrappedCount > 0) && Array.isArray(ast.body)) {
|
|
8013
10144
|
const sharedDecoyDecl = createSharedDecoyDeclaration(
|
|
8014
10145
|
decoyIdent,
|
|
8015
|
-
|
|
10146
|
+
Math.max(1, opaqueFalseCount),
|
|
8016
10147
|
seed
|
|
8017
10148
|
);
|
|
8018
|
-
const
|
|
10149
|
+
const { varDecl, aliasInitStmt, latchFnDecl, mxFnDecl } = createColdFlowRuntimeDeclarations(
|
|
8019
10150
|
sboxIdent,
|
|
8020
10151
|
aliasIdentA,
|
|
8021
10152
|
aliasIdentB,
|
|
8022
10153
|
scratchVarIdent,
|
|
8023
10154
|
seed,
|
|
10155
|
+
latchFnName,
|
|
8024
10156
|
mxIdent
|
|
8025
10157
|
);
|
|
10158
|
+
aliasInitStmt._unpeekGuardSubtree = true;
|
|
10159
|
+
latchFnDecl._unpeekGuardSubtree = true;
|
|
10160
|
+
if (mxFnDecl) {
|
|
10161
|
+
mxFnDecl._unpeekGuardSubtree = true;
|
|
10162
|
+
}
|
|
8026
10163
|
if (hotExemptNodes) {
|
|
8027
10164
|
hotExemptNodes.add(sharedDecoyDecl);
|
|
8028
|
-
hotExemptNodes.add(
|
|
8029
|
-
|
|
8030
|
-
|
|
8031
|
-
|
|
8032
|
-
|
|
10165
|
+
hotExemptNodes.add(varDecl);
|
|
10166
|
+
hotExemptNodes.add(aliasInitStmt);
|
|
10167
|
+
hotExemptNodes.add(latchFnDecl);
|
|
10168
|
+
if (mxFnDecl) {
|
|
10169
|
+
hotExemptNodes.add(mxFnDecl);
|
|
8033
10170
|
}
|
|
8034
10171
|
for (const decl of sharedDecoyDecl.declarations || []) {
|
|
8035
10172
|
if (decl.init?.type === "ArrayExpression" && decl.init.elements) {
|
|
@@ -8049,9 +10186,16 @@ function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExempt
|
|
|
8049
10186
|
break;
|
|
8050
10187
|
}
|
|
8051
10188
|
}
|
|
8052
|
-
|
|
10189
|
+
const injectedDecls = [
|
|
10190
|
+
varDecl,
|
|
10191
|
+
aliasInitStmt,
|
|
10192
|
+
latchFnDecl,
|
|
10193
|
+
...mxFnDecl ? [mxFnDecl] : [],
|
|
10194
|
+
sharedDecoyDecl
|
|
10195
|
+
];
|
|
10196
|
+
ast.body.splice(insertIdx, 0, ...injectedDecls);
|
|
8053
10197
|
}
|
|
8054
|
-
return
|
|
10198
|
+
return opaqueFalseCount;
|
|
8055
10199
|
}
|
|
8056
10200
|
|
|
8057
10201
|
// src/ast/micro-vm.ts
|
|
@@ -8100,7 +10244,8 @@ var OP_TRY = 39;
|
|
|
8100
10244
|
var OP_ENDTRY = 40;
|
|
8101
10245
|
var OP_NULLISH = 41;
|
|
8102
10246
|
var OP_DUP = 42;
|
|
8103
|
-
var
|
|
10247
|
+
var OP_TOSTR = 43;
|
|
10248
|
+
var OPCODE_COUNT = 44;
|
|
8104
10249
|
function shuffleRange(n, seed) {
|
|
8105
10250
|
const arr = [];
|
|
8106
10251
|
for (let i = 0; i < n; i++) arr.push(i);
|
|
@@ -8284,7 +10429,8 @@ function createInterpreterSource(vmHelperName, opMap, seed = 2654435769, windowK
|
|
|
8284
10429
|
[OP_TRY]: { body: "var $cp=($rd($f)-$Z)&0xffff,$cs=$f.c[$f.p++]^$f.st,$ca=$f.c[$f.p++]^$f.ac;$f.e.push({p:$cp,r:$f.r,s:$cs,a:$ca});" },
|
|
8285
10430
|
[OP_ENDTRY]: { body: "$f.r=($f.r+$Z)|0;$f.e.pop();$f.r=($f.r-$B)|0;" },
|
|
8286
10431
|
[OP_NULLISH]: { body: "$f.r=($f.r+$Z)|0;var $v=$f.q[$f.r-(1+$B)];$f.q[$f.r-(1+$B)]=($v===null||$v===void 0);$f.r=($f.r-$B)|0;" },
|
|
8287
|
-
[OP_DUP]: { body: "$f.r=($f.r+$Z)|0;var $top=$f.q[$f.r-(1+$B)];$f.q[($f.r++)-$B]=$top;$f.r=($f.r-$B)|0;" }
|
|
10432
|
+
[OP_DUP]: { body: "$f.r=($f.r+$Z)|0;var $top=$f.q[$f.r-(1+$B)];$f.q[($f.r++)-$B]=$top;$f.r=($f.r-$B)|0;" },
|
|
10433
|
+
[OP_TOSTR]: { body: "$f.r=($f.r+$Z)|0;var $v=$f.q[$f.r-(1+$B)];$f.r=($f.r-$B)|0;$f.q[$f.r-1]=`${$v}`;" }
|
|
8288
10434
|
};
|
|
8289
10435
|
const handlers = new Array(opCount);
|
|
8290
10436
|
for (let op = 0; op < opCount; op++) {
|
|
@@ -8891,6 +11037,7 @@ function compileFunctionToBytecode(fnNode, opMap, enclosingBindings, seed = 2654
|
|
|
8891
11037
|
emit(OP_LDC, addConst(quasis[0]?.value?.cooked ?? quasis[0]?.value?.raw ?? ""));
|
|
8892
11038
|
for (let i = 0; i < exprs.length; i++) {
|
|
8893
11039
|
if (!compileExpr(exprs[i], true)) return false;
|
|
11040
|
+
emit(OP_TOSTR);
|
|
8894
11041
|
emit(OP_ADD);
|
|
8895
11042
|
const qStr = quasis[i + 1]?.value?.cooked ?? quasis[i + 1]?.value?.raw ?? "";
|
|
8896
11043
|
if (qStr.length > 0) {
|
|
@@ -10063,7 +12210,7 @@ function scrubResidualPaths(ast, options = {}, hotExemptNodes, exemptSubtreeRoot
|
|
|
10063
12210
|
if (exemptSet?.has(val)) return;
|
|
10064
12211
|
const parent = ancestors[ancestors.length - 2];
|
|
10065
12212
|
if (!parent) return;
|
|
10066
|
-
if ((
|
|
12213
|
+
if (isStaticModuleOrAssetPosition(node, parent)) {
|
|
10067
12214
|
return;
|
|
10068
12215
|
}
|
|
10069
12216
|
if (parent.type === "ExpressionStatement" && Boolean(parent.directive)) {
|
|
@@ -10085,7 +12232,7 @@ function scrubResidualPaths(ast, options = {}, hotExemptNodes, exemptSubtreeRoot
|
|
|
10085
12232
|
return;
|
|
10086
12233
|
}
|
|
10087
12234
|
const parent = ancestors[ancestors.length - 2];
|
|
10088
|
-
if (
|
|
12235
|
+
if (isStaticModuleOrAssetPosition(node, parent)) {
|
|
10089
12236
|
return;
|
|
10090
12237
|
}
|
|
10091
12238
|
for (const quasi of node.quasis) {
|
|
@@ -10140,7 +12287,7 @@ function isAstAlreadyProtected(ast) {
|
|
|
10140
12287
|
}
|
|
10141
12288
|
if (stmt.type === "FunctionDeclaration" && stmt.id?.type === "Identifier") {
|
|
10142
12289
|
const fnName = stmt.id.name;
|
|
10143
|
-
if (typeof fnName === "string" && (fnName.startsWith("_uD") || fnName.startsWith("_uVm") || fnName.startsWith("_wKdf") || fnName === "_hX")) {
|
|
12290
|
+
if (typeof fnName === "string" && (fnName.startsWith("_uD") || fnName.startsWith("_uVm") || fnName.startsWith("_wKdf") || fnName.startsWith("_uLr") || fnName.startsWith("_uMx") || fnName === "_hX")) {
|
|
10144
12291
|
return true;
|
|
10145
12292
|
}
|
|
10146
12293
|
}
|
|
@@ -10291,17 +12438,20 @@ function transformJavaScript(code, options = {}) {
|
|
|
10291
12438
|
);
|
|
10292
12439
|
virtualizedFunctionsCount = vmRes.virtualizedCount;
|
|
10293
12440
|
}
|
|
12441
|
+
let coldFlowGuardedCount = 0;
|
|
10294
12442
|
if (effectiveProfile !== "minimal" && effectiveConfig?.coldFlow !== false) {
|
|
10295
|
-
applyColdFlowGuard(
|
|
12443
|
+
coldFlowGuardedCount = applyColdFlowGuard(
|
|
10296
12444
|
ast,
|
|
10297
12445
|
coldFunctionNodes,
|
|
10298
12446
|
seed,
|
|
10299
12447
|
hotExemptNodes,
|
|
10300
12448
|
sym,
|
|
10301
12449
|
shieldDelta,
|
|
10302
|
-
shieldCoupled
|
|
12450
|
+
shieldCoupled,
|
|
12451
|
+
{ enableBitwiseMba: isAggressive || effectiveProfile === "strict" }
|
|
10303
12452
|
);
|
|
10304
12453
|
}
|
|
12454
|
+
const latchCoupling = coldFlowGuardedCount > 0 ? deriveColdLatchCoupling(seed) : void 0;
|
|
10305
12455
|
const mangledSymbolsCount = applyPolymorphicMangler(
|
|
10306
12456
|
ast,
|
|
10307
12457
|
seed,
|
|
@@ -10323,7 +12473,8 @@ function transformJavaScript(code, options = {}) {
|
|
|
10323
12473
|
deterrentMode,
|
|
10324
12474
|
shieldCoupled,
|
|
10325
12475
|
detectHeadless,
|
|
10326
|
-
boundary.exemptSubtreeRoots
|
|
12476
|
+
boundary.exemptSubtreeRoots,
|
|
12477
|
+
latchCoupling
|
|
10327
12478
|
);
|
|
10328
12479
|
encryptedStringsCount = saltedRes.encryptedStringsCount;
|
|
10329
12480
|
integrityToken = saltedRes.integrityToken;
|
|
@@ -10340,7 +12491,8 @@ function transformJavaScript(code, options = {}) {
|
|
|
10340
12491
|
effectiveConfig,
|
|
10341
12492
|
shieldCoupled,
|
|
10342
12493
|
detectHeadless,
|
|
10343
|
-
boundary.exemptSubtreeRoots
|
|
12494
|
+
boundary.exemptSubtreeRoots,
|
|
12495
|
+
latchCoupling
|
|
10344
12496
|
);
|
|
10345
12497
|
encryptedStringsCount = vaultRes.encryptedStringsCount;
|
|
10346
12498
|
integrityToken = vaultRes.integrityToken;
|
|
@@ -10503,21 +12655,29 @@ function matchesManifestEntry(relPath, manifestEntries) {
|
|
|
10503
12655
|
}
|
|
10504
12656
|
return false;
|
|
10505
12657
|
}
|
|
10506
|
-
function isEntryChunk(relPath, index, allRelPaths, manifestEntries, isWorkerTarget) {
|
|
12658
|
+
function isEntryChunk(relPath, index, allRelPaths, manifestEntries, isWorkerTarget, htmlGraph) {
|
|
10507
12659
|
const norm = relPath.replace(/\\/g, "/");
|
|
10508
12660
|
const base2 = path5.basename(norm);
|
|
10509
12661
|
if (isWorkerTarget && isWorkerOrServiceWorkerFile(norm)) {
|
|
10510
12662
|
return true;
|
|
10511
12663
|
}
|
|
12664
|
+
if (htmlGraph?.workerEntryFiles.has(norm)) {
|
|
12665
|
+
return true;
|
|
12666
|
+
}
|
|
10512
12667
|
if (matchesManifestEntry(norm, manifestEntries)) {
|
|
10513
12668
|
return true;
|
|
10514
12669
|
}
|
|
12670
|
+
const hasKnownManifestEntry = manifestEntries.size > 0 && allRelPaths.some((p) => matchesManifestEntry(p.replace(/\\/g, "/"), manifestEntries));
|
|
12671
|
+
const hasKnownHtmlEntry = manifestEntries.size === 0 && Boolean(htmlGraph && allRelPaths.some((p) => htmlGraph.entryFiles.has(p.replace(/\\/g, "/"))));
|
|
12672
|
+
if (hasKnownHtmlEntry) {
|
|
12673
|
+
return htmlGraph.entryFiles.has(norm);
|
|
12674
|
+
}
|
|
10515
12675
|
if (ENTRY_PATTERN.test(norm) || ENTRY_PATTERN.test(base2)) {
|
|
10516
12676
|
return true;
|
|
10517
12677
|
}
|
|
10518
|
-
const hasAnyKnownEntry = allRelPaths.some((p) => {
|
|
12678
|
+
const hasAnyKnownEntry = hasKnownManifestEntry || allRelPaths.some((p) => {
|
|
10519
12679
|
const n = p.replace(/\\/g, "/");
|
|
10520
|
-
return
|
|
12680
|
+
return ENTRY_PATTERN.test(n) || ENTRY_PATTERN.test(path5.basename(n));
|
|
10521
12681
|
});
|
|
10522
12682
|
if (!hasAnyKnownEntry && index === 0) {
|
|
10523
12683
|
return true;
|
|
@@ -10616,6 +12776,13 @@ async function protectProject(options = {}) {
|
|
|
10616
12776
|
const allRelPaths = clientFiles.map((f) => path5.relative(outDir, f).replace(/\\/g, "/"));
|
|
10617
12777
|
const manifestEntries = collectManifestEntryFiles(outDir, cwd, config);
|
|
10618
12778
|
const isWorkerTarget = config.target === "worker";
|
|
12779
|
+
const extraHtmlDirs = [];
|
|
12780
|
+
const parentOutDir = path5.dirname(outDir);
|
|
12781
|
+
const relParent = path5.relative(cwd, parentOutDir);
|
|
12782
|
+
if (relParent && !relParent.startsWith("..") && !path5.isAbsolute(relParent) && !hasNextServerSegment(parentOutDir, cwd)) {
|
|
12783
|
+
extraHtmlDirs.push(parentOutDir);
|
|
12784
|
+
}
|
|
12785
|
+
const htmlGraph = discoverHtmlEntryGraph(outDir, config, extraHtmlDirs);
|
|
10619
12786
|
let originalBytes = 0;
|
|
10620
12787
|
let protectedBytes = 0;
|
|
10621
12788
|
let totalHotFunctionsSkipped = 0;
|
|
@@ -10671,7 +12838,14 @@ async function protectProject(options = {}) {
|
|
|
10671
12838
|
const relPath = allRelPaths[i];
|
|
10672
12839
|
const originalMap = keepPrivateMaps ? readOriginalSourceMapForFile(filePath, relPath, vaultResult.vaultDir, deferVaultWrites) : void 0;
|
|
10673
12840
|
const preExistingHits = countPreExistingEvalCalls(codeToTransform);
|
|
10674
|
-
const isEntry = isEntryChunk(
|
|
12841
|
+
const isEntry = isEntryChunk(
|
|
12842
|
+
relPath,
|
|
12843
|
+
i,
|
|
12844
|
+
allRelPaths,
|
|
12845
|
+
manifestEntries,
|
|
12846
|
+
isWorkerTarget,
|
|
12847
|
+
htmlGraph
|
|
12848
|
+
);
|
|
10675
12849
|
const shouldInjectShield = injectRuntimeShield && isEntry;
|
|
10676
12850
|
const fileSeed = computeFnv1aHash(`${relPath}:${i}`, seed);
|
|
10677
12851
|
const transformOptions = {
|
|
@@ -10904,7 +13078,7 @@ async function protectProject(options = {}) {
|
|
|
10904
13078
|
import fs7 from "fs";
|
|
10905
13079
|
import path6 from "path";
|
|
10906
13080
|
import * as acorn8 from "acorn";
|
|
10907
|
-
import { simple as walkSimple2 } from "acorn-walk";
|
|
13081
|
+
import { recursive as walkRecursive, simple as walkSimple2 } from "acorn-walk";
|
|
10908
13082
|
function walkPublicMaps(currentDir, rootOutDir, found) {
|
|
10909
13083
|
if (hasNextServerSegment(currentDir, rootOutDir)) {
|
|
10910
13084
|
return;
|
|
@@ -10980,11 +13154,13 @@ function isHitFromNodeModules(hit, map, allSourcesNodeModules) {
|
|
|
10980
13154
|
}
|
|
10981
13155
|
return allSourcesNodeModules;
|
|
10982
13156
|
}
|
|
10983
|
-
|
|
10984
|
-
|
|
10985
|
-
|
|
13157
|
+
var MAX_PLAINTEXT_SECRET_FINDINGS_PER_FILE = 5;
|
|
13158
|
+
var UNPEEK_VAULT_IDENT_PREFIXES = ["_uP", "_uC", "_uD", "_uS", "_uSb", "_uDec"];
|
|
13159
|
+
var BINARY_DATA_URI_RE2 = /^data:(?:application\/(?:wasm|octet-stream|pdf|zip|gzip|(?:x-)?font-[a-z0-9-]+)|image\/|font\/|audio\/|video\/)/i;
|
|
13160
|
+
var SECRET_LIMIT_REACHED = {};
|
|
13161
|
+
function parseCheckAst(code) {
|
|
10986
13162
|
try {
|
|
10987
|
-
|
|
13163
|
+
return acorn8.parse(code, {
|
|
10988
13164
|
ecmaVersion: "latest",
|
|
10989
13165
|
sourceType: "module",
|
|
10990
13166
|
allowReturnOutsideFunction: true,
|
|
@@ -10992,16 +13168,137 @@ function findEvalOrFunctionCalls(code) {
|
|
|
10992
13168
|
});
|
|
10993
13169
|
} catch {
|
|
10994
13170
|
try {
|
|
10995
|
-
|
|
13171
|
+
return acorn8.parse(code, {
|
|
10996
13172
|
ecmaVersion: "latest",
|
|
10997
13173
|
sourceType: "script",
|
|
10998
13174
|
allowReturnOutsideFunction: true,
|
|
10999
13175
|
locations: true
|
|
11000
13176
|
});
|
|
11001
13177
|
} catch {
|
|
11002
|
-
return
|
|
13178
|
+
return null;
|
|
13179
|
+
}
|
|
13180
|
+
}
|
|
13181
|
+
}
|
|
13182
|
+
function isUnpeekVaultIdentifier(name) {
|
|
13183
|
+
return UNPEEK_VAULT_IDENT_PREFIXES.some((prefix) => name.startsWith(prefix));
|
|
13184
|
+
}
|
|
13185
|
+
function isUnpeekShaderVaultIife(init) {
|
|
13186
|
+
if (!init || init.type !== "CallExpression" || init.callee.type !== "FunctionExpression" || init.callee.id?.name !== "f" || init.callee.params.length !== 1 || init.arguments.length !== 1 || init.arguments[0]?.type !== "ArrayExpression") {
|
|
13187
|
+
return false;
|
|
13188
|
+
}
|
|
13189
|
+
const first = init.callee.body?.body?.[0];
|
|
13190
|
+
return first?.type === "VariableDeclaration" && first.declarations.some(
|
|
13191
|
+
(d) => d.id.type === "Identifier" && d.id.name === "h" && d.init?.type === "Literal" && d.init.value === 2166136261
|
|
13192
|
+
);
|
|
13193
|
+
}
|
|
13194
|
+
function isSkippedBinaryPayload(value) {
|
|
13195
|
+
return value.startsWith("AGFzbQE") || BINARY_DATA_URI_RE2.test(value);
|
|
13196
|
+
}
|
|
13197
|
+
function redactSecretPreview(value) {
|
|
13198
|
+
const singleLine = value.replace(/[\r\n\t]+/g, " ");
|
|
13199
|
+
if (singleLine.length <= 10) {
|
|
13200
|
+
return "[redacted]";
|
|
13201
|
+
}
|
|
13202
|
+
return `${singleLine.slice(0, 4)}...${singleLine.slice(-4)}`;
|
|
13203
|
+
}
|
|
13204
|
+
function findPlaintextSecrets(code, parsedAst) {
|
|
13205
|
+
const ast = parsedAst !== void 0 ? parsedAst : parseCheckAst(code);
|
|
13206
|
+
if (!ast) {
|
|
13207
|
+
return [];
|
|
13208
|
+
}
|
|
13209
|
+
const hits = [];
|
|
13210
|
+
const pushHit = (category, rawValue, node) => {
|
|
13211
|
+
hits.push({
|
|
13212
|
+
category,
|
|
13213
|
+
preview: redactSecretPreview(rawValue),
|
|
13214
|
+
line: node.loc ? node.loc.start.line : 1
|
|
13215
|
+
});
|
|
13216
|
+
if (hits.length >= MAX_PLAINTEXT_SECRET_FINDINGS_PER_FILE) {
|
|
13217
|
+
throw SECRET_LIMIT_REACHED;
|
|
13218
|
+
}
|
|
13219
|
+
};
|
|
13220
|
+
const inspectString = (value, node) => {
|
|
13221
|
+
if (!value || isSkippedBinaryPayload(value)) return;
|
|
13222
|
+
const match = classifyPlaintextSecret(value);
|
|
13223
|
+
if (match) {
|
|
13224
|
+
pushHit(match.category, value, node);
|
|
13225
|
+
}
|
|
13226
|
+
};
|
|
13227
|
+
try {
|
|
13228
|
+
walkRecursive(ast, void 0, {
|
|
13229
|
+
VariableDeclarator(node, state, c) {
|
|
13230
|
+
if (node.id.type === "Identifier" && isUnpeekVaultIdentifier(node.id.name) || isUnpeekShaderVaultIife(node.init)) {
|
|
13231
|
+
return;
|
|
13232
|
+
}
|
|
13233
|
+
c(node.id, state);
|
|
13234
|
+
if (node.init) {
|
|
13235
|
+
c(node.init, state);
|
|
13236
|
+
}
|
|
13237
|
+
},
|
|
13238
|
+
FunctionDeclaration(node, state, c) {
|
|
13239
|
+
if (node.id && isUnpeekVaultIdentifier(node.id.name)) {
|
|
13240
|
+
return;
|
|
13241
|
+
}
|
|
13242
|
+
for (const param of node.params) {
|
|
13243
|
+
c(param, state);
|
|
13244
|
+
}
|
|
13245
|
+
c(node.body, state);
|
|
13246
|
+
},
|
|
13247
|
+
ExpressionStatement(node, state, c) {
|
|
13248
|
+
if (node.directive) {
|
|
13249
|
+
return;
|
|
13250
|
+
}
|
|
13251
|
+
c(node.expression, state);
|
|
13252
|
+
},
|
|
13253
|
+
ImportDeclaration() {
|
|
13254
|
+
},
|
|
13255
|
+
ExportNamedDeclaration(node, state, c) {
|
|
13256
|
+
if (node.declaration) {
|
|
13257
|
+
c(node.declaration, state);
|
|
13258
|
+
}
|
|
13259
|
+
},
|
|
13260
|
+
ExportAllDeclaration() {
|
|
13261
|
+
},
|
|
13262
|
+
ImportExpression(node, state, c) {
|
|
13263
|
+
if (node.options) {
|
|
13264
|
+
c(node.options, state);
|
|
13265
|
+
}
|
|
13266
|
+
},
|
|
13267
|
+
ImportSpecifier() {
|
|
13268
|
+
},
|
|
13269
|
+
ExportSpecifier() {
|
|
13270
|
+
},
|
|
13271
|
+
ImportAttribute() {
|
|
13272
|
+
},
|
|
13273
|
+
Literal(node) {
|
|
13274
|
+
if (typeof node.value === "string") {
|
|
13275
|
+
inspectString(node.value, node);
|
|
13276
|
+
} else if (node.regex && typeof node.regex.pattern === "string") {
|
|
13277
|
+
if (hasRegexSecretRun(node.regex.pattern)) {
|
|
13278
|
+
pushHit("regex-secret", node.regex.pattern, node);
|
|
13279
|
+
}
|
|
13280
|
+
}
|
|
13281
|
+
},
|
|
13282
|
+
TemplateElement(node) {
|
|
13283
|
+
const val = node.value.cooked ?? node.value.raw;
|
|
13284
|
+
if (typeof val === "string") {
|
|
13285
|
+
inspectString(val, node);
|
|
13286
|
+
}
|
|
13287
|
+
}
|
|
13288
|
+
});
|
|
13289
|
+
} catch (err) {
|
|
13290
|
+
if (err !== SECRET_LIMIT_REACHED) {
|
|
13291
|
+
throw err;
|
|
11003
13292
|
}
|
|
11004
13293
|
}
|
|
13294
|
+
return hits;
|
|
13295
|
+
}
|
|
13296
|
+
function findEvalOrFunctionCalls(code, parsedAst) {
|
|
13297
|
+
const hits = [];
|
|
13298
|
+
const ast = parsedAst !== void 0 ? parsedAst : parseCheckAst(code);
|
|
13299
|
+
if (!ast) {
|
|
13300
|
+
return hits;
|
|
13301
|
+
}
|
|
11005
13302
|
walkSimple2(ast, {
|
|
11006
13303
|
CallExpression(node) {
|
|
11007
13304
|
if (node.callee.type === "Identifier") {
|
|
@@ -11151,7 +13448,8 @@ async function checkProject(options = {}) {
|
|
|
11151
13448
|
file: rel
|
|
11152
13449
|
});
|
|
11153
13450
|
}
|
|
11154
|
-
const
|
|
13451
|
+
const ast = parseCheckAst(content);
|
|
13452
|
+
const evalHits = findEvalOrFunctionCalls(content, ast);
|
|
11155
13453
|
if (evalHits.length > 0) {
|
|
11156
13454
|
if (!requireZeroEval) {
|
|
11157
13455
|
const kinds = Array.from(new Set(evalHits.map((h) => h.kind)));
|
|
@@ -11213,6 +13511,20 @@ async function checkProject(options = {}) {
|
|
|
11213
13511
|
}
|
|
11214
13512
|
}
|
|
11215
13513
|
}
|
|
13514
|
+
const secretHits = findPlaintextSecrets(content, ast);
|
|
13515
|
+
if (secretHits.length > 0) {
|
|
13516
|
+
const failOnSecrets = options.failOnPlaintextSecrets === true;
|
|
13517
|
+
const targetBucket = failOnSecrets ? violations : warnings;
|
|
13518
|
+
const severity = failOnSecrets ? "error" : "warning";
|
|
13519
|
+
for (const hit of secretHits) {
|
|
13520
|
+
targetBucket.push({
|
|
13521
|
+
rule: "plaintext-secret",
|
|
13522
|
+
severity,
|
|
13523
|
+
message: `Plaintext secret (${hit.category}) "${hit.preview}" detected in "${rel}" at line ${hit.line}.`,
|
|
13524
|
+
file: rel
|
|
13525
|
+
});
|
|
13526
|
+
}
|
|
13527
|
+
}
|
|
11216
13528
|
}
|
|
11217
13529
|
const relVault = path6.relative(cwd, vaultDir).replace(/\\/g, "/");
|
|
11218
13530
|
const ignoreEntry = relVault && !relVault.startsWith("..") ? `${relVault}/` : ".unpeek-maps/";
|
|
@@ -11283,7 +13595,7 @@ async function checkProject(options = {}) {
|
|
|
11283
13595
|
}
|
|
11284
13596
|
|
|
11285
13597
|
// src/cli/index.ts
|
|
11286
|
-
var VERSION = "4.
|
|
13598
|
+
var VERSION = "4.3.0";
|
|
11287
13599
|
var cachedPackageVersion;
|
|
11288
13600
|
function getPackageVersion() {
|
|
11289
13601
|
if (cachedPackageVersion !== void 0) {
|