Browse Source

Don't allow changes to shared Undefined property descriptor (#606)

Marko Lahma 6 years ago
parent
commit
28b7ea613d
2 changed files with 23 additions and 1 deletions
  1. 10 0
      Jint.Tests/Runtime/EngineTests.cs
  2. 13 1
      Jint/Runtime/Descriptors/PropertyDescriptor.cs

+ 10 - 0
Jint.Tests/Runtime/EngineTests.cs

@@ -1009,6 +1009,16 @@ namespace Jint.Tests.Runtime
             Assert.Throws<ArgumentException>(() => _engine.Invoke(foo, obj, new object[] { }));
         }
 
+        [Fact]
+        public void ShouldNotAllowModifyingSharedUndefinedDescriptor()
+        {
+            var e = new Engine();
+            e.Execute("var x = { literal: true };");
+
+            var pd = e.GetValue("x").AsObject().GetProperty("doesNotExist");
+            Assert.Throws<InvalidOperationException>(() => pd.Value = "oh no, assigning this breaks things");
+        }
+
         [Theory]
         [InlineData("0", 0, 16)]
         [InlineData("1", 1, 16)]

+ 13 - 1
Jint/Runtime/Descriptors/PropertyDescriptor.cs

@@ -7,7 +7,7 @@ namespace Jint.Runtime.Descriptors
 {
     public class PropertyDescriptor
     {
-        public static readonly PropertyDescriptor Undefined = new PropertyDescriptor(PropertyFlag.None);
+        public static readonly PropertyDescriptor Undefined = new UndefinedPropertyDescriptor();
 
         internal PropertyFlag _flags;
         internal JsValue _value;
@@ -383,5 +383,17 @@ namespace Jint.Runtime.Descriptors
 
             return true;
         }
+
+        private sealed class UndefinedPropertyDescriptor : PropertyDescriptor
+        {
+            public UndefinedPropertyDescriptor() : base(PropertyFlag.None | PropertyFlag.CustomJsValue)
+            {
+            }
+
+            protected internal override JsValue CustomValue
+            {
+                set => ExceptionHelper.ThrowInvalidOperationException("making changes to undefined property's descriptor is not allowed");
+            }
+        }
     }
 }