Skip to content

[New Query - githubsecuritylab/cs/explicit-insecure-dtd-resolver] - XML parser explicitly configured to resolve external entities - #280

Draft
felickz wants to merge 2 commits into
mainfrom
copilot/add-query-for-xml-parser
Draft

felickz wants to merge 2 commits into
mainfrom
copilot/add-query-for-xml-parser

Conversation

@felickz

@felickz felickz commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Adds a C# query that reports an XML parser whose code turns on DTD processing and assigns an unrestricted XmlResolver. Such a parser resolves external entities (XXE, CWE-611, CWE-827). The configuration is the weakness, so no source of untrusted data is required, unlike cs/xml/insecure-dtd-handling.

var settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
settings.XmlResolver = new XmlUrlResolver();
XmlReader reader = XmlReader.Create(stream, settings); // reported

var textReader = new XmlTextReader(new StringReader(xml));
textReader.DtdProcessing = DtdProcessing.Parse;        // not reported: no resolver assigned

What is reported

  • XmlReader.Create with XmlReaderSettings that set DtdProcessing.Parse (or ProhibitDtd = false) and assign a resolver that is neither null nor an XmlSecureResolver. The settings may be built in the same method or returned from a helper.
  • XmlTextReader with such a resolver when DTD processing is not set to Prohibit or Ignore. XmlTextReader parses DTDs by default.
  • For each property, the last assignment before the parser is created or used counts.
  • Not reported: DTD processing on with no resolver (modern .NET resolves nothing then), and XmlReader.Create that wraps an existing XmlReader.
  • Metadata: security query, @precision medium, @security-severity 9.1, tagged experimental. It is selected by the default csharp.qls suite.

Changes from the draft in the issue

  • Id: cs/xml/explicit-insecure-dtd-resolver becomes githubsecuritylab/cs/explicit-insecure-dtd-resolver.
  • Query help: .md is now .qhelp.
    • The CWE links are removed from the references, because they are generated from the tags.
    • The recommendation is split per parser, since XmlTextReader defaults to DtdProcessing.Parse.
  • Performance: the draft's getAConfigurationEnd(create, use) paired every settings creation with every element of every other method (118k rows on the test database).
    • It now returns only same-method XmlReader.Create calls and return statements of the settings (19 rows).
    • The pairing with a Create in another method happens after the data flow check.
    • Property writes are limited to creations of XmlReaderSettings and XmlTextReader.
  • Write order in loops: the draft ordered writes with reachableFrom, which follows loop back edges. Inside a loop body each write then hid the others, and could even hide itself. That caused missed alerts and false positives.
    • The new followsWrite uses the order within a basic block, and successor blocks only across blocks.
  • Message: "with DtdProcessing.Parse" becomes "with DTD processing enabled", so it is also right for ProhibitDtd = false.
  • Test:
    • Uses the shared Microsoft.NETCore.App stubs, like the other C# tests here.
    • The InlineExpectationsTestQuery postprocess is dropped: it does not compile with the pinned package cache, and no C# test here uses it. Cases are marked // BAD and // GOOD instead.

Tests

  • The draft's cases keep their line numbers, and their 9 rows match the draft's .expected (only the message text differs).
  • 10 cases were added: the issue's examples, ProhibitDtd = false, Parse then Prohibit, XmlTextReader with Ignore, and three loop cases.
  • One known false positive is kept: DTD processing and the resolver set on branches that exclude each other.

Compared with cs/insecure-xml-read on the same test

  • cs/insecure-xml-read reports 22 lines and this query 13; 12 are common to both.
  • This query also reports an XmlTextReader left at its default DTD setting with an XmlUrlResolver, which cs/insecure-xml-read misses.
  • cs/insecure-xml-read reports 11 lines that are safe on modern .NET, such as a null resolver, XmlSecureResolver, Prohibit, Ignore and a wrapped reader. This query does not report them.

Known limitations

  • Assignments on branches that exclude each other are not told apart.
  • Settings built in another method are checked only where they are returned, so later changes by the caller are ignored.
  • Settings stored in a field, returned by an expression-bodied member, or passed as an argument are not reported.
  • An XmlTextReader is reported only when Read or ReadAsync is called in the method that creates it. XmlDocument.Load(reader) alone is missed.
  • Writes in different basic blocks of one loop can still be ordered through the back edge.
  • getAValueForDtdProp grows with DTD writes times the elements reachable in the same method. It needs a timing run on a large codebase.

Real project true positive

To be added by the maintainer.

Follow-ups

  • Version bump of githubsecuritylab/codeql-csharp-queries, left for the release.

Copilot AI balanced review requested due to automatic review settings November 26, 2026 06:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request. Check if the Files changed in this pull request are included in default exclusions.

…nd test

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 06:59
@felickz felickz changed the title [WIP] Add C# query for XML parser with external entities [New Query - githubsecuritylab/cs/explicit-insecure-dtd-resolver] - XML parser explicitly configured to resolve external entities Nov 26, 2026
{
public string ParseWithUrlResolver(string xml)
{
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // BAD
{
public string ParseWithUrlResolver(string xml)
{
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // BAD
{
// Constant XML, DTD parsing on, resolver never set: external entities are not resolved.
string xml = "<?xml version=\"1.0\"?><!DOCTYPE foo [<!ENTITY xxe SYSTEM \"file:///etc/passwd\" >]><foo>&xxe;</foo>";
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // GOOD
{
// Constant XML, DTD parsing on, resolver never set: external entities are not resolved.
string xml = "<?xml version=\"1.0\"?><!DOCTYPE foo [<!ENTITY xxe SYSTEM \"file:///etc/passwd\" >]><foo>&xxe;</foo>";
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // GOOD
public string DefaultDtdWithUrlResolver(string xml)
{
// XmlTextReader parses DTDs by default, so only the resolver needs to be opted in.
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // BAD
public string DefaultDtdWithUrlResolver(string xml)
{
// XmlTextReader parses DTDs by default, so only the resolver needs to be opted in.
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // BAD

public string ProhibitWithUrlResolver(string xml)
{
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // GOOD

public string ProhibitWithUrlResolver(string xml)
{
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // GOOD

public string ProhibitDtdWithUrlResolver(string xml)
{
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // GOOD

public string ProhibitDtdWithUrlResolver(string xml)
{
XmlTextReader reader = new XmlTextReader(new StringReader(xml)); // GOOD

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The query still produces false positives and false negatives for aliased resolver and DTD values, and loop back edges let post-use resets suppress vulnerable calls.

3 open issues

Review effort: Balanced

Comment on lines +22 to +26
DataFlow::localExprFlow(value, e) and
not (value = e and e instanceof LocalVariableAccess) and
not value instanceof NullLiteral and
not value.getType().(RefType).hasFullyQualifiedName("System.Xml", "XmlSecureResolver") and
value.getType().(RefType).getABaseType*().hasFullyQualifiedName("System.Xml", "XmlResolver")
Comment on lines +98 to +102
end.reachableFrom(write) and
not exists(AssignExpr later |
writesProperty(create, prop, later) and
followsWrite(create, write, later) and
end.reachableFrom(later)
followsWrite(create, write, later) and
end.reachableFrom(later)
) and
result = write.getRightOperand()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C#: add a query for an XML parser that is configured to resolve external entities

3 participants