unpeek 1.6.2 → 1.7.2

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.
Files changed (45) hide show
  1. package/dist/ast/index.cjs +262 -85
  2. package/dist/ast/index.cjs.map +1 -1
  3. package/dist/ast/index.js +262 -85
  4. package/dist/ast/index.js.map +1 -1
  5. package/dist/browser.cjs +262 -85
  6. package/dist/browser.cjs.map +1 -1
  7. package/dist/browser.js +262 -85
  8. package/dist/browser.js.map +1 -1
  9. package/dist/bundler/unplugin.cjs +262 -85
  10. package/dist/bundler/unplugin.cjs.map +1 -1
  11. package/dist/bundler/unplugin.js +262 -85
  12. package/dist/bundler/unplugin.js.map +1 -1
  13. package/dist/cli/index.cjs +263 -86
  14. package/dist/cli/index.cjs.map +1 -1
  15. package/dist/cli/index.js +263 -86
  16. package/dist/cli/index.js.map +1 -1
  17. package/dist/cli/protect.cjs +262 -85
  18. package/dist/cli/protect.cjs.map +1 -1
  19. package/dist/cli/protect.js +262 -85
  20. package/dist/cli/protect.js.map +1 -1
  21. package/dist/esbuild.cjs +262 -85
  22. package/dist/esbuild.cjs.map +1 -1
  23. package/dist/esbuild.js +262 -85
  24. package/dist/esbuild.js.map +1 -1
  25. package/dist/index.cjs +263 -86
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.js +263 -86
  28. package/dist/index.js.map +1 -1
  29. package/dist/rollup.cjs +262 -85
  30. package/dist/rollup.cjs.map +1 -1
  31. package/dist/rollup.js +262 -85
  32. package/dist/rollup.js.map +1 -1
  33. package/dist/runtime/index.cjs +2 -15
  34. package/dist/runtime/index.cjs.map +1 -1
  35. package/dist/runtime/index.js +2 -15
  36. package/dist/runtime/index.js.map +1 -1
  37. package/dist/vite.cjs +262 -85
  38. package/dist/vite.cjs.map +1 -1
  39. package/dist/vite.js +262 -85
  40. package/dist/vite.js.map +1 -1
  41. package/dist/webpack.cjs +262 -85
  42. package/dist/webpack.cjs.map +1 -1
  43. package/dist/webpack.js +262 -85
  44. package/dist/webpack.js.map +1 -1
  45. package/package.json +1 -1
@@ -839,27 +839,25 @@ var DIRECTIVE_STRINGS = /* @__PURE__ */ new Set([
839
839
  "use asm"
840
840
  ]);
841
841
  var HELPER_TEMPLATE = acorn2.parse(
842
- `var _ = [].map(function f(a) {
843
- var s = "";
844
- for (var j = 0; j < a.length; j++) {
845
- s += String.fromCharCode(a.charCodeAt(j) ^ (((0 ^ ((a._u || /\\n/.test(f) * 14) | ((j & 1) * 0))) & 30) | 1));
846
- }
847
- return s;
848
- });`,
842
+ `var _ = (function f(a){return a.map(function(s){var o="";for(var j=0;j<s.length;j++)o+=String.fromCharCode(s.charCodeAt(j)^(((0^((a._u||/\\n/.test(f)*14)|((j&1)*0)))&30)|1));return o;});})([]);`,
849
843
  { ecmaVersion: "latest", sourceType: "script" }
850
844
  ).body[0];
851
845
  function createVaultHelperAst(cacheVarName, encryptedPayloads, mask, step) {
852
846
  const node = structuredClone(HELPER_TEMPLATE);
853
847
  node.declarations[0].id.name = cacheVarName;
854
- node.declarations[0].init.callee.object.elements = encryptedPayloads.map(
848
+ node.declarations[0].init.arguments[0].elements = encryptedPayloads.map(
855
849
  (str) => ({
856
850
  type: "Literal",
857
851
  value: str,
858
852
  raw: JSON.stringify(str)
859
853
  })
860
854
  );
861
- const fn = node.declarations[0].init.arguments[0];
862
- const binaryXor = fn.body.body[1].body.body[0].expression.right.arguments[0];
855
+ const outerFn = node.declarations[0].init.callee;
856
+ const returnStmt = outerFn.body.body[0];
857
+ const innerFn = returnStmt.argument.arguments[0];
858
+ const forStmt = innerFn.body.body[1];
859
+ const expr = forStmt.body.type === "BlockStatement" ? forStmt.body.body[0].expression : forStmt.body.expression;
860
+ const binaryXor = expr.right.arguments[0];
863
861
  const innerLeft = binaryXor.right.left.left;
864
862
  innerLeft.left.value = mask;
865
863
  innerLeft.left.raw = String(mask);
@@ -897,44 +895,58 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
897
895
  };
898
896
  }
899
897
  const mask = seed >>> 0 & 30 | 3;
900
- const step = (seed >>> 5 & 7) * 2 + 3;
898
+ const step = 1;
901
899
  const stringToIndex = /* @__PURE__ */ new Map();
902
900
  const encryptedPayloads = [];
903
901
  const targetsToReplace = [];
904
- walk2.ancestor(ast, {
905
- Literal(node, _state, ancestors) {
906
- if (typeof node.value !== "string") return;
907
- const val = node.value;
908
- if (val.length < minStringLength) return;
909
- if (DIRECTIVE_STRINGS.has(val)) return;
910
- if (hotExemptNodes) {
911
- for (let i = ancestors.length - 2; i >= 0; i--) {
912
- if (hotExemptNodes.has(ancestors[i])) {
913
- return;
902
+ const customWalkBase = {
903
+ ...walk2.base,
904
+ Property(node, st, c) {
905
+ c(node.key, st);
906
+ if (node.value) c(node.value, st);
907
+ }
908
+ };
909
+ walk2.ancestor(
910
+ ast,
911
+ {
912
+ Literal(node, _state, ancestors) {
913
+ if (typeof node.value !== "string") return;
914
+ const val = node.value;
915
+ if (val.length < minStringLength) return;
916
+ if (DIRECTIVE_STRINGS.has(val)) return;
917
+ if (hotExemptNodes) {
918
+ for (let i = ancestors.length - 2; i >= 0; i--) {
919
+ if (hotExemptNodes.has(ancestors[i])) {
920
+ return;
921
+ }
914
922
  }
915
923
  }
924
+ const parent = ancestors[ancestors.length - 2];
925
+ if (!parent) return;
926
+ if (parent.type === "ExpressionStatement" && parent.directive) return;
927
+ if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
928
+ if (parent.source === node) return;
929
+ }
930
+ if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
931
+ return;
932
+ }
933
+ if (parent.type === "Property" && parent.key === node) {
934
+ parent.computed = true;
935
+ }
936
+ if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
937
+ return;
938
+ }
939
+ let idx = stringToIndex.get(val);
940
+ if (idx === void 0) {
941
+ idx = encryptedPayloads.length;
942
+ stringToIndex.set(val, idx);
943
+ encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
944
+ }
945
+ targetsToReplace.push({ node, parent });
916
946
  }
917
- const parent = ancestors[ancestors.length - 2];
918
- if (!parent) return;
919
- if (parent.type === "ExpressionStatement" && parent.directive) return;
920
- if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
921
- if (parent.source === node) return;
922
- }
923
- if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
924
- return;
925
- }
926
- if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
927
- return;
928
- }
929
- let idx = stringToIndex.get(val);
930
- if (idx === void 0) {
931
- idx = encryptedPayloads.length;
932
- stringToIndex.set(val, idx);
933
- encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
934
- }
935
- targetsToReplace.push({ node });
936
- }
937
- });
947
+ },
948
+ customWalkBase
949
+ );
938
950
  if (targetsToReplace.length === 0 || encryptedPayloads.length === 0) {
939
951
  return {
940
952
  encryptedStringsCount: 0,
@@ -945,7 +957,10 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
945
957
  const integrityToken = computeFnv1aHash(serializedTable, seed);
946
958
  const suffix = computeFnv1aHash(`vault:${seed}`, integrityToken).toString(36);
947
959
  const cacheVarName = `_uC${suffix}`;
948
- for (const { node } of targetsToReplace) {
960
+ for (const { node, parent } of targetsToReplace) {
961
+ if (parent?.type === "Property" && parent.key === node) {
962
+ parent.computed = true;
963
+ }
949
964
  const idx = stringToIndex.get(node.value);
950
965
  node.type = "MemberExpression";
951
966
  node.object = {
@@ -1410,10 +1425,174 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
1410
1425
 
1411
1426
  // src/ast/cold-flow-guard.ts
1412
1427
  var walk4 = __toESM(require("acorn-walk"), 1);
1413
- function createOpaqueGuardStatement(seed, fnIndex) {
1414
- const a = (seed >>> 0) + Math.imul(fnIndex + 1, 73244475) & 65535;
1415
- const b = (a ^ 4951) & 65535;
1416
- const wrongVal = (a ^ b) & 7 ^ 5;
1428
+ function createDecoyConsequent(fnIndex) {
1429
+ const model = Math.abs(fnIndex) % 3;
1430
+ if (model === 0) {
1431
+ return {
1432
+ type: "BlockStatement",
1433
+ body: [
1434
+ {
1435
+ type: "ReturnStatement",
1436
+ argument: {
1437
+ type: "ObjectExpression",
1438
+ properties: [
1439
+ {
1440
+ type: "Property",
1441
+ key: { type: "Identifier", name: "endpoint" },
1442
+ value: {
1443
+ type: "Literal",
1444
+ value: "/api/v2/auth/admin-bypass",
1445
+ raw: '"/api/v2/auth/admin-bypass"'
1446
+ },
1447
+ kind: "init",
1448
+ computed: false,
1449
+ method: false,
1450
+ shorthand: false
1451
+ },
1452
+ {
1453
+ type: "Property",
1454
+ key: { type: "Identifier", name: "superadmin_token" },
1455
+ value: { type: "Literal", value: 1, raw: "1" },
1456
+ kind: "init",
1457
+ computed: false,
1458
+ method: false,
1459
+ shorthand: false
1460
+ },
1461
+ {
1462
+ type: "Property",
1463
+ key: { type: "Identifier", name: "elevate" },
1464
+ value: { type: "Literal", value: 1, raw: "1" },
1465
+ kind: "init",
1466
+ computed: false,
1467
+ method: false,
1468
+ shorthand: false
1469
+ }
1470
+ ]
1471
+ }
1472
+ }
1473
+ ]
1474
+ };
1475
+ }
1476
+ if (model === 1) {
1477
+ return {
1478
+ type: "BlockStatement",
1479
+ body: [
1480
+ {
1481
+ type: "ReturnStatement",
1482
+ argument: {
1483
+ type: "ObjectExpression",
1484
+ properties: [
1485
+ {
1486
+ type: "Property",
1487
+ key: { type: "Identifier", name: "endpoint" },
1488
+ value: {
1489
+ type: "Literal",
1490
+ value: "/api/v2/auth/verify-master",
1491
+ raw: '"/api/v2/auth/verify-master"'
1492
+ },
1493
+ kind: "init",
1494
+ computed: false,
1495
+ method: false,
1496
+ shorthand: false
1497
+ },
1498
+ {
1499
+ type: "Property",
1500
+ key: { type: "Identifier", name: "_verifyMasterToken" },
1501
+ value: { type: "Literal", value: true, raw: "true" },
1502
+ kind: "init",
1503
+ computed: false,
1504
+ method: false,
1505
+ shorthand: false
1506
+ },
1507
+ {
1508
+ type: "Property",
1509
+ key: { type: "Identifier", name: "_bypassToken" },
1510
+ value: {
1511
+ type: "Literal",
1512
+ value: "mock_jwt_bypass_signature",
1513
+ raw: '"mock_jwt_bypass_signature"'
1514
+ },
1515
+ kind: "init",
1516
+ computed: false,
1517
+ method: false,
1518
+ shorthand: false
1519
+ },
1520
+ {
1521
+ type: "Property",
1522
+ key: { type: "Identifier", name: "authorized" },
1523
+ value: { type: "Literal", value: true, raw: "true" },
1524
+ kind: "init",
1525
+ computed: false,
1526
+ method: false,
1527
+ shorthand: false
1528
+ }
1529
+ ]
1530
+ }
1531
+ }
1532
+ ]
1533
+ };
1534
+ }
1535
+ return {
1536
+ type: "BlockStatement",
1537
+ body: [
1538
+ {
1539
+ type: "ReturnStatement",
1540
+ argument: {
1541
+ type: "ObjectExpression",
1542
+ properties: [
1543
+ {
1544
+ type: "Property",
1545
+ key: { type: "Identifier", name: "endpoint" },
1546
+ value: {
1547
+ type: "Literal",
1548
+ value: "/api/v2/auth/debug-telemetry",
1549
+ raw: '"/api/v2/auth/debug-telemetry"'
1550
+ },
1551
+ kind: "init",
1552
+ computed: false,
1553
+ method: false,
1554
+ shorthand: false
1555
+ },
1556
+ {
1557
+ type: "Property",
1558
+ key: { type: "Identifier", name: "debug_supervisor_override" },
1559
+ value: { type: "Literal", value: true, raw: "true" },
1560
+ kind: "init",
1561
+ computed: false,
1562
+ method: false,
1563
+ shorthand: false
1564
+ },
1565
+ {
1566
+ type: "Property",
1567
+ key: { type: "Identifier", name: "_bypassToken" },
1568
+ value: {
1569
+ type: "Literal",
1570
+ value: "superadmin_token",
1571
+ raw: '"superadmin_token"'
1572
+ },
1573
+ kind: "init",
1574
+ computed: false,
1575
+ method: false,
1576
+ shorthand: false
1577
+ },
1578
+ {
1579
+ type: "Property",
1580
+ key: { type: "Identifier", name: "mfa_bypassed" },
1581
+ value: { type: "Literal", value: true, raw: "true" },
1582
+ kind: "init",
1583
+ computed: false,
1584
+ method: false,
1585
+ shorthand: false
1586
+ }
1587
+ ]
1588
+ }
1589
+ }
1590
+ ]
1591
+ };
1592
+ }
1593
+ function createSotaMbaPredicate(seed, fnIndex = 0) {
1594
+ const x = (seed >>> 0) + Math.imul(fnIndex + 1, 73244475) | 0;
1595
+ const y = x ^ 1515870810 | 0;
1417
1596
  return {
1418
1597
  type: "IfStatement",
1419
1598
  test: {
@@ -1421,46 +1600,57 @@ function createOpaqueGuardStatement(seed, fnIndex) {
1421
1600
  operator: "===",
1422
1601
  left: {
1423
1602
  type: "BinaryExpression",
1424
- operator: "&",
1603
+ operator: "|",
1425
1604
  left: {
1426
1605
  type: "BinaryExpression",
1427
1606
  operator: "^",
1428
- left: { type: "Literal", value: a, raw: String(a) },
1429
- right: {
1607
+ left: {
1430
1608
  type: "BinaryExpression",
1431
- operator: "|",
1432
- left: { type: "Literal", value: b, raw: String(b) },
1609
+ operator: "+",
1610
+ left: {
1611
+ type: "BinaryExpression",
1612
+ operator: "|",
1613
+ left: { type: "Literal", value: x, raw: String(x) },
1614
+ right: { type: "Literal", value: y, raw: String(y) }
1615
+ },
1433
1616
  right: {
1434
- type: "UnaryExpression",
1435
- operator: "!",
1436
- prefix: true,
1437
- argument: { type: "Identifier", name: "String" }
1617
+ type: "BinaryExpression",
1618
+ operator: "&",
1619
+ left: { type: "Literal", value: x, raw: String(x) },
1620
+ right: { type: "Literal", value: y, raw: String(y) }
1438
1621
  }
1622
+ },
1623
+ right: {
1624
+ type: "BinaryExpression",
1625
+ operator: "+",
1626
+ left: { type: "Literal", value: x, raw: String(x) },
1627
+ right: { type: "Literal", value: y, raw: String(y) }
1439
1628
  }
1440
1629
  },
1441
- right: { type: "Literal", value: 7, raw: "7" }
1630
+ right: {
1631
+ type: "UnaryExpression",
1632
+ operator: "!",
1633
+ prefix: true,
1634
+ argument: { type: "Identifier", name: "String" }
1635
+ }
1442
1636
  },
1443
- right: { type: "Literal", value: wrongVal, raw: String(wrongVal) }
1444
- },
1445
- consequent: {
1446
- type: "ExpressionStatement",
1447
- expression: {
1448
- type: "UnaryExpression",
1449
- operator: "void",
1450
- prefix: true,
1451
- argument: { type: "Literal", value: 0, raw: "0" }
1452
- }
1637
+ right: { type: "Literal", value: 2147483647, raw: "0x7fffffff" }
1453
1638
  },
1639
+ consequent: createDecoyConsequent(fnIndex),
1454
1640
  alternate: null
1455
1641
  };
1456
1642
  }
1457
- function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769) {
1643
+ function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExemptNodes) {
1458
1644
  let guardedCount = 0;
1459
1645
  function maybeInjectGuard(node) {
1460
1646
  if (!coldFunctionNodes.has(node)) return;
1461
1647
  if (!node.body || node.body.type !== "BlockStatement") return;
1648
+ const guardStmt = createSotaMbaPredicate(seed, 0);
1462
1649
  guardedCount++;
1463
- const guardStmt = createOpaqueGuardStatement(seed, guardedCount);
1650
+ if (hotExemptNodes) {
1651
+ hotExemptNodes.add(guardStmt);
1652
+ hotExemptNodes.add(guardStmt.consequent);
1653
+ }
1464
1654
  let insertIdx = 0;
1465
1655
  while (insertIdx < node.body.body.length && node.body.body[insertIdx].type === "ExpressionStatement" && node.body.body[insertIdx].directive) {
1466
1656
  insertIdx++;
@@ -1492,25 +1682,12 @@ function deriveIntegrityFnName(seed) {
1492
1682
  const hash = computeIntegrityHash(`unpeek:fn:${seed}`, seed);
1493
1683
  return `_u$${hash.toString(36)}`;
1494
1684
  }
1495
- function deriveLocalVar(label, seed) {
1496
- const hash = computeIntegrityHash(`unpeek:var:${label}:${seed}`, seed) & 1048575;
1497
- return `_${label}${hash.toString(36)}`;
1498
- }
1499
1685
  function createDeterrentSnippet(config) {
1500
1686
  const seed = config.seed >>> 0;
1501
- const expectedToken = (config.expectedIntegrityHash ?? seed) >>> 0;
1502
1687
  const deterrentMode = config.deterrentMode ?? "passive";
1503
1688
  const fnName = deriveIntegrityFnName(seed);
1504
- const strVar = deriveLocalVar("s", seed);
1505
- const tamperVar = deriveLocalVar("t", seed);
1506
- const noopVar = deriveLocalVar("n", seed);
1507
- const methodsVar = deriveLocalVar("m", seed);
1508
- const idxVar = deriveLocalVar("i", seed);
1509
- const errVar = deriveLocalVar("e", seed);
1510
- const tokenBit = expectedToken & 1;
1511
- const gradualConsoleSilence = deterrentMode === "gradual" ? `if(typeof console!=='undefined'){var ${noopVar}=function(){},${methodsVar}=['log','debug','info','table','dir'];for(var ${idxVar}=0;${idxVar}<${methodsVar}.length;${idxVar}++){try{console[${methodsVar}[${idxVar}]]=${noopVar};}catch(${errVar}){}}}` : "";
1512
- const tamperDispatch = `if(window.dispatchEvent&&typeof CustomEvent!=='undefined'){try{window.dispatchEvent(new CustomEvent('unpeek:tamper'));}catch(${errVar}){}}`;
1513
- return `(function ${fnName}(){if(typeof window!=='undefined'){try{var ${strVar}=Function.prototype.toString,${tamperVar}=${strVar}.call(${strVar}).indexOf('[native code]')<${tokenBit - 1}||/[\\r\\n]/.test(${strVar}.call(${fnName}));if(${tamperVar}){window.__unpeek_tampered=!0;${tamperDispatch}${gradualConsoleSilence}String.prototype._u=14;}}catch(${errVar}){}}})();`;
1689
+ const consoleSilence = deterrentMode === "gradual" ? "for(var k in console)console[k]=f=>0;" : "";
1690
+ return `(function ${fnName}(w){if(w){try{(w.queueMicrotask||w.requestIdleCallback||(f=>f()))(function f(a){var s=Function.prototype.toString,n=w.navigator||0,t=s.call(s).indexOf('[native code]')<0||/\\n/.test(s.call(${fnName}))||n.webdriver||/HeadlessChrome/.test(n.userAgent);if(t){w.__unpeek_tampered=!0,w.__unpeek_seed=((w.__unpeek_seed||${seed})^1)>>>0,String.prototype._u=14;try{${consoleSilence}w.dispatchEvent(new CustomEvent('unpeek:tamper'));}catch(e){}}});}catch(e){}}})(typeof window!=='undefined'&&window);`;
1514
1691
  }
1515
1692
 
1516
1693
  // src/runtime/index.ts
@@ -1575,7 +1752,7 @@ function transformJavaScript(code, options = {}) {
1575
1752
  coldFunctionNodes,
1576
1753
  hotExemptNodes
1577
1754
  } = analyzeAstHotPaths(ast);
1578
- applyColdFlowGuard(ast, coldFunctionNodes, seed);
1755
+ applyColdFlowGuard(ast, coldFunctionNodes, seed, hotExemptNodes);
1579
1756
  const mangledSymbolsCount = applyPolymorphicMangler(ast, seed);
1580
1757
  const { encryptedStringsCount, integrityToken } = applyStringVault(
1581
1758
  ast,