@nekzus/liop 2.4.0-alpha.25 → 2.4.0-alpha.26

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 (61) hide show
  1. package/dist/bin/agent.js +27 -933
  2. package/dist/bridge.js +1 -5
  3. package/dist/chunk-2XHSALII.js +3 -0
  4. package/dist/chunk-2ZYX736J.js +2 -0
  5. package/dist/chunk-3WRKTVOF.js +2 -0
  6. package/dist/chunk-6KFKO6ZX.js +2 -0
  7. package/dist/chunk-6VK2XALI.js +2 -0
  8. package/dist/chunk-AYDF3IFZ.js +2 -0
  9. package/dist/chunk-CTTTLCGL.js +31 -0
  10. package/dist/chunk-ECRS6N33.js +4 -0
  11. package/dist/chunk-FDJMH4CB.js +2 -0
  12. package/dist/chunk-FTF6AMF6.js +0 -1
  13. package/dist/chunk-GWVSCZUS.js +2 -0
  14. package/dist/chunk-LEXMMIQI.js +15 -0
  15. package/dist/chunk-LM26HOJ6.js +31 -0
  16. package/dist/chunk-MQ5SU26J.js +2 -0
  17. package/dist/chunk-N35BC62H.js +2 -0
  18. package/dist/chunk-P6IJ3VBD.js +3 -0
  19. package/dist/chunk-TKLRW52V.js +2 -0
  20. package/dist/chunk-V35CXJQN.js +2 -0
  21. package/dist/chunk-WP5ER7RH.js +3 -0
  22. package/dist/chunk-XLVEDKM6.js +2 -0
  23. package/dist/chunk-Y675R34Z.js +2 -0
  24. package/dist/chunk-YD2AF2M6.js +46 -0
  25. package/dist/chunk-YLCNV764.js +3 -0
  26. package/dist/chunk-ZZMSFCJP.js +3 -0
  27. package/dist/client.js +1 -12
  28. package/dist/gateway.js +1 -13
  29. package/dist/index.js +4 -320
  30. package/dist/kyber-PULV7PQX.js +2 -0
  31. package/dist/mesh.js +1 -5
  32. package/dist/server.js +1 -10
  33. package/dist/types.js +1 -3
  34. package/dist/verifier-J4NQYZX5.js +2 -0
  35. package/dist/workers/logic-execution.js +1 -355
  36. package/dist/workers/zk-verifier.js +1 -275
  37. package/package.json +1 -1
  38. package/dist/chunk-3664H3CV.js +0 -136
  39. package/dist/chunk-3QH44NKZ.js +0 -115
  40. package/dist/chunk-6MYJBM2E.js +0 -818
  41. package/dist/chunk-7PITYWOV.js +0 -68
  42. package/dist/chunk-DSOB2JKM.js +0 -1507
  43. package/dist/chunk-ET6VRUK2.js +0 -58
  44. package/dist/chunk-FIVRGOA4.js +0 -280
  45. package/dist/chunk-IHKKWXHJ.js +0 -876
  46. package/dist/chunk-IK7CGWAZ.js +0 -24
  47. package/dist/chunk-JPWC6XG6.js +0 -941
  48. package/dist/chunk-K3DGJLUY.js +0 -106
  49. package/dist/chunk-KR5UZUEY.js +0 -98
  50. package/dist/chunk-NXCAFF73.js +0 -59
  51. package/dist/chunk-Q3FKHJ3B.js +0 -345
  52. package/dist/chunk-R52TZEIL.js +0 -426
  53. package/dist/chunk-S3RAZTEM.js +0 -807
  54. package/dist/chunk-V47P2AQR.js +0 -281
  55. package/dist/chunk-WXOWJXY6.js +0 -564
  56. package/dist/chunk-WXYDFCIW.js +0 -293
  57. package/dist/chunk-Y62GORQF.js +0 -51
  58. package/dist/chunk-ZP3VVXOI.js +0 -66
  59. package/dist/chunk-ZU3WVVLC.js +0 -3752
  60. package/dist/kyber-CGOA7Q6A.js +0 -3
  61. package/dist/verifier-55HW6AFN.js +0 -6
@@ -1,426 +0,0 @@
1
- import crypto from 'crypto';
2
- import * as fs from 'fs/promises';
3
- import * as os from 'os';
4
- import * as path from 'path';
5
- import vm from 'vm';
6
- import { WASI } from 'wasi';
7
- import * as acorn from 'acorn';
8
- import { simple } from 'acorn-walk';
9
-
10
- // src/sandbox/guardian.ts
11
- var GuardianError = class extends Error {
12
- constructor(message) {
13
- super(`AST Sec-Policy Violation: ${message}`);
14
- this.name = "GuardianError";
15
- }
16
- };
17
- var ASTGuardian = {
18
- /**
19
- * Analyzes the WebAssembly Module interface proactively.
20
- *
21
- * @param module - The compiled WebAssembly.Module to inspect
22
- * @throws {GuardianError} If illegal imports or capabilities are detected
23
- */
24
- analyze(module) {
25
- const imports = WebAssembly.Module.imports(module);
26
- let _importCount = 0;
27
- const ALLOWED_WASI_FUNCTIONS = /* @__PURE__ */ new Set([
28
- "fd_write",
29
- "fd_read",
30
- "fd_close",
31
- "fd_seek",
32
- "environ_get",
33
- "environ_sizes_get",
34
- "args_get",
35
- "args_sizes_get",
36
- "clock_time_get",
37
- "random_get",
38
- "proc_exit",
39
- "fd_prestat_get",
40
- "fd_prestat_dir_name",
41
- "fd_fdstat_get"
42
- ]);
43
- for (const imp of imports) {
44
- if (imp.module === "wasi_snapshot_preview1") {
45
- if (!ALLOWED_WASI_FUNCTIONS.has(imp.name)) {
46
- throw new GuardianError(
47
- `Banned WASI Import Detected: ${imp.module}/${imp.name}`
48
- );
49
- }
50
- } else {
51
- throw new GuardianError(
52
- `Banned Host Import Module Detected: ${imp.module}`
53
- );
54
- }
55
- _importCount++;
56
- if (_importCount > 128) {
57
- throw new GuardianError(
58
- "Import limit exceeded. Possible resource exhaustion attack."
59
- );
60
- }
61
- }
62
- }
63
- };
64
- var originalEmit = process.emit;
65
- process.emit = (name, data, ...args) => {
66
- if (name === "warning" && typeof data === "object" && data.name === "ExperimentalWarning" && String(data.message).includes("WASI") || String(data.message).includes("importing WASI")) {
67
- return false;
68
- }
69
- return originalEmit.call(process, name, data, ...args);
70
- };
71
- function getDefaultEnvironment() {
72
- const isWindows = process.platform === "win32";
73
- const safeKeys = isWindows ? [
74
- "APPDATA",
75
- "HOMEDRIVE",
76
- "HOMEPATH",
77
- "LOCALAPPDATA",
78
- "PATH",
79
- "PROCESSOR_ARCHITECTURE",
80
- "SYSTEMDRIVE",
81
- "SYSTEMROOT",
82
- "TEMP",
83
- "USERNAME",
84
- "USERPROFILE",
85
- "PROGRAMFILES"
86
- ] : ["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"];
87
- const env = {
88
- NODE_ENV: "production",
89
- LIOP_NODE: "true"
90
- };
91
- for (const key of safeKeys) {
92
- const val = process.env[key];
93
- if (val !== void 0 && !val.startsWith("()")) {
94
- env[key] = val;
95
- }
96
- }
97
- return env;
98
- }
99
- function calculateAstInstructionFuel(code) {
100
- try {
101
- const ast = acorn.parse(code, {
102
- ecmaVersion: 2022,
103
- sourceType: "script",
104
- allowReturnOutsideFunction: true
105
- });
106
- let score = 100;
107
- simple(ast, {
108
- Identifier() {
109
- score += 1;
110
- },
111
- Literal() {
112
- score += 1;
113
- },
114
- BinaryExpression() {
115
- score += 2;
116
- },
117
- UnaryExpression() {
118
- score += 2;
119
- },
120
- LogicalExpression() {
121
- score += 2;
122
- },
123
- // biome-ignore lint/suspicious/noExplicitAny: ESTree node
124
- VariableDeclaration(node) {
125
- score += 5 * (node.declarations?.length || 1);
126
- },
127
- AssignmentExpression() {
128
- score += 3;
129
- },
130
- MemberExpression() {
131
- score += 3;
132
- },
133
- // biome-ignore lint/suspicious/noExplicitAny: ESTree node
134
- ObjectExpression(node) {
135
- score += 5 + (node.properties?.length || 0) * 2;
136
- },
137
- // biome-ignore lint/suspicious/noExplicitAny: ESTree node
138
- ArrayExpression(node) {
139
- score += 5 + (node.elements?.length || 0) * 2;
140
- },
141
- // biome-ignore lint/suspicious/noExplicitAny: ESTree node
142
- CallExpression(node) {
143
- score += 10 + (node.arguments?.length || 0) * 2;
144
- },
145
- FunctionDeclaration() {
146
- score += 20;
147
- },
148
- FunctionExpression() {
149
- score += 15;
150
- },
151
- ArrowFunctionExpression() {
152
- score += 15;
153
- },
154
- IfStatement() {
155
- score += 5;
156
- },
157
- SwitchStatement() {
158
- score += 10;
159
- },
160
- ForStatement() {
161
- score += 50;
162
- },
163
- ForInStatement() {
164
- score += 50;
165
- },
166
- ForOfStatement() {
167
- score += 50;
168
- },
169
- WhileStatement() {
170
- score += 50;
171
- },
172
- DoWhileStatement() {
173
- score += 50;
174
- },
175
- TryStatement() {
176
- score += 15;
177
- },
178
- ReturnStatement() {
179
- score += 2;
180
- }
181
- });
182
- return Math.ceil(score / 100) * 100;
183
- } catch (_e) {
184
- return 500;
185
- }
186
- }
187
- var WasiSandbox = class {
188
- wasi;
189
- sandboxId;
190
- workingDir;
191
- config;
192
- stdoutHandle = null;
193
- stderrHandle = null;
194
- constructor(config = {}) {
195
- this.sandboxId = crypto.randomUUID();
196
- this.workingDir = path.join(
197
- os.tmpdir(),
198
- "liop-mesh",
199
- "sandboxes",
200
- this.sandboxId
201
- );
202
- this.config = config;
203
- }
204
- /**
205
- * Initializes the physical sandbox environment with strict directory lockdown.
206
- */
207
- async init() {
208
- try {
209
- await fs.mkdir(this.workingDir, { recursive: true });
210
- this.stdoutHandle = await fs.open(
211
- path.join(this.workingDir, "stdout.log"),
212
- "w+"
213
- );
214
- this.stderrHandle = await fs.open(
215
- path.join(this.workingDir, "stderr.log"),
216
- "w+"
217
- );
218
- this.wasi = new WASI({
219
- version: "preview1",
220
- args: ["liop_runtime"],
221
- env: this.config.allowEnv ? { ...getDefaultEnvironment(), RUNTIME_ID: this.sandboxId } : {
222
- NODE_ENV: "production",
223
- LIOP_NODE: "true",
224
- RUNTIME_ID: this.sandboxId
225
- },
226
- preopens: {
227
- "/sandbox": this.workingDir,
228
- ...this.config.allowedDirectories
229
- },
230
- stdout: this.stdoutHandle.fd,
231
- stderr: this.stderrHandle.fd
232
- });
233
- } catch (error) {
234
- throw new Error(
235
- `Sandbox Initialization Failed: ${error instanceof Error ? error.message : "FS Error"}`
236
- );
237
- }
238
- }
239
- /**
240
- * Executes logic (WASM or JS-Wrapped) with hard resource limits.
241
- */
242
- async execute(compiledLogic, records = [], inputs = {}) {
243
- const startTime = performance.now();
244
- if (compiledLogic instanceof Buffer) {
245
- try {
246
- const module = await WebAssembly.compile(new Uint8Array(compiledLogic));
247
- ASTGuardian.analyze(module);
248
- const instance = await WebAssembly.instantiate(
249
- module,
250
- this.wasi.getImportObject()
251
- );
252
- this.wasi.start(instance);
253
- const stdoutPath = path.join(this.workingDir, "stdout.log");
254
- const stderrPath = path.join(this.workingDir, "stderr.log");
255
- const stdout = await fs.readFile(stdoutPath, "utf-8");
256
- const stderr = await fs.readFile(stderrPath, "utf-8");
257
- const duration = performance.now() - startTime;
258
- return {
259
- output: stdout || (stderr ? `Error: ${stderr}` : "WASM_EXECUTION_SUCCESS"),
260
- fuelConsumed: Math.max(
261
- 100,
262
- Math.ceil(Math.floor(duration * 1e3) / 100) * 100
263
- )
264
- };
265
- } catch (error) {
266
- throw new Error(
267
- `WASM Runtime Error: ${error instanceof Error ? error.message : String(error)}`
268
- );
269
- }
270
- } else {
271
- const sandboxEnv = /* @__PURE__ */ Object.create(null);
272
- const env = { records, ...inputs };
273
- sandboxEnv.require = void 0;
274
- sandboxEnv.process = void 0;
275
- sandboxEnv.global = void 0;
276
- sandboxEnv.globalThis = void 0;
277
- sandboxEnv.Buffer = void 0;
278
- sandboxEnv.setTimeout = void 0;
279
- sandboxEnv.setInterval = void 0;
280
- sandboxEnv.setImmediate = void 0;
281
- sandboxEnv.queueMicrotask = void 0;
282
- sandboxEnv.eval = void 0;
283
- sandboxEnv.Function = void 0;
284
- sandboxEnv.SharedArrayBuffer = void 0;
285
- sandboxEnv.Date = void 0;
286
- sandboxEnv.performance = void 0;
287
- sandboxEnv.Intl = void 0;
288
- sandboxEnv.ArrayBuffer = void 0;
289
- sandboxEnv.Uint8Array = void 0;
290
- sandboxEnv.Int8Array = void 0;
291
- sandboxEnv.Uint16Array = void 0;
292
- sandboxEnv.Int16Array = void 0;
293
- sandboxEnv.Uint32Array = void 0;
294
- sandboxEnv.Int32Array = void 0;
295
- sandboxEnv.Float32Array = void 0;
296
- sandboxEnv.Float64Array = void 0;
297
- sandboxEnv.BigInt64Array = void 0;
298
- sandboxEnv.BigUint64Array = void 0;
299
- sandboxEnv.DataView = void 0;
300
- const toNullPrototype = (obj) => {
301
- if (!obj || typeof obj !== "object") {
302
- return obj;
303
- }
304
- if (Array.isArray(obj)) {
305
- return obj.map(toNullPrototype);
306
- }
307
- const clone = /* @__PURE__ */ Object.create(null);
308
- for (const [key, val] of Object.entries(obj)) {
309
- clone[key] = toNullPrototype(val);
310
- }
311
- return clone;
312
- };
313
- sandboxEnv.records = toNullPrototype(JSON.parse(JSON.stringify(records)));
314
- sandboxEnv.env = toNullPrototype(JSON.parse(JSON.stringify(env)));
315
- for (const [key, value] of Object.entries(inputs)) {
316
- sandboxEnv[key] = toNullPrototype(JSON.parse(JSON.stringify(value)));
317
- }
318
- const deepFreeze = (obj) => {
319
- if (obj && typeof obj === "object" && !Object.isFrozen(obj)) {
320
- Object.freeze(obj);
321
- for (const key of Object.keys(obj)) {
322
- deepFreeze(obj[key]);
323
- }
324
- }
325
- return obj;
326
- };
327
- deepFreeze(sandboxEnv.records);
328
- deepFreeze(sandboxEnv.env);
329
- for (const key of Object.keys(sandboxEnv)) {
330
- Object.defineProperty(sandboxEnv, key, {
331
- writable: false,
332
- configurable: false
333
- });
334
- }
335
- let processedLogic = String(compiledLogic);
336
- if (/^\s*return\s/m.test(processedLogic) || !processedLogic.includes("function liop_main")) {
337
- if (!processedLogic.includes("function liop_main")) {
338
- processedLogic = `function liop_main(env) {
339
- ${processedLogic}
340
- }`;
341
- }
342
- }
343
- const scriptCode = `
344
- (function() {
345
- "use strict";
346
- try {
347
- // Pre-execution prototype freezing (PCI-DSS Compliance)
348
- Object.freeze(Object.prototype);
349
- Object.freeze(Array.prototype);
350
- Object.freeze(String.prototype);
351
- Object.freeze(Number.prototype);
352
- Object.freeze(Boolean.prototype);
353
- Object.freeze(RegExp.prototype);
354
- Object.freeze(Map.prototype);
355
- Object.freeze(Set.prototype);
356
- Object.freeze(Promise.prototype);
357
- Object.freeze(Error.prototype);
358
- Object.freeze(Object.getPrototypeOf(function(){}));
359
-
360
- ${processedLogic}
361
- if (typeof liop_main === 'function') {
362
- return liop_main(env);
363
- }
364
- return "ERR_NO_ENTRY_POINT";
365
- } catch(e) {
366
- return "LogicError: " + e.message;
367
- }
368
- })();
369
- `;
370
- try {
371
- const script = new vm.Script(scriptCode, {
372
- filename: `liop-sandbox-${this.sandboxId.slice(0, 8)}.js`
373
- });
374
- if (!process.env.VITEST && typeof Object.prototype === "object" && !Object.isFrozen(Object.prototype)) {
375
- Object.freeze(Object.prototype);
376
- Object.freeze(Array.prototype);
377
- Object.freeze(String.prototype);
378
- Object.freeze(Number.prototype);
379
- Object.freeze(Boolean.prototype);
380
- Object.freeze(RegExp.prototype);
381
- Object.freeze(Map.prototype);
382
- Object.freeze(Set.prototype);
383
- Object.freeze(Promise.prototype);
384
- Object.freeze(Error.prototype);
385
- }
386
- const context = vm.createContext(sandboxEnv, {
387
- name: "LIOP Isolate",
388
- origin: "liop://sandbox",
389
- microtaskMode: "afterEvaluate"
390
- });
391
- const output = script.runInContext(context, {
392
- timeout: 5e3,
393
- breakOnSigint: true,
394
- displayErrors: true
395
- });
396
- const logicStr = typeof compiledLogic === "string" ? compiledLogic : compiledLogic.toString("utf-8");
397
- const fuelUsed = calculateAstInstructionFuel(logicStr);
398
- if (fuelUsed > 1e6) {
399
- throw new Error(
400
- "LIOP_RESOURCE_EXHAUSTED: Execution fuel limit exceeded."
401
- );
402
- }
403
- return { output, fuelConsumed: fuelUsed };
404
- } catch (error) {
405
- throw new Error(
406
- `V8 Isolate Fault: ${error instanceof Error ? error.message : "Execution Timeout"}`
407
- );
408
- }
409
- }
410
- }
411
- /**
412
- * Physically cleans up the sandbox and releases resources.
413
- */
414
- async teardown() {
415
- try {
416
- if (this.stdoutHandle) await this.stdoutHandle.close();
417
- if (this.stderrHandle) await this.stderrHandle.close();
418
- await fs.rm(this.workingDir, { recursive: true, force: true });
419
- } catch (_e) {
420
- }
421
- }
422
- };
423
-
424
- export { ASTGuardian, WasiSandbox, calculateAstInstructionFuel, getDefaultEnvironment };
425
- //# sourceMappingURL=chunk-R52TZEIL.js.map
426
- //# sourceMappingURL=chunk-R52TZEIL.js.map