@enclave-vm/core 2.10.0 → 2.10.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/adapters/worker-pool/worker-pool-adapter.d.ts +1 -0
- package/enclave.d.ts +1 -0
- package/esm/index.mjs +211 -12
- package/index.js +211 -12
- package/package.json +5 -5
- package/src/adapters/worker-pool/package.json +61 -0
- package/src/adapters/worker-pool/worker-script.js +759 -0
|
@@ -27,6 +27,7 @@ export declare class WorkerPoolAdapter implements SandboxAdapter {
|
|
|
27
27
|
private readonly rateLimiter;
|
|
28
28
|
private disposed;
|
|
29
29
|
private initialized;
|
|
30
|
+
private readonly boundMemoryExceededHandler;
|
|
30
31
|
private _totalExecutions;
|
|
31
32
|
private _successfulExecutions;
|
|
32
33
|
private _failedExecutions;
|
package/enclave.d.ts
CHANGED
package/esm/index.mjs
CHANGED
|
@@ -2913,8 +2913,110 @@ ${stackTraceHardeningCode}
|
|
|
2913
2913
|
// Remove dangerous globals from inner VM (AFTER memory patching)
|
|
2914
2914
|
${sanitizeContextCode}
|
|
2915
2915
|
|
|
2916
|
-
//
|
|
2917
|
-
//
|
|
2916
|
+
// ============================================================
|
|
2917
|
+
// PHASE 2 HARDENING: CVE-2023-29017 Stack Overflow Escape Defense
|
|
2918
|
+
// ============================================================
|
|
2919
|
+
//
|
|
2920
|
+
// CRITICAL: Hardening MUST run BEFORE prototypes are frozen!
|
|
2921
|
+
// Otherwise Object.defineProperty calls will silently fail on frozen prototypes.
|
|
2922
|
+
//
|
|
2923
|
+
// Defense-in-depth against prototype chain escape via stack overflow errors:
|
|
2924
|
+
// 1. __proto__ shadowing to block prototype chain traversal
|
|
2925
|
+
// 2. Legacy method blocking (__lookupGetter__, etc.)
|
|
2926
|
+
// 3. Error constructor wrapping to secure V8-generated errors
|
|
2927
|
+
// 4. Then freeze all prototypes to lock in hardening
|
|
2928
|
+
|
|
2929
|
+
// HARDENING 1: Shadow __proto__ on PARENT VM error prototypes with null
|
|
2930
|
+
// This prevents e["__proto__"]["__proto__"] traversal attacks by making
|
|
2931
|
+
// __proto__ return null instead of the actual prototype chain.
|
|
2932
|
+
// MUST run before freeze!
|
|
2933
|
+
(function() {
|
|
2934
|
+
var errorProtos = [
|
|
2935
|
+
Error.prototype,
|
|
2936
|
+
TypeError.prototype,
|
|
2937
|
+
RangeError.prototype,
|
|
2938
|
+
SyntaxError.prototype,
|
|
2939
|
+
ReferenceError.prototype,
|
|
2940
|
+
URIError.prototype,
|
|
2941
|
+
EvalError.prototype
|
|
2942
|
+
];
|
|
2943
|
+
for (var i = 0; i < errorProtos.length; i++) {
|
|
2944
|
+
// Shadow __proto__ with a null-returning getter
|
|
2945
|
+
// This blocks: error["__proto__"]["__proto__"]["__proto__"] attacks
|
|
2946
|
+
Object.defineProperty(errorProtos[i], '__proto__', {
|
|
2947
|
+
get: function() { return null; },
|
|
2948
|
+
set: function() { /* silently ignore */ },
|
|
2949
|
+
configurable: false,
|
|
2950
|
+
enumerable: false
|
|
2951
|
+
});
|
|
2952
|
+
}
|
|
2953
|
+
})();
|
|
2954
|
+
|
|
2955
|
+
// HARDENING 2: Shadow __proto__ in INNER VM error prototypes
|
|
2956
|
+
// MUST run before freeze!
|
|
2957
|
+
(function() {
|
|
2958
|
+
var shadowProtoCode =
|
|
2959
|
+
'(function() {' +
|
|
2960
|
+
' var errorProtos = [' +
|
|
2961
|
+
' Error.prototype,' +
|
|
2962
|
+
' TypeError.prototype,' +
|
|
2963
|
+
' RangeError.prototype,' +
|
|
2964
|
+
' SyntaxError.prototype,' +
|
|
2965
|
+
' ReferenceError.prototype,' +
|
|
2966
|
+
' URIError.prototype,' +
|
|
2967
|
+
' EvalError.prototype' +
|
|
2968
|
+
' ];' +
|
|
2969
|
+
' for (var i = 0; i < errorProtos.length; i++) {' +
|
|
2970
|
+
' Object.defineProperty(errorProtos[i], "__proto__", {' +
|
|
2971
|
+
' get: function() { return null; },' +
|
|
2972
|
+
' set: function() {},' +
|
|
2973
|
+
' configurable: false,' +
|
|
2974
|
+
' enumerable: false' +
|
|
2975
|
+
' });' +
|
|
2976
|
+
' }' +
|
|
2977
|
+
'})();';
|
|
2978
|
+
var shadowScript = new vm.Script(shadowProtoCode);
|
|
2979
|
+
shadowScript.runInContext(innerContext);
|
|
2980
|
+
})();
|
|
2981
|
+
|
|
2982
|
+
// HARDENING 3: Block legacy prototype manipulation methods in PARENT VM
|
|
2983
|
+
// These deprecated methods (__lookupGetter__, __lookupSetter__, __defineGetter__, __defineSetter__)
|
|
2984
|
+
// can be used to bypass frozen prototype protections.
|
|
2985
|
+
// MUST run before freeze!
|
|
2986
|
+
(function() {
|
|
2987
|
+
var legacyMethods = ['__lookupGetter__', '__lookupSetter__', '__defineGetter__', '__defineSetter__'];
|
|
2988
|
+
for (var i = 0; i < legacyMethods.length; i++) {
|
|
2989
|
+
Object.defineProperty(Object.prototype, legacyMethods[i], {
|
|
2990
|
+
value: function() { return undefined; },
|
|
2991
|
+
writable: false,
|
|
2992
|
+
configurable: false,
|
|
2993
|
+
enumerable: false
|
|
2994
|
+
});
|
|
2995
|
+
}
|
|
2996
|
+
})();
|
|
2997
|
+
|
|
2998
|
+
// HARDENING 4: Block legacy prototype methods in INNER VM
|
|
2999
|
+
// MUST run before freeze!
|
|
3000
|
+
(function() {
|
|
3001
|
+
var blockLegacyCode =
|
|
3002
|
+
'(function() {' +
|
|
3003
|
+
' var methods = ["__lookupGetter__", "__lookupSetter__", "__defineGetter__", "__defineSetter__"];' +
|
|
3004
|
+
' for (var i = 0; i < methods.length; i++) {' +
|
|
3005
|
+
' Object.defineProperty(Object.prototype, methods[i], {' +
|
|
3006
|
+
' value: function() { return undefined; },' +
|
|
3007
|
+
' writable: false,' +
|
|
3008
|
+
' configurable: false,' +
|
|
3009
|
+
' enumerable: false' +
|
|
3010
|
+
' });' +
|
|
3011
|
+
' }' +
|
|
3012
|
+
'})();';
|
|
3013
|
+
var blockScript = new vm.Script(blockLegacyCode);
|
|
3014
|
+
blockScript.runInContext(innerContext);
|
|
3015
|
+
})();
|
|
3016
|
+
|
|
3017
|
+
// ============================================================
|
|
3018
|
+
// PROTOTYPE FREEZING: Lock in hardening by freezing all prototypes
|
|
3019
|
+
// ============================================================
|
|
2918
3020
|
//
|
|
2919
3021
|
// We freeze prototypes in TWO places:
|
|
2920
3022
|
// 1. PARENT VM prototypes - because SafeObject.prototype = Object.prototype uses parent's prototype
|
|
@@ -2936,6 +3038,8 @@ ${stackTraceHardeningCode}
|
|
|
2936
3038
|
Object.freeze(RangeError.prototype);
|
|
2937
3039
|
Object.freeze(SyntaxError.prototype);
|
|
2938
3040
|
Object.freeze(ReferenceError.prototype);
|
|
3041
|
+
Object.freeze(URIError.prototype);
|
|
3042
|
+
Object.freeze(EvalError.prototype);
|
|
2939
3043
|
Object.freeze(Promise.prototype);
|
|
2940
3044
|
|
|
2941
3045
|
// Freeze INNER VM prototypes (used by literals like '', [], etc.)
|
|
@@ -2953,11 +3057,64 @@ ${stackTraceHardeningCode}
|
|
|
2953
3057
|
'Object.freeze(RangeError.prototype);' +
|
|
2954
3058
|
'Object.freeze(SyntaxError.prototype);' +
|
|
2955
3059
|
'Object.freeze(ReferenceError.prototype);' +
|
|
3060
|
+
'Object.freeze(URIError.prototype);' +
|
|
3061
|
+
'Object.freeze(EvalError.prototype);' +
|
|
2956
3062
|
'Object.freeze(Promise.prototype);';
|
|
2957
3063
|
var freezeScript = new vm.Script(freezeCode);
|
|
2958
3064
|
freezeScript.runInContext(innerContext);
|
|
2959
3065
|
})();
|
|
2960
3066
|
|
|
3067
|
+
// HARDENING 5: Wrap error constructors to freeze instances
|
|
3068
|
+
// V8-generated errors (like stack overflow RangeError) are created internally,
|
|
3069
|
+
// not via the constructor. But wrapping provides defense-in-depth for any
|
|
3070
|
+
// errors that ARE created via constructors.
|
|
3071
|
+
// NOTE: This can run after freeze because it replaces global constructors, not prototype properties
|
|
3072
|
+
(function() {
|
|
3073
|
+
var wrapErrorCode =
|
|
3074
|
+
'(function() {' +
|
|
3075
|
+
' var origError = Error;' +
|
|
3076
|
+
' var origTypeError = TypeError;' +
|
|
3077
|
+
' var origRangeError = RangeError;' +
|
|
3078
|
+
' var origSyntaxError = SyntaxError;' +
|
|
3079
|
+
' var origReferenceError = ReferenceError;' +
|
|
3080
|
+
' var origEvalError = EvalError;' +
|
|
3081
|
+
' var origURIError = URIError;' +
|
|
3082
|
+
' function wrapErrorCtor(OrigCtor, name) {' +
|
|
3083
|
+
' var WrappedCtor = function(msg) {' +
|
|
3084
|
+
' var err;' +
|
|
3085
|
+
' if (new.target) {' +
|
|
3086
|
+
' err = new OrigCtor(msg);' +
|
|
3087
|
+
' } else {' +
|
|
3088
|
+
' err = OrigCtor(msg);' +
|
|
3089
|
+
' }' +
|
|
3090
|
+
' try { Object.freeze(err); } catch (e) {}' +
|
|
3091
|
+
' return err;' +
|
|
3092
|
+
' };' +
|
|
3093
|
+
' WrappedCtor.prototype = OrigCtor.prototype;' +
|
|
3094
|
+
' try { Object.setPrototypeOf(WrappedCtor, OrigCtor); } catch (e) {}' +
|
|
3095
|
+
' try { Object.freeze(WrappedCtor); } catch (e) {}' +
|
|
3096
|
+
' return WrappedCtor;' +
|
|
3097
|
+
' }' +
|
|
3098
|
+
' try { Error = wrapErrorCtor(origError, "Error"); } catch (e) {}' +
|
|
3099
|
+
' try { TypeError = wrapErrorCtor(origTypeError, "TypeError"); } catch (e) {}' +
|
|
3100
|
+
' try { RangeError = wrapErrorCtor(origRangeError, "RangeError"); } catch (e) {}' +
|
|
3101
|
+
' try { SyntaxError = wrapErrorCtor(origSyntaxError, "SyntaxError"); } catch (e) {}' +
|
|
3102
|
+
' try { ReferenceError = wrapErrorCtor(origReferenceError, "ReferenceError"); } catch (e) {}' +
|
|
3103
|
+
' try { EvalError = wrapErrorCtor(origEvalError, "EvalError"); } catch (e) {}' +
|
|
3104
|
+
' try { URIError = wrapErrorCtor(origURIError, "URIError"); } catch (e) {}' +
|
|
3105
|
+
'})();';
|
|
3106
|
+
try {
|
|
3107
|
+
var wrapScript = new vm.Script(wrapErrorCode);
|
|
3108
|
+
wrapScript.runInContext(innerContext);
|
|
3109
|
+
} catch (e) { /* ignore */ }
|
|
3110
|
+
})();
|
|
3111
|
+
|
|
3112
|
+
// NOTE: HARDENING 6 and 7 (Object.getPrototypeOf / Reflect.getPrototypeOf wrappers)
|
|
3113
|
+
// were removed as they break legitimate internal functionality like __safe_template.
|
|
3114
|
+
// The existing defenses (prototype freezing, __proto__ shadowing, error constructor
|
|
3115
|
+
// wrapping, codeGeneration.strings=false) provide sufficient protection against
|
|
3116
|
+
// the CVE-2023-29017 attack pattern.
|
|
3117
|
+
|
|
2961
3118
|
// Inject safe runtime functions (non-writable, non-configurable)
|
|
2962
3119
|
// Wrap with secure proxy to block dangerous property access
|
|
2963
3120
|
var safeRuntime = {
|
|
@@ -3142,6 +3299,47 @@ ${stackTraceHardeningCode}
|
|
|
3142
3299
|
// Execute User Code
|
|
3143
3300
|
// ============================================================
|
|
3144
3301
|
|
|
3302
|
+
// HARDENING 4: Runtime prototype verification before user code execution
|
|
3303
|
+
// Verifies that critical prototypes are still frozen. If any have been
|
|
3304
|
+
// unfrozen (which shouldn't be possible but defense-in-depth), abort.
|
|
3305
|
+
(function() {
|
|
3306
|
+
var criticalPrototypes = [
|
|
3307
|
+
{ name: 'Object.prototype', proto: Object.prototype },
|
|
3308
|
+
{ name: 'Array.prototype', proto: Array.prototype },
|
|
3309
|
+
{ name: 'Function.prototype', proto: Function.prototype },
|
|
3310
|
+
{ name: 'Error.prototype', proto: Error.prototype },
|
|
3311
|
+
{ name: 'RangeError.prototype', proto: RangeError.prototype }
|
|
3312
|
+
];
|
|
3313
|
+
for (var i = 0; i < criticalPrototypes.length; i++) {
|
|
3314
|
+
var item = criticalPrototypes[i];
|
|
3315
|
+
if (!Object.isFrozen(item.proto)) {
|
|
3316
|
+
throw new Error('SECURITY VIOLATION: ' + item.name + ' is not frozen. Aborting execution.');
|
|
3317
|
+
}
|
|
3318
|
+
}
|
|
3319
|
+
})();
|
|
3320
|
+
|
|
3321
|
+
// HARDENING 5: Runtime verification in INNER VM
|
|
3322
|
+
(function() {
|
|
3323
|
+
var verifyCode =
|
|
3324
|
+
'(function() {' +
|
|
3325
|
+
' var protos = [Object.prototype, Array.prototype, Error.prototype, RangeError.prototype];' +
|
|
3326
|
+
' for (var i = 0; i < protos.length; i++) {' +
|
|
3327
|
+
' if (!Object.isFrozen(protos[i])) {' +
|
|
3328
|
+
' throw new Error("SECURITY VIOLATION: Inner VM prototype not frozen");' +
|
|
3329
|
+
' }' +
|
|
3330
|
+
' }' +
|
|
3331
|
+
'})();';
|
|
3332
|
+
try {
|
|
3333
|
+
var verifyScript = new vm.Script(verifyCode);
|
|
3334
|
+
verifyScript.runInContext(innerContext);
|
|
3335
|
+
} catch (e) {
|
|
3336
|
+
if (e.message && e.message.indexOf('SECURITY VIOLATION') >= 0) {
|
|
3337
|
+
throw e;
|
|
3338
|
+
}
|
|
3339
|
+
/* ignore other errors */
|
|
3340
|
+
}
|
|
3341
|
+
})();
|
|
3342
|
+
|
|
3145
3343
|
var userCode = ${JSON.stringify(userCode)};
|
|
3146
3344
|
// IMPORTANT: For stack overflow errors (RangeError: Maximum call stack size exceeded),
|
|
3147
3345
|
// Node/V8 may ignore Error.prepareStackTrace when it was installed by a different Script
|
|
@@ -5351,12 +5549,7 @@ var init_vm_adapter = __esm({
|
|
|
5351
5549
|
}
|
|
5352
5550
|
const _protectedSandbox = createProtectedSandbox(baseSandbox);
|
|
5353
5551
|
this.context = baseSandbox;
|
|
5354
|
-
const wrappedCode =
|
|
5355
|
-
(async () => {
|
|
5356
|
-
${code}
|
|
5357
|
-
return typeof __ag_main === 'function' ? await __ag_main() : undefined;
|
|
5358
|
-
})();
|
|
5359
|
-
`;
|
|
5552
|
+
const wrappedCode = "(async () => {\n" + code + '\nreturn typeof __ag_main === "function" ? await __ag_main() : undefined;\n})();';
|
|
5360
5553
|
const script = new vm3.Script(wrappedCode, {
|
|
5361
5554
|
filename: "agentscript.js"
|
|
5362
5555
|
});
|
|
@@ -6576,6 +6769,8 @@ var init_worker_pool_adapter = __esm({
|
|
|
6576
6769
|
rateLimiter;
|
|
6577
6770
|
disposed = false;
|
|
6578
6771
|
initialized = false;
|
|
6772
|
+
// Bound event handler for proper cleanup
|
|
6773
|
+
boundMemoryExceededHandler = this.handleMemoryExceeded.bind(this);
|
|
6579
6774
|
// Metrics
|
|
6580
6775
|
_totalExecutions = 0;
|
|
6581
6776
|
_successfulExecutions = 0;
|
|
@@ -6598,7 +6793,7 @@ var init_worker_pool_adapter = __esm({
|
|
|
6598
6793
|
this.slots
|
|
6599
6794
|
);
|
|
6600
6795
|
this.rateLimiter = createRateLimiter(this.config.maxMessagesPerSecond);
|
|
6601
|
-
this.memoryMonitor.on("memoryExceeded", this.
|
|
6796
|
+
this.memoryMonitor.on("memoryExceeded", this.boundMemoryExceededHandler);
|
|
6602
6797
|
}
|
|
6603
6798
|
/**
|
|
6604
6799
|
* Initialize the worker pool
|
|
@@ -6663,6 +6858,7 @@ var init_worker_pool_adapter = __esm({
|
|
|
6663
6858
|
return;
|
|
6664
6859
|
}
|
|
6665
6860
|
this.disposed = true;
|
|
6861
|
+
this.memoryMonitor.off("memoryExceeded", this.boundMemoryExceededHandler);
|
|
6666
6862
|
this.memoryMonitor.stop();
|
|
6667
6863
|
this.executionQueue.clear();
|
|
6668
6864
|
const terminatePromises = [];
|
|
@@ -9778,6 +9974,7 @@ var Enclave = class {
|
|
|
9778
9974
|
referenceConfig;
|
|
9779
9975
|
scoringGate;
|
|
9780
9976
|
doubleVmConfig;
|
|
9977
|
+
customGlobalNames;
|
|
9781
9978
|
adapter;
|
|
9782
9979
|
constructor(options = {}) {
|
|
9783
9980
|
this.securityLevel = options.securityLevel ?? DEFAULT_SECURITY_LEVEL;
|
|
@@ -9849,8 +10046,8 @@ var Enclave = class {
|
|
|
9849
10046
|
secureProxyConfig: securityConfig.secureProxyConfig,
|
|
9850
10047
|
globals: initialGlobals
|
|
9851
10048
|
};
|
|
9852
|
-
|
|
9853
|
-
const customAllowedGlobals = customGlobalNames.flatMap((name) => [name, `__safe_${name}`]);
|
|
10049
|
+
this.customGlobalNames = Object.keys(initialGlobals);
|
|
10050
|
+
const customAllowedGlobals = this.customGlobalNames.flatMap((name) => [name, `__safe_${name}`]);
|
|
9854
10051
|
this.validator = this.createValidator(presetName, customAllowedGlobals);
|
|
9855
10052
|
this.validateCode = options.validate !== false;
|
|
9856
10053
|
this.transformCode = options.transform !== false;
|
|
@@ -9914,7 +10111,9 @@ var Enclave = class {
|
|
|
9914
10111
|
transformedCode = transformAgentScript(code, {
|
|
9915
10112
|
wrapInMain: needsWrapping,
|
|
9916
10113
|
transformCallTool: true,
|
|
9917
|
-
transformLoops: true
|
|
10114
|
+
transformLoops: true,
|
|
10115
|
+
// Pass custom global names so they aren't transformed to __safe_ prefix
|
|
10116
|
+
additionalIdentifiers: this.customGlobalNames
|
|
9918
10117
|
});
|
|
9919
10118
|
}
|
|
9920
10119
|
if (this.validateCode) {
|
package/index.js
CHANGED
|
@@ -2935,8 +2935,110 @@ ${stackTraceHardeningCode}
|
|
|
2935
2935
|
// Remove dangerous globals from inner VM (AFTER memory patching)
|
|
2936
2936
|
${sanitizeContextCode}
|
|
2937
2937
|
|
|
2938
|
-
//
|
|
2939
|
-
//
|
|
2938
|
+
// ============================================================
|
|
2939
|
+
// PHASE 2 HARDENING: CVE-2023-29017 Stack Overflow Escape Defense
|
|
2940
|
+
// ============================================================
|
|
2941
|
+
//
|
|
2942
|
+
// CRITICAL: Hardening MUST run BEFORE prototypes are frozen!
|
|
2943
|
+
// Otherwise Object.defineProperty calls will silently fail on frozen prototypes.
|
|
2944
|
+
//
|
|
2945
|
+
// Defense-in-depth against prototype chain escape via stack overflow errors:
|
|
2946
|
+
// 1. __proto__ shadowing to block prototype chain traversal
|
|
2947
|
+
// 2. Legacy method blocking (__lookupGetter__, etc.)
|
|
2948
|
+
// 3. Error constructor wrapping to secure V8-generated errors
|
|
2949
|
+
// 4. Then freeze all prototypes to lock in hardening
|
|
2950
|
+
|
|
2951
|
+
// HARDENING 1: Shadow __proto__ on PARENT VM error prototypes with null
|
|
2952
|
+
// This prevents e["__proto__"]["__proto__"] traversal attacks by making
|
|
2953
|
+
// __proto__ return null instead of the actual prototype chain.
|
|
2954
|
+
// MUST run before freeze!
|
|
2955
|
+
(function() {
|
|
2956
|
+
var errorProtos = [
|
|
2957
|
+
Error.prototype,
|
|
2958
|
+
TypeError.prototype,
|
|
2959
|
+
RangeError.prototype,
|
|
2960
|
+
SyntaxError.prototype,
|
|
2961
|
+
ReferenceError.prototype,
|
|
2962
|
+
URIError.prototype,
|
|
2963
|
+
EvalError.prototype
|
|
2964
|
+
];
|
|
2965
|
+
for (var i = 0; i < errorProtos.length; i++) {
|
|
2966
|
+
// Shadow __proto__ with a null-returning getter
|
|
2967
|
+
// This blocks: error["__proto__"]["__proto__"]["__proto__"] attacks
|
|
2968
|
+
Object.defineProperty(errorProtos[i], '__proto__', {
|
|
2969
|
+
get: function() { return null; },
|
|
2970
|
+
set: function() { /* silently ignore */ },
|
|
2971
|
+
configurable: false,
|
|
2972
|
+
enumerable: false
|
|
2973
|
+
});
|
|
2974
|
+
}
|
|
2975
|
+
})();
|
|
2976
|
+
|
|
2977
|
+
// HARDENING 2: Shadow __proto__ in INNER VM error prototypes
|
|
2978
|
+
// MUST run before freeze!
|
|
2979
|
+
(function() {
|
|
2980
|
+
var shadowProtoCode =
|
|
2981
|
+
'(function() {' +
|
|
2982
|
+
' var errorProtos = [' +
|
|
2983
|
+
' Error.prototype,' +
|
|
2984
|
+
' TypeError.prototype,' +
|
|
2985
|
+
' RangeError.prototype,' +
|
|
2986
|
+
' SyntaxError.prototype,' +
|
|
2987
|
+
' ReferenceError.prototype,' +
|
|
2988
|
+
' URIError.prototype,' +
|
|
2989
|
+
' EvalError.prototype' +
|
|
2990
|
+
' ];' +
|
|
2991
|
+
' for (var i = 0; i < errorProtos.length; i++) {' +
|
|
2992
|
+
' Object.defineProperty(errorProtos[i], "__proto__", {' +
|
|
2993
|
+
' get: function() { return null; },' +
|
|
2994
|
+
' set: function() {},' +
|
|
2995
|
+
' configurable: false,' +
|
|
2996
|
+
' enumerable: false' +
|
|
2997
|
+
' });' +
|
|
2998
|
+
' }' +
|
|
2999
|
+
'})();';
|
|
3000
|
+
var shadowScript = new vm.Script(shadowProtoCode);
|
|
3001
|
+
shadowScript.runInContext(innerContext);
|
|
3002
|
+
})();
|
|
3003
|
+
|
|
3004
|
+
// HARDENING 3: Block legacy prototype manipulation methods in PARENT VM
|
|
3005
|
+
// These deprecated methods (__lookupGetter__, __lookupSetter__, __defineGetter__, __defineSetter__)
|
|
3006
|
+
// can be used to bypass frozen prototype protections.
|
|
3007
|
+
// MUST run before freeze!
|
|
3008
|
+
(function() {
|
|
3009
|
+
var legacyMethods = ['__lookupGetter__', '__lookupSetter__', '__defineGetter__', '__defineSetter__'];
|
|
3010
|
+
for (var i = 0; i < legacyMethods.length; i++) {
|
|
3011
|
+
Object.defineProperty(Object.prototype, legacyMethods[i], {
|
|
3012
|
+
value: function() { return undefined; },
|
|
3013
|
+
writable: false,
|
|
3014
|
+
configurable: false,
|
|
3015
|
+
enumerable: false
|
|
3016
|
+
});
|
|
3017
|
+
}
|
|
3018
|
+
})();
|
|
3019
|
+
|
|
3020
|
+
// HARDENING 4: Block legacy prototype methods in INNER VM
|
|
3021
|
+
// MUST run before freeze!
|
|
3022
|
+
(function() {
|
|
3023
|
+
var blockLegacyCode =
|
|
3024
|
+
'(function() {' +
|
|
3025
|
+
' var methods = ["__lookupGetter__", "__lookupSetter__", "__defineGetter__", "__defineSetter__"];' +
|
|
3026
|
+
' for (var i = 0; i < methods.length; i++) {' +
|
|
3027
|
+
' Object.defineProperty(Object.prototype, methods[i], {' +
|
|
3028
|
+
' value: function() { return undefined; },' +
|
|
3029
|
+
' writable: false,' +
|
|
3030
|
+
' configurable: false,' +
|
|
3031
|
+
' enumerable: false' +
|
|
3032
|
+
' });' +
|
|
3033
|
+
' }' +
|
|
3034
|
+
'})();';
|
|
3035
|
+
var blockScript = new vm.Script(blockLegacyCode);
|
|
3036
|
+
blockScript.runInContext(innerContext);
|
|
3037
|
+
})();
|
|
3038
|
+
|
|
3039
|
+
// ============================================================
|
|
3040
|
+
// PROTOTYPE FREEZING: Lock in hardening by freezing all prototypes
|
|
3041
|
+
// ============================================================
|
|
2940
3042
|
//
|
|
2941
3043
|
// We freeze prototypes in TWO places:
|
|
2942
3044
|
// 1. PARENT VM prototypes - because SafeObject.prototype = Object.prototype uses parent's prototype
|
|
@@ -2958,6 +3060,8 @@ ${stackTraceHardeningCode}
|
|
|
2958
3060
|
Object.freeze(RangeError.prototype);
|
|
2959
3061
|
Object.freeze(SyntaxError.prototype);
|
|
2960
3062
|
Object.freeze(ReferenceError.prototype);
|
|
3063
|
+
Object.freeze(URIError.prototype);
|
|
3064
|
+
Object.freeze(EvalError.prototype);
|
|
2961
3065
|
Object.freeze(Promise.prototype);
|
|
2962
3066
|
|
|
2963
3067
|
// Freeze INNER VM prototypes (used by literals like '', [], etc.)
|
|
@@ -2975,11 +3079,64 @@ ${stackTraceHardeningCode}
|
|
|
2975
3079
|
'Object.freeze(RangeError.prototype);' +
|
|
2976
3080
|
'Object.freeze(SyntaxError.prototype);' +
|
|
2977
3081
|
'Object.freeze(ReferenceError.prototype);' +
|
|
3082
|
+
'Object.freeze(URIError.prototype);' +
|
|
3083
|
+
'Object.freeze(EvalError.prototype);' +
|
|
2978
3084
|
'Object.freeze(Promise.prototype);';
|
|
2979
3085
|
var freezeScript = new vm.Script(freezeCode);
|
|
2980
3086
|
freezeScript.runInContext(innerContext);
|
|
2981
3087
|
})();
|
|
2982
3088
|
|
|
3089
|
+
// HARDENING 5: Wrap error constructors to freeze instances
|
|
3090
|
+
// V8-generated errors (like stack overflow RangeError) are created internally,
|
|
3091
|
+
// not via the constructor. But wrapping provides defense-in-depth for any
|
|
3092
|
+
// errors that ARE created via constructors.
|
|
3093
|
+
// NOTE: This can run after freeze because it replaces global constructors, not prototype properties
|
|
3094
|
+
(function() {
|
|
3095
|
+
var wrapErrorCode =
|
|
3096
|
+
'(function() {' +
|
|
3097
|
+
' var origError = Error;' +
|
|
3098
|
+
' var origTypeError = TypeError;' +
|
|
3099
|
+
' var origRangeError = RangeError;' +
|
|
3100
|
+
' var origSyntaxError = SyntaxError;' +
|
|
3101
|
+
' var origReferenceError = ReferenceError;' +
|
|
3102
|
+
' var origEvalError = EvalError;' +
|
|
3103
|
+
' var origURIError = URIError;' +
|
|
3104
|
+
' function wrapErrorCtor(OrigCtor, name) {' +
|
|
3105
|
+
' var WrappedCtor = function(msg) {' +
|
|
3106
|
+
' var err;' +
|
|
3107
|
+
' if (new.target) {' +
|
|
3108
|
+
' err = new OrigCtor(msg);' +
|
|
3109
|
+
' } else {' +
|
|
3110
|
+
' err = OrigCtor(msg);' +
|
|
3111
|
+
' }' +
|
|
3112
|
+
' try { Object.freeze(err); } catch (e) {}' +
|
|
3113
|
+
' return err;' +
|
|
3114
|
+
' };' +
|
|
3115
|
+
' WrappedCtor.prototype = OrigCtor.prototype;' +
|
|
3116
|
+
' try { Object.setPrototypeOf(WrappedCtor, OrigCtor); } catch (e) {}' +
|
|
3117
|
+
' try { Object.freeze(WrappedCtor); } catch (e) {}' +
|
|
3118
|
+
' return WrappedCtor;' +
|
|
3119
|
+
' }' +
|
|
3120
|
+
' try { Error = wrapErrorCtor(origError, "Error"); } catch (e) {}' +
|
|
3121
|
+
' try { TypeError = wrapErrorCtor(origTypeError, "TypeError"); } catch (e) {}' +
|
|
3122
|
+
' try { RangeError = wrapErrorCtor(origRangeError, "RangeError"); } catch (e) {}' +
|
|
3123
|
+
' try { SyntaxError = wrapErrorCtor(origSyntaxError, "SyntaxError"); } catch (e) {}' +
|
|
3124
|
+
' try { ReferenceError = wrapErrorCtor(origReferenceError, "ReferenceError"); } catch (e) {}' +
|
|
3125
|
+
' try { EvalError = wrapErrorCtor(origEvalError, "EvalError"); } catch (e) {}' +
|
|
3126
|
+
' try { URIError = wrapErrorCtor(origURIError, "URIError"); } catch (e) {}' +
|
|
3127
|
+
'})();';
|
|
3128
|
+
try {
|
|
3129
|
+
var wrapScript = new vm.Script(wrapErrorCode);
|
|
3130
|
+
wrapScript.runInContext(innerContext);
|
|
3131
|
+
} catch (e) { /* ignore */ }
|
|
3132
|
+
})();
|
|
3133
|
+
|
|
3134
|
+
// NOTE: HARDENING 6 and 7 (Object.getPrototypeOf / Reflect.getPrototypeOf wrappers)
|
|
3135
|
+
// were removed as they break legitimate internal functionality like __safe_template.
|
|
3136
|
+
// The existing defenses (prototype freezing, __proto__ shadowing, error constructor
|
|
3137
|
+
// wrapping, codeGeneration.strings=false) provide sufficient protection against
|
|
3138
|
+
// the CVE-2023-29017 attack pattern.
|
|
3139
|
+
|
|
2983
3140
|
// Inject safe runtime functions (non-writable, non-configurable)
|
|
2984
3141
|
// Wrap with secure proxy to block dangerous property access
|
|
2985
3142
|
var safeRuntime = {
|
|
@@ -3164,6 +3321,47 @@ ${stackTraceHardeningCode}
|
|
|
3164
3321
|
// Execute User Code
|
|
3165
3322
|
// ============================================================
|
|
3166
3323
|
|
|
3324
|
+
// HARDENING 4: Runtime prototype verification before user code execution
|
|
3325
|
+
// Verifies that critical prototypes are still frozen. If any have been
|
|
3326
|
+
// unfrozen (which shouldn't be possible but defense-in-depth), abort.
|
|
3327
|
+
(function() {
|
|
3328
|
+
var criticalPrototypes = [
|
|
3329
|
+
{ name: 'Object.prototype', proto: Object.prototype },
|
|
3330
|
+
{ name: 'Array.prototype', proto: Array.prototype },
|
|
3331
|
+
{ name: 'Function.prototype', proto: Function.prototype },
|
|
3332
|
+
{ name: 'Error.prototype', proto: Error.prototype },
|
|
3333
|
+
{ name: 'RangeError.prototype', proto: RangeError.prototype }
|
|
3334
|
+
];
|
|
3335
|
+
for (var i = 0; i < criticalPrototypes.length; i++) {
|
|
3336
|
+
var item = criticalPrototypes[i];
|
|
3337
|
+
if (!Object.isFrozen(item.proto)) {
|
|
3338
|
+
throw new Error('SECURITY VIOLATION: ' + item.name + ' is not frozen. Aborting execution.');
|
|
3339
|
+
}
|
|
3340
|
+
}
|
|
3341
|
+
})();
|
|
3342
|
+
|
|
3343
|
+
// HARDENING 5: Runtime verification in INNER VM
|
|
3344
|
+
(function() {
|
|
3345
|
+
var verifyCode =
|
|
3346
|
+
'(function() {' +
|
|
3347
|
+
' var protos = [Object.prototype, Array.prototype, Error.prototype, RangeError.prototype];' +
|
|
3348
|
+
' for (var i = 0; i < protos.length; i++) {' +
|
|
3349
|
+
' if (!Object.isFrozen(protos[i])) {' +
|
|
3350
|
+
' throw new Error("SECURITY VIOLATION: Inner VM prototype not frozen");' +
|
|
3351
|
+
' }' +
|
|
3352
|
+
' }' +
|
|
3353
|
+
'})();';
|
|
3354
|
+
try {
|
|
3355
|
+
var verifyScript = new vm.Script(verifyCode);
|
|
3356
|
+
verifyScript.runInContext(innerContext);
|
|
3357
|
+
} catch (e) {
|
|
3358
|
+
if (e.message && e.message.indexOf('SECURITY VIOLATION') >= 0) {
|
|
3359
|
+
throw e;
|
|
3360
|
+
}
|
|
3361
|
+
/* ignore other errors */
|
|
3362
|
+
}
|
|
3363
|
+
})();
|
|
3364
|
+
|
|
3167
3365
|
var userCode = ${JSON.stringify(userCode)};
|
|
3168
3366
|
// IMPORTANT: For stack overflow errors (RangeError: Maximum call stack size exceeded),
|
|
3169
3367
|
// Node/V8 may ignore Error.prepareStackTrace when it was installed by a different Script
|
|
@@ -5373,12 +5571,7 @@ var init_vm_adapter = __esm({
|
|
|
5373
5571
|
}
|
|
5374
5572
|
const _protectedSandbox = createProtectedSandbox(baseSandbox);
|
|
5375
5573
|
this.context = baseSandbox;
|
|
5376
|
-
const wrappedCode =
|
|
5377
|
-
(async () => {
|
|
5378
|
-
${code}
|
|
5379
|
-
return typeof __ag_main === 'function' ? await __ag_main() : undefined;
|
|
5380
|
-
})();
|
|
5381
|
-
`;
|
|
5574
|
+
const wrappedCode = "(async () => {\n" + code + '\nreturn typeof __ag_main === "function" ? await __ag_main() : undefined;\n})();';
|
|
5382
5575
|
const script = new vm3.Script(wrappedCode, {
|
|
5383
5576
|
filename: "agentscript.js"
|
|
5384
5577
|
});
|
|
@@ -6598,6 +6791,8 @@ var init_worker_pool_adapter = __esm({
|
|
|
6598
6791
|
rateLimiter;
|
|
6599
6792
|
disposed = false;
|
|
6600
6793
|
initialized = false;
|
|
6794
|
+
// Bound event handler for proper cleanup
|
|
6795
|
+
boundMemoryExceededHandler = this.handleMemoryExceeded.bind(this);
|
|
6601
6796
|
// Metrics
|
|
6602
6797
|
_totalExecutions = 0;
|
|
6603
6798
|
_successfulExecutions = 0;
|
|
@@ -6620,7 +6815,7 @@ var init_worker_pool_adapter = __esm({
|
|
|
6620
6815
|
this.slots
|
|
6621
6816
|
);
|
|
6622
6817
|
this.rateLimiter = createRateLimiter(this.config.maxMessagesPerSecond);
|
|
6623
|
-
this.memoryMonitor.on("memoryExceeded", this.
|
|
6818
|
+
this.memoryMonitor.on("memoryExceeded", this.boundMemoryExceededHandler);
|
|
6624
6819
|
}
|
|
6625
6820
|
/**
|
|
6626
6821
|
* Initialize the worker pool
|
|
@@ -6685,6 +6880,7 @@ var init_worker_pool_adapter = __esm({
|
|
|
6685
6880
|
return;
|
|
6686
6881
|
}
|
|
6687
6882
|
this.disposed = true;
|
|
6883
|
+
this.memoryMonitor.off("memoryExceeded", this.boundMemoryExceededHandler);
|
|
6688
6884
|
this.memoryMonitor.stop();
|
|
6689
6885
|
this.executionQueue.clear();
|
|
6690
6886
|
const terminatePromises = [];
|
|
@@ -9873,6 +10069,7 @@ var Enclave = class {
|
|
|
9873
10069
|
referenceConfig;
|
|
9874
10070
|
scoringGate;
|
|
9875
10071
|
doubleVmConfig;
|
|
10072
|
+
customGlobalNames;
|
|
9876
10073
|
adapter;
|
|
9877
10074
|
constructor(options = {}) {
|
|
9878
10075
|
this.securityLevel = options.securityLevel ?? DEFAULT_SECURITY_LEVEL;
|
|
@@ -9944,8 +10141,8 @@ var Enclave = class {
|
|
|
9944
10141
|
secureProxyConfig: securityConfig.secureProxyConfig,
|
|
9945
10142
|
globals: initialGlobals
|
|
9946
10143
|
};
|
|
9947
|
-
|
|
9948
|
-
const customAllowedGlobals = customGlobalNames.flatMap((name) => [name, `__safe_${name}`]);
|
|
10144
|
+
this.customGlobalNames = Object.keys(initialGlobals);
|
|
10145
|
+
const customAllowedGlobals = this.customGlobalNames.flatMap((name) => [name, `__safe_${name}`]);
|
|
9949
10146
|
this.validator = this.createValidator(presetName, customAllowedGlobals);
|
|
9950
10147
|
this.validateCode = options.validate !== false;
|
|
9951
10148
|
this.transformCode = options.transform !== false;
|
|
@@ -10009,7 +10206,9 @@ var Enclave = class {
|
|
|
10009
10206
|
transformedCode = (0, import_ast4.transformAgentScript)(code, {
|
|
10010
10207
|
wrapInMain: needsWrapping,
|
|
10011
10208
|
transformCallTool: true,
|
|
10012
|
-
transformLoops: true
|
|
10209
|
+
transformLoops: true,
|
|
10210
|
+
// Pass custom global names so they aren't transformed to __safe_ prefix
|
|
10211
|
+
additionalIdentifiers: this.customGlobalNames
|
|
10013
10212
|
});
|
|
10014
10213
|
}
|
|
10015
10214
|
if (this.validateCode) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enclave-vm/core",
|
|
3
|
-
"version": "2.10.
|
|
3
|
+
"version": "2.10.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",
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@babel/standalone": "^7.
|
|
41
|
-
"@enclave-vm/types": "2.10.
|
|
42
|
-
"@enclave-vm/ast": "2.10.
|
|
40
|
+
"@babel/standalone": "^7.29.0",
|
|
41
|
+
"@enclave-vm/types": "2.10.1",
|
|
42
|
+
"@enclave-vm/ast": "2.10.1",
|
|
43
43
|
"acorn": "8.15.0",
|
|
44
44
|
"acorn-walk": "8.3.4",
|
|
45
45
|
"astring": "1.9.0",
|
|
46
|
-
"zod": "^4.
|
|
46
|
+
"zod": "^4.3.6"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"@huggingface/transformers": "^3.2.2",
|