2018-11-08 09:39:52 +01:00
|
|
|
|
/*
|
2019-01-09 11:25:21 +01:00
|
|
|
|
* Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-11-28 15:43:30 +01:00
|
|
|
|
using System;
|
2018-11-30 14:50:28 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-11-09 14:15:56 +01:00
|
|
|
|
using System.IO;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Linq;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2018-12-14 09:50:10 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.Cryptography;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration.DataFormats;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Configuration.DataFormats
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
public class XmlParser : IDataParser
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-11-28 15:43:30 +01:00
|
|
|
|
private const string XML_PREFIX = "<?xm";
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
private ILogger logger;
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
public XmlParser(ILogger logger)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 14:15:56 +01:00
|
|
|
|
public bool CanParse(Stream data)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-11-28 15:43:30 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var prefixData = new byte[XML_PREFIX.Length];
|
|
|
|
|
var longEnough = data.Length > prefixData.Length;
|
|
|
|
|
|
|
|
|
|
if (longEnough)
|
|
|
|
|
{
|
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
data.Read(prefixData, 0, prefixData.Length);
|
2018-12-21 11:36:20 +01:00
|
|
|
|
|
2018-11-28 15:43:30 +01:00
|
|
|
|
var prefix = Encoding.UTF8.GetString(prefixData);
|
2019-01-08 14:10:45 +01:00
|
|
|
|
var isXml = prefix == XML_PREFIX;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
|
2019-01-08 14:10:45 +01:00
|
|
|
|
logger.Debug($"'{data}' starting with '{prefix}' {(isXml ? "matches" : "does not match")} the {FormatType.Xml} format.");
|
2018-11-28 15:43:30 +01:00
|
|
|
|
|
2019-01-08 14:10:45 +01:00
|
|
|
|
return isXml;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Debug($"'{data}' is not long enough ({data.Length} bytes) to match the {FormatType.Xml} format.");
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Error($"Failed to determine whether '{data}' with {data.Length / 1000.0} KB data matches the {FormatType.Xml} format!", e);
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-08 09:39:52 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-14 09:50:10 +01:00
|
|
|
|
public ParseResult TryParse(Stream data, PasswordParameters password = null)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var result = new ParseResult { Format = FormatType.Xml, Status = LoadStatus.InvalidData };
|
|
|
|
|
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
|
2018-11-28 15:43:30 +01:00
|
|
|
|
|
|
|
|
|
data.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
using (var reader = XmlReader.Create(data, settings))
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var hasRoot = reader.ReadToFollowing(XmlElement.Root);
|
|
|
|
|
var hasDictionary = reader.ReadToDescendant(XmlElement.Dictionary);
|
2018-11-30 14:50:28 +01:00
|
|
|
|
var rawData = new Dictionary<string, object>();
|
2018-11-28 15:43:30 +01:00
|
|
|
|
|
2018-11-30 14:50:28 +01:00
|
|
|
|
if (hasRoot && hasDictionary)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Debug($"Found root node, starting to parse data...");
|
2018-11-30 14:50:28 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
result.Status = ParseDictionary(reader, rawData);
|
|
|
|
|
result.RawData = rawData;
|
2018-11-30 14:50:28 +01:00
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
logger.Debug($"Finished parsing -> Result: {result.Status}.");
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Error($"Could not find root {(!hasRoot ? $"node '{XmlElement.Root}'" : $"dictionary '{XmlElement.Dictionary}'")}!");
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:06:10 +01:00
|
|
|
|
return result;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 14:50:28 +01:00
|
|
|
|
private LoadStatus ParseArray(XmlReader reader, List<object> array)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
if (reader.IsEmptyElement)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
return LoadStatus.Success;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
while (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
var status = ParseElement(reader, out object element);
|
|
|
|
|
|
|
|
|
|
if (status == LoadStatus.Success)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
array.Add(element);
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
return status;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
if (reader.NodeType == XmlNodeType.EndElement && reader.Name == XmlElement.Array)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
return LoadStatus.Success;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Error($"Expected closing tag for '{XmlElement.Array}', but found '{reader.Name}{reader.Value}'!");
|
2018-11-30 14:50:28 +01:00
|
|
|
|
|
|
|
|
|
return LoadStatus.InvalidData;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 14:50:28 +01:00
|
|
|
|
private LoadStatus ParseDictionary(XmlReader reader, Dictionary<string, object> dictionary)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
|
{
|
|
|
|
|
return LoadStatus.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 15:43:30 +01:00
|
|
|
|
reader.Read();
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
while (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
var status = ParseKeyValuePair(reader, dictionary);
|
|
|
|
|
|
|
|
|
|
if (status != LoadStatus.Success)
|
|
|
|
|
{
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2018-11-28 15:43:30 +01:00
|
|
|
|
|
|
|
|
|
reader.Read();
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
if (reader.NodeType == XmlNodeType.EndElement && reader.Name == XmlElement.Dictionary)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
return LoadStatus.Success;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Error($"Expected closing tag for '{XmlElement.Dictionary}', but found '{reader.Name}{reader.Value}'!");
|
2018-11-30 14:50:28 +01:00
|
|
|
|
|
|
|
|
|
return LoadStatus.InvalidData;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 14:50:28 +01:00
|
|
|
|
private LoadStatus ParseKeyValuePair(XmlReader reader, Dictionary<string, object> dictionary)
|
2018-11-08 09:39:52 +01:00
|
|
|
|
{
|
2018-11-28 15:43:30 +01:00
|
|
|
|
var key = XNode.ReadFrom(reader) as XElement;
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
if (key.Name.LocalName != XmlElement.Key)
|
2018-11-30 14:50:28 +01:00
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
logger.Error($"Expected element '{XmlElement.Key}', but found '{key}'!");
|
2018-11-30 14:50:28 +01:00
|
|
|
|
|
|
|
|
|
return LoadStatus.InvalidData;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 15:43:30 +01:00
|
|
|
|
reader.Read();
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
2018-11-30 14:50:28 +01:00
|
|
|
|
var status = ParseElement(reader, out object value);
|
|
|
|
|
|
|
|
|
|
if (status == LoadStatus.Success)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
dictionary[key.Value] = value;
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
2018-11-30 14:50:28 +01:00
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LoadStatus ParseElement(XmlReader reader, out object element)
|
|
|
|
|
{
|
|
|
|
|
var array = default(List<object>);
|
|
|
|
|
var dictionary = default(Dictionary<string, object>);
|
|
|
|
|
var status = default(LoadStatus);
|
|
|
|
|
var value = default(object);
|
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
if (reader.Name == XmlElement.Array)
|
2018-11-30 14:50:28 +01:00
|
|
|
|
{
|
|
|
|
|
array = new List<object>();
|
|
|
|
|
status = ParseArray(reader, array);
|
|
|
|
|
}
|
2018-12-21 11:36:20 +01:00
|
|
|
|
else if (reader.Name == XmlElement.Dictionary)
|
2018-11-28 15:43:30 +01:00
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
dictionary = new Dictionary<string, object>();
|
|
|
|
|
status = ParseDictionary(reader, dictionary);
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-11-30 14:50:28 +01:00
|
|
|
|
status = ParseSimpleType(XNode.ReadFrom(reader) as XElement, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
element = array ?? dictionary ?? value;
|
|
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LoadStatus ParseSimpleType(XElement element, out object value)
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
var status = LoadStatus.Success;
|
|
|
|
|
|
2018-11-30 14:50:28 +01:00
|
|
|
|
value = null;
|
|
|
|
|
|
|
|
|
|
switch (element.Name.LocalName)
|
|
|
|
|
{
|
2018-12-21 11:36:20 +01:00
|
|
|
|
case XmlElement.Data:
|
2018-11-30 14:50:28 +01:00
|
|
|
|
value = Convert.FromBase64String(element.Value);
|
|
|
|
|
break;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
case XmlElement.Date:
|
2018-11-30 14:50:28 +01:00
|
|
|
|
value = XmlConvert.ToDateTime(element.Value, XmlDateTimeSerializationMode.Utc);
|
|
|
|
|
break;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
case XmlElement.False:
|
2018-11-30 14:50:28 +01:00
|
|
|
|
value = false;
|
|
|
|
|
break;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
case XmlElement.Integer:
|
2018-11-30 14:50:28 +01:00
|
|
|
|
value = Convert.ToInt32(element.Value);
|
|
|
|
|
break;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
case XmlElement.Real:
|
2018-11-30 14:50:28 +01:00
|
|
|
|
value = Convert.ToDouble(element.Value);
|
|
|
|
|
break;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
case XmlElement.String:
|
2018-12-21 11:52:04 +01:00
|
|
|
|
value = element.IsEmpty ? null : element.Value;
|
2018-11-30 14:50:28 +01:00
|
|
|
|
break;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
case XmlElement.True:
|
2018-11-30 14:50:28 +01:00
|
|
|
|
value = true;
|
|
|
|
|
break;
|
2018-12-21 11:36:20 +01:00
|
|
|
|
default:
|
|
|
|
|
status = LoadStatus.InvalidData;
|
|
|
|
|
break;
|
2018-11-30 14:50:28 +01:00
|
|
|
|
}
|
2018-11-28 15:43:30 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
if (status != LoadStatus.Success)
|
2018-11-30 14:50:28 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Error($"Element '{element}' is not supported!");
|
2018-11-28 15:43:30 +01:00
|
|
|
|
}
|
2018-11-30 14:50:28 +01:00
|
|
|
|
|
2018-12-21 11:36:20 +01:00
|
|
|
|
return status;
|
2018-11-08 09:39:52 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|