-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathAssert.cs
More file actions
309 lines (263 loc) · 9.58 KB
/
Assert.cs
File metadata and controls
309 lines (263 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using Waher.Events;
namespace Waher.Security.CallStack
{
/// <summary>
/// Static class containing methods that can be used to make sure calls are made from appropriate locations.
/// </summary>
public static class Assert
{
/// <summary>
/// Makes sure the call is made from one of the listed assemblies.
/// </summary>
/// <param name="Assemblies">Original call must be made from one of these assemblies.</param>
[Obsolete("Use CallFromSource(ICallStackCheck[]) instead.")]
public static void CallFromAssembly(params Assembly[] Assemblies)
{
int i, c = Assemblies.Length;
ICallStackCheck[] Sources = new ICallStackCheck[c];
for (i = 0; i < c; i++)
Sources[i] = new ApproveAssembly(Assemblies[i]);
CallFromSource(Sources);
}
/// <summary>
/// Makes sure the call is made from one of the listed classes.
/// </summary>
/// <param name="Classes">Original call must be made from one of these classes.</param>
[Obsolete("Use CallFromSource(ICallStackCheck[]) instead.")]
public static void CallFromClass(params Type[] Classes)
{
int i, c = Classes.Length;
ICallStackCheck[] Sources = new ICallStackCheck[c];
for (i = 0; i < c; i++)
Sources[i] = new ApproveType(Classes[i]);
CallFromSource(Sources);
}
/// <summary>
/// Makes sure the call is made from one of the listed sources.
/// </summary>
/// <param name="Sources">Original call must be made from one of these sources. Source strings are checked against
/// Assemblies, classes and method names.</param>
[Obsolete("Use CallFromSource(ICallStackCheck[]) instead.")]
public static void CallFromSource(params string[] Sources)
{
int i, c = Sources.Length;
ICallStackCheck[] Sources2 = new ICallStackCheck[c];
for (i = 0; i < c; i++)
Sources2[i] = new ApproveString(Sources[i]);
CallFromSource(Sources2);
}
/// <summary>
/// Makes sure the call is made from one of the listed sources.
/// </summary>
/// <param name="Sources">Original call must be made from one of these sources. Source strings are checked against
/// Assemblies, classes and method names.</param>
[Obsolete("Use CallFromSource(ICallStackCheck[]) instead.")]
public static void CallFromSource(params Regex[] Sources)
{
int i, c = Sources.Length;
ICallStackCheck[] Sources2 = new ICallStackCheck[c];
for (i = 0; i < c; i++)
Sources2[i] = new ApproveRegex(Sources[i]);
CallFromSource(Sources2);
}
/// <summary>
/// Makes sure the call is made from one of the listed sources.
/// </summary>
/// <param name="Sources">Original call must be made from one of these sources. Can be a mix of
/// <see cref="Assembly"/>, <see cref="Type"/>, <see cref="string"/> and <see cref="Regex"/> objects.</param>
[Obsolete("Use CallFromSource(ICallStackCheck[]) instead.")]
public static void CallFromSource(params object[] Sources)
{
CallFromSource(Convert(Sources));
}
/// <summary>
/// Converts an array of objects into an array of <see cref="ICallStackCheck"/>
/// objects, assuming each listed source is approved.
/// </summary>
/// <param name="Sources">Sources</param>
/// <returns>Array of corresponding Callstack checks.</returns>
public static ICallStackCheck[] Convert(params object[] Sources)
{
int i, c = Sources.Length;
ICallStackCheck[] Sources2 = new ICallStackCheck[c];
for (i = 0; i < c; i++)
{
object Source = Sources[i];
if (Source is ICallStackCheck Check)
Sources2[i] = Check;
else if (Source is Assembly A)
Sources2[i] = new ApproveAssembly(A);
else if (Source is Type T)
Sources2[i] = new ApproveType(T);
else if (Source is Regex Regex)
Sources2[i] = new ApproveRegex(Regex);
else if (Source is string s)
Sources2[i] = new ApproveString(s);
else
throw new ArgumentException("Invalid source type: " + Source.GetType().FullName, nameof(Sources));
}
return Sources2;
}
/// <summary>
/// Makes sure the call is made from one of the approved sources and not from one
/// of the prohibited sources.
/// </summary>
/// <param name="Sources">The stack trace from the original call is checked for approved
/// or prohibited sources to assert the code can proceed.</param>
public static void CallFromSource(params ICallStackCheck[] Sources)
{
CallFromSource(new ICallStackCheck[][] { Sources });
}
/// <summary>
/// Makes sure the call is made from one of the approved sources and not from one
/// of the prohibited sources.
/// </summary>
/// <param name="SourcesByPriority">The stack trace from the original call is checked
/// for approved or prohibited sources to assert the code can proceed. The Sources
/// lists are evaluated in order, and the first list resulting in a conclusion is
/// used.</param>
public static void CallFromSource(params ICallStackCheck[][] SourcesByPriority)
{
FrameInformation FrameInfo;
int Skip = 1;
bool WaherPersistence = false;
bool WaherRuntime = false;
bool AsynchTask = false;
bool Other = false;
while (true)
{
FrameInfo = new FrameInformation(new StackFrame(Skip));
if (FrameInfo.Last)
break;
if (FrameInfo.Valid && FrameInfo.Type != typeof(Assert))
break;
Skip++;
}
int Caller = Skip;
bool Prohibited = false;
bool? Status;
foreach (ICallStackCheck[] Sources in SourcesByPriority)
{
Skip = Caller + 1;
while (!Prohibited)
{
FrameInfo = new FrameInformation(new StackFrame(Skip++));
if (FrameInfo.Last)
break;
if (!FrameInfo.Valid)
continue;
foreach (ICallStackCheck Source in Sources)
{
Status = Source.Check(FrameInfo);
if (Status.HasValue)
{
if (Status.Value)
return;
else
{
Prohibited = true;
break;
}
}
}
if (Prohibited)
break;
if (!Other || !AsynchTask || !WaherPersistence)
{
if (string.IsNullOrEmpty(FrameInfo.Assembly.Location))
{
if (FrameInfo.AssemblyName.StartsWith("WPSA."))
WaherPersistence = true;
else
Other = true;
}
else
{
string AssemblyName = FrameInfo.AssemblyName;
switch (AssemblyName)
{
case "Waher.Persistence.Serialization.Compiled":
AssemblyName = "Waher.Persistence.Serialization";
break;
case "Waher.Persistence.FilesLW":
AssemblyName = "Waher.Persistence.Files";
break;
default:
if (AssemblyName.EndsWith(".UWP"))
AssemblyName = AssemblyName.Substring(0, AssemblyName.Length - 4);
break;
}
if (FrameInfo.Type == typeof(System.Threading.Tasks.Task))
AsynchTask = true;
else if (FrameInfo.TypeName.StartsWith(AssemblyName) &&
FrameInfo.AssemblyName + "." == Path.ChangeExtension(Path.GetFileName(FrameInfo.Assembly.Location), string.Empty))
{
if (FrameInfo.AssemblyName.StartsWith("Waher.Persistence.") ||
FrameInfo.AssemblyName.StartsWith("WPSA."))
{
WaherPersistence = true;
}
else if (FrameInfo.AssemblyName.StartsWith("Waher.Runtime."))
WaherRuntime = true;
else if (FrameInfo.AssemblyName.StartsWith("WPSA."))
WaherPersistence = true;
else if (!FrameInfo.AssemblyName.StartsWith("Waher.") && !FrameInfo.AssemblyName.StartsWith("System."))
Other = true;
}
else if (!Path.GetFileName(FrameInfo.Assembly.Location).StartsWith("System."))
Other = true;
}
}
}
if (Prohibited)
break;
}
// In asynch call - stack trace not showing asynchronous call stack after JIT.
// If loading from database, i.e. populating object asynchronously, (possibly,
// check is vulnerable), give check a pass. Access will be restricted at a later
// stage, when accessing properties synchronously.
if (!Prohibited && AsynchTask && (WaherPersistence || WaherRuntime) && !Other)
return;
FrameInfo = new FrameInformation(new StackFrame(Skip = Caller));
string ObjectId = FrameInfo.Type.FullName + "." + FrameInfo.Method.Name;
StackTrace Trace = new StackTrace(Skip, false);
UnauthorizedAccessEventArgs e = new UnauthorizedAccessEventArgs(FrameInfo, Trace);
List<KeyValuePair<string, object>> Tags = new List<KeyValuePair<string, object>>()
{
new KeyValuePair<string, object>("Method", FrameInfo.Method.Name),
new KeyValuePair<string, object>("Type", FrameInfo.Type.FullName),
new KeyValuePair<string, object>("Assembly", FrameInfo.Assembly.FullName)
};
Skip = 0;
while (true)
{
FrameInfo = new FrameInformation(new StackFrame(Skip));
if (FrameInfo.Last)
break;
if (FrameInfo.Valid)
{
Tags.Add(new KeyValuePair<string, object>("Pos" + Skip.ToString(),
FrameInfo.Assembly.GetName().Name + ", " + FrameInfo.TypeName + ", " +
FrameInfo.Method.Name));
}
else
Tags.Add(new KeyValuePair<string, object>("Pos" + Skip.ToString(), FrameInfo.ToString()));
Skip++;
}
Log.Warning("Unauthorized access detected and prevented.", ObjectId, string.Empty, "UnauthorizedAccess", EventLevel.Major,
string.Empty, e.Assembly.FullName, Trace.ToString(), Tags.ToArray());
UnauthorizedAccess?.Raise(null, e);
throw new UnauthorizedCallstackException("Unauthorized access.");
}
/// <summary>
/// Event raised when an unauthorized access has been detected.
/// </summary>
public static event EventHandler<UnauthorizedAccessEventArgs> UnauthorizedAccess = null;
}
}