public void TestGetHeaderValue() { List<string> headers = new List<string>(); headers.Add("Colour: Grey"); headers.Add("Year:1985"); headers.Add("Current: 1.21 gigawatts"); headers.Add("SomeOtherField:"); headers.Add("InvalidField > Invalid because the separator is not a colon"); headers.Add("BlackMesa:4:4:2"); string fileHeaderValue = string.Join(Environment.NewLine, headers.ToArray()); HeaderParser parser = new HeaderParser(fileHeaderValue); Assert.AreEqual( " Grey", parser.GetHeaderValue( "colour" )); Assert.AreEqual( " Grey", parser.GetHeaderValue( "Colour" )); Assert.AreEqual(" 1.21 gigawatts", parser.GetHeaderValue("Current")); Assert.AreEqual("1985", parser.GetHeaderValue("Year")); Assert.AreEqual( string.Empty, parser.GetHeaderValue("SomeOtherField")); Assert.AreEqual("4:4:2", parser.GetHeaderValue("BlackMesa")); Assert.IsNull( parser.GetHeaderValue("InvalidField")); }
/// <summary> /// This method checks a certain property is present in the FileHeaders property, and that the value matches /// a hash of some request-specific properties. /// </summary> /// <param name="request"></param> /// <returns></returns> /// <remarks> /// Only expect this to be true for messages that were processed by Protect on the client, and then picked up at another /// egress point such as the MTA or Network monitor. /// Only messages processed by protect client will have the prerequisite fileheader property. ///</remarks> private static bool VerifyRequest(Request request) { try { if (request == null || request.Properties == null) { return false; } CustomProperty fileHeaderProperty = Array.Find(request.Properties, cp => cp.Name.Equals("FileHeader", StringComparison.InvariantCultureIgnoreCase)); if (fileHeaderProperty == null || string.IsNullOrEmpty(fileHeaderProperty.Value)) { return false; } HeaderParser parser = new HeaderParser(fileHeaderProperty.Value); string actualValue = parser.GetHeaderValue(RequestDigestProvider.XHEADER_MESSAGE_PROTECTED); if (string.IsNullOrEmpty(actualValue)) { return false; } actualValue = actualValue.Trim(); string expectedHashValue = new RequestDigestProvider().GetDigest(request); if (!actualValue.Equals(expectedHashValue, StringComparison.InvariantCultureIgnoreCase)) { Logger.LogInfo("A request failed hash validation. It will be processed by all policies. FileHeader contents: " + fileHeaderProperty.Value); return false; } Logger.LogInfo("Request passed hash validation, therefore it was sent via a Protect-compatible email client."); return true; } catch (Exception e) { //Some requests passed in here will currently pass unexpected arguments, namely the NetworkMonitor. //NetMon code is currently passing an unexpected number of routing items. //In all cases where unexpected arguments are passed or any other problem is encountered, //treat this as failure to verify the message, therefore returning false. Logger.LogError("A request failed hash validation due to an exception. It will be processed by all policies."); Logger.LogError(e); return false; } }