@@ -79,6 +79,7 @@ function generateInnerIframeScript(userCode: string, config: SerializedIframeCon
7979 var _getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
8080 var _ReflectGet = typeof Reflect !== 'undefined' ? Reflect.get : function(t,p) { return t[p]; };
8181 var _ReflectSet = typeof Reflect !== 'undefined' ? Reflect.set : function(t,p,v) { t[p]=v; return true; };
82+ var _Proxy = typeof Proxy !== 'undefined' ? Proxy : undefined;
8283
8384 var blockedPropertiesSet = new Set(${ blockedProperties } );
8485 var proxyCache = new WeakMap();
@@ -89,7 +90,7 @@ function generateInnerIframeScript(userCode: string, config: SerializedIframeCon
8990 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) return obj;
9091 if (proxyCache.has(obj)) return proxyCache.get(obj);
9192
92- var proxy = new Proxy (obj, {
93+ var proxy = new _Proxy (obj, {
9394 get: function(target, property, receiver) {
9495 var propName = String(property);
9596 var descriptor = _getOwnPropertyDescriptor(target, property);
@@ -101,10 +102,10 @@ function generateInnerIframeScript(userCode: string, config: SerializedIframeCon
101102
102103 if (blockedPropertiesSet.has(propName)) {
103104 if (isNonConfigurable) return _ReflectGet(target, property, receiver);
104- if ( ${ throwOnBlocked } ) {
105- throw createSafeError("Security violation: Access to '" + propName + "' is blocked.");
106- } else {
107- return undefined;
105+ $ {
106+ throwOnBlocked
107+ ? `throw createSafeError("Security violation: Access to '" + propName + "' is blocked.");`
108+ : ` return undefined;`
108109 }
109110 }
110111
@@ -121,10 +122,10 @@ function generateInnerIframeScript(userCode: string, config: SerializedIframeCon
121122 set: function(target, property, value, receiver) {
122123 var propName = String(property);
123124 if (blockedPropertiesSet.has(propName)) {
124- if ( ${ throwOnBlocked } ) {
125- throw createSafeError("Security violation: Setting '" + propName + "' is blocked.");
126- } else {
127- return false;
125+ $ {
126+ throwOnBlocked
127+ ? `throw createSafeError("Security violation: Setting '" + propName + "' is blocked.");`
128+ : ` return false;`
128129 }
129130 }
130131 return _ReflectSet(target, property, value, receiver);
@@ -151,6 +152,7 @@ function generateInnerIframeScript(userCode: string, config: SerializedIframeCon
151152 window.addEventListener('message', function(event) {
152153 var data = event.data;
153154 if (!data || data.__enclave_msg__ !== true) return;
155+ if (data.requestId && data.requestId !== requestId) return;
154156
155157 if (data.type === 'tool-response') {
156158 var pending = pendingToolCalls[data.callId];
@@ -386,21 +388,7 @@ function generateInnerIframeScript(userCode: string, config: SerializedIframeCon
386388 }
387389 })();
388390
389- // Freeze all prototypes
390- (function() {
391- var protos = [
392- Object.prototype, Array.prototype, Function.prototype,
393- String.prototype, Number.prototype, Boolean.prototype,
394- Date.prototype, Error.prototype, TypeError.prototype,
395- RangeError.prototype, SyntaxError.prototype, ReferenceError.prototype,
396- URIError.prototype, EvalError.prototype, Promise.prototype
397- ];
398- for (var i = 0; i < protos.length; i++) {
399- try { Object.freeze(protos[i]); } catch(e) {}
400- }
401- })();
402-
403- // Memory-safe prototype patches
391+ // Memory-safe prototype patches (must run BEFORE freeze)
404392 (function() {
405393 var ml = ${ memoryLimit } ;
406394 if (ml <= 0) return;
@@ -457,6 +445,20 @@ function generateInnerIframeScript(userCode: string, config: SerializedIframeCon
457445 } catch(e) {}
458446 })();
459447
448+ // Freeze all prototypes
449+ (function() {
450+ var protos = [
451+ Object.prototype, Array.prototype, Function.prototype,
452+ String.prototype, Number.prototype, Boolean.prototype,
453+ Date.prototype, Error.prototype, TypeError.prototype,
454+ RangeError.prototype, SyntaxError.prototype, ReferenceError.prototype,
455+ URIError.prototype, EvalError.prototype, Promise.prototype
456+ ];
457+ for (var i = 0; i < protos.length; i++) {
458+ try { Object.freeze(protos[i]); } catch(e) {}
459+ }
460+ })();
461+
460462 // ============================================================
461463 // Remove Dangerous Globals
462464 // ============================================================
0 commit comments