@enclave-vm/core 2.18.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.
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Execution Deadline: a script that only waits
3
+ *
4
+ * `timeout` stops a script that keeps the thread: `vm`'s own watchdog covers its synchronous
5
+ * start, the cooperative check (./deadline-guard) everything it runs after its first `await`.
6
+ * Neither stops a script that is simply WAITING. Nothing runs, so nothing looks at the clock:
7
+ *
8
+ * ```js
9
+ * return { then: () => 0 }; // a thenable that never calls back
10
+ * ```
11
+ *
12
+ * was enough to keep `run()` from ever settling, whatever `timeout` said. The adapter awaited the
13
+ * script's promise, and the enclave's timer only set a flag for the script's next operation,
14
+ * which never came. A tool call whose handler never settles did the same.
15
+ *
16
+ * So the host keeps count of what it has in flight for an execution (tool calls, async
17
+ * functions passed in `globals`), and from the deadline on the execution ends, with the timeout
18
+ * error, in one of two ways:
19
+ *
20
+ * - Nothing is in flight. Everything else a script can wait for is its own, and a sandbox has no
21
+ * timers and no I/O, so nothing can ever wake it. It ends once the script has had a moment
22
+ * ({@link IDLE_GRACE_MS}) to finish with what was delivered last.
23
+ * - Something is in flight. Cutting it off would mean that a tool ran and the script was told it
24
+ * timed out, so its result is still delivered, but not for ever: `toolCallGraceMs` after the
25
+ * deadline the execution ends whatever is still in flight. The default is the length of the
26
+ * timeout itself, so `run()` takes at most twice `timeout`.
27
+ *
28
+ * The worker pool is left out: seen from the host's thread, a script that waits in a worker and
29
+ * one that keeps its worker busy look the same, and the pool ends both itself.
30
+ *
31
+ * @packageDocumentation
32
+ */
33
+ /**
34
+ * How long a script gets, after the deadline, to finish with the result of the last host call
35
+ * that was in flight. Within one thread that takes no time at all (promise jobs); the margin is
36
+ * for an adapter of the application's that runs the script somewhere else.
37
+ */
38
+ export declare const IDLE_GRACE_MS = 250;
39
+ /** What the host has in flight for one execution, and when that makes the execution over */
40
+ export interface HostActivity {
41
+ /**
42
+ * A host operation the script can wait for has started.
43
+ *
44
+ * @returns To be called once it has settled, either way (calling it again does nothing)
45
+ */
46
+ begin(): () => void;
47
+ /** The deadline has passed: from now on the execution is over as described in the module */
48
+ deadlinePassed(): void;
49
+ /** Resolves, and never rejects, when the execution is over although its script has not settled */
50
+ readonly over: Promise<void>;
51
+ /** How many host operations are in flight right now */
52
+ readonly inFlight: number;
53
+ /** The execution has settled by itself: stop watching */
54
+ close(): void;
55
+ }
56
+ /**
57
+ * Create the tracker of one execution.
58
+ *
59
+ * @param inFlightGraceMs How long after the deadline a host call that is still in flight may
60
+ * take to deliver. 0 ends the execution at the deadline; Infinity waits for it however long.
61
+ */
62
+ export declare function createHostActivity(inFlightGraceMs?: number): HostActivity;
63
+ /**
64
+ * A tool handler that tells the tracker when a call starts and when it has settled.
65
+ *
66
+ * @param handler The host's tool handler
67
+ * @param activity The tracker of the execution the handler is used for
68
+ */
69
+ export declare function trackToolCalls<A extends unknown[], R>(handler: (...args: A) => Promise<R> | R, activity: HostActivity): (...args: A) => Promise<R>;
@@ -23,11 +23,27 @@
23
23
  * @packageDocumentation
24
24
  */
25
25
  import type { ExecutionContext } from './types';
26
+ /**
27
+ * Whether `prototype` is one of the built-in prototypes whose methods are not gated. The
28
+ * validation of `globals` stops there when it walks the classes of a value: what lies beyond is
29
+ * the language's own.
30
+ */
31
+ export declare function isBuiltinPrototype(prototype: unknown): boolean;
26
32
  /**
27
33
  * Whether `fn` is a method of a built-in prototype (see the module description). Such calls
28
34
  * operate on data and are not gated; every other host function is.
29
35
  */
30
36
  export declare function isBuiltinMethod(fn: unknown): boolean;
37
+ /**
38
+ * Whether `fn` is a built-in method that changes its receiver (see {@link MUTATING_BUILTIN_METHODS}).
39
+ */
40
+ export declare function isMutatingBuiltinMethod(fn: unknown): boolean;
41
+ /**
42
+ * Whether `fn` is a built-in method that runs a regular expression (see {@link REGEXP_MATCH_METHODS}).
43
+ */
44
+ export declare function isRegExpMatchMethod(fn: unknown): boolean;
45
+ /** What a script is told when it runs a regular expression of the host */
46
+ export declare const HOST_REGEXP_MESSAGE: string;
31
47
  /** Default `maxGlobalFunctionCalls` is this many times `maxToolCalls`. */
32
48
  export declare const DEFAULT_GLOBAL_FUNCTION_CALLS_PER_TOOL_CALL = 10;
33
49
  /**
@@ -39,6 +55,11 @@ export declare function resolveMaxGlobalFunctionCalls(config: {
39
55
  }): number;
40
56
  /** Error message for an exceeded `maxGlobalFunctionCalls` (shared with the double VM). */
41
57
  export declare function globalFunctionLimitMessage(max: number): string;
58
+ /**
59
+ * Returns the object behind a sandbox-side wrapper, for reading a builtin's internal slots (the
60
+ * single VM's secure proxy wraps the script's `Date`s). See `SanitizeOptions.unwrap`.
61
+ */
62
+ export type UnwrapSandboxValue = (value: object) => object;
42
63
  /**
43
64
  * Host side of the double VM's global-function gate.
44
65
  *
@@ -71,4 +92,4 @@ export interface GlobalFunctionCallGate {
71
92
  * return value like a tool result and re-creates it in the sandbox realm (a promise becomes a
72
93
  * sandbox-realm promise), and turns host errors into safe errors.
73
94
  */
74
- export declare function createGlobalFunctionGate(context: ExecutionContext, realm: SandboxRealmBridge): GlobalFunctionCallGate;
95
+ export declare function createGlobalFunctionGate(context: ExecutionContext, realm: SandboxRealmBridge, unwrap?: UnwrapSandboxValue): GlobalFunctionCallGate;
@@ -12,6 +12,11 @@
12
12
  * the script, bypassing the global-function gate, so only plain data may live inside them
13
13
  * - Opaque containers (promises, WeakMaps, iterators, generators): their contents cannot be
14
14
  * validated, and their built-in methods hand those contents to the script directly
15
+ * - Host code that is not on the value itself: the methods and accessors of a class instance
16
+ * live on its class, a function can hide in a property that is not enumerable or is keyed by
17
+ * a symbol, and a Proxy runs its handler on every access. A script reaches all of these, so
18
+ * the rules for functions and for getters apply to them as they do to an own enumerable member.
19
+ * (A function passed as a global is judged as a function; what hangs on it is not looked at.)
15
20
  *
16
21
  * @packageDocumentation
17
22
  */
@@ -38,6 +43,13 @@ export interface GlobalsValidationOptions {
38
43
  * List of specifically allowed function names (if allowFunctions is false)
39
44
  */
40
45
  allowedFunctionNames?: string[];
46
+ /**
47
+ * Whether a script can read the `constructor` of a class instance. Then the class itself is a
48
+ * function the script can reach, and an instance is refused unless functions are allowed.
49
+ * The enclave passes false when its membrane blocks `constructor` (every level but PERMISSIVE).
50
+ * @default true
51
+ */
52
+ classConstructorsReachable?: boolean;
41
53
  }
42
54
  /**
43
55
  * Validate a single global value recursively
package/index.d.ts CHANGED
@@ -11,6 +11,7 @@
11
11
  * @packageDocumentation
12
12
  */
13
13
  export { Enclave, runAgentScript } from './enclave';
14
+ export { disableSandboxRejectionGuard, isHostPromise } from './sandbox-rejections';
14
15
  export type { ExecutionResult, ExecutionError, ExecutionStats, EnclaveConfig, CreateEnclaveOptions, ToolHandler, SafeRuntimeContext, ExecutionContext, SandboxAdapter, SecurityLevel, AstPreset, SecurityLevelConfig, SecureProxyLevelConfig, ReferenceSidecarOptions, WorkerPoolConfig, PartialDoubleVmConfig, CallToolOptions, ToolCallResult, ToolCallErrorInfo, ToolNamespaces, NormalizedToolNamespace, ConsoleHandler, ConsoleLevel, } from './types';
15
16
  export { SECURITY_LEVEL_CONFIGS } from './types';
16
17
  export { ScoringGate, ScoringGateError, createScoringGate, FeatureExtractor, ScoringCache, BaseScorer, DisabledScorer, RuleBasedScorer, ExternalApiScorer, ExternalApiScorerError, DEFAULT_SCORING_CONFIG, RULE_THRESHOLDS, } from './scoring';