Repository navigation
Conversation
Contributor
There was a problem hiding this comment.
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>
| { | ||
| 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 |
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() |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.