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