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
package/dist/browser.cjs CHANGED
@@ -600,27 +600,25 @@ var DIRECTIVE_STRINGS = /* @__PURE__ */ new Set([
600
600
  "use asm"
601
601
  ]);
602
602
  var HELPER_TEMPLATE = acorn2.parse(
603
- `var _ = [].map(function f(a) {
604
- var s = "";
605
- for (var j = 0; j < a.length; j++) {
606
- s += String.fromCharCode(a.charCodeAt(j) ^ (((0 ^ ((a._u || /\\n/.test(f) * 14) | ((j & 1) * 0))) & 30) | 1));
607
- }
608
- return s;
609
- });`,
603
+ `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;});})([]);`,
610
604
  { ecmaVersion: "latest", sourceType: "script" }
611
605
  ).body[0];
612
606
  function createVaultHelperAst(cacheVarName, encryptedPayloads, mask, step) {
613
607
  const node = structuredClone(HELPER_TEMPLATE);
614
608
  node.declarations[0].id.name = cacheVarName;
615
- node.declarations[0].init.callee.object.elements = encryptedPayloads.map(
609
+ node.declarations[0].init.arguments[0].elements = encryptedPayloads.map(
616
610
  (str) => ({
617
611
  type: "Literal",
618
612
  value: str,
619
613
  raw: JSON.stringify(str)
620
614
  })
621
615
  );
622
- const fn = node.declarations[0].init.arguments[0];
623
- const binaryXor = fn.body.body[1].body.body[0].expression.right.arguments[0];
616
+ const outerFn = node.declarations[0].init.callee;
617
+ const returnStmt = outerFn.body.body[0];
618
+ const innerFn = returnStmt.argument.arguments[0];
619
+ const forStmt = innerFn.body.body[1];
620
+ const expr = forStmt.body.type === "BlockStatement" ? forStmt.body.body[0].expression : forStmt.body.expression;
621
+ const binaryXor = expr.right.arguments[0];
624
622
  const innerLeft = binaryXor.right.left.left;
625
623
  innerLeft.left.value = mask;
626
624
  innerLeft.left.raw = String(mask);
@@ -658,44 +656,58 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
658
656
  };
659
657
  }
660
658
  const mask = seed >>> 0 & 30 | 3;
661
- const step = (seed >>> 5 & 7) * 2 + 3;
659
+ const step = 1;
662
660
  const stringToIndex = /* @__PURE__ */ new Map();
663
661
  const encryptedPayloads = [];
664
662
  const targetsToReplace = [];
665
- walk2.ancestor(ast, {
666
- Literal(node, _state, ancestors) {
667
- if (typeof node.value !== "string") return;
668
- const val = node.value;
669
- if (val.length < minStringLength) return;
670
- if (DIRECTIVE_STRINGS.has(val)) return;
671
- if (hotExemptNodes) {
672
- for (let i = ancestors.length - 2; i >= 0; i--) {
673
- if (hotExemptNodes.has(ancestors[i])) {
674
- return;
663
+ const customWalkBase = {
664
+ ...walk2.base,
665
+ Property(node, st, c) {
666
+ c(node.key, st);
667
+ if (node.value) c(node.value, st);
668
+ }
669
+ };
670
+ walk2.ancestor(
671
+ ast,
672
+ {
673
+ Literal(node, _state, ancestors) {
674
+ if (typeof node.value !== "string") return;
675
+ const val = node.value;
676
+ if (val.length < minStringLength) return;
677
+ if (DIRECTIVE_STRINGS.has(val)) return;
678
+ if (hotExemptNodes) {
679
+ for (let i = ancestors.length - 2; i >= 0; i--) {
680
+ if (hotExemptNodes.has(ancestors[i])) {
681
+ return;
682
+ }
675
683
  }
676
684
  }
685
+ const parent = ancestors[ancestors.length - 2];
686
+ if (!parent) return;
687
+ if (parent.type === "ExpressionStatement" && parent.directive) return;
688
+ if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
689
+ if (parent.source === node) return;
690
+ }
691
+ if ((parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
692
+ return;
693
+ }
694
+ if (parent.type === "Property" && parent.key === node) {
695
+ parent.computed = true;
696
+ }
697
+ if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
698
+ return;
699
+ }
700
+ let idx = stringToIndex.get(val);
701
+ if (idx === void 0) {
702
+ idx = encryptedPayloads.length;
703
+ stringToIndex.set(val, idx);
704
+ encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
705
+ }
706
+ targetsToReplace.push({ node, parent });
677
707
  }
678
- const parent = ancestors[ancestors.length - 2];
679
- if (!parent) return;
680
- if (parent.type === "ExpressionStatement" && parent.directive) return;
681
- if (parent.type === "ImportDeclaration" || parent.type === "ExportNamedDeclaration" || parent.type === "ExportAllDeclaration" || parent.type === "ImportExpression") {
682
- if (parent.source === node) return;
683
- }
684
- if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !parent.computed) {
685
- return;
686
- }
687
- if (parent.type === "ImportAttribute" || parent.type === "ImportSpecifier" || parent.type === "ExportSpecifier") {
688
- return;
689
- }
690
- let idx = stringToIndex.get(val);
691
- if (idx === void 0) {
692
- idx = encryptedPayloads.length;
693
- stringToIndex.set(val, idx);
694
- encryptedPayloads.push(encodeStringWithSeed(val, mask, step));
695
- }
696
- targetsToReplace.push({ node });
697
- }
698
- });
708
+ },
709
+ customWalkBase
710
+ );
699
711
  if (targetsToReplace.length === 0 || encryptedPayloads.length === 0) {
700
712
  return {
701
713
  encryptedStringsCount: 0,
@@ -706,7 +718,10 @@ function applyStringVault(ast, seed = 2654435769, minStringLength = 4, hotExempt
706
718
  const integrityToken = computeFnv1aHash(serializedTable, seed);
707
719
  const suffix = computeFnv1aHash(`vault:${seed}`, integrityToken).toString(36);
708
720
  const cacheVarName = `_uC${suffix}`;
709
- for (const { node } of targetsToReplace) {
721
+ for (const { node, parent } of targetsToReplace) {
722
+ if (parent?.type === "Property" && parent.key === node) {
723
+ parent.computed = true;
724
+ }
710
725
  const idx = stringToIndex.get(node.value);
711
726
  node.type = "MemberExpression";
712
727
  node.object = {
@@ -1171,10 +1186,174 @@ function applyPolymorphicMangler(ast, seed = 2654435769) {
1171
1186
 
1172
1187
  // src/ast/cold-flow-guard.ts
1173
1188
  var walk4 = __toESM(require("acorn-walk"), 1);
1174
- function createOpaqueGuardStatement(seed, fnIndex) {
1175
- const a = (seed >>> 0) + Math.imul(fnIndex + 1, 73244475) & 65535;
1176
- const b = (a ^ 4951) & 65535;
1177
- const wrongVal = (a ^ b) & 7 ^ 5;
1189
+ function createDecoyConsequent(fnIndex) {
1190
+ const model = Math.abs(fnIndex) % 3;
1191
+ if (model === 0) {
1192
+ return {
1193
+ type: "BlockStatement",
1194
+ body: [
1195
+ {
1196
+ type: "ReturnStatement",
1197
+ argument: {
1198
+ type: "ObjectExpression",
1199
+ properties: [
1200
+ {
1201
+ type: "Property",
1202
+ key: { type: "Identifier", name: "endpoint" },
1203
+ value: {
1204
+ type: "Literal",
1205
+ value: "/api/v2/auth/admin-bypass",
1206
+ raw: '"/api/v2/auth/admin-bypass"'
1207
+ },
1208
+ kind: "init",
1209
+ computed: false,
1210
+ method: false,
1211
+ shorthand: false
1212
+ },
1213
+ {
1214
+ type: "Property",
1215
+ key: { type: "Identifier", name: "superadmin_token" },
1216
+ value: { type: "Literal", value: 1, raw: "1" },
1217
+ kind: "init",
1218
+ computed: false,
1219
+ method: false,
1220
+ shorthand: false
1221
+ },
1222
+ {
1223
+ type: "Property",
1224
+ key: { type: "Identifier", name: "elevate" },
1225
+ value: { type: "Literal", value: 1, raw: "1" },
1226
+ kind: "init",
1227
+ computed: false,
1228
+ method: false,
1229
+ shorthand: false
1230
+ }
1231
+ ]
1232
+ }
1233
+ }
1234
+ ]
1235
+ };
1236
+ }
1237
+ if (model === 1) {
1238
+ return {
1239
+ type: "BlockStatement",
1240
+ body: [
1241
+ {
1242
+ type: "ReturnStatement",
1243
+ argument: {
1244
+ type: "ObjectExpression",
1245
+ properties: [
1246
+ {
1247
+ type: "Property",
1248
+ key: { type: "Identifier", name: "endpoint" },
1249
+ value: {
1250
+ type: "Literal",
1251
+ value: "/api/v2/auth/verify-master",
1252
+ raw: '"/api/v2/auth/verify-master"'
1253
+ },
1254
+ kind: "init",
1255
+ computed: false,
1256
+ method: false,
1257
+ shorthand: false
1258
+ },
1259
+ {
1260
+ type: "Property",
1261
+ key: { type: "Identifier", name: "_verifyMasterToken" },
1262
+ value: { type: "Literal", value: true, raw: "true" },
1263
+ kind: "init",
1264
+ computed: false,
1265
+ method: false,
1266
+ shorthand: false
1267
+ },
1268
+ {
1269
+ type: "Property",
1270
+ key: { type: "Identifier", name: "_bypassToken" },
1271
+ value: {
1272
+ type: "Literal",
1273
+ value: "mock_jwt_bypass_signature",
1274
+ raw: '"mock_jwt_bypass_signature"'
1275
+ },
1276
+ kind: "init",
1277
+ computed: false,
1278
+ method: false,
1279
+ shorthand: false
1280
+ },
1281
+ {
1282
+ type: "Property",
1283
+ key: { type: "Identifier", name: "authorized" },
1284
+ value: { type: "Literal", value: true, raw: "true" },
1285
+ kind: "init",
1286
+ computed: false,
1287
+ method: false,
1288
+ shorthand: false
1289
+ }
1290
+ ]
1291
+ }
1292
+ }
1293
+ ]
1294
+ };
1295
+ }
1296
+ return {
1297
+ type: "BlockStatement",
1298
+ body: [
1299
+ {
1300
+ type: "ReturnStatement",
1301
+ argument: {
1302
+ type: "ObjectExpression",
1303
+ properties: [
1304
+ {
1305
+ type: "Property",
1306
+ key: { type: "Identifier", name: "endpoint" },
1307
+ value: {
1308
+ type: "Literal",
1309
+ value: "/api/v2/auth/debug-telemetry",
1310
+ raw: '"/api/v2/auth/debug-telemetry"'
1311
+ },
1312
+ kind: "init",
1313
+ computed: false,
1314
+ method: false,
1315
+ shorthand: false
1316
+ },
1317
+ {
1318
+ type: "Property",
1319
+ key: { type: "Identifier", name: "debug_supervisor_override" },
1320
+ value: { type: "Literal", value: true, raw: "true" },
1321
+ kind: "init",
1322
+ computed: false,
1323
+ method: false,
1324
+ shorthand: false
1325
+ },
1326
+ {
1327
+ type: "Property",
1328
+ key: { type: "Identifier", name: "_bypassToken" },
1329
+ value: {
1330
+ type: "Literal",
1331
+ value: "superadmin_token",
1332
+ raw: '"superadmin_token"'
1333
+ },
1334
+ kind: "init",
1335
+ computed: false,
1336
+ method: false,
1337
+ shorthand: false
1338
+ },
1339
+ {
1340
+ type: "Property",
1341
+ key: { type: "Identifier", name: "mfa_bypassed" },
1342
+ value: { type: "Literal", value: true, raw: "true" },
1343
+ kind: "init",
1344
+ computed: false,
1345
+ method: false,
1346
+ shorthand: false
1347
+ }
1348
+ ]
1349
+ }
1350
+ }
1351
+ ]
1352
+ };
1353
+ }
1354
+ function createSotaMbaPredicate(seed, fnIndex = 0) {
1355
+ const x = (seed >>> 0) + Math.imul(fnIndex + 1, 73244475) | 0;
1356
+ const y = x ^ 1515870810 | 0;
1178
1357
  return {
1179
1358
  type: "IfStatement",
1180
1359
  test: {
@@ -1182,46 +1361,57 @@ function createOpaqueGuardStatement(seed, fnIndex) {
1182
1361
  operator: "===",
1183
1362
  left: {
1184
1363
  type: "BinaryExpression",
1185
- operator: "&",
1364
+ operator: "|",
1186
1365
  left: {
1187
1366
  type: "BinaryExpression",
1188
1367
  operator: "^",
1189
- left: { type: "Literal", value: a, raw: String(a) },
1190
- right: {
1368
+ left: {
1191
1369
  type: "BinaryExpression",
1192
- operator: "|",
1193
- left: { type: "Literal", value: b, raw: String(b) },
1370
+ operator: "+",
1371
+ left: {
1372
+ type: "BinaryExpression",
1373
+ operator: "|",
1374
+ left: { type: "Literal", value: x, raw: String(x) },
1375
+ right: { type: "Literal", value: y, raw: String(y) }
1376
+ },
1194
1377
  right: {
1195
- type: "UnaryExpression",
1196
- operator: "!",
1197
- prefix: true,
1198
- argument: { type: "Identifier", name: "String" }
1378
+ type: "BinaryExpression",
1379
+ operator: "&",
1380
+ left: { type: "Literal", value: x, raw: String(x) },
1381
+ right: { type: "Literal", value: y, raw: String(y) }
1199
1382
  }
1383
+ },
1384
+ right: {
1385
+ type: "BinaryExpression",
1386
+ operator: "+",
1387
+ left: { type: "Literal", value: x, raw: String(x) },
1388
+ right: { type: "Literal", value: y, raw: String(y) }
1200
1389
  }
1201
1390
  },
1202
- right: { type: "Literal", value: 7, raw: "7" }
1391
+ right: {
1392
+ type: "UnaryExpression",
1393
+ operator: "!",
1394
+ prefix: true,
1395
+ argument: { type: "Identifier", name: "String" }
1396
+ }
1203
1397
  },
1204
- right: { type: "Literal", value: wrongVal, raw: String(wrongVal) }
1205
- },
1206
- consequent: {
1207
- type: "ExpressionStatement",
1208
- expression: {
1209
- type: "UnaryExpression",
1210
- operator: "void",
1211
- prefix: true,
1212
- argument: { type: "Literal", value: 0, raw: "0" }
1213
- }
1398
+ right: { type: "Literal", value: 2147483647, raw: "0x7fffffff" }
1214
1399
  },
1400
+ consequent: createDecoyConsequent(fnIndex),
1215
1401
  alternate: null
1216
1402
  };
1217
1403
  }
1218
- function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769) {
1404
+ function applyColdFlowGuard(ast, coldFunctionNodes, seed = 2654435769, hotExemptNodes) {
1219
1405
  let guardedCount = 0;
1220
1406
  function maybeInjectGuard(node) {
1221
1407
  if (!coldFunctionNodes.has(node)) return;
1222
1408
  if (!node.body || node.body.type !== "BlockStatement") return;
1409
+ const guardStmt = createSotaMbaPredicate(seed, 0);
1223
1410
  guardedCount++;
1224
- const guardStmt = createOpaqueGuardStatement(seed, guardedCount);
1411
+ if (hotExemptNodes) {
1412
+ hotExemptNodes.add(guardStmt);
1413
+ hotExemptNodes.add(guardStmt.consequent);
1414
+ }
1225
1415
  let insertIdx = 0;
1226
1416
  while (insertIdx < node.body.body.length && node.body.body[insertIdx].type === "ExpressionStatement" && node.body.body[insertIdx].directive) {
1227
1417
  insertIdx++;
@@ -1257,25 +1447,12 @@ function deriveIntegrityFnName(seed) {
1257
1447
  const hash = computeIntegrityHash(`unpeek:fn:${seed}`, seed);
1258
1448
  return `_u$${hash.toString(36)}`;
1259
1449
  }
1260
- function deriveLocalVar(label, seed) {
1261
- const hash = computeIntegrityHash(`unpeek:var:${label}:${seed}`, seed) & 1048575;
1262
- return `_${label}${hash.toString(36)}`;
1263
- }
1264
1450
  function createDeterrentSnippet(config) {
1265
1451
  const seed = config.seed >>> 0;
1266
- const expectedToken = (config.expectedIntegrityHash ?? seed) >>> 0;
1267
1452
  const deterrentMode = config.deterrentMode ?? "passive";
1268
1453
  const fnName = deriveIntegrityFnName(seed);
1269
- const strVar = deriveLocalVar("s", seed);
1270
- const tamperVar = deriveLocalVar("t", seed);
1271
- const noopVar = deriveLocalVar("n", seed);
1272
- const methodsVar = deriveLocalVar("m", seed);
1273
- const idxVar = deriveLocalVar("i", seed);
1274
- const errVar = deriveLocalVar("e", seed);
1275
- const tokenBit = expectedToken & 1;
1276
- 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}){}}}` : "";
1277
- const tamperDispatch = `if(window.dispatchEvent&&typeof CustomEvent!=='undefined'){try{window.dispatchEvent(new CustomEvent('unpeek:tamper'));}catch(${errVar}){}}`;
1278
- 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}){}}})();`;
1454
+ const consoleSilence = deterrentMode === "gradual" ? "for(var k in console)console[k]=f=>0;" : "";
1455
+ 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);`;
1279
1456
  }
1280
1457
 
1281
1458
  // src/runtime/index.ts
@@ -1340,7 +1517,7 @@ function transformJavaScript(code, options = {}) {
1340
1517
  coldFunctionNodes,
1341
1518
  hotExemptNodes
1342
1519
  } = analyzeAstHotPaths(ast);
1343
- applyColdFlowGuard(ast, coldFunctionNodes, seed);
1520
+ applyColdFlowGuard(ast, coldFunctionNodes, seed, hotExemptNodes);
1344
1521
  const mangledSymbolsCount = applyPolymorphicMangler(ast, seed);
1345
1522
  const { encryptedStringsCount, integrityToken } = applyStringVault(
1346
1523
  ast,