SEBWIN-309: Corrected implementation of configuration key.

This commit is contained in:
dbuechel 2020-02-06 09:56:32 +01:00
parent f89b0d8a2a
commit 7df1fe5f03
7 changed files with 66 additions and 7 deletions

View file

@ -107,7 +107,7 @@ namespace SafeExamBrowser.Browser.Handlers
var headers = new NameValueCollection(request.Headers); var headers = new NameValueCollection(request.Headers);
var urlWithoutFragment = request.Url.Split('#')[0]; var urlWithoutFragment = request.Url.Split('#')[0];
var hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(urlWithoutFragment + settings.HashValue)); var hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(urlWithoutFragment + settings.HashValue));
var configurationKey = BitConverter.ToString(hash).Replace("-", string.Empty); var configurationKey = BitConverter.ToString(hash).ToLower().Replace("-", string.Empty);
// TODO: Implement Browser Exam Key calculation. // TODO: Implement Browser Exam Key calculation.
// headers["X-SafeExamBrowser-RequestHash"] = ...; // headers["X-SafeExamBrowser-RequestHash"] = ...;

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2020 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 System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SafeExamBrowser.Configuration.ConfigurationData;
using SafeExamBrowser.Settings;
namespace SafeExamBrowser.Configuration.UnitTests.ConfigurationData
{
[TestClass]
public class DataProcessorTests
{
private DataProcessor sut;
[TestInitialize]
public void Initialize()
{
sut = new DataProcessor();
}
[TestMethod]
public void MustCalculateCorrectHashValue()
{
var formatter = new BinaryFormatter();
var path1 = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(ConfigurationData)}.TestDictionary1.bin";
var path2 = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(ConfigurationData)}.TestDictionary2.bin";
var path3 = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(ConfigurationData)}.TestDictionary3.bin";
var stream1 = Assembly.GetAssembly(GetType()).GetManifestResourceStream(path1);
var stream2 = Assembly.GetAssembly(GetType()).GetManifestResourceStream(path2);
var stream3 = Assembly.GetAssembly(GetType()).GetManifestResourceStream(path3);
var data1 = formatter.Deserialize(stream1) as IDictionary<string, object>;
var data2 = formatter.Deserialize(stream2) as IDictionary<string, object>;
var data3 = formatter.Deserialize(stream3) as IDictionary<string, object>;
var settings1 = new AppSettings();
var settings2 = new AppSettings();
var settings3 = new AppSettings();
sut.Process(data1, settings1);
sut.Process(data2, settings2);
sut.Process(data3, settings3);
Assert.AreEqual("6063c3351ed1ac878c05072598d5079e30ca763c957d8e04bd45131c08f88d1a", settings1.Browser.HashValue);
Assert.AreEqual("4fc002d2ae4faf994a14bede54d95ac58a1a2cb9b59bc5b4277ff29559b46e3d", settings2.Browser.HashValue);
Assert.AreEqual("ab426e25b795c917f1fb40f7ef8e5757ef97d7c7ad6792e655c4421d47329d7a", settings3.Browser.HashValue);
}
}
}

View file

@ -87,6 +87,7 @@
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ConfigurationData\DataProcessorTests.cs" />
<Compile Include="ConfigurationRepositoryTests.cs" /> <Compile Include="ConfigurationRepositoryTests.cs" />
<Compile Include="Cryptography\HashAlgorithmTests.cs" /> <Compile Include="Cryptography\HashAlgorithmTests.cs" />
<Compile Include="Cryptography\PasswordEncryptionTests.cs" /> <Compile Include="Cryptography\PasswordEncryptionTests.cs" />
@ -106,6 +107,9 @@
</None> </None>
<EmbeddedResource Include="DataFormats\XmlTestSettings.xml" /> <EmbeddedResource Include="DataFormats\XmlTestSettings.xml" />
<EmbeddedResource Include="UnitTestCert.pfx" /> <EmbeddedResource Include="UnitTestCert.pfx" />
<EmbeddedResource Include="ConfigurationData\TestDictionary1.bin" />
<EmbeddedResource Include="ConfigurationData\TestDictionary2.bin" />
<EmbeddedResource Include="ConfigurationData\TestDictionary3.bin" />
<None Include="packages.config"> <None Include="packages.config">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</None> </None>

View file

@ -12,7 +12,6 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
using SafeExamBrowser.Settings; using SafeExamBrowser.Settings;
namespace SafeExamBrowser.Configuration.ConfigurationData namespace SafeExamBrowser.Configuration.ConfigurationData
@ -28,22 +27,23 @@ namespace SafeExamBrowser.Configuration.ConfigurationData
{ {
using (var algorithm = new SHA256Managed()) using (var algorithm = new SHA256Managed())
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream, Encoding.UTF8)) using (var writer = new StreamWriter(stream))
{ {
Serialize(rawData, writer); Serialize(rawData, writer);
writer.Flush(); writer.Flush();
stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin);
var hash = algorithm.ComputeHash(stream); var hashBytes = algorithm.ComputeHash(stream);
var hashString = BitConverter.ToString(hash).Replace("-", string.Empty); var hashValue = BitConverter.ToString(hashBytes).ToLower().Replace("-", string.Empty);
settings.Browser.HashValue = hashString; settings.Browser.HashValue = hashValue;
} }
} }
private void Serialize(IDictionary<string, object> dictionary, StreamWriter stream) private void Serialize(IDictionary<string, object> dictionary, StreamWriter stream)
{ {
var orderedByKey = dictionary.OrderBy(d => d.Key, StringComparer.OrdinalIgnoreCase).ToList(); var orderedByKey = dictionary.OrderBy(d => d.Key, StringComparer.InvariantCulture).ToList();
stream.Write('{'); stream.Write('{');