Skip to content

Commit ee73af9

Browse files
Added blocking of Delegate.Method when AllowReflection is false (GitHub Issue ClearFoundry#2); other minor fixes.
1 parent 91935c6 commit ee73af9

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed

ClearScript/ClearScript.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<Compile Include="HostList.cs" />
9999
<Compile Include="Util\INativeCallback.cs" />
100100
<Compile Include="Util\IHostInvokeContext.cs" />
101+
<Compile Include="Util\MemberComparer.cs" />
101102
<Compile Include="Util\NativeCallbackTimer.cs" />
102103
<Compile Include="Util\NativeMethods.cs" />
103104
<Compile Include="Util\COMDispatch.cs" />

ClearScript/HostItem.InvokeMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ public override bool IsUnblockedMethod(HostItem hostItem)
480480

481481
public override object Invoke(HostItem hostItem)
482482
{
483-
if (reflectionMethods.Contains(method))
483+
if (reflectionMethods.Contains(method, MemberComparer<MethodInfo>.Instance))
484484
{
485485
hostItem.Engine.CheckReflection();
486486
}

ClearScript/HostItem.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ internal partial class HostItem : DynamicObject, IReflect, IDynamic, IEnumVARIAN
3232
internal static bool EnableVTablePatching;
3333
[ThreadStatic] private static bool bypassVTablePatching;
3434

35+
private static readonly PropertyInfo[] reflectionProperties =
36+
{
37+
typeof(Delegate).GetProperty("Method")
38+
};
39+
3540
#endregion
3641

3742
#region constructors
@@ -1389,6 +1394,11 @@ private object GetHostProperty(string name, BindingFlags invokeFlags, object[] a
13891394

13901395
private object GetHostProperty(PropertyInfo property, BindingFlags invokeFlags, object[] args, CultureInfo culture)
13911396
{
1397+
if (reflectionProperties.Contains(property, MemberComparer<PropertyInfo>.Instance))
1398+
{
1399+
engine.CheckReflection();
1400+
}
1401+
13921402
if (property.GetGetMethod(invokeFlags.HasFlag(BindingFlags.NonPublic)) == null)
13931403
{
13941404
throw new UnauthorizedAccessException("Property get method is unavailable or inaccessible");

ClearScript/Util/MemberComparer.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
8+
namespace Microsoft.ClearScript.Util
9+
{
10+
internal sealed class MemberComparer<T> : EqualityComparer<T> where T : MemberInfo
11+
{
12+
private static readonly MemberComparer<T> instance = new MemberComparer<T>();
13+
14+
public static MemberComparer<T> Instance { get { return instance; } }
15+
16+
public override bool Equals(T x, T y)
17+
{
18+
try
19+
{
20+
return (x.Module == y.Module) && (x.MetadataToken == y.MetadataToken);
21+
}
22+
catch (Exception)
23+
{
24+
return x == y;
25+
}
26+
}
27+
28+
public override int GetHashCode(T obj)
29+
{
30+
return (obj == null) ? 0 : obj.GetHashCode();
31+
}
32+
}
33+
}

ClearScript/Util/NativeMethods.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ [In] [MarshalAs(UnmanagedType.LPWStr)] string path
3131

3232
[DllImport("kernel32.dll", SetLastError = true)]
3333
[return: MarshalAs(UnmanagedType.Bool)]
34-
public static extern bool FreeLibrary(IntPtr hLibrary);
34+
public static extern bool FreeLibrary(
35+
[In] IntPtr hLibrary
36+
);
3537

3638
[DllImport("ole32.dll", ExactSpelling = true)]
3739
public static extern uint CLSIDFromProgID(
@@ -86,9 +88,13 @@ [Out] out uint oldProtect
8688
);
8789

8890
[DllImport("kernel32.dll", SetLastError = false)]
89-
public static extern void GetSystemInfo(out SystemInfo info);
91+
public static extern void GetSystemInfo(
92+
[Out] out SystemInfo info
93+
);
9094

9195
[DllImport("kernel32.dll")]
92-
public static extern void GetNativeSystemInfo(out SystemInfo info);
96+
public static extern void GetNativeSystemInfo(
97+
[Out] out SystemInfo info
98+
);
9399
}
94100
}

ClearScriptTest/MemberAccessTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ public void MemberAccess_Property_Struct_BadAssignment()
208208
TestUtil.AssertException<ArgumentException>(() => engine.Execute("testObject.StructProperty = System.DateTime.Now"));
209209
}
210210

211+
[TestMethod, TestCategory("MemberAccess")]
212+
public void MemberAccess_Property_Blocked()
213+
{
214+
TestUtil.AssertException<UnauthorizedAccessException>(() => engine.Execute("host.proc(0, function () {}).Method"));
215+
}
216+
211217
[TestMethod, TestCategory("MemberAccess")]
212218
public void MemberAccess_ReadOnlyProperty()
213219
{

0 commit comments

Comments
 (0)