System.Xml: XSD allows patterns that should be invalid

未关闭
#136,967 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp
领域
backend

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
C#
星标
18.4k
派生
5.8k
平均合并
2 天 14 小时
30 天内合并 PR
585

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/runtime 的其他 Issue

查看 dotnet/runtime 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。