-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathApproveRegex.cs
More file actions
61 lines (54 loc) · 1.58 KB
/
ApproveRegex.cs
File metadata and controls
61 lines (54 loc) · 1.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
using System;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Waher.Security.CallStack
{
/// <summary>
/// Checks for approved sources in the call stack using a regular expression.
/// </summary>
public class ApproveRegex : CallStackCheck
{
private readonly Regex regex;
/// <summary>
/// Checks for approved sources in the call stack using a regular expression.
/// </summary>
public ApproveRegex(Regex Regex)
{
this.regex = Regex;
}
/// <summary>
/// Performs a check of a stack frame.
/// </summary>
/// <param name="Frame">Stack frame.</param>
/// <returns>
/// true, if frame represents a valid frame, and the check can conclude positively.
/// false, if frame represents a prohibited frame, and the check can conclude negatively.
/// null, if the frame is not conclusive, and the check should continue with the next frame.
/// </returns>
public override bool? Check(FrameInformation Frame)
{
if (IsMatch(this.regex, Frame.TypeName + "." + Frame.Method.Name) ||
IsMatch(this.regex, Frame.TypeName) ||
IsMatch(this.regex, Frame.AssemblyName))
{
return true;
}
Type T = Frame.Type;
while (!(T is null) && T.Attributes.HasFlag(TypeAttributes.NestedPrivate))
{
T = T.DeclaringType;
if (IsMatch(this.regex, T.FullName + "." + Frame.Method.Name) ||
IsMatch(this.regex, T.FullName))
{
return true;
}
}
return null;
}
internal static bool IsMatch(Regex Regex, string s)
{
Match M = Regex.Match(s);
return M.Success && M.Index == 0 && M.Length == s.Length;
}
}
}