@enclave-vm/core 2.19.1 → 2.20.0-rc.1
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 +291 -105
- package/adapters/worker-pool/config.d.ts +15 -5
- package/adapters/worker-pool/protocol.d.ts +27 -1
- package/adapters/worker-pool/worker-pool-adapter.d.ts +24 -0
- package/adapters/worker-pool/worker-slot.d.ts +35 -6
- package/async-context.d.ts +34 -0
- package/babel/babel-wrapper.d.ts +21 -12
- package/babel/multi-file-transform.d.ts +3 -0
- package/babel/text-scan.d.ts +54 -0
- package/bounded-json.d.ts +33 -0
- package/deadline-guard.d.ts +16 -0
- package/dynamic-import.d.ts +25 -0
- package/enclave-options.d.ts +62 -0
- package/enclave.d.ts +35 -8
- package/esm/index.mjs +5520 -917
- package/esm/worker.mjs +2777 -195
- package/execution-deadline.d.ts +6 -0
- package/global-function-gate.d.ts +34 -1
- package/index.d.ts +3 -3
- package/index.js +5562 -959
- package/interpreter/interpreter.d.ts +207 -10
- package/memory-tracker.d.ts +7 -0
- package/package.json +3 -3
- package/portable-enclave.d.ts +51 -0
- package/portable-globals.d.ts +2 -0
- package/realm-airlock.d.ts +6 -1
- package/realm-memory.d.ts +89 -0
- package/realm-regex-backstop.d.ts +18 -0
- package/realm-stack.d.ts +25 -0
- package/safe-runtime.d.ts +2 -0
- package/sandbox-rejections.d.ts +3 -1
- package/scoring/feature-extractor.d.ts +52 -1
- package/scoring/index.d.ts +2 -1
- package/scoring/scorers/index.d.ts +2 -1
- package/scoring/scorers/local-llm.scorer.d.ts +9 -3
- package/scoring/scorers/progressive.scorer.d.ts +28 -1
- package/scoring/scorers/rule-based.scorer.d.ts +11 -1
- package/scoring/scoring-gate.d.ts +2 -0
- package/scoring/types.d.ts +36 -8
- package/secure-proxy.d.ts +17 -3
- package/session/session.d.ts +8 -1
- package/session-types.d.ts +6 -4
- package/sidecar/index.d.ts +1 -1
- package/sidecar/reference-config.d.ts +18 -1
- package/sidecar/reference-resolver.d.ts +26 -3
- package/sidecar/reference-sidecar.d.ts +37 -0
- package/src/adapters/worker-pool/package.json +3 -3
- package/src/adapters/worker-pool/worker-script.js +2835 -465
- package/thrown-value.d.ts +59 -0
- package/types.d.ts +85 -19
- package/value-sanitizer.d.ts +6 -1
- package/worker.d.ts +2 -0
- package/worker.js +2767 -195
|
@@ -6,26 +6,76 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @packageDocumentation
|
|
8
8
|
*/
|
|
9
|
+
import type { NormalizedToolNamespace } from '@enclave-vm/ast';
|
|
9
10
|
import type { ExtractedFeatures } from './types';
|
|
11
|
+
/**
|
|
12
|
+
* Options of the feature extractor
|
|
13
|
+
*/
|
|
14
|
+
export interface FeatureExtractorOptions {
|
|
15
|
+
/**
|
|
16
|
+
* The tool namespaces of the sandbox the code is written for. A call of a namespace method
|
|
17
|
+
* (`mail.send(args)`) is the tool call it stands for (`callTool('mail.send', args)`) and is
|
|
18
|
+
* extracted as one.
|
|
19
|
+
*/
|
|
20
|
+
toolNamespaces?: ReadonlyArray<NormalizedToolNamespace>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Error thrown when code cannot be analysed. The scoring gate treats it like any other scoring
|
|
24
|
+
* error: `failOpen` decides whether the script runs.
|
|
25
|
+
*/
|
|
26
|
+
export declare class FeatureExtractionError extends Error {
|
|
27
|
+
constructor(message: string);
|
|
28
|
+
}
|
|
10
29
|
/**
|
|
11
30
|
* Feature Extractor - extracts security-relevant features from code
|
|
12
31
|
*/
|
|
13
32
|
export declare class FeatureExtractor {
|
|
33
|
+
/** Namespace name -> method name -> tool name */
|
|
34
|
+
private readonly toolNamespaces;
|
|
35
|
+
constructor(options?: FeatureExtractorOptions);
|
|
14
36
|
/**
|
|
15
37
|
* Extract features from code
|
|
16
38
|
*
|
|
17
39
|
* @param code - Source code to analyze
|
|
18
40
|
* @returns Extracted features for scoring
|
|
41
|
+
* @throws FeatureExtractionError if the code cannot be parsed: text that cannot be read has
|
|
42
|
+
* no features, which is not the same as having none that count
|
|
19
43
|
*/
|
|
20
44
|
extract(code: string): ExtractedFeatures;
|
|
21
45
|
/**
|
|
22
46
|
* Parse code into AST
|
|
47
|
+
*
|
|
48
|
+
* The code is read as a module, in which a top-level `await` is allowed, and a top-level
|
|
49
|
+
* `return` is accepted as well: both forms of AgentScript can be analysed, the script as its
|
|
50
|
+
* author wrote it and the script wrapped in its main function.
|
|
51
|
+
*
|
|
52
|
+
* @throws FeatureExtractionError if the code does not parse
|
|
23
53
|
*/
|
|
24
54
|
private parseCode;
|
|
55
|
+
/**
|
|
56
|
+
* The string a node spells out in the source: a string literal, or a template literal without
|
|
57
|
+
* substitutions. `undefined` for anything else.
|
|
58
|
+
*/
|
|
59
|
+
private staticString;
|
|
60
|
+
/**
|
|
61
|
+
* The tool a member expression on a tool namespace refers to: `mail.send`, `mail['send']`
|
|
62
|
+
* (static), `mail[name]` (dynamic). `undefined` when the object is not a tool namespace, or
|
|
63
|
+
* the member is not one of its methods (nothing is called then).
|
|
64
|
+
*/
|
|
65
|
+
private toolReferenceOfMember;
|
|
66
|
+
/**
|
|
67
|
+
* The tool a call expression calls, by its callee: `callTool(name, args)`,
|
|
68
|
+
* `__safe_callTool(name, args)`, or a method of a tool namespace. `undefined` for any other call.
|
|
69
|
+
*/
|
|
70
|
+
private toolReferenceOfCallee;
|
|
25
71
|
/**
|
|
26
72
|
* Extract tool call information
|
|
27
73
|
*/
|
|
28
74
|
private extractToolCall;
|
|
75
|
+
/**
|
|
76
|
+
* Add one tool call to the features
|
|
77
|
+
*/
|
|
78
|
+
private recordToolCall;
|
|
29
79
|
/**
|
|
30
80
|
* Extract information from tool arguments
|
|
31
81
|
*/
|
|
@@ -43,7 +93,8 @@ export declare class FeatureExtractor {
|
|
|
43
93
|
*/
|
|
44
94
|
private findMaxLimitValue;
|
|
45
95
|
/**
|
|
46
|
-
* Hash code for caching
|
|
96
|
+
* Hash code for caching: the whole SHA-256 digest. The hash is the only key of a cached
|
|
97
|
+
* decision, so two texts may share a decision only if they are the same text.
|
|
47
98
|
*/
|
|
48
99
|
private hashCode;
|
|
49
100
|
/**
|
package/scoring/index.d.ts
CHANGED
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
* @packageDocumentation
|
|
25
25
|
*/
|
|
26
26
|
export { ScoringGate, ScoringGateError, createScoringGate } from './scoring-gate';
|
|
27
|
-
export { FeatureExtractor } from './feature-extractor';
|
|
27
|
+
export { FeatureExtractor, FeatureExtractionError } from './feature-extractor';
|
|
28
|
+
export type { FeatureExtractorOptions } from './feature-extractor';
|
|
28
29
|
export { ScoringCache } from './cache';
|
|
29
30
|
export { BaseScorer } from './scorer.interface';
|
|
30
31
|
export type { Scorer } from './scorer.interface';
|
|
@@ -6,5 +6,6 @@
|
|
|
6
6
|
export { DisabledScorer } from './disabled.scorer';
|
|
7
7
|
export { RuleBasedScorer } from './rule-based.scorer';
|
|
8
8
|
export { ExternalApiScorer, ExternalApiScorerError } from './external-api.scorer';
|
|
9
|
-
export { ProgressiveScorer } from './progressive.scorer';
|
|
9
|
+
export { ProgressiveScorer, ProgressiveScorerError } from './progressive.scorer';
|
|
10
|
+
export type { ProgressiveScorerOptions } from './progressive.scorer';
|
|
10
11
|
export { LocalLlmScorer, LocalLlmScorerError, DISABLE_MODEL_LOAD_ENV } from './local-llm.scorer';
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Local LLM Scorer
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Scores the extracted features with one of:
|
|
5
5
|
* - Built-in heuristics: Keyword-based risk detection (default)
|
|
6
6
|
* - Custom analyzer: Plug in external LLM or static code analyzer
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* At initialization the scorer loads a Hugging Face transformers.js pipeline when the optional
|
|
9
|
+
* dependency is installed (the model is downloaded on first use, into `cacheDir`). No score is
|
|
10
|
+
* computed from that model: the pipeline is not called. Whether it loaded decides which of two
|
|
11
|
+
* scorings applies when there is no custom analyzer: the keyword heuristics when it did, the
|
|
12
|
+
* rule-based fallback when it did not (and `fallbackToRules` is on). Set
|
|
13
|
+
* `ENCLAVE_DISABLE_LOCAL_LLM_MODEL=1` to skip the load.
|
|
9
14
|
*
|
|
10
15
|
* @packageDocumentation
|
|
11
16
|
*/
|
|
@@ -13,7 +18,8 @@ import { BaseScorer } from '../scorer.interface';
|
|
|
13
18
|
import type { ExtractedFeatures, ScoringResult, LocalLlmConfig } from '../types';
|
|
14
19
|
export declare const DISABLE_MODEL_LOAD_ENV = "ENCLAVE_DISABLE_LOCAL_LLM_MODEL";
|
|
15
20
|
/**
|
|
16
|
-
* Local LLM Scorer -
|
|
21
|
+
* Local LLM Scorer - keyword heuristics or a host-supplied analyzer (see the file header for
|
|
22
|
+
* what the loaded model is and is not used for)
|
|
17
23
|
*
|
|
18
24
|
* @example Basic usage with built-in heuristics
|
|
19
25
|
* ```typescript
|
|
@@ -8,10 +8,36 @@
|
|
|
8
8
|
* This provides optimal latency for benign code while still catching
|
|
9
9
|
* sophisticated attacks through ML analysis.
|
|
10
10
|
*
|
|
11
|
+
* When the detailed stage is called for and cannot answer (it throws, or it was not
|
|
12
|
+
* configured), the scorer either answers with the fast score alone or fails, depending on
|
|
13
|
+
* `requireDetailed` (see `ProgressiveScorerOptions`).
|
|
14
|
+
*
|
|
11
15
|
* @packageDocumentation
|
|
12
16
|
*/
|
|
13
17
|
import { BaseScorer } from '../scorer.interface';
|
|
14
18
|
import type { ExtractedFeatures, ScoringResult, ProgressiveScoringConfig } from '../types';
|
|
19
|
+
/**
|
|
20
|
+
* Options of the progressive scorer
|
|
21
|
+
*/
|
|
22
|
+
export interface ProgressiveScorerOptions {
|
|
23
|
+
/**
|
|
24
|
+
* What happens when the fast score reaches the escalation threshold and the detailed stage
|
|
25
|
+
* cannot answer (it throws, or none is configured):
|
|
26
|
+
*
|
|
27
|
+
* - false: the fast result is the answer
|
|
28
|
+
* - true: `score()` throws a `ProgressiveScorerError`, so that whoever asked decides. The
|
|
29
|
+
* scoring gate sets this when it is configured with `failOpen: false`.
|
|
30
|
+
*
|
|
31
|
+
* @default false
|
|
32
|
+
*/
|
|
33
|
+
requireDetailed?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown when the detailed stage is required and cannot answer
|
|
37
|
+
*/
|
|
38
|
+
export declare class ProgressiveScorerError extends Error {
|
|
39
|
+
constructor(message: string);
|
|
40
|
+
}
|
|
15
41
|
/**
|
|
16
42
|
* Progressive Scorer - fast check first, escalate if suspicious
|
|
17
43
|
*
|
|
@@ -37,7 +63,8 @@ export declare class ProgressiveScorer extends BaseScorer {
|
|
|
37
63
|
private readonly escalationThreshold;
|
|
38
64
|
private readonly combination;
|
|
39
65
|
private readonly config;
|
|
40
|
-
|
|
66
|
+
private readonly requireDetailed;
|
|
67
|
+
constructor(config: ProgressiveScoringConfig, options?: ProgressiveScorerOptions);
|
|
41
68
|
/**
|
|
42
69
|
* Initialize both scorers
|
|
43
70
|
*/
|
|
@@ -24,8 +24,18 @@ import type { ExtractedFeatures, ScoringResult } from '../types';
|
|
|
24
24
|
export declare class RuleBasedScorer extends BaseScorer {
|
|
25
25
|
readonly type: "rule-based";
|
|
26
26
|
readonly name = "RuleBasedScorer";
|
|
27
|
-
|
|
27
|
+
/** The base score of each rule: the built-in one, or the host's from `customRules` */
|
|
28
|
+
private readonly ruleScores;
|
|
29
|
+
/**
|
|
30
|
+
* @param customRules Scores by rule id that replace the built-in ones (clamped to 0-100).
|
|
31
|
+
* Ids that are not rule ids and values that are not numbers are ignored.
|
|
32
|
+
*/
|
|
28
33
|
constructor(customRules?: Record<string, number>);
|
|
34
|
+
/**
|
|
35
|
+
* The most a rule contributes: its built-in cap, or its base score where a host set one
|
|
36
|
+
* above the cap
|
|
37
|
+
*/
|
|
38
|
+
private capFor;
|
|
29
39
|
score(features: ExtractedFeatures): Promise<ScoringResult>;
|
|
30
40
|
/**
|
|
31
41
|
* Rule 1: Check for sensitive field access
|
|
@@ -20,6 +20,7 @@ export declare class ScoringGate {
|
|
|
20
20
|
private scorer;
|
|
21
21
|
private cache;
|
|
22
22
|
private initialized;
|
|
23
|
+
private initializing;
|
|
23
24
|
constructor(config: ScoringGateConfig);
|
|
24
25
|
/**
|
|
25
26
|
* Initialize the scoring gate
|
|
@@ -28,6 +29,7 @@ export declare class ScoringGate {
|
|
|
28
29
|
* (e.g., local-llm needs to download the model)
|
|
29
30
|
*/
|
|
30
31
|
initialize(): Promise<void>;
|
|
32
|
+
private setUpScorer;
|
|
31
33
|
/**
|
|
32
34
|
* Create the appropriate scorer based on configuration
|
|
33
35
|
*/
|
package/scoring/types.d.ts
CHANGED
|
@@ -6,14 +6,17 @@
|
|
|
6
6
|
*
|
|
7
7
|
* @packageDocumentation
|
|
8
8
|
*/
|
|
9
|
+
import { type NormalizedToolNamespace, type ToolNamespaces } from '@enclave-vm/ast';
|
|
9
10
|
/**
|
|
10
11
|
* Scorer implementation types
|
|
11
12
|
*
|
|
12
13
|
* - `disabled`: No scoring, pass-through mode (0ms latency)
|
|
13
14
|
* - `rule-based`: Pure TypeScript rules, zero dependencies (~1ms latency)
|
|
14
|
-
* - `local-llm`:
|
|
15
|
+
* - `local-llm`: The host's `customAnalyzer`, or a built-in keyword heuristic over the extracted
|
|
16
|
+
* features. A Hugging Face model is loaded at initialization when the optional dependency is
|
|
17
|
+
* installed (downloaded on first use), but no score is computed from it.
|
|
15
18
|
* - `external-api`: External API-based scoring (~100ms latency, best detection)
|
|
16
|
-
* - `progressive`: Fast rule-based check, escalate to
|
|
19
|
+
* - `progressive`: Fast rule-based check, escalate to a detailed scorer if suspicious
|
|
17
20
|
*/
|
|
18
21
|
export type ScorerType = 'disabled' | 'rule-based' | 'local-llm' | 'external-api' | 'progressive';
|
|
19
22
|
/**
|
|
@@ -369,8 +372,11 @@ export interface LocalLlmConfig {
|
|
|
369
372
|
modelId: 'Xenova/codebert-base' | 'Xenova/all-MiniLM-L6-v2' | string;
|
|
370
373
|
/**
|
|
371
374
|
* Scoring mode
|
|
372
|
-
* - classification:
|
|
373
|
-
*
|
|
375
|
+
* - classification: the score is the `customAnalyzer`'s, or the built-in keyword heuristic
|
|
376
|
+
* over the extracted features. No classification model is run.
|
|
377
|
+
* - similarity: additionally searches a VectoriaDB index for similar patterns. The index is
|
|
378
|
+
* created empty and this configuration has no way to add patterns to it, so the search
|
|
379
|
+
* finds nothing and the score is the same as in classification mode.
|
|
374
380
|
* @default 'classification'
|
|
375
381
|
*/
|
|
376
382
|
mode?: 'classification' | 'similarity';
|
|
@@ -410,8 +416,8 @@ export interface LocalLlmConfig {
|
|
|
410
416
|
* Custom analyzer for external LLM or static code analysis
|
|
411
417
|
*
|
|
412
418
|
* If provided, this analyzer will be used instead of the built-in
|
|
413
|
-
* heuristic-based analysis. The HuggingFace pipeline
|
|
414
|
-
*
|
|
419
|
+
* heuristic-based analysis. The HuggingFace pipeline is still loaded
|
|
420
|
+
* when the optional dependency is installed; its output is not used.
|
|
415
421
|
*
|
|
416
422
|
* @example
|
|
417
423
|
* ```typescript
|
|
@@ -568,7 +574,8 @@ export interface ScoringGateConfig {
|
|
|
568
574
|
blockThreshold?: number;
|
|
569
575
|
/**
|
|
570
576
|
* Score threshold for warning (0-100)
|
|
571
|
-
* Scripts with scores >= this value
|
|
577
|
+
* Scripts with scores >= this value (and below `blockThreshold`) run, with `warned` set in the
|
|
578
|
+
* result. The gate writes to the console only with `verbose`.
|
|
572
579
|
* @default 40
|
|
573
580
|
*/
|
|
574
581
|
warnThreshold?: number;
|
|
@@ -592,13 +599,30 @@ export interface ScoringGateConfig {
|
|
|
592
599
|
* - true (fail-open): Allow execution if scoring fails
|
|
593
600
|
* - false (fail-closed): Block execution if scoring fails
|
|
594
601
|
*
|
|
602
|
+
* A scoring error is anything that keeps the configured scorer from giving its answer: code
|
|
603
|
+
* the feature extractor cannot parse, a scorer that throws, and in progressive mode a detailed
|
|
604
|
+
* stage that fails or is missing once the fast score called for it. With `failOpen: false`
|
|
605
|
+
* each of these blocks the script; with `failOpen: true` the script runs, and `reason` of the
|
|
606
|
+
* result says why. In progressive mode a detailed stage that fails leaves the fast score to
|
|
607
|
+
* decide, and the result has no reason for it.
|
|
608
|
+
*
|
|
595
609
|
* @default true
|
|
596
610
|
*/
|
|
597
611
|
failOpen?: boolean;
|
|
598
612
|
/**
|
|
599
|
-
*
|
|
613
|
+
* Scores for the rules of the rule-based scorer, by rule id (see `RuleId`): the base score a
|
|
614
|
+
* rule contributes when it fires, instead of the built-in one. Values are clamped to 0-100;
|
|
615
|
+
* 0 turns a rule off. Ids that are not rule ids, and values that are not numbers, are ignored.
|
|
616
|
+
*
|
|
617
|
+
* @example { EXFIL_PATTERN: 100, BULK_OPERATION: 0 }
|
|
600
618
|
*/
|
|
601
619
|
customRules?: Record<string, number>;
|
|
620
|
+
/**
|
|
621
|
+
* The tool namespaces of the sandbox whose scripts are scored (the `toolNamespaces` of the
|
|
622
|
+
* enclave). A call of a namespace method, `mail.send(args)`, is scored as the tool call it
|
|
623
|
+
* stands for, `callTool('mail.send', args)`. Without this the gate only sees `callTool`.
|
|
624
|
+
*/
|
|
625
|
+
toolNamespaces?: ToolNamespaces | ReadonlyArray<NormalizedToolNamespace>;
|
|
602
626
|
/**
|
|
603
627
|
* Log detailed scoring information
|
|
604
628
|
* @default false
|
|
@@ -620,6 +644,10 @@ export interface NormalizedScoringConfig {
|
|
|
620
644
|
* The effective scorer type
|
|
621
645
|
*/
|
|
622
646
|
scorerType: ScorerType;
|
|
647
|
+
/**
|
|
648
|
+
* Validated tool namespaces whose method calls are scored as tool calls (empty when none)
|
|
649
|
+
*/
|
|
650
|
+
toolNamespaces: ReadonlyArray<NormalizedToolNamespace>;
|
|
623
651
|
/**
|
|
624
652
|
* Progressive config if using progressive strategy
|
|
625
653
|
*/
|
package/secure-proxy.d.ts
CHANGED
|
@@ -163,11 +163,25 @@ export interface SecureProxyOptions {
|
|
|
163
163
|
exposeToSandbox?: (value: unknown) => unknown;
|
|
164
164
|
/**
|
|
165
165
|
* Whether an object was made in the sandbox realm (by the script, or for it). Such a value is
|
|
166
|
-
* the script's own
|
|
167
|
-
* `reduce` to collect into)
|
|
168
|
-
*
|
|
166
|
+
* the script's own, wherever it comes back from: passed to a callback by a host built-in (the
|
|
167
|
+
* object a script gave `reduce` to collect into), read out of what a built-in returned (a
|
|
168
|
+
* function a `map` callback returned), or returned by a call. It leaves the membrane as it is,
|
|
169
|
+
* never behind a proxy: a proxy would make the script's object read-only and would send a call
|
|
170
|
+
* to the script's function through the gate for host functions. The test must only say yes to
|
|
171
|
+
* what certainly is of that realm.
|
|
169
172
|
*/
|
|
170
173
|
ownedBySandbox?: (value: object) => boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Whether a value is the form in which the sandbox holds a value of the host: what
|
|
176
|
+
* `exposeToSandbox` returned for it earlier (the single VM's airlock proxy). A script passes
|
|
177
|
+
* such a value into a call as it is. Whatever the callee does with it (hands it on to a
|
|
178
|
+
* function of the script, keeps it in what it returns), it leaves the membrane as it went in,
|
|
179
|
+
* and it is not wrapped a second time.
|
|
180
|
+
*
|
|
181
|
+
* With this hook set, a built-in method of host data runs on host data only: on an object
|
|
182
|
+
* that is not a value of the host the call is refused (see the apply trap).
|
|
183
|
+
*/
|
|
184
|
+
heldBySandbox?: (value: object) => boolean;
|
|
171
185
|
}
|
|
172
186
|
/**
|
|
173
187
|
* Create a secure proxy that blocks dangerous property access
|
package/session/session.d.ts
CHANGED
|
@@ -44,10 +44,17 @@ export declare class Session implements ISession {
|
|
|
44
44
|
private readonly config;
|
|
45
45
|
private readonly toolHandler?;
|
|
46
46
|
private _pendingToolCall;
|
|
47
|
+
/**
|
|
48
|
+
* How the promise of the pending tool call is settled. Kept beside the pending call and tied
|
|
49
|
+
* to its call id from the moment the call exists, so a result always reaches the call it was
|
|
50
|
+
* submitted for, whenever the tool handler of that call settles.
|
|
51
|
+
*/
|
|
52
|
+
private pendingSettlement;
|
|
47
53
|
private readonly stats;
|
|
48
54
|
private stdoutBytes;
|
|
55
|
+
/** The promise every caller of wait() gets while the session is running, and how it is resolved */
|
|
56
|
+
private waitPromise;
|
|
49
57
|
private resolveWait;
|
|
50
|
-
private rejectWait;
|
|
51
58
|
private heartbeatInterval;
|
|
52
59
|
constructor(options?: SessionOptions);
|
|
53
60
|
/**
|
package/session-types.d.ts
CHANGED
|
@@ -40,7 +40,8 @@ export interface SessionConfig extends CreateEnclaveOptions {
|
|
|
40
40
|
*/
|
|
41
41
|
heartbeatIntervalMs?: number;
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* Not implemented: nothing in the session reads this value, and its events are not encrypted
|
|
44
|
+
* whatever it is set to.
|
|
44
45
|
* @default false
|
|
45
46
|
*/
|
|
46
47
|
encryption?: boolean;
|
|
@@ -214,8 +215,8 @@ export interface SessionFinalResult {
|
|
|
214
215
|
* Runtime channel abstraction
|
|
215
216
|
*
|
|
216
217
|
* Provides bidirectional communication between the broker and runtime.
|
|
217
|
-
*
|
|
218
|
-
*
|
|
218
|
+
* This package implements the embedded (in-process) channel only; the remote (WebSocket)
|
|
219
|
+
* channel is a type of the design, with no implementation here.
|
|
219
220
|
*/
|
|
220
221
|
export interface RuntimeChannel {
|
|
221
222
|
/**
|
|
@@ -246,7 +247,8 @@ export interface EmbeddedChannelConfig {
|
|
|
246
247
|
/**
|
|
247
248
|
* WebSocket runtime channel configuration
|
|
248
249
|
*
|
|
249
|
-
* For remote execution where the runtime is in a separate process/worker.
|
|
250
|
+
* For remote execution where the runtime is in a separate process/worker. A type of the
|
|
251
|
+
* design: this package has no channel that takes it.
|
|
250
252
|
*/
|
|
251
253
|
export interface WebSocketChannelConfig {
|
|
252
254
|
type: 'websocket';
|
package/sidecar/index.d.ts
CHANGED
|
@@ -7,6 +7,6 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @packageDocumentation
|
|
9
9
|
*/
|
|
10
|
-
export { ReferenceConfig, REFERENCE_CONFIGS, REF_ID_PREFIX, REF_ID_SUFFIX, REF_ID_PATTERN, isReferenceId, getReferenceConfig, } from './reference-config';
|
|
10
|
+
export { ReferenceConfig, REFERENCE_CONFIGS, REF_ID_PREFIX, REF_ID_SUFFIX, REF_ID_PATTERN, isReferenceId, getReferenceConfig, getMaxTotalResolvedSize, } from './reference-config';
|
|
11
11
|
export { ReferenceSidecar, ReferenceSource, ReferenceMetadata, SidecarLimitError, ReferenceNotFoundError, } from './reference-sidecar';
|
|
12
12
|
export { ReferenceResolver, ResolutionLimitError, CompositeHandle, isCompositeHandle } from './reference-resolver';
|
|
@@ -27,10 +27,22 @@ export interface ReferenceConfig {
|
|
|
27
27
|
*/
|
|
28
28
|
extractionThreshold: number;
|
|
29
29
|
/**
|
|
30
|
-
* Maximum expanded size when resolving references for tool
|
|
30
|
+
* Maximum expanded size when resolving references for one tool call
|
|
31
31
|
* Prevents predictive expansion attacks
|
|
32
32
|
*/
|
|
33
33
|
maxResolvedSize: number;
|
|
34
|
+
/**
|
|
35
|
+
* Maximum number of bytes the references of one execution may be expanded to, summed over
|
|
36
|
+
* all its tool calls. `maxResolvedSize` limits one call; a script can make many calls, and
|
|
37
|
+
* what was resolved for a call is held until its handler settles.
|
|
38
|
+
*
|
|
39
|
+
* A stored value counts once, the first time it is resolved: every later resolution hands
|
|
40
|
+
* out the same string. A concatenation of references counts every time it is built.
|
|
41
|
+
*
|
|
42
|
+
* @default twice `maxTotalSize`: every stored value can be passed as it is and once more
|
|
43
|
+
* inside a concatenation
|
|
44
|
+
*/
|
|
45
|
+
maxTotalResolvedSize?: number;
|
|
34
46
|
/**
|
|
35
47
|
* Whether to allow composite handles from concatenation
|
|
36
48
|
* If false, __safe_concat__ throws when references are detected
|
|
@@ -75,6 +87,11 @@ export declare const REF_ID_PATTERN: RegExp;
|
|
|
75
87
|
* Check if a string is a reference ID
|
|
76
88
|
*/
|
|
77
89
|
export declare function isReferenceId(value: unknown): value is string;
|
|
90
|
+
/**
|
|
91
|
+
* The limit on what one execution resolves in total: `maxTotalResolvedSize` when the
|
|
92
|
+
* configuration sets it to a number that is not negative, otherwise twice `maxTotalSize`.
|
|
93
|
+
*/
|
|
94
|
+
export declare function getMaxTotalResolvedSize(config: ReferenceConfig): number;
|
|
78
95
|
/**
|
|
79
96
|
* Get reference config for a security level, with optional overrides
|
|
80
97
|
*/
|
|
@@ -73,10 +73,28 @@ export declare class ReferenceResolver {
|
|
|
73
73
|
/**
|
|
74
74
|
* Check if resolution would exceed size limits
|
|
75
75
|
*
|
|
76
|
+
* Two limits apply: `maxResolvedSize` to what this value expands to, and
|
|
77
|
+
* `maxTotalResolvedSize` to what the execution has had resolved so far plus what resolving
|
|
78
|
+
* this value would add (see `predictAddedSize`).
|
|
79
|
+
*
|
|
76
80
|
* @param value - The value to check
|
|
77
|
-
* @returns true if resolution would exceed maxResolvedSize
|
|
81
|
+
* @returns true if resolution would exceed maxResolvedSize or the execution's total
|
|
78
82
|
*/
|
|
79
83
|
wouldExceedLimit(value: unknown): boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Predict what resolving a value would add to the execution's total of resolved data,
|
|
86
|
+
* WITHOUT allocating memory
|
|
87
|
+
*
|
|
88
|
+
* A stored value adds its size the first time it is resolved in the execution and nothing
|
|
89
|
+
* afterwards (`counted` holds the ones this walk has seen). A composite adds the size of the
|
|
90
|
+
* string it is joined into, every time, besides what its parts add.
|
|
91
|
+
*/
|
|
92
|
+
private predictAddedSize;
|
|
93
|
+
/**
|
|
94
|
+
* Run a step that counts resolved data against the execution's total; when the sidecar
|
|
95
|
+
* refuses it, the refusal is thrown as the resolution error callers of `resolve` handle
|
|
96
|
+
*/
|
|
97
|
+
private withinTotal;
|
|
80
98
|
/**
|
|
81
99
|
* Resolve all reference IDs in a value tree
|
|
82
100
|
*
|
|
@@ -97,11 +115,16 @@ export declare class ReferenceResolver {
|
|
|
97
115
|
/**
|
|
98
116
|
* Check if a value contains any reference IDs
|
|
99
117
|
*
|
|
118
|
+
* Every value is looked at, at any depth: a reference or composite past `maxResolutionDepth`
|
|
119
|
+
* cannot be resolved, and it must still be found, so that `resolve()` refuses the call rather
|
|
120
|
+
* than the handler receiving it unresolved. The value is walked with a list instead of by
|
|
121
|
+
* recursion, so a deep value cannot exhaust the stack, and an object is looked at once.
|
|
122
|
+
*
|
|
100
123
|
* @param value - The value to check
|
|
101
|
-
* @param
|
|
124
|
+
* @param _depth - Unused; kept for callers that pass a depth
|
|
102
125
|
* @returns true if the value or any nested value is a reference
|
|
103
126
|
*/
|
|
104
|
-
containsReferences(value: unknown,
|
|
127
|
+
containsReferences(value: unknown, _depth?: number): boolean;
|
|
105
128
|
/**
|
|
106
129
|
* Create a composite handle from multiple reference parts
|
|
107
130
|
*
|
|
@@ -77,7 +77,12 @@ export declare class ReferenceSidecar {
|
|
|
77
77
|
private readonly config;
|
|
78
78
|
private readonly storage;
|
|
79
79
|
private readonly metadata;
|
|
80
|
+
/** The stored values that were resolved for a tool call, as the string they were resolved to */
|
|
81
|
+
private readonly resolved;
|
|
80
82
|
private totalSize;
|
|
83
|
+
/** Bytes of resolved data built for the tool calls of this execution so far */
|
|
84
|
+
private resolvedSize;
|
|
85
|
+
private readonly maxTotalResolvedSize;
|
|
81
86
|
private disposed;
|
|
82
87
|
constructor(config: ReferenceConfig);
|
|
83
88
|
/**
|
|
@@ -108,6 +113,38 @@ export declare class ReferenceSidecar {
|
|
|
108
113
|
* @returns The stored data as a string or undefined
|
|
109
114
|
*/
|
|
110
115
|
retrieveString(refId: string, encoding?: BufferEncoding): string | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* Resolve a reference for a tool call: the stored data as a string
|
|
118
|
+
*
|
|
119
|
+
* A stored value is decoded once in the life of the sidecar (one execution) and counted
|
|
120
|
+
* against `maxTotalResolvedSize` then. Every later resolution, in the same call or in another,
|
|
121
|
+
* returns the same string and counts nothing: a string cannot be changed, so handing it out
|
|
122
|
+
* again takes no memory.
|
|
123
|
+
*
|
|
124
|
+
* @param refId - The reference ID to resolve
|
|
125
|
+
* @returns The stored data as a string, or undefined if the reference is not known
|
|
126
|
+
* @throws SidecarLimitError if the execution's total of resolved data would be exceeded
|
|
127
|
+
*/
|
|
128
|
+
resolveString(refId: string): string | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* Whether a reference was resolved before, so that resolving it again counts nothing
|
|
131
|
+
*/
|
|
132
|
+
isResolved(refId: string): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Count resolved data that is about to be built for a tool call of this execution
|
|
135
|
+
*
|
|
136
|
+
* @param bytes - Size of the data in bytes
|
|
137
|
+
* @throws SidecarLimitError if the total would exceed `maxTotalResolvedSize`; nothing is counted then
|
|
138
|
+
*/
|
|
139
|
+
countResolved(bytes: number): void;
|
|
140
|
+
/**
|
|
141
|
+
* Whether `bytes` more of resolved data fit in what this execution may resolve in total
|
|
142
|
+
*/
|
|
143
|
+
canResolve(bytes: number): boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Bytes of resolved data built for the tool calls of this execution so far
|
|
146
|
+
*/
|
|
147
|
+
getResolvedSize(): number;
|
|
111
148
|
/**
|
|
112
149
|
* Check if a reference ID exists
|
|
113
150
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enclave-vm/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.20.0-rc.1",
|
|
4
4
|
"description": "Sandbox runtime for secure JavaScript code execution",
|
|
5
5
|
"author": "AgentFront <info@agentfront.dev>",
|
|
6
6
|
"homepage": "https://github.com/agentfront/enclave",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@babel/standalone": "^7.29.0",
|
|
49
|
-
"@enclave-vm/ast": "2.
|
|
50
|
-
"@enclave-vm/types": "2.
|
|
49
|
+
"@enclave-vm/ast": "2.20.0-rc.1",
|
|
50
|
+
"@enclave-vm/types": "2.20.0-rc.1",
|
|
51
51
|
"@types/estree": "1.0.8",
|
|
52
52
|
"acorn": "8.15.0",
|
|
53
53
|
"acorn-walk": "8.3.4",
|