@enclave-vm/core 2.17.0 → 2.19.0
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/README.md +265 -38
- package/SECURITY-AUDIT.md +85 -77
- package/adapters/interpreter-adapter.d.ts +27 -0
- package/adapters/worker-pool/config.d.ts +4 -2
- package/adapters/worker-pool/memory-monitor.d.ts +16 -2
- package/adapters/worker-pool/protocol.d.ts +63 -11
- package/adapters/worker-pool/worker-pool-adapter.d.ts +45 -2
- package/adapters/worker-pool/worker-script.d.ts +14 -4
- package/adapters/worker-pool/worker-slot.d.ts +99 -2
- package/console-handler.d.ts +33 -0
- package/deadline-guard.d.ts +65 -0
- package/double-vm/double-vm-wrapper.d.ts +11 -2
- package/double-vm/parent-vm-bootstrap.d.ts +118 -0
- package/enclave.d.ts +31 -0
- package/esm/index.mjs +12581 -8226
- package/esm/worker.mjs +2935 -212
- package/execution-deadline.d.ts +69 -0
- package/global-function-gate.d.ts +22 -1
- package/globals-validator.d.ts +12 -0
- package/index.d.ts +2 -1
- package/index.js +12604 -8250
- package/interpreter/interpreter.d.ts +149 -5
- package/interpreter/safe-globals.d.ts +14 -0
- package/package.json +4 -4
- package/realm-airlock.d.ts +51 -0
- package/safe-error.d.ts +6 -0
- package/safe-runtime.d.ts +16 -0
- package/sandbox-console.d.ts +26 -0
- package/sandbox-rejections.d.ts +67 -0
- package/sandbox-script.d.ts +156 -0
- package/secure-proxy.d.ts +21 -0
- package/src/adapters/worker-pool/package.json +4 -4
- package/src/adapters/worker-pool/worker-script.js +8738 -625
- package/stack-reserve.d.ts +46 -0
- package/tool-bridge.d.ts +34 -2
- package/types.d.ts +105 -5
- package/worker.js +2935 -212
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stack Reserve (host side)
|
|
3
|
+
*
|
|
4
|
+
* A script can recurse until the stack is nearly full and call into the host from there: a
|
|
5
|
+
* function passed in `globals`, the console. Host code that starts with almost no stack left
|
|
6
|
+
* does not fail cleanly. It fails wherever the stack happens to end, and that may be in the
|
|
7
|
+
* middle of Node.js's own code: a write to `process.stdout` that is cut off after the stream has
|
|
8
|
+
* marked itself as writing leaves the stream waiting forever, and nothing the host writes to it
|
|
9
|
+
* afterwards comes out. A host function that is cut off after it has acted, while its result is
|
|
10
|
+
* on the way back, has acted although the script is told that the call failed.
|
|
11
|
+
*
|
|
12
|
+
* "Almost no stack" is more than it sounds. V8 refuses to compile a function for its first call
|
|
13
|
+
* when less than about 40 KB of stack is left (measured: 39.9 KB with Node.js 24, 46.4 KB with
|
|
14
|
+
* Node.js 22). In that zone every function that has not run before throws a RangeError at its
|
|
15
|
+
* call site, however little stack it would need itself.
|
|
16
|
+
*
|
|
17
|
+
* So code that the application supplied is not started with less than
|
|
18
|
+
* {@link HOST_STACK_RESERVE_BYTES} left. The caller refuses the call instead, and the script gets
|
|
19
|
+
* the RangeError it would have got a few frames later anyway. (The membrane keeps a reserve of
|
|
20
|
+
* its own for the runtime's code, STACK_RESERVE_ARGUMENTS in ./double-vm/parent-vm-bootstrap;
|
|
21
|
+
* this one is measured where the application's code begins, whatever lies in between.)
|
|
22
|
+
*
|
|
23
|
+
* What it does not cover, and why that is enough: a tool handler needs none (the bridge starts it
|
|
24
|
+
* from a microtask, on an empty stack), and reading a property is not checked, but the validation
|
|
25
|
+
* of `globals` refuses getters and proxies, so no code of the application runs on a read.
|
|
26
|
+
*
|
|
27
|
+
* @packageDocumentation
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* How much stack must be left before code supplied by the application is started, in bytes: the
|
|
31
|
+
* zone in which the engine refuses to compile, and as much again for the code to run in.
|
|
32
|
+
*/
|
|
33
|
+
export declare const HOST_STACK_RESERVE_BYTES: number;
|
|
34
|
+
/** What a script is told when a call is refused for lack of stack: the engine's own wording */
|
|
35
|
+
export declare const STACK_EXHAUSTED_MESSAGE = "Maximum call stack size exceeded";
|
|
36
|
+
/**
|
|
37
|
+
* Whether at least {@link HOST_STACK_RESERVE_BYTES} of stack are left below the caller.
|
|
38
|
+
*
|
|
39
|
+
* The engine has to push every argument of a call before it makes the call, and checks first
|
|
40
|
+
* that they fit: applying a function with that many arguments fails exactly when they do not.
|
|
41
|
+
* JavaScript has no cheaper way to ask; this one takes about 5 microseconds.
|
|
42
|
+
*
|
|
43
|
+
* It can itself throw a RangeError when called with no stack left at all. Callers treat that as
|
|
44
|
+
* "no".
|
|
45
|
+
*/
|
|
46
|
+
export declare function hasHostStackReserve(): boolean;
|
package/tool-bridge.d.ts
CHANGED
|
@@ -1,4 +1,36 @@
|
|
|
1
1
|
import type { ExecutionContext } from './types';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Encode a tool call as a tool-bridge request.
|
|
4
|
+
*/
|
|
5
|
+
export declare function encodeToolBridgeRequest(tool: string, args: Record<string, unknown>): string;
|
|
6
|
+
/**
|
|
7
|
+
* Decode a tool-bridge request into the tool name and arguments it carries.
|
|
8
|
+
*
|
|
9
|
+
* @returns The tool call, or the reason the request is malformed
|
|
10
|
+
*/
|
|
11
|
+
export declare function decodeToolBridgeRequest(requestJson: string): {
|
|
12
|
+
ok: true;
|
|
13
|
+
tool: string;
|
|
14
|
+
args: Record<string, unknown>;
|
|
15
|
+
} | {
|
|
16
|
+
ok: false;
|
|
17
|
+
error: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Encode a failure as a tool-bridge response (a refusal by the enclave, not a failure of the tool).
|
|
21
|
+
*/
|
|
22
|
+
export declare function encodeToolBridgeFailure(name: string, message: string, code: string, maxPayloadBytes: number): string;
|
|
23
|
+
/** Error message for an exceeded `maxToolCalls` (shared by every place that enforces it). */
|
|
24
|
+
export declare function toolCallLimitMessage(maxToolCalls: number): string;
|
|
25
|
+
export interface HostToolBridgeOptions {
|
|
26
|
+
/** Count the call in `stats.toolCallCount` here (false when the caller counted it already) */
|
|
3
27
|
updateStats: boolean;
|
|
4
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The caller counted the call AND checked it against `maxToolCalls` where the script made it,
|
|
30
|
+
* and only passes on calls that were within the limit. The bridge then does not compare the
|
|
31
|
+
* running count again: by the time it runs, calls made later in the same tick have been
|
|
32
|
+
* counted too, and it would refuse a call that was within the limit when it was made.
|
|
33
|
+
*/
|
|
34
|
+
limitChecked?: boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare function createHostToolBridge(executionContext: ExecutionContext, options: HostToolBridgeOptions): (requestJson: string) => Promise<string>;
|
package/types.d.ts
CHANGED
|
@@ -284,8 +284,9 @@ export interface EnclaveConfig {
|
|
|
284
284
|
/**
|
|
285
285
|
* Maximum execution time in milliseconds. A tool call in progress when it passes is not cut
|
|
286
286
|
* off and its result is delivered; the script's next tool call then fails with "Execution
|
|
287
|
-
* aborted".
|
|
288
|
-
*
|
|
287
|
+
* aborted". How long it may still take is `toolCallGraceMs`; after that, and at once for a
|
|
288
|
+
* script that is waiting for anything else (a thenable that never calls back, a promise nobody
|
|
289
|
+
* settles), the execution ends with the timeout error.
|
|
289
290
|
* Default: 30000 (30 seconds)
|
|
290
291
|
*/
|
|
291
292
|
timeout?: number;
|
|
@@ -312,9 +313,18 @@ export interface EnclaveConfig {
|
|
|
312
313
|
*/
|
|
313
314
|
maxIterations?: number;
|
|
314
315
|
/**
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
316
|
+
* Where the sandbox runs.
|
|
317
|
+
*
|
|
318
|
+
* - `'vm'` (default): in this process. The script runs in the double VM (or in a single VM
|
|
319
|
+
* with `doubleVm: { enabled: false }`).
|
|
320
|
+
* - `'worker_threads'`: in a pool of worker threads. Inside the worker the script runs in the
|
|
321
|
+
* same sandbox as with `'vm'` (the double VM by default); the worker adds a heap limit that
|
|
322
|
+
* the engine enforces (`workerPoolConfig.memoryLimitPerWorker`) and a hard stop: a script
|
|
323
|
+
* that does not end is terminated with its worker. Custom globals reach a worker as JSON
|
|
324
|
+
* data, so functions in `globals` are not available there.
|
|
325
|
+
* - `'isolated-vm'`: reserved, not implemented.
|
|
326
|
+
*
|
|
327
|
+
* To run scripts with an adapter of your own, pass `sandboxAdapter`.
|
|
318
328
|
* Default: 'vm'
|
|
319
329
|
*/
|
|
320
330
|
adapter?: 'vm' | 'isolated-vm' | 'worker_threads';
|
|
@@ -461,6 +471,8 @@ import type { ScoringGateConfig, ScoringGateResult } from './scoring/types';
|
|
|
461
471
|
import type { WorkerPoolConfig } from './adapters/worker-pool/config';
|
|
462
472
|
import type { DoubleVmConfig, PartialDoubleVmConfig, ParentValidationConfig } from './double-vm/types';
|
|
463
473
|
import type { NormalizedToolNamespace, ToolNamespaces } from '@enclave-vm/ast';
|
|
474
|
+
import type { ConsoleHandler, ConsoleLevel } from './console-handler';
|
|
475
|
+
import type { HostActivity } from './execution-deadline';
|
|
464
476
|
/**
|
|
465
477
|
* Internal execution context (tracks state during execution)
|
|
466
478
|
*/
|
|
@@ -483,6 +495,12 @@ export interface ExecutionContext {
|
|
|
483
495
|
* Validated tool namespaces to bind in the sandbox (see `CreateEnclaveOptions.toolNamespaces`)
|
|
484
496
|
*/
|
|
485
497
|
toolNamespaces?: readonly NormalizedToolNamespace[];
|
|
498
|
+
/**
|
|
499
|
+
* The global names that may exist in the script's realm (see `CreateEnclaveOptions.realmGlobals`).
|
|
500
|
+
* When set, the adapter removes every other standard global from the realm before the script
|
|
501
|
+
* runs. Absent means the security level's list of dangerous globals is removed instead.
|
|
502
|
+
*/
|
|
503
|
+
realmGlobalAllowlist?: readonly string[];
|
|
486
504
|
/**
|
|
487
505
|
* Statistics tracker
|
|
488
506
|
*/
|
|
@@ -499,6 +517,23 @@ export interface ExecutionContext {
|
|
|
499
517
|
* Tool handler
|
|
500
518
|
*/
|
|
501
519
|
toolHandler?: ToolHandler;
|
|
520
|
+
/**
|
|
521
|
+
* What the host has in flight for this execution: tool calls and async functions from
|
|
522
|
+
* `globals` (see ./execution-deadline). Set by the enclave, which ends the execution once the
|
|
523
|
+
* deadline has passed and nothing is in flight; the gate for host functions reports to it.
|
|
524
|
+
*/
|
|
525
|
+
hostActivity?: HostActivity;
|
|
526
|
+
/**
|
|
527
|
+
* Set when the host of this execution is on another thread (the worker pool sets it inside a
|
|
528
|
+
* worker): the function that carries a tool request, in the string tool-bridge format, to the
|
|
529
|
+
* host thread and resolves with its response. An adapter then relays tool calls through it
|
|
530
|
+
* instead of calling `toolHandler`, which does not exist on this side.
|
|
531
|
+
*/
|
|
532
|
+
hostToolBridge?: (requestJson: string) => Promise<string>;
|
|
533
|
+
/**
|
|
534
|
+
* Receives the script's console calls (see `CreateEnclaveOptions.consoleHandler`)
|
|
535
|
+
*/
|
|
536
|
+
consoleHandler?: ConsoleHandler;
|
|
502
537
|
/**
|
|
503
538
|
* Reference sidecar for pass-by-reference support (per-execution)
|
|
504
539
|
* Created and disposed by the Enclave for each run
|
|
@@ -642,6 +677,25 @@ export interface CreateEnclaveOptions extends EnclaveConfig {
|
|
|
642
677
|
* Default: true
|
|
643
678
|
*/
|
|
644
679
|
transform?: boolean;
|
|
680
|
+
/**
|
|
681
|
+
* Which standard globals exist in the realm the script runs in.
|
|
682
|
+
*
|
|
683
|
+
* - `'allowlist'`: only the names a validated script may use exist: the AgentScript globals of
|
|
684
|
+
* the security level (`Math`, `JSON`, `Array`, `Object`, `String`, `Number`, `Date`, ...), the
|
|
685
|
+
* enclave's own runtime helpers, your `globals` and your `toolNamespaces`. Every other
|
|
686
|
+
* standard global (`Function`, `eval`, `globalThis`, `Proxy`, `Reflect`, `Symbol`, `Promise`,
|
|
687
|
+
* `RegExp`, `WebAssembly`, ...) is removed from the realm before the script runs, and regular
|
|
688
|
+
* expressions cannot be executed there. The validator already refuses those names; this makes
|
|
689
|
+
* the realm agree with it, so a gap in the static rules does not hand the script a global.
|
|
690
|
+
* - `'security-level'`: only the dangerous globals listed for the security level are removed.
|
|
691
|
+
*
|
|
692
|
+
* Applies to the double VM (the default), also inside a `worker_threads` worker.
|
|
693
|
+
*
|
|
694
|
+
* Default: `'allowlist'` with the `agentscript` or `babel` preset while `validate` is on,
|
|
695
|
+
* `'security-level'` otherwise (other presets, or `validate: false`, let a script name
|
|
696
|
+
* standard globals that the allowlist would remove).
|
|
697
|
+
*/
|
|
698
|
+
realmGlobals?: 'allowlist' | 'security-level';
|
|
645
699
|
/**
|
|
646
700
|
* Whether to allow functions in custom globals
|
|
647
701
|
* Default: Determined by securityLevel (false for STRICT/SECURE/STANDARD)
|
|
@@ -666,6 +720,20 @@ export interface CreateEnclaveOptions extends EnclaveConfig {
|
|
|
666
720
|
* Only enable if you understand the security implications.
|
|
667
721
|
*/
|
|
668
722
|
allowFunctionsInGlobals?: boolean;
|
|
723
|
+
/**
|
|
724
|
+
* How long, after `timeout` has passed, a tool call (or an async function from `globals`) that
|
|
725
|
+
* was in progress at that moment may still take to deliver its result, in milliseconds. When
|
|
726
|
+
* it runs out the execution ends with the timeout error, whatever is still in progress; the
|
|
727
|
+
* handler's own promise is left to settle and its result is dropped.
|
|
728
|
+
*
|
|
729
|
+
* 0 ends every execution at the deadline. `Infinity` waits for the handler however long it
|
|
730
|
+
* takes, which was the behaviour up to 2.18: a handler that never settles then keeps `run()`
|
|
731
|
+
* from settling. Not used by the `worker_threads` adapter, which ends an execution at the
|
|
732
|
+
* deadline.
|
|
733
|
+
*
|
|
734
|
+
* Default: the value of `timeout`, so `run()` takes at most twice `timeout`.
|
|
735
|
+
*/
|
|
736
|
+
toolCallGraceMs?: number;
|
|
669
737
|
/**
|
|
670
738
|
* Maximum number of calls the script may make into functions passed in `globals`
|
|
671
739
|
* (including functions nested in objects), per execution.
|
|
@@ -703,6 +771,32 @@ export interface CreateEnclaveOptions extends EnclaveConfig {
|
|
|
703
771
|
* ```
|
|
704
772
|
*/
|
|
705
773
|
toolNamespaces?: ToolNamespaces;
|
|
774
|
+
/**
|
|
775
|
+
* Receives the script's `console.log/info/warn/error/debug` calls instead of the host console.
|
|
776
|
+
*
|
|
777
|
+
* With a handler, scripts may use `console` at every security level. Each call is formatted as
|
|
778
|
+
* the built-in console formats it (arguments joined by spaces, objects JSON-serialized) and
|
|
779
|
+
* handed over as `(level, message)` strings, once `maxConsoleCalls` and `maxConsoleOutputBytes`
|
|
780
|
+
* have allowed it; past a limit the script's call throws and the handler is not called. The
|
|
781
|
+
* handler's return value is ignored and anything it throws or rejects with is swallowed. The
|
|
782
|
+
* script's `console` is a frozen, null-prototype object built inside the sandbox, so it leads
|
|
783
|
+
* to neither the handler nor any host object.
|
|
784
|
+
*
|
|
785
|
+
* Without a handler nothing changes: `console` is allowed only at PERMISSIVE and writes to the
|
|
786
|
+
* host process console.
|
|
787
|
+
*
|
|
788
|
+
* Supported by the double VM (default), the `vm` adapter and the `worker_threads` adapter. A
|
|
789
|
+
* `sandboxAdapter` receives the handler as `ExecutionContext.consoleHandler`; the built-in
|
|
790
|
+
* `InterpreterAdapter` has no `console`, so a script that uses it fails there.
|
|
791
|
+
*
|
|
792
|
+
* @example
|
|
793
|
+
* ```typescript
|
|
794
|
+
* const enclave = new Enclave({
|
|
795
|
+
* consoleHandler: (level, message) => logger[level](message),
|
|
796
|
+
* });
|
|
797
|
+
* ```
|
|
798
|
+
*/
|
|
799
|
+
consoleHandler?: ConsoleHandler;
|
|
706
800
|
/**
|
|
707
801
|
* Whether to sanitize stack traces in error messages
|
|
708
802
|
* Default: Determined by securityLevel (true for STRICT/SECURE)
|
|
@@ -904,6 +998,12 @@ export type { DoubleVmConfig, PartialDoubleVmConfig, ParentValidationConfig };
|
|
|
904
998
|
* @see {@link NormalizedToolNamespace} A validated namespace
|
|
905
999
|
*/
|
|
906
1000
|
export type { ToolNamespaces, NormalizedToolNamespace };
|
|
1001
|
+
/**
|
|
1002
|
+
* Re-exported console handler types
|
|
1003
|
+
* @see {@link ConsoleHandler} The `consoleHandler` option
|
|
1004
|
+
* @see {@link ConsoleLevel} A console method a script can call
|
|
1005
|
+
*/
|
|
1006
|
+
export type { ConsoleHandler, ConsoleLevel };
|
|
907
1007
|
/**
|
|
908
1008
|
* Default double VM configuration
|
|
909
1009
|
*
|