-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathWebApplicationFirewall.cs
More file actions
202 lines (177 loc) · 6.31 KB
/
WebApplicationFirewall.cs
File metadata and controls
202 lines (177 loc) · 6.31 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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using Waher.Content.Xml;
using Waher.Content.Xsl;
using Waher.Events;
using Waher.Networking.HTTP;
using Waher.Networking.HTTP.Interfaces;
using Waher.Runtime.Cache;
using Waher.Security.WAF.Model;
namespace Waher.Security.WAF
{
/// <summary>
/// Web Application Firewall for <see cref="HttpServer"/>.
/// </summary>
public class WebApplicationFirewall : IWebApplicationFirewall, IDisposable
{
/// <summary>
/// http://waher.se/Schema/WAF.xsd
/// </summary>
public const string Namespace = "http://waher.se/Schema/WAF.xsd";
/// <summary>
/// Schema to validate WAF XML files.
/// </summary>
private static readonly XmlSchema schema = XSL.LoadSchema(typeof(WebApplicationFirewall).Namespace + ".Schema.WAF.xsd");
private readonly Cache<string, object> internalCache;
private readonly ILoginAuditor loginAuditor;
private readonly string fileName;
private readonly string appDataFolder;
private Dictionary<string, WafAction> actionsById = new Dictionary<string, WafAction>();
private Root root;
/// <summary>
/// Web Application Firewall for <see cref="HttpServer"/>.
/// </summary>
/// <param name="Xml">XML Definition</param>
/// <param name="FileName">WAF file name.</param>
/// <param name="LoginAuditor">Login Auditor used by the Firewall.</param>
/// <param name="AppDataFolder">Application data folder, where content files are stored.</param>
public WebApplicationFirewall(string Xml, string FileName, ILoginAuditor LoginAuditor,
string AppDataFolder)
: this(XML.ParseXml(Xml, true), FileName, LoginAuditor, AppDataFolder)
{
}
/// <summary>
/// Web Application Firewall for <see cref="HttpServer"/>.
/// </summary>
/// <param name="Xml">XML Definition</param>
/// <param name="FileName">WAF file name.</param>
/// <param name="LoginAuditor">Login Auditor used by the Firewall.</param>
/// <param name="AppDataFolder">Application data folder, where content files are stored.</param>
public WebApplicationFirewall(XmlDocument Xml, string FileName, ILoginAuditor LoginAuditor,
string AppDataFolder)
: this(Xml.DocumentElement, FileName, LoginAuditor, AppDataFolder)
{
}
/// <summary>
/// Web Application Firewall for <see cref="HttpServer"/>.
/// </summary>
/// <param name="Xml">XML Definition</param>
/// <param name="FileName">WAF file name.</param>
/// <param name="LoginAuditor">Login Auditor used by the Firewall.</param>
/// <param name="AppDataFolder">Application data folder, where content files are stored.</param>
public WebApplicationFirewall(XmlElement Xml, string FileName, ILoginAuditor LoginAuditor,
string AppDataFolder)
{
this.fileName = FileName;
this.appDataFolder = AppDataFolder;
this.loginAuditor = LoginAuditor;
this.root = WafAction.Parse(Xml, null, this) as Root;
if (this.root is null)
throw new Exception("Invalid root element of WAF definition.");
this.internalCache = new Cache<string, object>(int.MaxValue,
TimeSpan.FromDays(1), TimeSpan.FromDays(1))
{
MaxTimerIntervalMs = 1000
};
this.root.Prepare();
}
/// <summary>
/// Loads a WAF definition from file.
/// </summary>
/// <param name="FileName">WAF file name.</param>
/// <param name="LoginAuditor">Login Auditor used by the Firewall.</param>
/// <param name="AppDataFolder">Application data folder, where content files are stored.</param>
/// <returns>Parsed Web Application Firewall.</returns>
public static WebApplicationFirewall LoadFromFile(string FileName, ILoginAuditor LoginAuditor,
string AppDataFolder)
{
XmlDocument Doc = XML.LoadFromFile(FileName, true);
XSL.Validate(FileName, Doc, nameof(Root), Namespace, schema);
return new WebApplicationFirewall(Doc, FileName, LoginAuditor, AppDataFolder);
}
/// <summary>
/// File Name
/// </summary>
public string FileName => this.fileName;
/// <summary>
/// Application data folder, where content files are stored.
/// </summary>
public string AppDataFolder => this.appDataFolder;
/// <summary>
/// Login Auditor used by the Firewall.
/// </summary>
public ILoginAuditor LoginAuditor => this.loginAuditor;
/// <summary>
/// Registers an action by its ID.
/// </summary>
/// <param name="Action">Action</param>
internal void RegisterAction(WafAction Action)
{
if (!string.IsNullOrEmpty(Action.Id))
this.actionsById[Action.Id] = Action;
}
/// <summary>
/// Internal cache.
/// </summary>
internal Cache<string, object> Cache => this.internalCache;
/// <summary>
/// Disposes of the WAF.
/// </summary>
public void Dispose()
{
this.internalCache.Dispose();
}
/// <summary>
/// Tries to get an action node by its ID.
/// </summary>
/// <param name="Id">Action node ID</param>
/// <param name="Action">Action, if found.</param>
/// <returns>If an action was found.</returns>
public bool TryGetActionById(string Id, [NotNullWhen(true)] out WafAction Action)
{
return this.actionsById.TryGetValue(Id, out Action);
}
/// <summary>
/// Reviews an incoming request.
/// </summary>
/// <param name="Request">Current HTTP Request</param>
/// <param name="Resource">Corresponding HTTP Resource, if found.</param>
/// <returns>Action to take.</returns>
public async Task<WafResult> Review(HttpRequest Request, HttpResource Resource)
{
try
{
ProcessingState State = new ProcessingState(Request, Resource, this);
return await this.root.Review(State) ?? this.root.DefaultResult;
}
catch (Exception ex)
{
Log.Exception(ex);
return this.root.DefaultResult;
}
}
/// <summary>
/// Reloads the firewall configuration from its original source.
/// </summary>
public Task Reload()
{
XmlDocument Doc = XML.LoadFromFile(this.fileName, true);
XSL.Validate(this.fileName, Doc, nameof(Root), Namespace, schema);
Dictionary<string, WafAction> Bak = this.actionsById;;
this.actionsById = new Dictionary<string, WafAction>();
if (!(WafAction.Parse(Doc.DocumentElement, null, this) is Root Root2))
{
this.actionsById = Bak;
throw new Exception("Invalid root element of WAF definition.");
}
this.root = Root2;
this.root.Prepare();
this.internalCache.Clear();
return Task.CompletedTask;
}
}
}