diff --git a/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.Network.cs b/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.Network.cs new file mode 100644 index 00000000..9a9da810 --- /dev/null +++ b/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.Network.cs @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.Collections.Generic; +using SafeExamBrowser.Contracts.Configuration.Settings; +using SafeExamBrowser.Contracts.Network.Filter; + +namespace SafeExamBrowser.Configuration.ConfigurationData +{ + internal partial class DataMapper + { + private void MapEnableContentRequestFilter(Settings settings, object value) + { + if (value is bool enable) + { + settings.Network.EnableContentRequestFilter = enable; + } + } + + private void MapEnableMainRequestFilter(Settings settings, object value) + { + if (value is bool enable) + { + settings.Network.EnableMainRequestFilter = enable; + } + } + + private void MapUrlFilterRules(Settings settings, object value) + { + const int ALLOW = 1; + + if (value is IEnumerable> ruleDataList) + { + foreach (var ruleData in ruleDataList) + { + if (ruleData.TryGetValue(Keys.Network.Filter.RuleIsActive, out var v) && v is bool active && active) + { + var rule = new FilterRule(); + + if (ruleData.TryGetValue(Keys.Network.Filter.RuleExpression, out v) && v is string expression) + { + rule.Expression = expression; + } + + if (ruleData.TryGetValue(Keys.Network.Filter.RuleAction, out v) && v is int action) + { + rule.Result = action == ALLOW ? FilterResult.Allow : FilterResult.Block ; + } + + if (ruleData.TryGetValue(Keys.Network.Filter.RuleExpressionIsRegex, out v) && v is bool regex) + { + rule.Type = regex ? FilterType.Regex : FilterType.Simplified; + } + + settings.Network.FilterRules.Add(rule); + } + } + } + } + } +} diff --git a/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.cs b/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.cs index eb7bf305..38f2af60 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/DataMapper.cs @@ -22,6 +22,7 @@ namespace SafeExamBrowser.Configuration.ConfigurationData MapConfigurationFileSettings(item.Key, item.Value, settings); MapGeneralSettings(item.Key, item.Value, settings); MapInputSettings(item.Key, item.Value, settings); + MapNetworkSettings(item.Key, item.Value, settings); MapSecuritySettings(item.Key, item.Value, settings); MapUserInterfaceSettings(item.Key, item.Value, settings); } @@ -192,6 +193,22 @@ namespace SafeExamBrowser.Configuration.ConfigurationData } } + private void MapNetworkSettings(string key, object value, Settings settings) + { + switch (key) + { + case Keys.Network.Filter.EnableContentRequestFilter: + MapEnableContentRequestFilter(settings, value); + break; + case Keys.Network.Filter.EnableMainRequestFilter: + MapEnableMainRequestFilter(settings, value); + break; + case Keys.Network.Filter.UrlFilterRules: + MapUrlFilterRules(settings, value); + break; + } + } + private void MapSecuritySettings(string key, object value, Settings settings) { switch (key) diff --git a/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs b/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs index 469827f2..9ca8d937 100644 --- a/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs +++ b/SafeExamBrowser.Configuration/ConfigurationData/Keys.cs @@ -113,6 +113,17 @@ namespace SafeExamBrowser.Configuration.ConfigurationData internal const string CertificateType = "type"; internal const string EmbeddedCertificates = "embeddedCertificates"; } + + internal static class Filter + { + internal const string EnableContentRequestFilter = "URLFilterEnableContentFilter"; + internal const string EnableMainRequestFilter = "URLFilterEnable"; + internal const string RuleAction = "action"; + internal const string RuleIsActive = "active"; + internal const string RuleExpression = "expression"; + internal const string RuleExpressionIsRegex = "regex"; + internal const string UrlFilterRules = "URLFilterRules"; + } } internal static class Registry diff --git a/SafeExamBrowser.Configuration/SafeExamBrowser.Configuration.csproj b/SafeExamBrowser.Configuration/SafeExamBrowser.Configuration.csproj index 27b02d5f..a6c02698 100644 --- a/SafeExamBrowser.Configuration/SafeExamBrowser.Configuration.csproj +++ b/SafeExamBrowser.Configuration/SafeExamBrowser.Configuration.csproj @@ -81,6 +81,9 @@ DataMapper.cs + + DataMapper.cs + DataMapper.cs diff --git a/SafeExamBrowser.Contracts/Configuration/Settings/NetworkSettings.cs b/SafeExamBrowser.Contracts/Configuration/Settings/NetworkSettings.cs new file mode 100644 index 00000000..72110f47 --- /dev/null +++ b/SafeExamBrowser.Contracts/Configuration/Settings/NetworkSettings.cs @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; +using System.Collections.Generic; +using SafeExamBrowser.Contracts.Network.Filter; + +namespace SafeExamBrowser.Contracts.Configuration.Settings +{ + /// + /// Defines all configuration options for network functionality. + /// + [Serializable] + public class NetworkSettings + { + /// + /// Defines whether all content requests for a web page should be filtered according to the defined . + /// + public bool EnableContentRequestFilter { get; set; } + + /// + /// Defines whether the main request for a web page should be filtered according to the defined . + /// + public bool EnableMainRequestFilter { get; set; } + + /// + /// Defines all rules to be used to filter network requests. + /// + public IList FilterRules { get; set; } + + public NetworkSettings() + { + FilterRules = new List(); + } + } +} diff --git a/SafeExamBrowser.Contracts/Configuration/Settings/Settings.cs b/SafeExamBrowser.Contracts/Configuration/Settings/Settings.cs index ac9fd0ed..33d6e08c 100644 --- a/SafeExamBrowser.Contracts/Configuration/Settings/Settings.cs +++ b/SafeExamBrowser.Contracts/Configuration/Settings/Settings.cs @@ -67,6 +67,11 @@ namespace SafeExamBrowser.Contracts.Configuration.Settings /// public MouseSettings Mouse { get; set; } + /// + /// All network-related settings. + /// + public NetworkSettings Network { get; set; } + /// /// The hash code of the quit password. /// @@ -94,6 +99,7 @@ namespace SafeExamBrowser.Contracts.Configuration.Settings Browser = new BrowserSettings(); Keyboard = new KeyboardSettings(); Mouse = new MouseSettings(); + Network = new NetworkSettings(); Service = new ServiceSettings(); Taskbar = new TaskbarSettings(); } diff --git a/SafeExamBrowser.Contracts/Network/Filter/FilterResult.cs b/SafeExamBrowser.Contracts/Network/Filter/FilterResult.cs new file mode 100644 index 00000000..a99504d8 --- /dev/null +++ b/SafeExamBrowser.Contracts/Network/Filter/FilterResult.cs @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +namespace SafeExamBrowser.Contracts.Network.Filter +{ + /// + /// Defines all possible results of a request filter operation. + /// + public enum FilterResult + { + /// + /// Indicates that a request should be allowed. + /// + Allow, + + /// + /// Indicates that a request should be blocked. + /// + Block + } +} diff --git a/SafeExamBrowser.Contracts/Network/Filter/FilterRule.cs b/SafeExamBrowser.Contracts/Network/Filter/FilterRule.cs new file mode 100644 index 00000000..aec3129b --- /dev/null +++ b/SafeExamBrowser.Contracts/Network/Filter/FilterRule.cs @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; + +namespace SafeExamBrowser.Contracts.Network.Filter +{ + /// + /// Defines a network filter rule. + /// + [Serializable] + public class FilterRule + { + /// + /// The expression according to which requests should be filtered. + /// + public string Expression { get; set; } + + /// + /// The filter result to be used when the matches. + /// + public FilterResult Result { get; set; } + + /// + /// The filter type which defines how the is processed. + /// + public FilterType Type { get; set; } + } +} diff --git a/SafeExamBrowser.Contracts/Network/Filter/FilterType.cs b/SafeExamBrowser.Contracts/Network/Filter/FilterType.cs new file mode 100644 index 00000000..f79fa6b2 --- /dev/null +++ b/SafeExamBrowser.Contracts/Network/Filter/FilterType.cs @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +namespace SafeExamBrowser.Contracts.Network.Filter +{ + /// + /// Defines all possible request filter types. + /// + public enum FilterType + { + /// + /// The filter is based on a regular expression. + /// + Regex, + + /// + /// The filter is based on a simplified expression with wildcards. + /// + Simplified + } +} diff --git a/SafeExamBrowser.Contracts/Network/Filter/IRequestFilter.cs b/SafeExamBrowser.Contracts/Network/Filter/IRequestFilter.cs new file mode 100644 index 00000000..f46a4e22 --- /dev/null +++ b/SafeExamBrowser.Contracts/Network/Filter/IRequestFilter.cs @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +namespace SafeExamBrowser.Contracts.Network.Filter +{ + /// + /// Defines a filter to process network requests. + /// + public interface IRequestFilter + { + /// + /// Defines the default result to be returned by if no filter rule matches. + /// + FilterResult Default { set; } + + /// + /// Loads the given to be used when processing a request. + /// + void Load(FilterRule rule); + + /// + /// Filters the given request according to the loaded . + /// + FilterResult Process(Request request); + } +} diff --git a/SafeExamBrowser.Contracts/Network/Request.cs b/SafeExamBrowser.Contracts/Network/Request.cs new file mode 100644 index 00000000..2d3eaba8 --- /dev/null +++ b/SafeExamBrowser.Contracts/Network/Request.cs @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System; + +namespace SafeExamBrowser.Contracts.Network +{ + /// + /// Defines a network request. + /// + public class Request + { + /// + /// The uri of the request. + /// + public Uri Uri { get; set; } + } +} diff --git a/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj b/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj index 9a64bc39..11782473 100644 --- a/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj +++ b/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj @@ -86,6 +86,8 @@ + + @@ -100,6 +102,10 @@ + + + +