3using System.Diagnostics.CodeAnalysis;
4using System.Threading.Tasks;
6using System.Xml.Schema;
25 public const string Namespace =
"http://waher.se/Schema/WAF.xsd";
34 private readonly
string fileName;
35 private readonly
string appDataFolder;
36 private Dictionary<string, WafAction> actionsById =
new Dictionary<string, WafAction>();
37 private Dictionary<int, RedirectionInfo> redirections =
new Dictionary<int, RedirectionInfo>();
38 private LinkedList<RedirectionInfo> pendingRedirections =
new LinkedList<RedirectionInfo>();
40 private int redirectionCounter = 0;
83 if (this.root is
null)
84 throw new Exception(
"Invalid root element of WAF definition.");
87 TimeSpan.FromDays(1), TimeSpan.FromDays(1))
89 MaxTimerIntervalMs = 1000
129 internal void RegisterAction(
WafAction Action)
131 if (!
string.IsNullOrEmpty(Action.Id))
132 this.actionsById[Action.Id] = Action;
145 this.internalCache.Dispose();
156 return this.actionsById.TryGetValue(Id, out Action);
188 Dictionary<string, WafAction> Bak = this.actionsById; ;
189 this.actionsById =
new Dictionary<string, WafAction>();
193 this.actionsById = Bak;
194 throw new Exception(
"Invalid root element of WAF definition.");
200 this.internalCache.Clear();
202 return Task.CompletedTask;
205 internal WafResult GetRedirectionResult(RedirectionType Type,
string Location)
207 DateTime Limit = DateTime.UtcNow.AddMinutes(-1);
209 lock (this.redirections)
211 RedirectionInfo Info;
213 while (!(this.pendingRedirections.First is
null) &&
214 (Info =
this.pendingRedirections.First.Value).Created <= Limit)
216 this.redirections.Remove(Info.Index);
217 this.pendingRedirections.RemoveFirst();
220 int Index = --this.redirectionCounter;
222 Index = this.redirectionCounter = -1;
224 Info =
new RedirectionInfo(Type, Location, Index);
225 this.redirections[Index] = Info;
226 Info.Node = this.pendingRedirections.AddLast(Info);
240 lock (this.redirections)
242 if (!this.redirections.TryGetValue((
int)Result, out RedirectionInfo Info))
248 this.redirections.Remove((
int)Result);
249 this.pendingRedirections.Remove(Info.Node);
253 case RedirectionType.TemporaryRedirection:
258 case RedirectionType.PermanentRedirection:
262 case RedirectionType.SeeOther:
Helps with common XML-related tasks.
static XmlDocument LoadFromFile(string FileName)
Loads an XML document from a file.
Static class managing loading of XSL resources stored as embedded resources or in content files.
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
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.
Base class of all HTTP Exceptions.
Represents an HTTP request.
Base class for all HTTP resources.
This means that the resource is now permanently located at another URI, specified by the Location: HT...
The response to the request can be found under a different URI and SHOULD be retrieved using a GET me...
The requested resource resides temporarily under a different URI. Since the redirection MAY be altere...
Implements an in-memory cache.
Contains the current state of a review process.
Root object of Web Application Firewall rules.
WafResult DefaultResult
Default result.
override Task< WafResult?> Review(ProcessingState State)
Reviews the processing state, and returns a WAF result, if any.
Abstract base class for Web Application Firewall actions.
static WafAction Parse(XmlElement Xml, WafAction Parent, WebApplicationFirewall Document)
Parses an XML Element defining a WAF action.
override void Prepare()
Prepares the node for processing.
Web Application Firewall for HttpServer.
Task Reload()
Reloads the firewall configuration from its original source.
WebApplicationFirewall(string Xml, string FileName, ILoginAuditor LoginAuditor, string AppDataFolder)
Web Application Firewall for HttpServer.
string AppDataFolder
Application data folder, where content files are stored.
ILoginAuditor LoginAuditor
Login Auditor used by the Firewall.
const string Namespace
http://waher.se/Schema/WAF.xsd
static WebApplicationFirewall LoadFromFile(string FileName, ILoginAuditor LoginAuditor, string AppDataFolder)
Loads a WAF definition from file.
bool TryGetRedirection(WafResult Result, out HttpException Redirection)
Tries to get a redirection, if the result is a redirection result.
bool TryGetActionById(string Id, [NotNullWhen(true)] out WafAction Action)
Tries to get an action node by its ID.
async Task< WafResult > Review(HttpRequest Request, HttpResource Resource)
Reviews an incoming request.
void Dispose()
Disposes of the WAF.
WebApplicationFirewall(XmlDocument Xml, string FileName, ILoginAuditor LoginAuditor, string AppDataFolder)
Web Application Firewall for HttpServer.
WebApplicationFirewall(XmlElement Xml, string FileName, ILoginAuditor LoginAuditor, string AppDataFolder)
Web Application Firewall for HttpServer.
Interface for Web Application Firewalls (WAF).
Interface for classes that monitor login events, and help applications determine malicious intent.
WafResult
Actions that a Web Server can take after reviewing a request.