2using System.Collections.Generic;
3using System.Diagnostics;
5using System.Reflection;
6using System.Text.RegularExpressions;
22 AssertSource(Assemblies);
31 AssertSource(Classes);
41 AssertSource(Sources);
51 AssertSource(Sources);
61 AssertSource(Sources);
69 private static void AssertSource(params
object[] Sources)
78 bool WaherPersistence =
false;
79 bool AsynchTask =
false;
84 Frame =
new StackFrame(Skip);
85 Method = Frame.GetMethod();
89 Type = Method.DeclaringType;
90 if (Type != typeof(
Assert))
100 Frame =
new StackFrame(Skip++);
101 Method = Frame.GetMethod();
105 Type = Method.DeclaringType;
106 TypeName = Type.FullName;
107 Assembly = Type.Assembly;
108 AssemblyName = Assembly.GetName().Name;
110 foreach (
object Source
in Sources)
112 if (Source is Assembly A)
117 else if (Source is Type T)
122 else if (Source is Regex Regex)
124 if (IsMatch(Regex, TypeName +
"." + Method.Name) ||
125 IsMatch(Regex, TypeName) ||
126 IsMatch(Regex, AssemblyName))
131 else if (Source is
string s)
133 if (TypeName +
"." + Method.Name == s ||
142 if (!Other || !AsynchTask || !WaherPersistence)
144 if (
string.IsNullOrEmpty(Assembly.Location))
146 if (AssemblyName.StartsWith(
"WPSA."))
147 WaherPersistence =
true;
153 if (Type == typeof(System.Threading.Tasks.Task))
155 else if (TypeName.StartsWith(AssemblyName) &&
156 AssemblyName +
"." == Path.ChangeExtension(Path.GetFileName(Assembly.Location),
string.Empty))
158 if (AssemblyName.StartsWith(
"Waher.Persistence."))
159 WaherPersistence =
true;
160 else if (!AssemblyName.StartsWith(
"Waher.") && !AssemblyName.StartsWith(
"System."))
163 else if (!Path.GetFileName(Assembly.Location).StartsWith(
"System."))
169 if (AsynchTask && WaherPersistence && !Other)
172 Frame =
new StackFrame(Skip = Caller);
173 Method = Frame.GetMethod();
174 Type = Method.DeclaringType;
175 Assembly = Type.Assembly;
177 string ObjectId = Type.FullName +
"." + Method.Name;
178 StackTrace Trace =
new StackTrace(Skip,
false);
179 UnauthorizedAccessEventArgs e =
new UnauthorizedAccessEventArgs(Method, Type, Assembly, Trace);
180 List<KeyValuePair<string, object>> Tags =
new List<KeyValuePair<string, object>>()
182 new KeyValuePair<string, object>(
"Method", Method.Name),
183 new KeyValuePair<string, object>(
"Type", Type.FullName),
184 new KeyValuePair<string, object>(
"Assembly", Assembly.FullName)
190 Frame =
new StackFrame(Skip);
191 Method = Frame.GetMethod();
195 Type = Method.DeclaringType;
196 TypeName = Type.FullName;
197 Assembly = Type.Assembly;
198 AssemblyName = Assembly.GetName().Name;
200 Tags.Add(
new KeyValuePair<string, object>(
"Pos" + Skip.ToString(), Assembly.GetName().Name +
", " + TypeName +
", " + Method.Name));
205 Log.
Warning(
"Unauthorized access detected and prevented.", ObjectId,
string.Empty,
"UnauthorizedAccess",
EventLevel.Major,
206 string.Empty, Assembly.FullName, Trace.ToString(), Tags.ToArray());
221 throw new UnauthorizedCallstackException(
"Unauthorized access.");
224 private static bool IsMatch(Regex Regex,
string s)
226 Match M = Regex.Match(s);
227 return M.Success && M.Index == 0 && M.Length == s.Length;
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
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.
Static class containing methods that can be used to make sure calls are made from appropriate locatio...
static void CallFromSource(params Regex[] Sources)
Makes sure the call is made from one of the listed sources.
static UnauthorizedAccessEventHandler 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 CallFromAssembly(params Assembly[] Assemblies)
Makes sure the call is made from one of the listed assemblies.
delegate void UnauthorizedAccessEventHandler(object Sender, UnauthorizedAccessEventArgs e)
Delegate for unauthorized access event handlers.