4using System.Text.RegularExpressions;
5using System.Threading.Tasks;
54 if (await this.
Applies(State, Property))
70 string Value = await this.value.EvaluateAsync(
Variables,
string.Empty);
75 Regex Regex = GetRegex(Value,
false);
76 Match M = Regex.Match(Property);
80 Regex = GetRegex(Value,
false);
81 M = Regex.Match(Property);
82 return M.Success && M.Index == 0;
85 Regex = GetRegex(Value,
false);
86 M = Regex.Match(Property);
87 return M.Success && M.Index + M.Length == Property.Length;
90 Regex = GetRegex(Value,
false);
91 M = Regex.Match(Property);
92 return M.Success && M.Index == 0 && M.Length == Property.Length;
95 return Property.Contains(Value, StringComparison.CurrentCulture);
98 return Property.StartsWith(Value, StringComparison.CurrentCulture);
101 return Property.EndsWith(Value, StringComparison.CurrentCulture);
104 return Property.Equals(Value, StringComparison.CurrentCulture);
107 Regex = GetRegex(Value,
true);
108 M = Regex.Match(Property);
112 Regex = GetRegex(Value,
true);
113 M = Regex.Match(Property);
114 return M.Success && M.Index == 0;
117 Regex = GetRegex(Value,
true);
118 M = Regex.Match(Property);
119 return M.Success && M.Index + M.Length == Property.Length;
122 Regex = GetRegex(Value,
true);
123 M = Regex.Match(Property);
124 return M.Success && M.Index == 0 && M.Length == Property.Length;
127 return Property.Contains(Value, StringComparison.CurrentCultureIgnoreCase);
130 return Property.StartsWith(Value, StringComparison.CurrentCultureIgnoreCase);
133 return Property.EndsWith(Value, StringComparison.CurrentCultureIgnoreCase);
136 return Property.Equals(Value, StringComparison.CurrentCultureIgnoreCase);
141 return Result is
bool b && b;
144 Dictionary<string, bool> List = GetList(Value,
false);
145 return List.ContainsKey(Property);
148 List = GetList(Value,
true);
149 return List.ContainsKey(Property);
152 List = await this.GetFileList(Value,
false);
153 return List.ContainsKey(Property);
156 List = await this.GetFileList(Value,
true);
157 return List.ContainsKey(Property);
160 string Key =
"DB|" + Value +
"|" + Property;
161 if (State.TryGetCachedObject(Key, out
bool Included))
169 Included = !(DbEntry is
null);
174 Key =
"DBCI|" + Value +
"|" + Property;
175 if (State.TryGetCachedObject(Key, out Included))
183 Included = !(DbEntry2 is
null);
192 private static Regex GetRegex(
string Pattern,
bool IgnoreCase)
194 Dictionary<string, Regex> Expressions = IgnoreCase ?
195 regularExpressionsIgnoreCase : regularExpressions;
199 if (!Expressions.TryGetValue(Pattern, out Regex Result))
201 RegexOptions Options = RegexOptions.Compiled | RegexOptions.Singleline;
203 Options |= RegexOptions.IgnoreCase;
205 Result =
new Regex(Pattern, Options);
206 Expressions[Pattern] = Result;
213 private static Expression GetExpression(
string s)
217 if (!expressions.TryGetValue(s, out
Expression Result))
220 expressions[s] = Result;
227 private static Dictionary<string, bool> GetList(
string Pattern,
bool IgnoreCase)
229 Dictionary<string, Dictionary<string, bool>> Lists = IgnoreCase ?
230 listsIgnoreCase : lists;
234 if (!Lists.TryGetValue(Pattern, out Dictionary<string, bool> Result))
236 Result =
new Dictionary<string, bool>(IgnoreCase ?
237 StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture);
239 foreach (
string Part
in Pattern.Split(
',', StringSplitOptions.RemoveEmptyEntries))
240 Result[Part.Trim()] =
true;
242 Lists[Pattern] = Result;
249 private async Task<Dictionary<string, bool>> GetFileList(
string FileName,
bool IgnoreCase)
251 Dictionary<string, FileListInfo> Lists = IgnoreCase ?
252 fileListsIgnoreCase : fileLists;
255 DateTime Check = DateTime.UtcNow;
256 DateTime LastWriteTime = DateTime.MinValue;
260 if (Lists.TryGetValue(FileName, out Result))
262 if (Check.Subtract(Result.LastTimeCheckUtc).TotalMinutes <= 1)
263 return Result.Values;
265 LastWriteTime = File.GetLastWriteTimeUtc(Result.FullPath);
266 if (LastWriteTime == Result.LastWriteTimeUtc)
268 Result.LastTimeCheckUtc = Check;
269 return Result.Values;
275 string FileContent = await File.ReadAllTextAsync(FullFileName);
277 if (LastWriteTime == DateTime.MinValue)
278 LastWriteTime = File.GetLastWriteTimeUtc(FullFileName);
280 Dictionary<string, bool> List =
new Dictionary<string, bool>(IgnoreCase ?
281 StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture);
283 Result =
new FileListInfo()
285 FullPath = FullFileName,
286 LastWriteTimeUtc = LastWriteTime,
287 LastTimeCheckUtc = Check,
291 foreach (
string Row
in FileContent.Split(
CommonTypes.
CRLF, StringSplitOptions.RemoveEmptyEntries))
292 List[Row.Trim()] =
true;
296 Lists[FileName] = Result;
299 return Result.Values;
302 private static readonly Dictionary<string, Regex> regularExpressions =
new Dictionary<string, Regex>();
303 private static readonly Dictionary<string, Regex> regularExpressionsIgnoreCase =
new Dictionary<string, Regex>();
304 private static readonly Dictionary<string, Expression> expressions =
new Dictionary<string, Expression>();
305 private static readonly Dictionary<string, Dictionary<string, bool>> lists =
new Dictionary<string, Dictionary<string, bool>>();
306 private static readonly Dictionary<string, Dictionary<string, bool>> listsIgnoreCase =
new Dictionary<string, Dictionary<string, bool>>();
307 private static readonly Dictionary<string, FileListInfo> fileLists =
new Dictionary<string, FileListInfo>();
308 private static readonly Dictionary<string, FileListInfo> fileListsIgnoreCase =
new Dictionary<string, FileListInfo>();
310 private class FileListInfo
312 public string FullPath;
313 public Dictionary<string, bool> Values;
314 public DateTime LastWriteTimeUtc;
315 public DateTime LastTimeCheckUtc;
Helps with parsing of commong data types.
static readonly char[] CRLF
Contains the CR LF character sequence.
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
This filter selects objects that conform to all child-filters provided.
This filter selects objects that have a named field equal to a given value.
Class managing a script expression.
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Abstract base class for Web Application Firewall comparisons.
Abstract base class for Web Application Firewall conditions.
async Task< bool > Applies(ProcessingState State, string Property)
Checks if the condition applies.
async Task< WafResult?> Review(ProcessingState State, string Property)
Reviews the processing state, and returns a WAF result, if any.
WafCondition(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Abstract base class for Web Application Firewall conditions.
WafCondition()
Abstract base class for Web Application Firewall conditions.
Contains information about a property in a database list.
Contains information about a property in a database list.
Contains the current state of a review process.
Variables Variables
Set of variables.
void AddToCache(string Key, object Value)
Adds a value to the cache.
Abstract base class for Web Application Firewall actions.
WebApplicationFirewall Document
Web Application Firewall document.
static readonly TimeSpan fiveMinutes
Five minutes time span.
async Task< WafResult?> ReviewChildren(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Web Application Firewall for HttpServer.
string AppDataFolder
Application data folder, where content files are stored.
ComparisonMode
Condition comparison mode.