Repository navigation
Description
I've discovered a bug in the .NET Core implementation of some of the XML Schema parsing functionality. This functionality works fine in .NET Framework, but is broken in .NET Core.
The broken functionality relates to importing XSD schemas for validation. When trying to import a second XSD document into a root XSD document, .NET Core does not parse the contents of the child XSD document. This is visible in the debugger, and in failures to parse future XML documents based on the schema. .NET Framework does not have this issue.
Here is an example repro:
Filename "Repro-Config.xsd":
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns="http://consanto.com/test/unit/configuration/v2.0"
xmlns:tns="http://consanto.com/test/unit/configuration/v2.0"
targetNamespace="http://consanto.com/test/unit/configuration/v2.0"
xmlns:specialized="http://consanto.com/test/unit/configuration/v2.0/specialized"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:import
namespace="http://consanto.com/test/unit/configuration/v2.0/specialized"
schemaLocation="Repro-Config-Specialized.xsd"/>
<xs:element name="RootProperty"/>
<xs:complexType name="Root">
<xs:sequence>
<xs:element ref="RootProperty" minOccurs="0" maxOccurs="1"/>
<xs:element ref="specialized:SpecialProperty" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Root" type="tns:Root" />
</xs:schema>
Filename "Repro-Config-Specialized.xsd":
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns="http://consanto.com/test/unit/configuration/v2.0/specialized"
xmlns:tns="http://consanto.com/test/unit/configuration/v2.0/specialized"
targetNamespace="http://consanto.com/test/unit/configuration/v2.0/specialized"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="SpecialProperty" />
</xs:schema>
Filename Program.cs:
using System;
using System.Xml;
using System.Xml.Schema;
namespace XSDRepro
{
class Program
{
static void Main(string[] args)
{
string xsdfile = "Repro-Config.xsd";
string xmlfile = "Repro-Config-Specialized.xml";
// Configure an XML reader with XSD validation enabled
var settings = new XmlReaderSettings();
XmlSchema schema = settings.Schemas.Add(null, xsdfile);
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(xmlfile, settings);
}
}
}
If this program is compiled with .NET Framework and run, it successfully validates the XML file. If I run it with .NET Core, it get an exception:
System.Xml.Schema.XmlSchemaValidationException: 'The 'http://consanto.com/test/unit/configuration/v2.0/specialized:SpecialProperty' element is not declared.'
The bug appears to be that XmlSchema is not recursively parsing XSD documents when loading the root XSD. This can be seen in the debugger when examining the schema variable in the above program.
This should repro on all versions of .NET Core. Specifically tested on: .NET Core 3.0.0-preview6-27804-01, .NET Core 2.1
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone


Activity
krwq commentedon Jun 26, 2019
@skalldri could you try setting XmlResolver on XmlSchemaSet (settings.Schemas)?
skalldri commentedon Jun 26, 2019
That seems to have fixed it. Thanks @krwq !
Any chance this behavior will become standard in future versions of .NET Core? It's odd that this works in .NET Framework but not in .NET Core, and I can't find any documentation on the topic.
krwq commentedon Jun 26, 2019
@skalldri, I have mixed feelings on this. We have disabled this for security reasons. In cases where schema comes from untrusted data (i.e. got uploaded over the wire) malicious schema could trigger xml parser to do web request or leak some information from the server it was run on.
What I think should happen instead here is we should improve error message (or basically make sure there is one and it mentions XmlResolver as potential solution).
skalldri commentedon Jun 26, 2019
I can definitely understand the security concern, given that you can link to any XSD, even on the web. It would help to have something to indicate that xs:import nodes are not being parsed without an XmlResolver set, and potentially some updates to the .NET Core documentation of XmlReaderSettings / XmlSchema to indicate the same.
jeffhandley commentedon Jul 9, 2022
Closing per @krwq's comment above.