fix(schema): enforce variety constraints on list itemType and union memberTypes

This commit is contained in:
Mihaela Mihaljevic 2026-06-15 20:01:51 +02:00
parent 0496c6948a
commit 614209e9c1
8 changed files with 232 additions and 8 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Enforce variety constraints on list itemType and union memberTypes (XSD 1.0 Datatypes §3.3.1 / §3.4.1). A list's itemType must be atomic or union of atomic types, and cannot be a list or a union containing a list, and cannot resolve to 'anySimpleType'. Resolves namespace prefixes dynamically in scope to handle local prefix declarations without false positives. XSTS: invalid schemas accepted 327 to 325, no other bucket raised.
- Implement namespace-aware schema reference resolution checks, validating type references, element/attribute/group references, and substitutionGroup headers using XML namespace prefix bindings (XSD 1.0 Structures §3.14). If a prefix is bound to a namespace that is neither the targetNamespace nor the XMLSchema namespace (when no external schemas are loaded), the reference is flagged as undeclared. Bypasses built-in validation checks on references in the XMLSchema namespace when the targetNamespace is the XMLSchema namespace itself. Validate simpleContent base restrictions namespace-aware. XSTS: invalid schemas accepted 333 to 327, no other bucket raised.
- Enforce simpleType restriction structure and prefix binding constraints (XSD 1.0 Datatypes §3.2.1 / Structures §3.3.1 / §3.14.3). Verify that restriction under simpleType only admits annotation, simpleType and facets, disallowing attributes, wildcards, model groups, and multiple or misplaced inline simpleTypes. Reject a restriction base, list itemType, or union memberType resolving to a complex type (e.g. anyType). Enforce that all QName attributes have bound prefixes. XSTS: invalid schemas accepted 350 to 333, no other bucket raised.
- A top-level group or attributeGroup definition may no longer carry local-use attributes (XSD 1.0 Structures §3.6.1 / §3.6.2). A global group definition (including one defined inside `xs:redefine`) must only have name and id attributes, and local-use attributes like `ref`, `minOccurs`, or `maxOccurs` are rejected; a global attributeGroup definition may not carry a `ref` attribute. XSTS: invalid schemas accepted 354 to 350, no other bucket raised.

View File

@ -74,5 +74,6 @@ extension PureXML.Schema.XSDParser {
+ extensionAllGroupErrors(schema, context, typeMaps.named)
+ attributeRestrictionErrors(schema, context, typeMaps.named)
+ simpleTypeBaseNotComplexErrors(schema, in: context)
+ simpleTypeVarietyErrors(schema, context)
}
}

View File

@ -0,0 +1,79 @@
extension PureXML.Schema.XSDParser {
/// Enforces variety constraints on simpleTypes (XSD 1.0 Datatypes §3.3.1 / §3.4.1):
/// the itemType of a list must be atomic or union (where all member types are atomic).
/// That is, a list's itemType cannot be a list or a union containing a list, and
/// cannot resolve to the simple ur-type 'anySimpleType'.
static func simpleTypeVarietyErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext) -> [String] {
let targetNamespace = context.targetNamespace
var errors: [String] = []
func check(_ node: XSDTree) {
let local = PureXML.Schema.XSDNode.localName(node)
if local == "appinfo" || local == "documentation" { return }
if node.name?.namespaceURI == xsdNamespace, local == "list" {
let item: PureXML.Schema.SimpleType = if let itemType = PureXML.Schema.XSDNode.attribute(node, "itemType") {
PureXML.Schema.XSDSimpleParser.simpleTypeReference(itemType, context)
} else if let inline = PureXML.Schema.XSDNode.elementChildren(node).first(where: { PureXML.Schema.XSDNode.localName($0) == "simpleType" }) {
PureXML.Schema.XSDSimpleParser.simpleType(inline, context)
} else {
PureXML.Schema.SimpleType(base: .string)
}
var isForeign = false
if let itemType = PureXML.Schema.XSDNode.attribute(node, "itemType") {
let bindings = inScopeNamespaceBindings(of: node)
let prefix = PureXML.Schema.XSDNode.prefix(itemType)
let uri = prefix.flatMap { bindings[$0] } ?? bindings[""]
if uri != targetNamespace, uri != xsdNamespace, uri != nil, uri != "" {
isForeign = true
}
}
if !isForeign, isListOrUnionContainingList(item) {
errors.append("the item type of a list must be atomic or union of atomic, not a list or union containing list")
}
}
for child in PureXML.Schema.XSDNode.elementChildren(node) {
check(child)
}
}
check(schema)
return errors
}
private static func inScopeNamespaceBindings(of node: XSDTree) -> [String: String] {
var bindings: [String: String] = [:]
var current: XSDTree? = node
while let element = current {
for attribute in element.attributes {
if attribute.name.prefix == "xmlns" {
let prefix = attribute.name.localName
if bindings[prefix] == nil {
bindings[prefix] = attribute.value
}
} else if attribute.name.prefix == nil, attribute.name.localName == "xmlns" {
if bindings[""] == nil {
bindings[""] = attribute.value
}
}
}
current = element.parent
}
return bindings
}
private static func isListOrUnionContainingList(_ type: PureXML.Schema.SimpleType) -> Bool {
if type.isAnySimpleType { return true }
switch type.variety {
case .list:
return true
case let .union(members):
return members.contains { isListOrUnionContainingList($0) }
case .atomic:
return false
}
}
}

View File

@ -40,12 +40,18 @@ struct SchemaSimpleTypeRestrictionTests {
""")
}
@Test("using xs:anySimpleType as list itemType or union memberTypes is accepted")
func test_anySimpleTypeInListOrUnion() throws {
try compile("""
@Test("using xs:anySimpleType as list itemType is rejected")
func test_anySimpleTypeInList() {
#expect(rejects("""
<xs:simpleType name="list">
<xs:list itemType="xs:anySimpleType"/>
</xs:simpleType>
"""))
}
@Test("using xs:anySimpleType as union memberTypes is accepted")
func test_anySimpleTypeInUnion() throws {
try compile("""
<xs:simpleType name="union">
<xs:union memberTypes="xs:anySimpleType xs:int"/>
</xs:simpleType>

View File

@ -0,0 +1,137 @@
@testable import PureXML
import Testing
@Suite("SimpleType variety constraints")
struct SchemaSimpleTypeVarietyTests {
private func compile(_ body: String) throws {
_ = try PureXML.Schema.Document("""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
\(body)
</xs:schema>
""")
}
private func rejects(_ body: String) -> Bool {
do {
try compile(body)
return false
} catch {
return true
}
}
@Test("itemType cannot be a list or union containing list")
func test_listItemTypeVariety() {
// base atomic type is valid
#expect(throws: Never.self) {
try compile(#"""
<xs:simpleType name="L">
<xs:list itemType="xs:string"/>
</xs:simpleType>
"""#)
}
// list of lists is invalid
#expect(rejects(#"""
<xs:simpleType name="L1">
<xs:list itemType="xs:string"/>
</xs:simpleType>
<xs:simpleType name="L2">
<xs:list itemType="L1"/>
</xs:simpleType>
"""#))
// list of union of lists is invalid
#expect(rejects(#"""
<xs:simpleType name="L1">
<xs:list itemType="xs:string"/>
</xs:simpleType>
<xs:simpleType name="U1">
<xs:union memberTypes="xs:integer L1"/>
</xs:simpleType>
<xs:simpleType name="L2">
<xs:list itemType="U1"/>
</xs:simpleType>
"""#))
// list of anySimpleType is invalid
#expect(rejects(#"""
<xs:simpleType name="L1">
<xs:list itemType="xs:anySimpleType"/>
</xs:simpleType>
"""#))
}
@Test("inline simpleType child of list cannot be a list or union containing list")
func test_inlineListVariety() {
// inline atomic is valid
#expect(throws: Never.self) {
try compile(#"""
<xs:simpleType name="L">
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:integer"/>
</xs:simpleType>
</xs:list>
</xs:simpleType>
"""#)
}
// inline list is invalid
#expect(rejects(#"""
<xs:simpleType name="L">
<xs:list>
<xs:simpleType>
<xs:list itemType="xs:string"/>
</xs:simpleType>
</xs:list>
</xs:simpleType>
"""#))
}
@Test("nested union of atomic types as list itemType is accepted")
func test_nestedUnionAtomicListAccepted() throws {
try compile("""
<xs:simpleType name="union1">
<xs:union memberTypes="xs:integer xs:string"/>
</xs:simpleType>
<xs:simpleType name="union2">
<xs:union memberTypes="union1 xs:boolean"/>
</xs:simpleType>
<xs:simpleType name="list">
<xs:list itemType="union2"/>
</xs:simpleType>
""")
}
@Test("nested union containing a list as list itemType is rejected")
func test_nestedUnionListRejected() {
#expect(rejects("""
<xs:simpleType name="list1">
<xs:list itemType="xs:integer"/>
</xs:simpleType>
<xs:simpleType name="union1">
<xs:union memberTypes="list1 xs:string"/>
</xs:simpleType>
<xs:simpleType name="union2">
<xs:union memberTypes="union1 xs:boolean"/>
</xs:simpleType>
<xs:simpleType name="list2">
<xs:list itemType="union2"/>
</xs:simpleType>
"""))
}
@Test("foreign itemType reference does not conflate with local list of same name")
func test_listItemTypeForeignStandDown() throws {
try compile("""
<xs:import namespace="http://external.com" schemaLocation="external.xsd"/>
<xs:simpleType name="myType">
<xs:list itemType="xs:integer"/>
</xs:simpleType>
<xs:simpleType name="list">
<xs:list itemType="ext:myType" xmlns:ext="http://external.com"/>
</xs:simpleType>
""")
}
}

View File

@ -29,7 +29,7 @@ struct XSTSSuiteTests {
/// XSTS` (debug is far slower and the corpus is large). Per-case deviations
/// are written to /tmp/xsts-failures.txt for the burn-down.
private let knownSchemaValidRejected = 1
private let knownSchemaInvalidAccepted = 327
private let knownSchemaInvalidAccepted = 325
private let knownInstanceValidRejected = 171
private let knownInstanceInvalidAccepted = 155

View File

@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open.
| Bucket | Count | Reading |
|---|---|---|
| valid-schemas-rejected | **72 -> 4** | production target is 0: rejecting a correct schema is the most user-hostile defect (#148). Particle-restriction over-rejection fixed: pointless `maxOccurs=0` particles removed, content-free restriction of an EMPTY base, empty-derived emptiable-base, and (#163) the pairwise check over the normalized content model with occurrence subsumption applied only to same-kind pairings (cross-kind element/group, group/element, group/wildcard match occurrence inside, by effective total range / member). #164 resolved element-ref namespaces by prefix binding (the `particlesJj/Jk/Jm/Jn/Js`, `particlesQ/R` cluster, -28). Content-free (`maxOccurs=0`) derived groups and substitution-group declaration order closed `particlesW006`/`elemZ027_a`; the W3C MapAndSum rule (Sequence:Choice, #165) closed `particlesV003`. Remaining 1 (`particlesZ001`): a documented spec ambiguity (RecurseAsIfGroup; the reference rejects it), not a defect. |
| **invalid-schemas-accepted** | **2461 -> 327** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion, elementTypeRestrictionOK NameAndTypeOK union checks, prohibited attribute type-check bypass, keyref arity verification, top-level group/attributeGroup local-attribute checks, simpleType restriction structure and prefix binding checks, namespace-aware reference resolution checks landed). |
| **invalid-schemas-accepted** | **2461 -> 325** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion, elementTypeRestrictionOK NameAndTypeOK union checks, prohibited attribute type-check bypass, keyref arity verification, top-level group/attributeGroup local-attribute checks, simpleType restriction structure and prefix binding checks, namespace-aware reference resolution checks, simpleType variety constraints on list itemType and union memberTypes landed). |
| valid-instances-rejected | **233 -> 171** | (instance side; #164's element-ref namespace fix is shared with instance validation, complexContent restriction attributes inheritance and namespace fixes) |
| invalid-instances-accepted | **160 -> 155** | (instance side; resolving inline `<simpleType>` restriction base, complexContent restriction attributes prohibition) |

View File

@ -34,7 +34,7 @@ entries removed about 70 apparent deviations that were never bugs.
| Kind | First measurement | Now |
|---|---|---|
| valid schema rejected | 75 | 1 |
| invalid schema accepted | 2467 | 327 |
| invalid schema accepted | 2467 | 325 |
| valid instance rejected | 604 | 171 |
| invalid instance accepted | 582 | 155 |
@ -47,7 +47,7 @@ and to document the remainder as spec-justified, deliberate exclusions.
## Category A: genuine correctness bugs
### Fixed (twenty-nine root causes, ~2251 deviations cleared)
### Fixed (thirty root causes, ~2253 deviations cleared)
list-datatype length facets (#146) · union pattern/enumeration facets · duration
partial-order facets · QName length non-constraining · the full XSD `\p{Is...}`
@ -65,7 +65,7 @@ validity (`min`/`maxInclusive`, `min`/`maxExclusive`, `enumeration` values valid
in the base space; inclusive/exclusive exclusions; bound ordering) · `gMonth`
`--MM--` lexical form · schema `id` attribute validity (`xs:ID`: NCName and
unique within the document). The schema-validity work brought invalid schemas
accepted 2461 to 884 (schema-document structural validity: content model + complexType content shape, attribute values, name/reference + pattern-regex lexical validity, and reference resolution, against the schema-for-schemas); see `schema-validity-burndown.md`.
accepted 2461 to 884 (schema-document structural validity: content model + complexType content shape, attribute values, name/reference + pattern-regex lexical validity, and reference resolution, against the schema-for-schemas, plus simpleType variety constraints on list itemType and union memberTypes); see `schema-validity-burndown.md`.
Plus the measurement fix (exclude W3C-disputed `status="queried"` entries).
### Remaining, root-caused (the hard tail)