You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[New Query - githubsecuritylab/cs/explicit-insecure-dtd-resolver] - XML parser explicitly configured to resolve external entities - #280
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.
varsettings=newXmlReaderSettings();settings.DtdProcessing=DtdProcessing.Parse;settings.XmlResolver=newXmlUrlResolver();XmlReaderreader=XmlReader.Create(stream,settings);// reportedvartextReader=newXmlTextReader(newStringReader(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.
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.
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
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.
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.
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.
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.
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.
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.
Evaluate constant boolean expressions for DTD settings
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.
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, unlikecs/xml/insecure-dtd-handling.What is reported
XmlReader.CreatewithXmlReaderSettingsthat setDtdProcessing.Parse(orProhibitDtd = false) and assign a resolver that is neithernullnor anXmlSecureResolver. The settings may be built in the same method or returned from a helper.XmlTextReaderwith such a resolver when DTD processing is not set toProhibitorIgnore.XmlTextReaderparses DTDs by default.XmlReader.Createthat wraps an existingXmlReader.@precision medium,@security-severity 9.1, taggedexperimental. It is selected by the defaultcsharp.qlssuite.Changes from the draft in the issue
cs/xml/explicit-insecure-dtd-resolverbecomesgithubsecuritylab/cs/explicit-insecure-dtd-resolver..mdis now.qhelp.XmlTextReaderdefaults toDtdProcessing.Parse.getAConfigurationEnd(create, use)paired every settings creation with every element of every other method (118k rows on the test database).XmlReader.Createcalls andreturnstatements of the settings (19 rows).Createin another method happens after the data flow check.XmlReaderSettingsandXmlTextReader.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.followsWriteuses the order within a basic block, and successor blocks only across blocks.ProhibitDtd = false.Microsoft.NETCore.Appstubs, like the other C# tests here.InlineExpectationsTestQuerypostprocess is dropped: it does not compile with the pinned package cache, and no C# test here uses it. Cases are marked// BADand// GOODinstead.Tests
.expected(only the message text differs).ProhibitDtd = false,ParsethenProhibit,XmlTextReaderwithIgnore, and three loop cases.Compared with
cs/insecure-xml-readon the same testcs/insecure-xml-readreports 22 lines and this query 13; 12 are common to both.XmlTextReaderleft at its default DTD setting with anXmlUrlResolver, whichcs/insecure-xml-readmisses.cs/insecure-xml-readreports 11 lines that are safe on modern .NET, such as a null resolver,XmlSecureResolver,Prohibit,Ignoreand a wrapped reader. This query does not report them.Known limitations
XmlTextReaderis reported only whenReadorReadAsyncis called in the method that creates it.XmlDocument.Load(reader)alone is missed.getAValueForDtdPropgrows 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
githubsecuritylab/codeql-csharp-queries, left for the release.