@enclave-vm/core 0.0.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/LICENSE +201 -0
- package/README.md +732 -0
- package/SECURITY-AUDIT.md +916 -0
- package/package.json +59 -0
- package/src/adapters/vm-adapter.d.ts +32 -0
- package/src/adapters/vm-adapter.js +1140 -0
- package/src/adapters/vm-adapter.js.map +1 -0
- package/src/adapters/worker-pool/config.d.ts +149 -0
- package/src/adapters/worker-pool/config.js +129 -0
- package/src/adapters/worker-pool/config.js.map +1 -0
- package/src/adapters/worker-pool/errors.d.ts +109 -0
- package/src/adapters/worker-pool/errors.js +181 -0
- package/src/adapters/worker-pool/errors.js.map +1 -0
- package/src/adapters/worker-pool/execution-queue.d.ts +85 -0
- package/src/adapters/worker-pool/execution-queue.js +179 -0
- package/src/adapters/worker-pool/execution-queue.js.map +1 -0
- package/src/adapters/worker-pool/index.d.ts +48 -0
- package/src/adapters/worker-pool/index.js +73 -0
- package/src/adapters/worker-pool/index.js.map +1 -0
- package/src/adapters/worker-pool/memory-monitor.d.ts +106 -0
- package/src/adapters/worker-pool/memory-monitor.js +184 -0
- package/src/adapters/worker-pool/memory-monitor.js.map +1 -0
- package/src/adapters/worker-pool/protocol.d.ts +314 -0
- package/src/adapters/worker-pool/protocol.js +143 -0
- package/src/adapters/worker-pool/protocol.js.map +1 -0
- package/src/adapters/worker-pool/rate-limiter.d.ts +60 -0
- package/src/adapters/worker-pool/rate-limiter.js +101 -0
- package/src/adapters/worker-pool/rate-limiter.js.map +1 -0
- package/src/adapters/worker-pool/safe-deserialize.d.ts +50 -0
- package/src/adapters/worker-pool/safe-deserialize.js +141 -0
- package/src/adapters/worker-pool/safe-deserialize.js.map +1 -0
- package/src/adapters/worker-pool/worker-pool-adapter.d.ts +84 -0
- package/src/adapters/worker-pool/worker-pool-adapter.js +506 -0
- package/src/adapters/worker-pool/worker-pool-adapter.js.map +1 -0
- package/src/adapters/worker-pool/worker-script.d.ts +12 -0
- package/src/adapters/worker-pool/worker-script.js +770 -0
- package/src/adapters/worker-pool/worker-script.js.map +1 -0
- package/src/adapters/worker-pool/worker-slot.d.ts +116 -0
- package/src/adapters/worker-pool/worker-slot.js +301 -0
- package/src/adapters/worker-pool/worker-slot.js.map +1 -0
- package/src/babel/babel-wrapper.d.ts +118 -0
- package/src/babel/babel-wrapper.js +395 -0
- package/src/babel/babel-wrapper.js.map +1 -0
- package/src/babel/index.d.ts +9 -0
- package/src/babel/index.js +17 -0
- package/src/babel/index.js.map +1 -0
- package/src/babel/multi-file-transform.d.ts +122 -0
- package/src/babel/multi-file-transform.js +344 -0
- package/src/babel/multi-file-transform.js.map +1 -0
- package/src/double-vm/double-vm-wrapper.d.ts +65 -0
- package/src/double-vm/double-vm-wrapper.js +523 -0
- package/src/double-vm/double-vm-wrapper.js.map +1 -0
- package/src/double-vm/index.d.ts +31 -0
- package/src/double-vm/index.js +34 -0
- package/src/double-vm/index.js.map +1 -0
- package/src/double-vm/parent-vm-bootstrap.d.ts +63 -0
- package/src/double-vm/parent-vm-bootstrap.js +1695 -0
- package/src/double-vm/parent-vm-bootstrap.js.map +1 -0
- package/src/double-vm/suspicious-patterns.d.ts +57 -0
- package/src/double-vm/suspicious-patterns.js +304 -0
- package/src/double-vm/suspicious-patterns.js.map +1 -0
- package/src/double-vm/types.d.ts +178 -0
- package/src/double-vm/types.js +11 -0
- package/src/double-vm/types.js.map +1 -0
- package/src/double-vm/wrap-adapter.d.ts +36 -0
- package/src/double-vm/wrap-adapter.js +59 -0
- package/src/double-vm/wrap-adapter.js.map +1 -0
- package/src/enclave.d.ts +159 -0
- package/src/enclave.js +671 -0
- package/src/enclave.js.map +1 -0
- package/src/globals-validator.d.ts +80 -0
- package/src/globals-validator.js +206 -0
- package/src/globals-validator.js.map +1 -0
- package/src/index.d.ts +29 -0
- package/src/index.js +130 -0
- package/src/index.js.map +1 -0
- package/src/memory-proxy.d.ts +69 -0
- package/src/memory-proxy.js +310 -0
- package/src/memory-proxy.js.map +1 -0
- package/src/memory-tracker.d.ts +170 -0
- package/src/memory-tracker.js +230 -0
- package/src/memory-tracker.js.map +1 -0
- package/src/safe-error.d.ts +18 -0
- package/src/safe-error.js +76 -0
- package/src/safe-error.js.map +1 -0
- package/src/safe-runtime.d.ts +72 -0
- package/src/safe-runtime.js +656 -0
- package/src/safe-runtime.js.map +1 -0
- package/src/scoring/cache.d.ts +68 -0
- package/src/scoring/cache.js +158 -0
- package/src/scoring/cache.js.map +1 -0
- package/src/scoring/feature-extractor.d.ts +61 -0
- package/src/scoring/feature-extractor.js +366 -0
- package/src/scoring/feature-extractor.js.map +1 -0
- package/src/scoring/index.d.ts +33 -0
- package/src/scoring/index.js +53 -0
- package/src/scoring/index.js.map +1 -0
- package/src/scoring/scorer.interface.d.ts +65 -0
- package/src/scoring/scorer.interface.js +49 -0
- package/src/scoring/scorer.interface.js.map +1 -0
- package/src/scoring/scorers/disabled.scorer.d.ts +19 -0
- package/src/scoring/scorers/disabled.scorer.js +34 -0
- package/src/scoring/scorers/disabled.scorer.js.map +1 -0
- package/src/scoring/scorers/external-api.scorer.d.ts +29 -0
- package/src/scoring/scorers/external-api.scorer.js +141 -0
- package/src/scoring/scorers/external-api.scorer.js.map +1 -0
- package/src/scoring/scorers/index.d.ts +10 -0
- package/src/scoring/scorers/index.js +22 -0
- package/src/scoring/scorers/index.js.map +1 -0
- package/src/scoring/scorers/local-llm.scorer.d.ts +116 -0
- package/src/scoring/scorers/local-llm.scorer.js +474 -0
- package/src/scoring/scorers/local-llm.scorer.js.map +1 -0
- package/src/scoring/scorers/progressive.scorer.d.ts +77 -0
- package/src/scoring/scorers/progressive.scorer.js +199 -0
- package/src/scoring/scorers/progressive.scorer.js.map +1 -0
- package/src/scoring/scorers/rule-based.scorer.d.ts +66 -0
- package/src/scoring/scorers/rule-based.scorer.js +316 -0
- package/src/scoring/scorers/rule-based.scorer.js.map +1 -0
- package/src/scoring/scoring-gate.d.ts +76 -0
- package/src/scoring/scoring-gate.js +246 -0
- package/src/scoring/scoring-gate.js.map +1 -0
- package/src/scoring/types.d.ts +690 -0
- package/src/scoring/types.js +107 -0
- package/src/scoring/types.js.map +1 -0
- package/src/secure-proxy.d.ts +157 -0
- package/src/secure-proxy.js +609 -0
- package/src/secure-proxy.js.map +1 -0
- package/src/session/channels/embedded-channel.d.ts +76 -0
- package/src/session/channels/embedded-channel.js +130 -0
- package/src/session/channels/embedded-channel.js.map +1 -0
- package/src/session/channels/index.d.ts +9 -0
- package/src/session/channels/index.js +14 -0
- package/src/session/channels/index.js.map +1 -0
- package/src/session/index.d.ts +15 -0
- package/src/session/index.js +27 -0
- package/src/session/index.js.map +1 -0
- package/src/session/session-emitter.d.ts +119 -0
- package/src/session/session-emitter.js +253 -0
- package/src/session/session-emitter.js.map +1 -0
- package/src/session/session-state-machine.d.ts +108 -0
- package/src/session/session-state-machine.js +182 -0
- package/src/session/session-state-machine.js.map +1 -0
- package/src/session/session.d.ts +135 -0
- package/src/session/session.js +398 -0
- package/src/session/session.js.map +1 -0
- package/src/session-types.d.ts +313 -0
- package/src/session-types.js +39 -0
- package/src/session-types.js.map +1 -0
- package/src/sidecar/index.d.ts +12 -0
- package/src/sidecar/index.js +31 -0
- package/src/sidecar/index.js.map +1 -0
- package/src/sidecar/reference-config.d.ts +81 -0
- package/src/sidecar/reference-config.js +118 -0
- package/src/sidecar/reference-config.js.map +1 -0
- package/src/sidecar/reference-resolver.d.ts +115 -0
- package/src/sidecar/reference-resolver.js +262 -0
- package/src/sidecar/reference-resolver.js.map +1 -0
- package/src/sidecar/reference-sidecar.d.ts +161 -0
- package/src/sidecar/reference-sidecar.js +226 -0
- package/src/sidecar/reference-sidecar.js.map +1 -0
- package/src/tool-bridge.d.ts +4 -0
- package/src/tool-bridge.js +306 -0
- package/src/tool-bridge.js.map +1 -0
- package/src/types.d.ts +775 -0
- package/src/types.js +187 -0
- package/src/types.js.map +1 -0
- package/src/value-sanitizer.d.ts +133 -0
- package/src/value-sanitizer.js +421 -0
- package/src/value-sanitizer.js.map +1 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Memory Tracker for VM Execution
|
|
4
|
+
*
|
|
5
|
+
* Provides allocation tracking and memory limit enforcement for the VM adapter.
|
|
6
|
+
* Since Node.js vm module doesn't support native memory limits, this tracker
|
|
7
|
+
* monitors allocations via instrumented code and proxy wrappers.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.MemoryTracker = exports.MemoryLimitError = void 0;
|
|
13
|
+
exports.estimateStringSize = estimateStringSize;
|
|
14
|
+
exports.estimateArraySize = estimateArraySize;
|
|
15
|
+
exports.estimateObjectSize = estimateObjectSize;
|
|
16
|
+
/**
|
|
17
|
+
* Error thrown when memory limit is exceeded
|
|
18
|
+
*/
|
|
19
|
+
class MemoryLimitError extends Error {
|
|
20
|
+
usedBytes;
|
|
21
|
+
limitBytes;
|
|
22
|
+
/** Error code for identification */
|
|
23
|
+
code = 'MEMORY_LIMIT_EXCEEDED';
|
|
24
|
+
constructor(message,
|
|
25
|
+
/** Bytes used when limit was exceeded */
|
|
26
|
+
usedBytes,
|
|
27
|
+
/** Configured limit in bytes */
|
|
28
|
+
limitBytes) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.usedBytes = usedBytes;
|
|
31
|
+
this.limitBytes = limitBytes;
|
|
32
|
+
this.name = 'MemoryLimitError';
|
|
33
|
+
// Maintain proper stack trace in V8
|
|
34
|
+
if (Error.captureStackTrace) {
|
|
35
|
+
Error.captureStackTrace(this, MemoryLimitError);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.MemoryLimitError = MemoryLimitError;
|
|
40
|
+
/**
|
|
41
|
+
* Estimate the memory size of a string in V8
|
|
42
|
+
*
|
|
43
|
+
* V8 uses 2 bytes per character (UTF-16) plus object overhead.
|
|
44
|
+
* For strings > 12 chars, V8 uses heap allocation with ~40 bytes overhead.
|
|
45
|
+
* For short strings, V8 may use inline storage.
|
|
46
|
+
*
|
|
47
|
+
* @param str String to estimate size for
|
|
48
|
+
* @returns Estimated size in bytes
|
|
49
|
+
*/
|
|
50
|
+
function estimateStringSize(str) {
|
|
51
|
+
// UTF-16 encoding: 2 bytes per character
|
|
52
|
+
// Object overhead: ~40 bytes for heap strings
|
|
53
|
+
// Short strings (<= 12 chars) may be inline, but we still count them
|
|
54
|
+
return str.length * 2 + 40;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Estimate the memory size of an array in V8
|
|
58
|
+
*
|
|
59
|
+
* Arrays have object overhead plus pointers to elements.
|
|
60
|
+
* We estimate 8 bytes per element (pointer size on 64-bit).
|
|
61
|
+
*
|
|
62
|
+
* @param length Array length
|
|
63
|
+
* @param elementEstimate Optional per-element size estimate
|
|
64
|
+
* @returns Estimated size in bytes
|
|
65
|
+
*/
|
|
66
|
+
function estimateArraySize(length, elementEstimate = 8) {
|
|
67
|
+
// Array object overhead: ~32 bytes
|
|
68
|
+
// Each element: pointer (8 bytes) + element overhead
|
|
69
|
+
return 32 + length * elementEstimate;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Estimate the memory size of an object in V8
|
|
73
|
+
*
|
|
74
|
+
* Objects have base overhead plus per-property storage.
|
|
75
|
+
*
|
|
76
|
+
* @param propertyCount Number of properties
|
|
77
|
+
* @returns Estimated size in bytes
|
|
78
|
+
*/
|
|
79
|
+
function estimateObjectSize(propertyCount) {
|
|
80
|
+
// Object overhead: ~56 bytes (header, hidden class pointer, etc.)
|
|
81
|
+
// Per property: ~32 bytes (key, value, descriptor)
|
|
82
|
+
return 56 + propertyCount * 32;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Memory tracker for VM execution
|
|
86
|
+
*
|
|
87
|
+
* Tracks allocations via instrumented code and enforces memory limits.
|
|
88
|
+
* Use `start()` before execution and `getSnapshot()` after for stats.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const tracker = new MemoryTracker({ memoryLimit: 10 * 1024 * 1024 }); // 10MB
|
|
93
|
+
* tracker.start();
|
|
94
|
+
*
|
|
95
|
+
* // During execution, track allocations:
|
|
96
|
+
* tracker.track(estimateStringSize(someString));
|
|
97
|
+
*
|
|
98
|
+
* // After execution, get stats:
|
|
99
|
+
* const snapshot = tracker.getSnapshot();
|
|
100
|
+
* console.log(`Peak memory: ${snapshot.peakTrackedBytes} bytes`);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
class MemoryTracker {
|
|
104
|
+
trackedBytes = 0;
|
|
105
|
+
peakTrackedBytes = 0;
|
|
106
|
+
allocationCount = 0;
|
|
107
|
+
config;
|
|
108
|
+
constructor(config = {}) {
|
|
109
|
+
this.config = {
|
|
110
|
+
memoryLimit: config.memoryLimit ?? 0,
|
|
111
|
+
trackStrings: config.trackStrings ?? true,
|
|
112
|
+
trackArrays: config.trackArrays ?? true,
|
|
113
|
+
trackObjects: config.trackObjects ?? true,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Start tracking (resets all counters)
|
|
118
|
+
*/
|
|
119
|
+
start() {
|
|
120
|
+
this.trackedBytes = 0;
|
|
121
|
+
this.peakTrackedBytes = 0;
|
|
122
|
+
this.allocationCount = 0;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Track an allocation
|
|
126
|
+
*
|
|
127
|
+
* @param bytes Number of bytes allocated
|
|
128
|
+
* @throws {MemoryLimitError} If memory limit is exceeded
|
|
129
|
+
*/
|
|
130
|
+
track(bytes) {
|
|
131
|
+
if (bytes <= 0)
|
|
132
|
+
return;
|
|
133
|
+
this.trackedBytes += bytes;
|
|
134
|
+
this.allocationCount++;
|
|
135
|
+
if (this.trackedBytes > this.peakTrackedBytes) {
|
|
136
|
+
this.peakTrackedBytes = this.trackedBytes;
|
|
137
|
+
}
|
|
138
|
+
// Check limit (0 = unlimited)
|
|
139
|
+
if (this.config.memoryLimit > 0 && this.trackedBytes > this.config.memoryLimit) {
|
|
140
|
+
throw new MemoryLimitError(`Memory limit exceeded: ${this.formatBytes(this.trackedBytes)} used > ${this.formatBytes(this.config.memoryLimit)} limit`, this.trackedBytes, this.config.memoryLimit);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Track a string allocation
|
|
145
|
+
*
|
|
146
|
+
* @param str String that was allocated
|
|
147
|
+
* @throws {MemoryLimitError} If memory limit is exceeded
|
|
148
|
+
*/
|
|
149
|
+
trackString(str) {
|
|
150
|
+
if (!this.config.trackStrings)
|
|
151
|
+
return;
|
|
152
|
+
this.track(estimateStringSize(str));
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Track an array allocation
|
|
156
|
+
*
|
|
157
|
+
* @param length Array length
|
|
158
|
+
* @param elementEstimate Optional per-element size estimate
|
|
159
|
+
* @throws {MemoryLimitError} If memory limit is exceeded
|
|
160
|
+
*/
|
|
161
|
+
trackArray(length, elementEstimate) {
|
|
162
|
+
if (!this.config.trackArrays)
|
|
163
|
+
return;
|
|
164
|
+
this.track(estimateArraySize(length, elementEstimate));
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Track an object allocation
|
|
168
|
+
*
|
|
169
|
+
* @param propertyCount Number of properties
|
|
170
|
+
* @throws {MemoryLimitError} If memory limit is exceeded
|
|
171
|
+
*/
|
|
172
|
+
trackObject(propertyCount) {
|
|
173
|
+
if (!this.config.trackObjects)
|
|
174
|
+
return;
|
|
175
|
+
this.track(estimateObjectSize(propertyCount));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Release tracked memory (for GC simulation)
|
|
179
|
+
*
|
|
180
|
+
* @param bytes Number of bytes to release
|
|
181
|
+
*/
|
|
182
|
+
release(bytes) {
|
|
183
|
+
this.trackedBytes = Math.max(0, this.trackedBytes - bytes);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Get current memory usage snapshot
|
|
187
|
+
*
|
|
188
|
+
* @returns Snapshot of memory usage
|
|
189
|
+
*/
|
|
190
|
+
getSnapshot() {
|
|
191
|
+
return {
|
|
192
|
+
trackedBytes: this.trackedBytes,
|
|
193
|
+
peakTrackedBytes: this.peakTrackedBytes,
|
|
194
|
+
allocationCount: this.allocationCount,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get configured memory limit
|
|
199
|
+
*
|
|
200
|
+
* @returns Memory limit in bytes (0 = unlimited)
|
|
201
|
+
*/
|
|
202
|
+
getLimit() {
|
|
203
|
+
return this.config.memoryLimit;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Check if tracking is enabled
|
|
207
|
+
*
|
|
208
|
+
* @returns True if any tracking is enabled
|
|
209
|
+
*/
|
|
210
|
+
isEnabled() {
|
|
211
|
+
return this.config.trackStrings || this.config.trackArrays || this.config.trackObjects;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Format bytes as human-readable string
|
|
215
|
+
*
|
|
216
|
+
* @param bytes Number of bytes
|
|
217
|
+
* @returns Formatted string (e.g., "10.5MB")
|
|
218
|
+
*/
|
|
219
|
+
formatBytes(bytes) {
|
|
220
|
+
if (bytes >= 1024 * 1024 * 1024)
|
|
221
|
+
return `${(bytes / 1024 / 1024 / 1024).toFixed(2)}GB`;
|
|
222
|
+
if (bytes >= 1024 * 1024)
|
|
223
|
+
return `${(bytes / 1024 / 1024).toFixed(2)}MB`;
|
|
224
|
+
if (bytes >= 1024)
|
|
225
|
+
return `${(bytes / 1024).toFixed(2)}KB`;
|
|
226
|
+
return `${bytes}B`;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
exports.MemoryTracker = MemoryTracker;
|
|
230
|
+
//# sourceMappingURL=memory-tracker.js.map
|
|
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,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;QACvF,IAAI,KAAK,IAAI,IAAI,GAAG,IAAI;YAAE,OAAO,GAAG,CAAC,KAAK,GAAG,IAAI,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;QACzE,IAAI,KAAK,IAAI,IAAI;YAAE,OAAO,GAAG,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;QAC3D,OAAO,GAAG,KAAK,GAAG,CAAC;IACrB,CAAC;CACF;AA1ID,sCA0IC","sourcesContent":["/**\n * Memory Tracker for VM Execution\n *\n * Provides allocation tracking and memory limit enforcement for the VM adapter.\n * Since Node.js vm module doesn't support native memory limits, this tracker\n * monitors allocations via instrumented code and proxy wrappers.\n *\n * @packageDocumentation\n */\n\n/**\n * Configuration for memory tracking\n */\nexport interface MemoryTrackerConfig {\n /** Maximum total bytes allowed (0 = unlimited) */\n memoryLimit: number;\n /** Enable string size tracking */\n trackStrings: boolean;\n /** Enable array size tracking */\n trackArrays: boolean;\n /** Enable object property tracking */\n trackObjects: boolean;\n}\n\n/**\n * Snapshot of memory usage at a point in time\n */\nexport interface MemoryUsageSnapshot {\n /** Current tracked bytes (sum of estimated allocation sizes) */\n trackedBytes: number;\n /** Peak tracked bytes during execution */\n peakTrackedBytes: number;\n /** Number of allocation operations tracked */\n allocationCount: number;\n}\n\n/**\n * Error thrown when memory limit is exceeded\n */\nexport class MemoryLimitError extends Error {\n /** Error code for identification */\n public readonly code = 'MEMORY_LIMIT_EXCEEDED';\n\n constructor(\n message: string,\n /** Bytes used when limit was exceeded */\n public readonly usedBytes: number,\n /** Configured limit in bytes */\n public readonly limitBytes: number,\n ) {\n super(message);\n this.name = 'MemoryLimitError';\n // Maintain proper stack trace in V8\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, MemoryLimitError);\n }\n }\n}\n\n/**\n * Estimate the memory size of a string in V8\n *\n * V8 uses 2 bytes per character (UTF-16) plus object overhead.\n * For strings > 12 chars, V8 uses heap allocation with ~40 bytes overhead.\n * For short strings, V8 may use inline storage.\n *\n * @param str String to estimate size for\n * @returns Estimated size in bytes\n */\nexport function estimateStringSize(str: string): number {\n // UTF-16 encoding: 2 bytes per character\n // Object overhead: ~40 bytes for heap strings\n // Short strings (<= 12 chars) may be inline, but we still count them\n return str.length * 2 + 40;\n}\n\n/**\n * Estimate the memory size of an array in V8\n *\n * Arrays have object overhead plus pointers to elements.\n * We estimate 8 bytes per element (pointer size on 64-bit).\n *\n * @param length Array length\n * @param elementEstimate Optional per-element size estimate\n * @returns Estimated size in bytes\n */\nexport function estimateArraySize(length: number, elementEstimate = 8): number {\n // Array object overhead: ~32 bytes\n // Each element: pointer (8 bytes) + element overhead\n return 32 + length * elementEstimate;\n}\n\n/**\n * Estimate the memory size of an object in V8\n *\n * Objects have base overhead plus per-property storage.\n *\n * @param propertyCount Number of properties\n * @returns Estimated size in bytes\n */\nexport function estimateObjectSize(propertyCount: number): number {\n // Object overhead: ~56 bytes (header, hidden class pointer, etc.)\n // Per property: ~32 bytes (key, value, descriptor)\n return 56 + propertyCount * 32;\n}\n\n/**\n * Memory tracker for VM execution\n *\n * Tracks allocations via instrumented code and enforces memory limits.\n * Use `start()` before execution and `getSnapshot()` after for stats.\n *\n * @example\n * ```typescript\n * const tracker = new MemoryTracker({ memoryLimit: 10 * 1024 * 1024 }); // 10MB\n * tracker.start();\n *\n * // During execution, track allocations:\n * tracker.track(estimateStringSize(someString));\n *\n * // After execution, get stats:\n * const snapshot = tracker.getSnapshot();\n * console.log(`Peak memory: ${snapshot.peakTrackedBytes} bytes`);\n * ```\n */\nexport class MemoryTracker {\n private trackedBytes = 0;\n private peakTrackedBytes = 0;\n private allocationCount = 0;\n private readonly config: MemoryTrackerConfig;\n\n constructor(config: Partial<MemoryTrackerConfig> = {}) {\n this.config = {\n memoryLimit: config.memoryLimit ?? 0,\n trackStrings: config.trackStrings ?? true,\n trackArrays: config.trackArrays ?? true,\n trackObjects: config.trackObjects ?? true,\n };\n }\n\n /**\n * Start tracking (resets all counters)\n */\n start(): void {\n this.trackedBytes = 0;\n this.peakTrackedBytes = 0;\n this.allocationCount = 0;\n }\n\n /**\n * Track an allocation\n *\n * @param bytes Number of bytes allocated\n * @throws {MemoryLimitError} If memory limit is exceeded\n */\n track(bytes: number): void {\n if (bytes <= 0) return;\n\n this.trackedBytes += bytes;\n this.allocationCount++;\n\n if (this.trackedBytes > this.peakTrackedBytes) {\n this.peakTrackedBytes = this.trackedBytes;\n }\n\n // Check limit (0 = unlimited)\n if (this.config.memoryLimit > 0 && this.trackedBytes > this.config.memoryLimit) {\n throw new MemoryLimitError(\n `Memory limit exceeded: ${this.formatBytes(this.trackedBytes)} used > ${this.formatBytes(\n this.config.memoryLimit,\n )} limit`,\n this.trackedBytes,\n this.config.memoryLimit,\n );\n }\n }\n\n /**\n * Track a string allocation\n *\n * @param str String that was allocated\n * @throws {MemoryLimitError} If memory limit is exceeded\n */\n trackString(str: string): void {\n if (!this.config.trackStrings) return;\n this.track(estimateStringSize(str));\n }\n\n /**\n * Track an array allocation\n *\n * @param length Array length\n * @param elementEstimate Optional per-element size estimate\n * @throws {MemoryLimitError} If memory limit is exceeded\n */\n trackArray(length: number, elementEstimate?: number): void {\n if (!this.config.trackArrays) return;\n this.track(estimateArraySize(length, elementEstimate));\n }\n\n /**\n * Track an object allocation\n *\n * @param propertyCount Number of properties\n * @throws {MemoryLimitError} If memory limit is exceeded\n */\n trackObject(propertyCount: number): void {\n if (!this.config.trackObjects) return;\n this.track(estimateObjectSize(propertyCount));\n }\n\n /**\n * Release tracked memory (for GC simulation)\n *\n * @param bytes Number of bytes to release\n */\n release(bytes: number): void {\n this.trackedBytes = Math.max(0, this.trackedBytes - bytes);\n }\n\n /**\n * Get current memory usage snapshot\n *\n * @returns Snapshot of memory usage\n */\n getSnapshot(): MemoryUsageSnapshot {\n return {\n trackedBytes: this.trackedBytes,\n peakTrackedBytes: this.peakTrackedBytes,\n allocationCount: this.allocationCount,\n };\n }\n\n /**\n * Get configured memory limit\n *\n * @returns Memory limit in bytes (0 = unlimited)\n */\n getLimit(): number {\n return this.config.memoryLimit;\n }\n\n /**\n * Check if tracking is enabled\n *\n * @returns True if any tracking is enabled\n */\n isEnabled(): boolean {\n return this.config.trackStrings || this.config.trackArrays || this.config.trackObjects;\n }\n\n /**\n * Format bytes as human-readable string\n *\n * @param bytes Number of bytes\n * @returns Formatted string (e.g., \"10.5MB\")\n */\n private formatBytes(bytes: number): string {\n if (bytes >= 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024 / 1024).toFixed(2)}GB`;\n if (bytes >= 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(2)}MB`;\n if (bytes >= 1024) return `${(bytes / 1024).toFixed(2)}KB`;\n return `${bytes}B`;\n }\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe Error Utilities
|
|
3
|
+
*
|
|
4
|
+
* SECURITY: Any Error object created in the host realm and exposed to sandbox code
|
|
5
|
+
* must not allow reaching the host Function constructor via prototype-chain walks.
|
|
6
|
+
*
|
|
7
|
+
* Threat model:
|
|
8
|
+
* - Sandbox code can intentionally trigger host-side failures (e.g. tool calls, proxy traps)
|
|
9
|
+
* - If a host Error crosses the boundary, attackers can climb:
|
|
10
|
+
* hostError -> Error.prototype -> Error -> Function -> new Function('...')()
|
|
11
|
+
* and execute arbitrary host code (e.g. `process.env`, command execution).
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Create an Error instance whose prototype chain is severed (actual [[Prototype]]),
|
|
15
|
+
* preventing prototype-chain escape to host constructors.
|
|
16
|
+
*/
|
|
17
|
+
export declare function createSafeError(message: string, name?: string): Error;
|
|
18
|
+
export declare function createSafeTypeError(message: string): Error;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Safe Error Utilities
|
|
4
|
+
*
|
|
5
|
+
* SECURITY: Any Error object created in the host realm and exposed to sandbox code
|
|
6
|
+
* must not allow reaching the host Function constructor via prototype-chain walks.
|
|
7
|
+
*
|
|
8
|
+
* Threat model:
|
|
9
|
+
* - Sandbox code can intentionally trigger host-side failures (e.g. tool calls, proxy traps)
|
|
10
|
+
* - If a host Error crosses the boundary, attackers can climb:
|
|
11
|
+
* hostError -> Error.prototype -> Error -> Function -> new Function('...')()
|
|
12
|
+
* and execute arbitrary host code (e.g. `process.env`, command execution).
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.createSafeError = createSafeError;
|
|
16
|
+
exports.createSafeTypeError = createSafeTypeError;
|
|
17
|
+
/**
|
|
18
|
+
* Create an Error instance whose prototype chain is severed (actual [[Prototype]]),
|
|
19
|
+
* preventing prototype-chain escape to host constructors.
|
|
20
|
+
*/
|
|
21
|
+
function createSafeError(message, name = 'Error') {
|
|
22
|
+
const error = new Error(message);
|
|
23
|
+
error.name = name;
|
|
24
|
+
// Null-prototype "constructor" object to break `err.constructor.constructor` chains.
|
|
25
|
+
const SafeConstructor = Object.create(null);
|
|
26
|
+
Object.defineProperties(SafeConstructor, {
|
|
27
|
+
constructor: {
|
|
28
|
+
value: SafeConstructor,
|
|
29
|
+
writable: false,
|
|
30
|
+
enumerable: false,
|
|
31
|
+
configurable: false,
|
|
32
|
+
},
|
|
33
|
+
prototype: {
|
|
34
|
+
value: null,
|
|
35
|
+
writable: false,
|
|
36
|
+
enumerable: false,
|
|
37
|
+
configurable: false,
|
|
38
|
+
},
|
|
39
|
+
name: {
|
|
40
|
+
value: 'SafeError',
|
|
41
|
+
writable: false,
|
|
42
|
+
enumerable: false,
|
|
43
|
+
configurable: false,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
Object.freeze(SafeConstructor);
|
|
47
|
+
// CRITICAL: sever the *actual* prototype chain (native getters / Object.getPrototypeOf).
|
|
48
|
+
// A shadowing `__proto__` data property is not sufficient.
|
|
49
|
+
Object.setPrototypeOf(error, null);
|
|
50
|
+
// Provide safe shadow properties for common escape paths / ergonomics.
|
|
51
|
+
Object.defineProperty(error, 'constructor', {
|
|
52
|
+
value: SafeConstructor,
|
|
53
|
+
writable: false,
|
|
54
|
+
enumerable: false,
|
|
55
|
+
configurable: false,
|
|
56
|
+
});
|
|
57
|
+
Object.defineProperty(error, '__proto__', {
|
|
58
|
+
value: null,
|
|
59
|
+
writable: false,
|
|
60
|
+
enumerable: false,
|
|
61
|
+
configurable: false,
|
|
62
|
+
});
|
|
63
|
+
// Do not leak stack traces from host internals.
|
|
64
|
+
Object.defineProperty(error, 'stack', {
|
|
65
|
+
value: undefined,
|
|
66
|
+
writable: false,
|
|
67
|
+
enumerable: false,
|
|
68
|
+
configurable: false,
|
|
69
|
+
});
|
|
70
|
+
Object.freeze(error);
|
|
71
|
+
return error;
|
|
72
|
+
}
|
|
73
|
+
function createSafeTypeError(message) {
|
|
74
|
+
return createSafeError(message, 'TypeError');
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=safe-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safe-error.js","sourceRoot":"","sources":["../../src/safe-error.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;AAMH,0CAyDC;AAED,kDAEC;AAjED;;;GAGG;AACH,SAAgB,eAAe,CAAC,OAAe,EAAE,IAAI,GAAG,OAAO;IAC7D,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IACjC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC;IAElB,qFAAqF;IACrF,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC5C,MAAM,CAAC,gBAAgB,CAAC,eAAe,EAAE;QACvC,WAAW,EAAE;YACX,KAAK,EAAE,eAAe;YACtB,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host realm and exposed to sandbox code\n * must not allow reaching the host Function constructor via prototype-chain walks.\n *\n * Threat model:\n * - Sandbox code can intentionally trigger host-side failures (e.g. tool calls, proxy traps)\n * - If a host Error crosses the boundary, attackers can climb:\n * hostError -> Error.prototype -> Error -> Function -> new Function('...')()\n * and execute arbitrary host code (e.g. `process.env`, command execution).\n */\n\n/**\n * Create an Error instance whose prototype chain is severed (actual [[Prototype]]),\n * preventing prototype-chain escape to host constructors.\n */\nexport function createSafeError(message: string, name = 'Error'): Error {\n const error = new Error(message);\n error.name = name;\n\n // Null-prototype \"constructor\" object to break `err.constructor.constructor` chains.\n const SafeConstructor = Object.create(null);\n Object.defineProperties(SafeConstructor, {\n constructor: {\n value: SafeConstructor,\n writable: false,\n enumerable: false,\n configurable: false,\n },\n prototype: {\n value: null,\n writable: false,\n enumerable: false,\n configurable: false,\n },\n name: {\n value: 'SafeError',\n writable: false,\n enumerable: false,\n configurable: false,\n },\n });\n Object.freeze(SafeConstructor);\n\n // CRITICAL: sever the *actual* prototype chain (native getters / Object.getPrototypeOf).\n // A shadowing `__proto__` data property is not sufficient.\n Object.setPrototypeOf(error, null);\n\n // Provide safe shadow properties for common escape paths / ergonomics.\n Object.defineProperty(error, 'constructor', {\n value: SafeConstructor,\n writable: false,\n enumerable: false,\n configurable: false,\n });\n\n Object.defineProperty(error, '__proto__', {\n value: null,\n writable: false,\n enumerable: false,\n configurable: false,\n });\n\n // Do not leak stack traces from host internals.\n Object.defineProperty(error, 'stack', {\n value: undefined,\n writable: false,\n enumerable: false,\n configurable: false,\n });\n\n Object.freeze(error);\n return error;\n}\n\nexport function createSafeTypeError(message: string): Error {\n return createSafeError(message, 'TypeError');\n}\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe Runtime Wrappers
|
|
3
|
+
*
|
|
4
|
+
* These functions are injected into the AgentScript sandbox to provide
|
|
5
|
+
* safe implementations of tool calls, loops, and other operations.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import type { ExecutionContext, SecureProxyLevelConfig } from './types';
|
|
10
|
+
import { ReferenceSidecar } from './sidecar/reference-sidecar';
|
|
11
|
+
import { ReferenceConfig } from './sidecar/reference-config';
|
|
12
|
+
import { MemoryTracker } from './memory-tracker';
|
|
13
|
+
/**
|
|
14
|
+
* Options for safe runtime creation
|
|
15
|
+
*/
|
|
16
|
+
export interface SafeRuntimeOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Reference sidecar for pass-by-reference support
|
|
19
|
+
* If not provided, reference resolution is disabled
|
|
20
|
+
*/
|
|
21
|
+
sidecar?: ReferenceSidecar;
|
|
22
|
+
/**
|
|
23
|
+
* Reference configuration for resolver
|
|
24
|
+
* Required when sidecar is provided
|
|
25
|
+
*/
|
|
26
|
+
referenceConfig?: ReferenceConfig;
|
|
27
|
+
/**
|
|
28
|
+
* Secure proxy configuration from security level
|
|
29
|
+
* Controls which properties are blocked by the proxy
|
|
30
|
+
*/
|
|
31
|
+
secureProxyConfig?: SecureProxyLevelConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Memory tracker for allocation monitoring
|
|
34
|
+
* If provided, String/Array constructors are wrapped to track allocations
|
|
35
|
+
* and enforce memory limits
|
|
36
|
+
*/
|
|
37
|
+
memoryTracker?: MemoryTracker;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create safe runtime context with all __safe_* functions
|
|
41
|
+
*
|
|
42
|
+
* @param context Execution context
|
|
43
|
+
* @param options Optional runtime options (sidecar, referenceConfig)
|
|
44
|
+
* @returns Object containing all safe runtime functions
|
|
45
|
+
*/
|
|
46
|
+
export declare function createSafeRuntime(context: ExecutionContext, options?: SafeRuntimeOptions): {
|
|
47
|
+
NaN: number;
|
|
48
|
+
Infinity: number;
|
|
49
|
+
undefined: undefined;
|
|
50
|
+
isNaN: typeof isNaN;
|
|
51
|
+
isFinite: typeof isFinite;
|
|
52
|
+
parseInt: typeof parseInt;
|
|
53
|
+
parseFloat: typeof parseFloat;
|
|
54
|
+
__safe_callTool: (toolName: string, args: Record<string, unknown>) => Promise<unknown>;
|
|
55
|
+
__safe_forOf: <T>(iterable: Iterable<T>) => Iterable<T>;
|
|
56
|
+
__safe_for: (init: () => void, test: () => boolean, update: () => void, body: () => void) => void;
|
|
57
|
+
__safe_while: (test: () => boolean, body: () => void) => void;
|
|
58
|
+
__safe_doWhile: (body: () => void, test: () => boolean) => void;
|
|
59
|
+
__safe_concat: (left: unknown, right: unknown) => unknown;
|
|
60
|
+
__safe_template: (quasis: string[], ...values: unknown[]) => unknown;
|
|
61
|
+
__safe_parallel: <T>(fns: Array<() => Promise<T>>, options?: {
|
|
62
|
+
maxConcurrency?: number;
|
|
63
|
+
}) => Promise<T[]>;
|
|
64
|
+
__maxIterations: number;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Serialize safe runtime code as a string for injection
|
|
68
|
+
* This is used by sandbox adapters that need to inject the runtime as code
|
|
69
|
+
*
|
|
70
|
+
* @returns JavaScript code string containing the safe runtime
|
|
71
|
+
*/
|
|
72
|
+
export declare function serializeSafeRuntime(): string;
|