@vorionsys/cognigate 1.0.0 → 1.0.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.
- package/CHANGELOG.md +45 -0
- package/LICENSE +190 -0
- package/README.md +460 -270
- package/dist/chunk-OIPPFRDP.js +35 -0
- package/dist/index.cjs +310 -54
- package/dist/index.d.cts +84 -318
- package/dist/index.d.ts +272 -0
- package/dist/index.js +279 -47
- package/dist/proof-bridge-BBmb7kVP.d.cts +394 -0
- package/dist/proof-bridge-BBmb7kVP.d.ts +394 -0
- package/dist/proof-bridge-CkzbawwC.d.cts +394 -0
- package/dist/proof-bridge-CkzbawwC.d.ts +394 -0
- package/dist/proof-bridge.cjs +59 -0
- package/dist/proof-bridge.d.cts +3 -0
- package/dist/proof-bridge.d.ts +3 -0
- package/dist/proof-bridge.js +6 -0
- package/package.json +77 -54
- package/src/__tests__/client.test.ts +0 -162
- package/src/client.ts +0 -421
- package/src/index.ts +0 -76
- package/src/types.ts +0 -272
- package/src/webhooks.ts +0 -146
- package/tsconfig.json +0 -25
- package/vitest.config.ts +0 -14
package/dist/index.js
CHANGED
|
@@ -1,26 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createProofBridge
|
|
3
|
+
} from "./chunk-OIPPFRDP.js";
|
|
4
|
+
|
|
1
5
|
// src/types.ts
|
|
2
6
|
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
TrustTier2[TrustTier2["T6_CERTIFIED"] = 6] = "T6_CERTIFIED";
|
|
11
|
-
TrustTier2[TrustTier2["T7_AUTONOMOUS"] = 7] = "T7_AUTONOMOUS";
|
|
12
|
-
return TrustTier2;
|
|
13
|
-
})(TrustTier || {});
|
|
14
|
-
var TIER_THRESHOLDS = {
|
|
15
|
-
[0 /* T0_SANDBOX */]: { min: 0, max: 199, name: "Sandbox" },
|
|
16
|
-
[1 /* T1_OBSERVED */]: { min: 200, max: 349, name: "Observed" },
|
|
17
|
-
[2 /* T2_PROVISIONAL */]: { min: 350, max: 499, name: "Provisional" },
|
|
18
|
-
[3 /* T3_VERIFIED */]: { min: 500, max: 649, name: "Verified" },
|
|
19
|
-
[4 /* T4_OPERATIONAL */]: { min: 650, max: 799, name: "Operational" },
|
|
20
|
-
[5 /* T5_TRUSTED */]: { min: 800, max: 875, name: "Trusted" },
|
|
21
|
-
[6 /* T6_CERTIFIED */]: { min: 876, max: 949, name: "Certified" },
|
|
22
|
-
[7 /* T7_AUTONOMOUS */]: { min: 950, max: 1e3, name: "Autonomous" }
|
|
23
|
-
};
|
|
7
|
+
import {
|
|
8
|
+
TrustTier,
|
|
9
|
+
TIER_THRESHOLDS,
|
|
10
|
+
scoreToTier,
|
|
11
|
+
getTierName,
|
|
12
|
+
getTierColor
|
|
13
|
+
} from "@vorionsys/shared-constants";
|
|
24
14
|
var TrustStatusSchema = z.object({
|
|
25
15
|
entityId: z.string(),
|
|
26
16
|
trustScore: z.number().min(0).max(1e3),
|
|
@@ -169,14 +159,14 @@ var Cognigate = class {
|
|
|
169
159
|
* Get tier from trust score
|
|
170
160
|
*/
|
|
171
161
|
static getTierFromScore(score) {
|
|
172
|
-
if (score >=
|
|
173
|
-
if (score >= 876) return
|
|
174
|
-
if (score >= 800) return
|
|
175
|
-
if (score >= 650) return
|
|
176
|
-
if (score >= 500) return
|
|
177
|
-
if (score >= 350) return
|
|
178
|
-
if (score >= 200) return
|
|
179
|
-
return
|
|
162
|
+
if (score >= 951) return TrustTier.T7_AUTONOMOUS;
|
|
163
|
+
if (score >= 876) return TrustTier.T6_CERTIFIED;
|
|
164
|
+
if (score >= 800) return TrustTier.T5_TRUSTED;
|
|
165
|
+
if (score >= 650) return TrustTier.T4_STANDARD;
|
|
166
|
+
if (score >= 500) return TrustTier.T3_MONITORED;
|
|
167
|
+
if (score >= 350) return TrustTier.T2_PROVISIONAL;
|
|
168
|
+
if (score >= 200) return TrustTier.T1_OBSERVED;
|
|
169
|
+
return TrustTier.T0_SANDBOX;
|
|
180
170
|
}
|
|
181
171
|
/**
|
|
182
172
|
* Get tier name
|
|
@@ -381,21 +371,18 @@ async function verifyWebhookSignature(payload, signature, secret) {
|
|
|
381
371
|
const expectedSignature = bufferToHex(signatureBuffer);
|
|
382
372
|
return timingSafeEqual(signature, expectedSignature);
|
|
383
373
|
}
|
|
384
|
-
function parseWebhookPayload(body, signature, secret) {
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
reject(new Error("Invalid webhook payload"));
|
|
397
|
-
}
|
|
398
|
-
});
|
|
374
|
+
async function parseWebhookPayload(body, signature, secret) {
|
|
375
|
+
const isValid = await verifyWebhookSignature(body, signature, secret);
|
|
376
|
+
if (!isValid) {
|
|
377
|
+
throw new Error("Invalid webhook signature");
|
|
378
|
+
}
|
|
379
|
+
try {
|
|
380
|
+
const event = JSON.parse(body);
|
|
381
|
+
event.timestamp = new Date(event.timestamp);
|
|
382
|
+
return event;
|
|
383
|
+
} catch {
|
|
384
|
+
throw new Error("Invalid webhook payload");
|
|
385
|
+
}
|
|
399
386
|
}
|
|
400
387
|
var WebhookRouter = class {
|
|
401
388
|
handlers = /* @__PURE__ */ new Map();
|
|
@@ -432,15 +419,15 @@ var WebhookRouter = class {
|
|
|
432
419
|
* Create an Express/Connect compatible middleware
|
|
433
420
|
*/
|
|
434
421
|
middleware(secret) {
|
|
435
|
-
return async (req, res
|
|
422
|
+
return async (req, res) => {
|
|
436
423
|
try {
|
|
437
424
|
const signature = req.headers["x-cognigate-signature"];
|
|
438
425
|
const body = typeof req.body === "string" ? req.body : JSON.stringify(req.body);
|
|
439
426
|
const event = await parseWebhookPayload(body, signature, secret);
|
|
440
427
|
await this.handle(event);
|
|
441
428
|
res.status(200).json({ received: true });
|
|
442
|
-
} catch (
|
|
443
|
-
res.status(400).json({ error:
|
|
429
|
+
} catch (err) {
|
|
430
|
+
res.status(400).json({ error: err.message });
|
|
444
431
|
}
|
|
445
432
|
};
|
|
446
433
|
}
|
|
@@ -459,6 +446,249 @@ function timingSafeEqual(a, b) {
|
|
|
459
446
|
return result === 0;
|
|
460
447
|
}
|
|
461
448
|
|
|
449
|
+
// src/sandbox/worker-sandbox.ts
|
|
450
|
+
import { Worker } from "worker_threads";
|
|
451
|
+
var DEFAULT_TIMEOUT2 = 3e4;
|
|
452
|
+
var DEFAULT_MEMORY_LIMIT_MB = 64;
|
|
453
|
+
var WORKER_SCRIPT = `
|
|
454
|
+
const { parentPort, workerData } = require('node:worker_threads');
|
|
455
|
+
const vm = require('node:vm');
|
|
456
|
+
|
|
457
|
+
const { code, context } = workerData;
|
|
458
|
+
const startTime = performance.now();
|
|
459
|
+
const memBefore = process.memoryUsage().heapUsed;
|
|
460
|
+
|
|
461
|
+
// Build restricted globals for the vm context
|
|
462
|
+
const safeGlobals = {
|
|
463
|
+
console: {
|
|
464
|
+
log: function() {},
|
|
465
|
+
warn: function() {},
|
|
466
|
+
error: function() {},
|
|
467
|
+
info: function() {},
|
|
468
|
+
},
|
|
469
|
+
JSON: JSON,
|
|
470
|
+
Math: Math,
|
|
471
|
+
Date: Date,
|
|
472
|
+
Array: Array,
|
|
473
|
+
Object: Object,
|
|
474
|
+
String: String,
|
|
475
|
+
Number: Number,
|
|
476
|
+
Boolean: Boolean,
|
|
477
|
+
Map: Map,
|
|
478
|
+
Set: Set,
|
|
479
|
+
WeakMap: WeakMap,
|
|
480
|
+
WeakSet: WeakSet,
|
|
481
|
+
Promise: Promise,
|
|
482
|
+
Symbol: Symbol,
|
|
483
|
+
RegExp: RegExp,
|
|
484
|
+
Error: Error,
|
|
485
|
+
TypeError: TypeError,
|
|
486
|
+
RangeError: RangeError,
|
|
487
|
+
SyntaxError: SyntaxError,
|
|
488
|
+
URIError: URIError,
|
|
489
|
+
parseInt: parseInt,
|
|
490
|
+
parseFloat: parseFloat,
|
|
491
|
+
isNaN: isNaN,
|
|
492
|
+
isFinite: isFinite,
|
|
493
|
+
encodeURIComponent: encodeURIComponent,
|
|
494
|
+
decodeURIComponent: decodeURIComponent,
|
|
495
|
+
encodeURI: encodeURI,
|
|
496
|
+
decodeURI: decodeURI,
|
|
497
|
+
undefined: undefined,
|
|
498
|
+
NaN: NaN,
|
|
499
|
+
Infinity: Infinity,
|
|
500
|
+
setTimeout: function(fn, ms) { return setTimeout(fn, Math.min(ms, 5000)); },
|
|
501
|
+
clearTimeout: clearTimeout,
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const vmContext = vm.createContext(safeGlobals);
|
|
505
|
+
|
|
506
|
+
// Wrap code in an async IIFE to support await and return
|
|
507
|
+
const wrappedCode = '(async () => { ' + code + ' })()';
|
|
508
|
+
|
|
509
|
+
async function run() {
|
|
510
|
+
try {
|
|
511
|
+
const script = new vm.Script(wrappedCode, {
|
|
512
|
+
filename: 'sandbox-' + context.agentId + '.js',
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
const result = await script.runInContext(vmContext, {
|
|
516
|
+
timeout: context.timeout,
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
const durationMs = performance.now() - startTime;
|
|
520
|
+
const memAfter = process.memoryUsage().heapUsed;
|
|
521
|
+
|
|
522
|
+
parentPort.postMessage({
|
|
523
|
+
type: 'result',
|
|
524
|
+
output: result,
|
|
525
|
+
durationMs: durationMs,
|
|
526
|
+
memoryUsedBytes: Math.max(0, memAfter - memBefore),
|
|
527
|
+
});
|
|
528
|
+
} catch (err) {
|
|
529
|
+
const durationMs = performance.now() - startTime;
|
|
530
|
+
const memAfter = process.memoryUsage().heapUsed;
|
|
531
|
+
|
|
532
|
+
parentPort.postMessage({
|
|
533
|
+
type: 'error',
|
|
534
|
+
error: err && err.message ? err.message : String(err),
|
|
535
|
+
durationMs: durationMs,
|
|
536
|
+
memoryUsedBytes: Math.max(0, memAfter - memBefore),
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
run();
|
|
542
|
+
`;
|
|
543
|
+
var WorkerSandbox = class {
|
|
544
|
+
worker = null;
|
|
545
|
+
isShutdown = false;
|
|
546
|
+
/**
|
|
547
|
+
* Execute code in an isolated worker thread.
|
|
548
|
+
*
|
|
549
|
+
* Spawns a new worker for each execution to ensure full isolation.
|
|
550
|
+
* The worker has memory limits enforced by V8 via `resourceLimits`,
|
|
551
|
+
* a vm-level timeout for synchronous code, and a main-thread timeout
|
|
552
|
+
* as a fallback for async code or hangs.
|
|
553
|
+
*
|
|
554
|
+
* @param code - JavaScript code string to execute inside the sandbox.
|
|
555
|
+
* The code is wrapped in an async IIFE, so `return` and `await` are valid.
|
|
556
|
+
* @param context - Execution context with identity and resource constraints
|
|
557
|
+
* @returns Promise resolving to a SandboxResult
|
|
558
|
+
*/
|
|
559
|
+
async execute(code, context) {
|
|
560
|
+
if (this.isShutdown) {
|
|
561
|
+
return {
|
|
562
|
+
success: false,
|
|
563
|
+
output: void 0,
|
|
564
|
+
error: "Sandbox has been shut down",
|
|
565
|
+
durationMs: 0,
|
|
566
|
+
memoryUsedBytes: 0
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
const timeout = context.timeout || DEFAULT_TIMEOUT2;
|
|
570
|
+
const memoryLimitMb = context.memoryLimitMb || DEFAULT_MEMORY_LIMIT_MB;
|
|
571
|
+
const startTime = performance.now();
|
|
572
|
+
return new Promise((resolve) => {
|
|
573
|
+
let settled = false;
|
|
574
|
+
let timeoutId = null;
|
|
575
|
+
const settle = (result) => {
|
|
576
|
+
if (settled) return;
|
|
577
|
+
settled = true;
|
|
578
|
+
if (timeoutId) {
|
|
579
|
+
clearTimeout(timeoutId);
|
|
580
|
+
timeoutId = null;
|
|
581
|
+
}
|
|
582
|
+
resolve(result);
|
|
583
|
+
};
|
|
584
|
+
try {
|
|
585
|
+
const worker = new Worker(WORKER_SCRIPT, {
|
|
586
|
+
eval: true,
|
|
587
|
+
workerData: {
|
|
588
|
+
code,
|
|
589
|
+
context: {
|
|
590
|
+
tenantId: context.tenantId,
|
|
591
|
+
agentId: context.agentId,
|
|
592
|
+
trustLevel: context.trustLevel ?? 0,
|
|
593
|
+
allowedModules: context.allowedModules ?? [],
|
|
594
|
+
timeout,
|
|
595
|
+
memoryLimitMb
|
|
596
|
+
}
|
|
597
|
+
},
|
|
598
|
+
resourceLimits: {
|
|
599
|
+
maxOldGenerationSizeMb: memoryLimitMb,
|
|
600
|
+
maxYoungGenerationSizeMb: Math.max(1, Math.floor(memoryLimitMb / 4)),
|
|
601
|
+
codeRangeSizeMb: Math.max(1, Math.floor(memoryLimitMb / 8))
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
this.worker = worker;
|
|
605
|
+
timeoutId = setTimeout(() => {
|
|
606
|
+
const durationMs = performance.now() - startTime;
|
|
607
|
+
worker.terminate().catch(() => {
|
|
608
|
+
});
|
|
609
|
+
settle({
|
|
610
|
+
success: false,
|
|
611
|
+
output: void 0,
|
|
612
|
+
error: `Execution timed out after ${timeout}ms`,
|
|
613
|
+
durationMs,
|
|
614
|
+
memoryUsedBytes: 0
|
|
615
|
+
});
|
|
616
|
+
}, timeout + 500);
|
|
617
|
+
worker.on("message", (message) => {
|
|
618
|
+
if (message.type === "result") {
|
|
619
|
+
settle({
|
|
620
|
+
success: true,
|
|
621
|
+
output: message.output,
|
|
622
|
+
durationMs: message.durationMs,
|
|
623
|
+
memoryUsedBytes: message.memoryUsedBytes
|
|
624
|
+
});
|
|
625
|
+
} else if (message.type === "error") {
|
|
626
|
+
settle({
|
|
627
|
+
success: false,
|
|
628
|
+
output: void 0,
|
|
629
|
+
error: message.error,
|
|
630
|
+
durationMs: message.durationMs,
|
|
631
|
+
memoryUsedBytes: message.memoryUsedBytes
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
worker.terminate().catch(() => {
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
worker.on("error", (error) => {
|
|
638
|
+
const durationMs = performance.now() - startTime;
|
|
639
|
+
settle({
|
|
640
|
+
success: false,
|
|
641
|
+
output: void 0,
|
|
642
|
+
error: error.message || "Worker error",
|
|
643
|
+
durationMs,
|
|
644
|
+
memoryUsedBytes: 0
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
worker.on("exit", (exitCode) => {
|
|
648
|
+
if (exitCode !== 0) {
|
|
649
|
+
const durationMs = performance.now() - startTime;
|
|
650
|
+
settle({
|
|
651
|
+
success: false,
|
|
652
|
+
output: void 0,
|
|
653
|
+
error: `Worker exited with code ${exitCode} (possible memory limit exceeded)`,
|
|
654
|
+
durationMs,
|
|
655
|
+
memoryUsedBytes: 0
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
this.worker = null;
|
|
659
|
+
});
|
|
660
|
+
} catch (err) {
|
|
661
|
+
const durationMs = performance.now() - startTime;
|
|
662
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
663
|
+
settle({
|
|
664
|
+
success: false,
|
|
665
|
+
output: void 0,
|
|
666
|
+
error: `Failed to spawn worker: ${errorMessage}`,
|
|
667
|
+
durationMs,
|
|
668
|
+
memoryUsedBytes: 0
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Gracefully shut down the sandbox.
|
|
675
|
+
* Terminates any running worker and prevents future executions.
|
|
676
|
+
*/
|
|
677
|
+
async shutdown() {
|
|
678
|
+
this.isShutdown = true;
|
|
679
|
+
if (this.worker) {
|
|
680
|
+
await this.worker.terminate();
|
|
681
|
+
this.worker = null;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Check whether the sandbox has been shut down.
|
|
686
|
+
*/
|
|
687
|
+
get terminated() {
|
|
688
|
+
return this.isShutdown;
|
|
689
|
+
}
|
|
690
|
+
};
|
|
691
|
+
|
|
462
692
|
// src/index.ts
|
|
463
693
|
import { z as z2 } from "zod";
|
|
464
694
|
export {
|
|
@@ -471,6 +701,8 @@ export {
|
|
|
471
701
|
TrustStatusSchema,
|
|
472
702
|
TrustTier,
|
|
473
703
|
WebhookRouter,
|
|
704
|
+
WorkerSandbox,
|
|
705
|
+
createProofBridge,
|
|
474
706
|
parseWebhookPayload,
|
|
475
707
|
verifyWebhookSignature,
|
|
476
708
|
z2 as z
|