3using System.Reflection;
39 if (t.IsEnum)
return true;
40 if (t.IsPrimitive)
return true;
41 if (t == typeof(
string))
return true;
42 if (t == typeof(decimal))
return true;
43 if (t == typeof(DateTime))
return true;
44 if (t == typeof(TimeSpan))
return true;
45 if (t == typeof(Guid))
return true;
51 if (
string.IsNullOrWhiteSpace(ns))
return false;
54 return ns.StartsWith(
"System.IO", StringComparison.Ordinal)
55 || ns.StartsWith(
"System.Reflection", StringComparison.Ordinal)
56 || ns.StartsWith(
"System.Diagnostics", StringComparison.Ordinal)
57 || ns.StartsWith(
"System.Runtime", StringComparison.Ordinal)
58 || ns.StartsWith(
"System.Threading", StringComparison.Ordinal)
59 || ns.StartsWith(
"System.Net", StringComparison.Ordinal)
60 || ns.StartsWith(
"Microsoft.Win32", StringComparison.Ordinal);
65 if (t ==
null)
return false;
78 var def = t.GetGenericTypeDefinition();
79 if (def == typeof(Nullable<>))
82 if (def == typeof(System.Collections.Generic.List<>)
83 || def == typeof(System.Collections.Generic.IList<>)
84 || def == typeof(System.Collections.Generic.ICollection<>)
85 || def == typeof(System.Collections.Generic.IEnumerable<>))
90 if (def == typeof(System.Collections.Generic.Dictionary<,>)
91 || def == typeof(System.Collections.Generic.IDictionary<,>))
93 var ga = t.GetGenericArguments();
99 if (typeof(System.Collections.IList).IsAssignableFrom(t))
return true;
100 if (typeof(System.Collections.IDictionary).IsAssignableFrom(t))
return true;
108 if (m ==
null)
return false;
111 if (m.Name.Equals(
"GetType", StringComparison.OrdinalIgnoreCase))
return false;
113 var declaring = m.DeclaringType;
114 if (declaring ==
null || !
IsTypeAllowed(declaring))
return false;
116 if (m is PropertyInfo p)
122 foreach (var ip
in p.GetIndexParameters())
129 if (m is FieldInfo f)
141 if (m ==
null)
return false;
143 var declaring = m.DeclaringType;
144 if (declaring ==
null || !
IsTypeAllowed(declaring))
return false;
147 if (m.Name.Equals(
"GetType", StringComparison.OrdinalIgnoreCase))
return false;
148 if (m.Name.Equals(
"GetHashCode", StringComparison.OrdinalIgnoreCase))
return true;
149 if (m.Name.Equals(
"ToString", StringComparison.OrdinalIgnoreCase))
return true;
152 if (m.ReturnType != typeof(
void) && !
IsTypeAllowed(m.ReturnType))
return false;
154 foreach (var p
in m.GetParameters())
160 if (declaring == typeof(
string))
164 "Contains",
"StartsWith",
"EndsWith",
"Substring",
"IndexOf",
"Replace",
165 "ToUpper",
"ToLower",
"Trim",
"TrimStart",
"TrimEnd",
"Split"
167 return allowed.Contains(m.Name, StringComparer.OrdinalIgnoreCase);
171 if (declaring.IsGenericType && declaring.GetGenericTypeDefinition() == typeof(System.Collections.Generic.List<>))
173 string[] allowed = {
"Add",
"Remove",
"RemoveAt",
"Clear",
"Contains" };
174 return allowed.Contains(m.Name, StringComparer.OrdinalIgnoreCase);
183 if (value ==
null)
return true;
184 if (value is NullReference)
return true;
Allows all CLR interop. Use only for trusted scripts.
bool IsCallAllowed(MethodInfo m)
bool IsMemberAllowed(MemberInfo m)
bool IsReturnValueAllowed(object? value)
bool IsTypeAllowed(Type t)
Denies all CLR interop. Default Policy!
bool IsReturnValueAllowed(object? value)
bool IsMemberAllowed(MemberInfo m)
bool IsTypeAllowed(Type t)
bool IsCallAllowed(MethodInfo m)
A conservative "safe" policy: allows access only to a small set of generally harmless BCL types and m...
bool IsCallAllowed(MethodInfo m)
static bool IsSafePrimitiveLike(Type t)
static bool IsBlockedNamespace(string? ns)
bool IsMemberAllowed(MemberInfo m)
bool IsReturnValueAllowed(object? value)
bool IsTypeAllowed(Type t)
Policy hook for CLR interop. Implementations decide what is accessible from scripts.