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 3 commits into
mainfrom
copilot/add-query-for-xml-parser
Draft

felickz wants to merge 3 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

@felickz

felickz commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Please address the three open review findings, each with GOOD and BAD regression cases:

  1. Classify resolver values by their origin: a null or XmlSecureResolver value that reaches the property through a local alias or a cast must count as safe.
  2. Ignore resets after the use: in a loop, a resolver reset to null, or Prohibit assigned, after XmlReader.Create must not suppress that call. Do not follow a back edge past the creation of the settings object.
  3. Resolve local values in the DTD checks: DtdProcessing and ProhibitDtd values held in locals must be classified like literals, for both enabling and disabling.

Leave the code quality notes about Dispose in Test.cs: the fixtures are minimal on purpose. Rerun the test and the gates of your profile, and report the result changes. The Build CodeQL Packs run on the current head is still in progress: check its result and fix any failure.

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 19:05
Copilot stopped work on behalf of felickz due to an error December 1, 2026 19:12

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 can miss vulnerable paths after conditional security resets, misclassify default resolver and constant boolean configurations, and lacks expected results for newly added positive cases.

1 open issue
3 previously missed issues

In code that hasn't changed since last review

High severity Preserve resolver values across conditional resets

csharp/​src/​security/​CWE-611/​ExplicitInsecureXmlParserConfig.ql:196

A conditional reset such as if (harden) settings.XmlResolver = null; before XmlReader.Create discards the earlier unrestricted resolver here, even though the parser remains vulnerable when harden is false. Conditional writes that disable DTD processing have the same problem. Retain a value whenever a path to the observation avoids overwriting that property without recreating the object, and add conditional-reset regressions.

Medium severity Exclude default-value expressions for XmlResolver

csharp/​src/​security/​CWE-611/​ExplicitInsecureXmlParserConfig.ql:26

XmlResolver = default and XmlResolver = default(XmlResolver) disable external resolution, just like null. Their DefaultValueExpr nodes pass this exclusion and the subsequent resolver-type check, causing safe parsers with DTD processing enabled to be reported. Exclude default-value expressions as well.

Medium severity Evaluate constant boolean expressions for DTD settings

csharp/​src/​security/​CWE-611/​ExplicitInsecureXmlParserConfig.ql:44

Restricting boolean origins to BoolLiteral reports a safe XmlTextReader configured with ProhibitDtd = !false and an unrestricted resolver, because the disabling value is not recognized. Conversely, XmlReaderSettings.ProhibitDtd = !true enables DTD processing but is missed. Check the constant value of each flowing boolean expression while retaining local flow for aliases.

3 resolved since last review

Review effort: Balanced

| Test.cs:397:16:397:49 | call to method Create | This XML parser resolves external entities: XmlReaderSettings with DTD processing enabled and an unrestricted $@. | Test.cs:396:36:396:55 | object creation of type XmlUrlResolver | XmlResolver |
| Test.cs:426:13:426:46 | call to method Create | This XML parser resolves external entities: XmlReaderSettings with DTD processing enabled and an unrestricted $@. | Test.cs:425:36:425:55 | object creation of type XmlUrlResolver | XmlResolver |
| Test.cs:438:13:438:46 | call to method Create | This XML parser resolves external entities: XmlReaderSettings with DTD processing enabled and an unrestricted $@. | Test.cs:437:36:437:55 | object creation of type XmlUrlResolver | XmlResolver |
| Test.cs:477:13:477:51 | call to method Create | This XML parser resolves external entities: XmlReaderSettings with DTD processing enabled and an unrestricted $@. | Test.cs:475:31:475:63 | (...) ... | XmlResolver |
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