SEBWIN-314: Started with network filter implementation.

This commit is contained in:
dbuechel 2019-08-29 10:29:19 +02:00
parent d54d7c17dc
commit 938afcb4a7
12 changed files with 290 additions and 0 deletions

View file

@ -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<IDictionary<string, object>> 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);
}
}
}
}
}
}

View file

@ -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)

View file

@ -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

View file

@ -81,6 +81,9 @@
<Compile Include="ConfigurationData\DataMapper.Input.cs">
<DependentUpon>DataMapper.cs</DependentUpon>
</Compile>
<Compile Include="ConfigurationData\DataMapper.Network.cs">
<DependentUpon>DataMapper.cs</DependentUpon>
</Compile>
<Compile Include="ConfigurationData\DataMapper.UserInterface.cs">
<DependentUpon>DataMapper.cs</DependentUpon>
</Compile>

View file

@ -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
{
/// <summary>
/// Defines all configuration options for network functionality.
/// </summary>
[Serializable]
public class NetworkSettings
{
/// <summary>
/// Defines whether all content requests for a web page should be filtered according to the defined <see cref="FilterRules"/>.
/// </summary>
public bool EnableContentRequestFilter { get; set; }
/// <summary>
/// Defines whether the main request for a web page should be filtered according to the defined <see cref="FilterRules"/>.
/// </summary>
public bool EnableMainRequestFilter { get; set; }
/// <summary>
/// Defines all rules to be used to filter network requests.
/// </summary>
public IList<FilterRule> FilterRules { get; set; }
public NetworkSettings()
{
FilterRules = new List<FilterRule>();
}
}
}

View file

@ -67,6 +67,11 @@ namespace SafeExamBrowser.Contracts.Configuration.Settings
/// </summary>
public MouseSettings Mouse { get; set; }
/// <summary>
/// All network-related settings.
/// </summary>
public NetworkSettings Network { get; set; }
/// <summary>
/// The hash code of the quit password.
/// </summary>
@ -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();
}

View file

@ -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
{
/// <summary>
/// Defines all possible results of a request filter operation.
/// </summary>
public enum FilterResult
{
/// <summary>
/// Indicates that a request should be allowed.
/// </summary>
Allow,
/// <summary>
/// Indicates that a request should be blocked.
/// </summary>
Block
}
}

View file

@ -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
{
/// <summary>
/// Defines a network filter rule.
/// </summary>
[Serializable]
public class FilterRule
{
/// <summary>
/// The expression according to which requests should be filtered.
/// </summary>
public string Expression { get; set; }
/// <summary>
/// The filter result to be used when the <see cref="Expression"/> matches.
/// </summary>
public FilterResult Result { get; set; }
/// <summary>
/// The filter type which defines how the <see cref="Expression"/> is processed.
/// </summary>
public FilterType Type { get; set; }
}
}

View file

@ -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
{
/// <summary>
/// Defines all possible request filter types.
/// </summary>
public enum FilterType
{
/// <summary>
/// The filter is based on a regular expression.
/// </summary>
Regex,
/// <summary>
/// The filter is based on a simplified expression with wildcards.
/// </summary>
Simplified
}
}

View file

@ -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
{
/// <summary>
/// Defines a filter to process network requests.
/// </summary>
public interface IRequestFilter
{
/// <summary>
/// Defines the default result to be returned by <see cref="Process(Request)"/> if no filter rule matches.
/// </summary>
FilterResult Default { set; }
/// <summary>
/// Loads the given <see cref="FilterRule"/> to be used when processing a request.
/// </summary>
void Load(FilterRule rule);
/// <summary>
/// Filters the given request according to the loaded <see cref="FilterRule"/>.
/// </summary>
FilterResult Process(Request request);
}
}

View file

@ -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
{
/// <summary>
/// Defines a network request.
/// </summary>
public class Request
{
/// <summary>
/// The uri of the request.
/// </summary>
public Uri Uri { get; set; }
}
}

View file

@ -86,6 +86,8 @@
<Compile Include="Configuration\Settings\ActionCenterSettings.cs" />
<Compile Include="Configuration\Settings\AudioSettings.cs" />
<Compile Include="Configuration\Settings\BrowserWindowSettings.cs" />
<Compile Include="Network\Filter\FilterRule.cs" />
<Compile Include="Configuration\Settings\NetworkSettings.cs" />
<Compile Include="Configuration\Settings\ServiceSettings.cs" />
<Compile Include="Configuration\Settings\UserInterfaceMode.cs" />
<Compile Include="Applications\Events\IconChangedEventHandler.cs" />
@ -100,6 +102,10 @@
<Compile Include="Lockdown\IFeatureConfigurationFactory.cs" />
<Compile Include="Lockdown\IFeatureConfigurationMonitor.cs" />
<Compile Include="Lockdown\ISystemConfigurationUpdate.cs" />
<Compile Include="Network\Filter\FilterType.cs" />
<Compile Include="Network\Filter\IRequestFilter.cs" />
<Compile Include="Network\Filter\FilterResult.cs" />
<Compile Include="Network\Request.cs" />
<Compile Include="Runtime\IRuntimeController.cs" />
<Compile Include="Core\OperationModel\Events\ActionRequiredEventArgs.cs" />
<Compile Include="Core\OperationModel\Events\ActionRequiredEventHandler.cs" />