Skip to content

XmlSchema parsing of <xs:import> nodes does not work #30054

Closed as not planned
Closed as not planned

Description

@skalldri

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.

On .NET Framework:
image

On .NET Core:
image

This should repro on all versions of .NET Core. Specifically tested on: .NET Core 3.0.0-preview6-27804-01, .NET Core 2.1

Activity

  1. krwq commented on Jun 26, 2019

    @krwq
    Member

    @skalldri could you try setting XmlResolver on XmlSchemaSet (settings.Schemas)?

  2. skalldri commented on Jun 26, 2019

    @skalldri
    Author

    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.

  3. krwq commented on Jun 26, 2019

    @krwq
    Member

    @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).

  4. skalldri commented on Jun 26, 2019

    @skalldri
    Author

    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.

  5. transferred this issue fromdotnet/corefxon Feb 1, 2020
  6. added this to the Future milestone on Feb 1, 2020
  7. added
    untriaged
    on Feb 23, 2020
  8. removed
    untriaged
    on Jul 8, 2020
  9. added
    documentation
    on Oct 6, 2020
  10. modified the milestones: Future, 6.0.0 on Oct 6, 2020
  11. modified the milestones: 6.0.0, 7.0.0 on Jul 15, 2021
  12. removed this from the 7.0.0 milestone on Jul 9, 2022
  13. ghost added
    untriaged
    on Jul 9, 2022
  14. jeffhandley commented on Jul 9, 2022

    @jeffhandley
    Member

    Closing per @krwq's comment above.

  15. Repository owner moved this from Future to Done in Triage POD for Reflection, META, etc.on Jul 9, 2022
  16. ghost removed
    untriaged
    on Jul 9, 2022
  17. ghost locked as resolved and limited conversation to collaborators on Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Xmldocumentation

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions