Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML fault finding #49

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions TameMyCerts.Tests/XMLPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Test_Unknown_XML_Element()

CertificateRequestPolicyCacheEntry cacheEntry = new CertificateRequestPolicyCacheEntry(filename);

Assert.Empty(cacheEntry.ErrorMessage);
Assert.NotEmpty(cacheEntry.ErrorMessage);
Assert.Equal(2, _listener.Events.Count);
Assert.Equal(92, _listener.Events[0].EventId);
output.WriteLine(_listener.Events[0].Message);
Expand All @@ -89,7 +89,7 @@ public void Test_Unknown_XML_Element2()

CertificateRequestPolicyCacheEntry cacheEntry = new CertificateRequestPolicyCacheEntry(filename);

Assert.Empty(cacheEntry.ErrorMessage);
Assert.NotEmpty(cacheEntry.ErrorMessage);
Assert.Equal(2, _listener.Events.Count);
Assert.Equal(92, _listener.Events[0].EventId);

Expand Down Expand Up @@ -145,4 +145,26 @@ public void Broken_XML_Policies()
File.Delete(filename);
}

[Fact]
public void Test_Broken_XML()
{
var filename = Path.GetTempFileName();
File.Delete(filename);

string sampleXML = @"<CertificateRequestPolicy xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<directoryservicesmapping><AllowedOrganizationalUnits><Test>This should fault</Test></AllowedOrganizationalUnits></directoryservicesmapping>
</CertificateRequestPolicy>
";
File.WriteAllText($"{filename}.xml", sampleXML);
_listener.ClearEvents();

CertificateRequestPolicyCache cache = new CertificateRequestPolicyCache(Path.GetTempPath());
var cacheEntry = cache.GetCertificateRequestPolicy(Path.GetFileName(filename));

Assert.NotNull(cacheEntry);
Assert.Null(cacheEntry.CertificateRequestPolicy);

File.Delete($"{filename}.xml");
}
}
4 changes: 3 additions & 1 deletion TameMyCerts/Models/CertificateRequestPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,15 @@ public string SaveToString()
}
private static void UnknownElementHandler(object sender, XmlElementEventArgs e)
{
ETWLogger.Log.TMC_92_Policy_Unknown_XML_Element(e.Element.Name, e.LineNumber, e.LinePosition);
ETWLogger.Log.TMC_92_Policy_Unknown_XML_Element(e.Element.Name, e.LineNumber, e.LinePosition);
throw new XmlException($"Unknown XML element {e.Element.Name} at line {e.LineNumber}, position {e.LinePosition}");
}

// Event handler for unknown attributes
private static void UnknownAttributeHandler(object sender, XmlAttributeEventArgs e)
{
ETWLogger.Log.TMC_93_Policy_Unknown_XML_Attribute(e.Attr.Name, e.Attr.Value, e.LineNumber, e.LinePosition);
throw new XmlException($"Unknown XML attribute {e.Attr.Name} with value {e.Attr.Value} at line {e.LineNumber}, position {e.LinePosition}");
}

}