瀏覽代碼

Add CLR write allowed option to engine for additional sandboxing capabilities (#575)

Arshia001 6 年之前
父節點
當前提交
58732d2d5c

+ 9 - 0
Jint/Options.cs

@@ -15,6 +15,7 @@ namespace Jint
         private bool _strict;
         private bool _allowDebuggerStatement;
         private bool _allowClr;
+        private bool _allowClrWrite = true;
         private readonly List<IObjectConverter> _objectConverters = new List<IObjectConverter>();
         private Func<object, ObjectInstance> _wrapObjectHandler;
         private int _maxStatements;
@@ -99,6 +100,12 @@ namespace Jint
             return this;
         }
 
+        public Options AllowClrWrite(bool allow = true)
+        {
+            _allowClrWrite = allow;
+            return this;
+        }
+
         /// <summary>
         /// Exceptions thrown from CLR code are converted to JavaScript errors and
         /// can be used in at try/catch statement. By default these exceptions are bubbled
@@ -181,6 +188,8 @@ namespace Jint
 
         internal bool _IsClrAllowed => _allowClr;
 
+        internal bool _IsClrWriteAllowed => _allowClrWrite;
+
         internal Predicate<Exception> _ClrExceptionsHandler => _clrExceptionsHandler;
 
         internal List<Assembly> _LookupAssemblies => _lookupAssemblies;

+ 1 - 1
Jint/Runtime/Descriptors/Specialized/FieldInfoDescriptor.cs

@@ -16,7 +16,7 @@ namespace Jint.Runtime.Descriptors.Specialized
             _fieldInfo = fieldInfo;
             _item = item;
 
-            Writable = !fieldInfo.Attributes.HasFlag(FieldAttributes.InitOnly); // don't write to fields marked as readonly
+            Writable = !fieldInfo.Attributes.HasFlag(FieldAttributes.InitOnly) && engine.Options._IsClrWriteAllowed; // don't write to fields marked as readonly
         }
 
         protected internal override JsValue CustomValue

+ 1 - 1
Jint/Runtime/Descriptors/Specialized/IndexDescriptor.cs

@@ -52,7 +52,7 @@ namespace Jint.Runtime.Descriptors.Specialized
                 ExceptionHelper.ThrowInvalidOperationException("No matching indexer found.");
             }
 
-            Writable = true;
+            Writable = engine.Options._IsClrWriteAllowed;
         }
 
         public IndexDescriptor(Engine engine, string key, object item)

+ 1 - 1
Jint/Runtime/Descriptors/Specialized/PropertyInfoDescriptor.cs

@@ -16,7 +16,7 @@ namespace Jint.Runtime.Descriptors.Specialized
             _propertyInfo = propertyInfo;
             _item = item;
 
-            Writable = propertyInfo.CanWrite;
+            Writable = propertyInfo.CanWrite && engine.Options._IsClrWriteAllowed;
         }
 
         protected internal override JsValue CustomValue