System.Xml: XSD allows patterns that should be invalid

Aperta
#136,967 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
backend

Direzione di ricerca

Start by tracing how System.Xml validates xs:pattern values and how its regex handling differs from the XML regex dialect described in the linked specification. The report provides two reproductions involving grouping and backreferences; use them to identify the relevant validation tests and confirm the expected schema errors. Done means those patterns are rejected while valid XML regex patterns continue to work.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area-System.Xml untriaged
Description

XSD schemas allows use of regex string that are not valid according to the XML regex dialect but are valid in .NET regex dialect. Some examples include:

  • Backtracking
  • Various grouping constructs
  • Look arounds
Reproduction Steps
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

Check("(?&lt;x&gt;y)", "<id>y</id>"); // '?' should not be allowed to follow '('. Instead this creates a named group.
Check("(y)\\2", "<id>yy</id>"); // "\\2" should be an invalid escape sequence. Instead this creates a back reference

static void Check(string pattern, string xml)
{
    string xsd = $@"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  <xs:element name='id'>
    <xs:simpleType>
      <xs:restriction base='xs:string'>
        <xs:pattern value='{pattern}'/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>";

    var settings = new XmlReaderSettings
    {
        ValidationType = ValidationType.Schema
    };
    settings.Schemas.Add(null, XmlReader.Create(new StringReader(xsd)));

    int errors = 0;
    settings.ValidationEventHandler += (_, e) =>
    {
        if (e.Severity == XmlSeverityType.Error)
            errors++;
        else
            throw e.Exception;
    };

    using var reader = XmlReader.Create(new StringReader(xml), settings);
    while (reader.Read()) { }

    Console.WriteLine(
        $"{pattern,-12} {xml,-36} {(errors == 0 ? "VALID" : "INVALID")}");
}
Expected behavior

Exceptions or output INVALID for all three since the patterns are not valid according to xml spec?

Actual behavior

Both checks outputs VALID.

Regression?

Not what i can find

Known Workarounds

No response

Configuration

No response

Other information

System.Xml has contribution bar "". I would expect these issues to be classified as edge-cased and the bug report to closed as won't fix, but at least it is logged.

Lingua principale
C#
Stelle
18.4k
Fork
5.8k
Merge medio
2g 14h
PR unite (30g)
585

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di dotnet/runtime

Tutte le issue di dotnet/runtime

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.