From 614209e9c1290898e51b0aa20ae92b065a8a198e Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Mon, 15 Jun 2026 20:01:51 +0200 Subject: [PATCH] fix(schema): enforce variety constraints on list itemType and union memberTypes --- CHANGELOG.md | 1 + Sources/Schema/SchemaIDAttribute.swift | 1 + Sources/Schema/SchemaSimpleTypeValidity.swift | 79 ++++++++++ Tests/SchemaSimpleTypeRestrictionTests.swift | 12 +- Tests/SchemaSimpleTypeVarietyTests.swift | 137 ++++++++++++++++++ Tests/XSTSSuiteTests.swift | 2 +- docs/schema-validity-burndown.md | 2 +- docs/xsts-deviations.md | 6 +- 8 files changed, 232 insertions(+), 8 deletions(-) create mode 100644 Sources/Schema/SchemaSimpleTypeValidity.swift create mode 100644 Tests/SchemaSimpleTypeVarietyTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index e9dd564..41b417f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Sources/Schema/SchemaIDAttribute.swift b/Sources/Schema/SchemaIDAttribute.swift index e63b39f..228bc81 100644 --- a/Sources/Schema/SchemaIDAttribute.swift +++ b/Sources/Schema/SchemaIDAttribute.swift @@ -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) } } diff --git a/Sources/Schema/SchemaSimpleTypeValidity.swift b/Sources/Schema/SchemaSimpleTypeValidity.swift new file mode 100644 index 0000000..b95e9c7 --- /dev/null +++ b/Sources/Schema/SchemaSimpleTypeValidity.swift @@ -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 + } + } +} diff --git a/Tests/SchemaSimpleTypeRestrictionTests.swift b/Tests/SchemaSimpleTypeRestrictionTests.swift index f6edd25..f86c73c 100644 --- a/Tests/SchemaSimpleTypeRestrictionTests.swift +++ b/Tests/SchemaSimpleTypeRestrictionTests.swift @@ -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(""" + """)) + } + + @Test("using xs:anySimpleType as union memberTypes is accepted") + func test_anySimpleTypeInUnion() throws { + try compile(""" diff --git a/Tests/SchemaSimpleTypeVarietyTests.swift b/Tests/SchemaSimpleTypeVarietyTests.swift new file mode 100644 index 0000000..5044026 --- /dev/null +++ b/Tests/SchemaSimpleTypeVarietyTests.swift @@ -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(""" + + \(body) + + """) + } + + 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(#""" + + + + """#) + } + + // list of lists is invalid + #expect(rejects(#""" + + + + + + + """#)) + + // list of union of lists is invalid + #expect(rejects(#""" + + + + + + + + + + """#)) + + // list of anySimpleType is invalid + #expect(rejects(#""" + + + + """#)) + } + + @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(#""" + + + + + + + + """#) + } + + // inline list is invalid + #expect(rejects(#""" + + + + + + + + """#)) + } + + @Test("nested union of atomic types as list itemType is accepted") + func test_nestedUnionAtomicListAccepted() throws { + try compile(""" + + + + + + + + + + """) + } + + @Test("nested union containing a list as list itemType is rejected") + func test_nestedUnionListRejected() { + #expect(rejects(""" + + + + + + + + + + + + + """)) + } + + @Test("foreign itemType reference does not conflate with local list of same name") + func test_listItemTypeForeignStandDown() throws { + try compile(""" + + + + + + + + """) + } +} diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index cca9c47..b42387f 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -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 diff --git a/docs/schema-validity-burndown.md b/docs/schema-validity-burndown.md index 401f353..f0367a2 100644 --- a/docs/schema-validity-burndown.md +++ b/docs/schema-validity-burndown.md @@ -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 `` restriction base, complexContent restriction attributes prohibition) | diff --git a/docs/xsts-deviations.md b/docs/xsts-deviations.md index 036e267..fcdbd84 100644 --- a/docs/xsts-deviations.md +++ b/docs/xsts-deviations.md @@ -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)