diff --git a/mRemoteNGTests/Security/SecureXmlHelperTests.cs b/mRemoteNGTests/Security/SecureXmlHelperTests.cs new file mode 100644 index 00000000..ef137f49 --- /dev/null +++ b/mRemoteNGTests/Security/SecureXmlHelperTests.cs @@ -0,0 +1,90 @@ +using System; +using System.Xml; +using mRemoteNG.Security; +using NUnit.Framework; + +namespace mRemoteNGTests.Security +{ + [TestFixture] + public class SecureXmlHelperTests + { + [Test] + public void LoadXmlFromString_LoadsValidXml() + { + string validXml = "test"; + XmlDocument doc = SecureXmlHelper.LoadXmlFromString(validXml); + + Assert.That(doc, Is.Not.Null); + Assert.That(doc.DocumentElement?.Name, Is.EqualTo("root")); + Assert.That(doc.SelectSingleNode("/root/item")?.InnerText, Is.EqualTo("test")); + } + + [Test] + public void LoadXmlFromString_RejectsXxeAttack() + { + // This is a typical XXE attack payload + string xxeXml = @" + +]> +&xxe;"; + + // Should throw exception because DTD processing is prohibited + Assert.Throws(() => SecureXmlHelper.LoadXmlFromString(xxeXml)); + } + + [Test] + public void CreateSecureXmlDocument_HasNullXmlResolver() + { + XmlDocument doc = SecureXmlHelper.CreateSecureXmlDocument(); + + Assert.That(doc, Is.Not.Null); + Assert.That(doc.XmlResolver, Is.Null); + } + + [Test] + public void LoadXmlFromString_RejectsExternalEntity() + { + // Another XXE variant using external entity + string externalEntityXml = @" + +]> +&ext;"; + + Assert.Throws(() => SecureXmlHelper.LoadXmlFromString(externalEntityXml)); + } + + [Test] + public void LoadXmlFromString_HandlesXmlWithComments() + { + string xmlWithComments = @" + + + test +"; + + // Comments should be ignored per the secure settings + XmlDocument doc = SecureXmlHelper.LoadXmlFromString(xmlWithComments); + + Assert.That(doc, Is.Not.Null); + Assert.That(doc.DocumentElement?.Name, Is.EqualTo("root")); + } + + [Test] + public void LoadXmlFromString_HandlesXmlWithProcessingInstructions() + { + string xmlWithPi = @" + + + test +"; + + // Processing instructions should be ignored per the secure settings + XmlDocument doc = SecureXmlHelper.LoadXmlFromString(xmlWithPi); + + Assert.That(doc, Is.Not.Null); + Assert.That(doc.DocumentElement?.Name, Is.EqualTo("root")); + } + } +}