3using System.Diagnostics;
5using System.Reflection;
6using System.Text.RegularExpressions;
20 [Obsolete(
"Use CallFromSource(ICallStackCheck[]) instead.")]
23 int i, c = Assemblies.Length;
26 for (i = 0; i < c; i++)
36 [Obsolete(
"Use CallFromSource(ICallStackCheck[]) instead.")]
39 int i, c = Classes.Length;
42 for (i = 0; i < c; i++)
53 [Obsolete(
"Use CallFromSource(ICallStackCheck[]) instead.")]
56 int i, c = Sources.Length;
59 for (i = 0; i < c; i++)
70 [Obsolete(
"Use CallFromSource(ICallStackCheck[]) instead.")]
73 int i, c = Sources.Length;
76 for (i = 0; i < c; i++)
87 [Obsolete(
"Use CallFromSource(ICallStackCheck[]) instead.")]
101 int i, c = Sources.Length;
104 for (i = 0; i < c; i++)
106 object Source = Sources[i];
110 else if (Source is Assembly A)
112 else if (Source is Type T)
114 else if (Source is Regex Regex)
116 else if (Source is
string s)
119 throw new ArgumentException(
"Invalid source type: " + Source.GetType().FullName, nameof(Sources));
148 bool WaherPersistence =
false;
149 bool WaherRuntime =
false;
150 bool AsynchTask =
false;
166 bool Prohibited =
false;
179 if (!FrameInfo.Valid)
184 Status = Source.
Check(FrameInfo);
200 if (!Other || !AsynchTask || !WaherPersistence)
202 if (
string.IsNullOrEmpty(FrameInfo.Assembly.Location))
204 if (FrameInfo.AssemblyName.StartsWith(
"WPSA."))
205 WaherPersistence =
true;
211 string AssemblyName = FrameInfo.AssemblyName;
213 switch (AssemblyName)
215 case "Waher.Persistence.Serialization.Compiled":
216 AssemblyName =
"Waher.Persistence.Serialization";
219 case "Waher.Persistence.FilesLW":
220 AssemblyName =
"Waher.Persistence.Files";
224 if (AssemblyName.EndsWith(
".UWP"))
225 AssemblyName = AssemblyName.Substring(0, AssemblyName.Length - 4);
229 if (FrameInfo.Type == typeof(
System.Threading.Tasks.Task))
231 else if (FrameInfo.TypeName.StartsWith(AssemblyName) &&
232 FrameInfo.AssemblyName +
"." == Path.ChangeExtension(Path.GetFileName(FrameInfo.Assembly.Location),
string.Empty))
234 if (FrameInfo.AssemblyName.StartsWith(
"Waher.Persistence.") ||
235 FrameInfo.AssemblyName.StartsWith(
"WPSA."))
237 WaherPersistence =
true;
239 else if (FrameInfo.AssemblyName.StartsWith(
"Waher.Runtime."))
241 else if (FrameInfo.AssemblyName.StartsWith(
"WPSA."))
242 WaherPersistence =
true;
243 else if (!FrameInfo.AssemblyName.StartsWith(
"Waher.") && !FrameInfo.AssemblyName.StartsWith(
"System."))
246 else if (!Path.GetFileName(FrameInfo.Assembly.Location).StartsWith(
"System."))
261 if (!Prohibited && AsynchTask && (WaherPersistence || WaherRuntime) && !Other)
266 string ObjectId = FrameInfo.Type.FullName +
"." + FrameInfo.Method.Name;
267 StackTrace Trace =
new StackTrace(Skip,
false);
269 List<KeyValuePair<string, object>> Tags =
new List<KeyValuePair<string, object>>()
271 new KeyValuePair<string, object>(
"Method", FrameInfo.Method.Name),
272 new KeyValuePair<string, object>(
"Type", FrameInfo.Type.FullName),
273 new KeyValuePair<string, object>(
"Assembly", FrameInfo.Assembly.FullName)
285 Tags.Add(
new KeyValuePair<string, object>(
"Pos" + Skip.ToString(),
286 FrameInfo.Assembly.GetName().Name +
", " + FrameInfo.TypeName +
", " +
287 FrameInfo.Method.Name));
290 Tags.Add(
new KeyValuePair<string, object>(
"Pos" + Skip.ToString(), FrameInfo.ToString()));
295 Log.
Warning(
"Unauthorized access detected and prevented.", ObjectId,
string.Empty,
"UnauthorizedAccess",
EventLevel.Major,
296 string.Empty, e.
Assembly.FullName, Trace.ToString(), Tags.ToArray());
Static class managing the application event log. Applications and services log events on this static ...
static void Warning(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a warning event.
Checks for an approved assembly in the call stack.
Checks for approved sources in the call stack using a regular expression.
Checks for approved sources in the call stack using a string.
Checks for an approved type in the call stack.
Static class containing methods that can be used to make sure calls are made from appropriate locatio...
static ICallStackCheck[] Convert(params object[] Sources)
Converts an array of objects into an array of ICallStackCheck objects, assuming each listed source is...
static void CallFromSource(params Regex[] Sources)
Makes sure the call is made from one of the listed sources.
static EventHandler< UnauthorizedAccessEventArgs > UnauthorizedAccess
Event raised when an unauthorized access has been detected.
static void CallFromSource(params string[] Sources)
Makes sure the call is made from one of the listed sources.
static void CallFromClass(params Type[] Classes)
Makes sure the call is made from one of the listed classes.
static void CallFromSource(params object[] Sources)
Makes sure the call is made from one of the listed sources.
static void CallFromSource(params ICallStackCheck[] Sources)
Makes sure the call is made from one of the approved sources and not from one of the prohibited sourc...
static void CallFromAssembly(params Assembly[] Assemblies)
Makes sure the call is made from one of the listed assemblies.
static void CallFromSource(params ICallStackCheck[][] SourcesByPriority)
Makes sure the call is made from one of the approved sources and not from one of the prohibited sourc...
Event arguments for the Assert.UnauthorizedAccess event.
Assembly Assembly
Assembly in which the type is defined.
Exception raised when code tries to access protected code while having a callstack out of the permitt...
Interface for call stack checks.
bool? Check(FrameInformation Frame)
Performs a check of a stack frame.