From 0143381f6742aa6f94e1c84133758b3bb9270705 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Sun, 14 Jun 2026 11:36:21 +0200 Subject: [PATCH] feat(schema): enforce facet applicability to simple-type variety A constraining facet must apply to its simple type's variety (XSD 1.0 Part 2): a list may be constrained only by length/minLength/maxLength, pattern, enumeration, whiteSpace; a union only by pattern and enumeration. A value-bound facet (maxInclusive, totalDigits, ...) constrains an atomic value space, so restricting a list of integer by maxInclusive was wrongly accepted. The base's variety is resolved from the raw tree (this runs before the types are compiled), namespace-aware so a user type sharing a built-in's local name does not flip the variety: a base in the XSD namespace is a built-in (only the list datatypes are list-variety), a base in the schema's own target namespace resolves to a declared global, and an external or unresolvable base is skipped. XSTS: invalid schemas accepted 664 to 638, valid schemas rejected held at 72, instance buckets unchanged. --- CHANGELOG.md | 1 + Sources/Schema/SchemaFacetApplicability.swift | 106 ++++++++++++++++++ Sources/Schema/SchemaStructure.swift | 1 + Tests/SchemaFacetApplicabilityTests.swift | 73 ++++++++++++ Tests/XSTSSuiteTests.swift | 2 +- docs/schema-validity-burndown.md | 2 +- 6 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 Sources/Schema/SchemaFacetApplicability.swift create mode 100644 Tests/SchemaFacetApplicabilityTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bb197a..bb9cd24 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 +- A constraining facet must apply to its simple type's variety (XSD 1.0 Part 2, #158). A `list` may be constrained only by `length`/`minLength`/`maxLength`, `pattern`, `enumeration`, and `whiteSpace`; a `union` only by `pattern` and `enumeration`. A value-bound facet (`maxInclusive`, `totalDigits`, ...) constrains an atomic value space, so restricting a list (`list of integer`) by `maxInclusive` was wrongly accepted. The base's variety is resolved from the raw tree (the check runs before the types are compiled), namespace-aware so a user type that shares a built-in's local name does not flip the variety: a base in the XSD namespace is a built-in (only the list datatypes are list-variety), a base in the schema's own target namespace resolves to a declared global, and an external or unresolvable base is skipped, so no valid restriction is rejected. XSTS: invalid schemas accepted 664 to 638, no other bucket raised (valid schemas rejected held at 72). - A complexType's shorthand content (without `simpleContent`/`complexContent`) is now checked for order, not just cardinality (#158). Its model is `(annotation?, (group | all | choice | sequence)?, (attribute | attributeGroup)*, anyAttribute?)`: an optional model group, then attributes, then an optional `anyAttribute`. The existing content-shape check caught the wrong number of model groups and the exclusivity of a content spec, but not the order, so a model group after an attribute, an attribute after `anyAttribute`, or a second `anyAttribute` (the ctB family) was accepted. The complexType's direct children, excluding `annotation` and the content specs, are matched against the same ordered slot model used for a `complexContent` derivation. XSTS: invalid schemas accepted 687 to 664, no other bucket raised (valid schemas rejected held at 72). - An `any`/`anyAttribute` wildcard's `namespace` constraint is now validated at compile time (#158). Its value space is `('##any' | '##other')` standing alone, or a list of namespace URIs and the `##targetNamespace`/`##local` tokens; the wildcard parser mapped any unrecognised token to a literal URI, so a misspelled token (`##all`, `##target`), an `##any`/`##other` placed inside a list (`##any ##other`), or any other `##`-prefixed token was accepted. The unprefixed `namespace` value is now tokenised and checked against that grammar, while `##any`/`##other` are admitted only as the whole value; a prefixed (foreign) attribute that merely shares the local name is the author's own and is not checked, so no valid schema is rejected. XSTS: invalid schemas accepted 736 to 687, no other bucket raised (valid schemas rejected held at 72). - An identity constraint's `selector`/`field` `xpath` is now validated against the XSD 1.0 restricted-XPath subset at compile time (#158). That subset (Part 1, 3.11.6) is far narrower than full XPath: a path is an optional leading `.//` then `/`-separated steps, a step is `.`, a name test (`QName`, `*`, or `NCName:*`), or a `child::` axis, and a `field` path may end in an attribute step (`@x` or `attribute::x`); there are no predicates (`[...]`), no other axes (`self::`, `descendant::`), no absolute or interior `//`. The xpath was stored raw and compiled lazily with `try?`, so a malformed one (a predicate, a bare `/`, an empty union branch, a spaced name-test colon) was silently accepted. It is now lexed into a whitespace-tolerant token stream and parsed against the grammar, so whitespace between tokens is allowed (`child :: a`, `. //.`) while `//` must be adjacent and a name test's own `:` may not be spaced; the name-test lexer uses the XML `NameChar` set, so a non-ASCII NCName is accepted. XSTS: invalid schemas accepted 785 to 736, no other bucket raised (valid schemas rejected held at 72). diff --git a/Sources/Schema/SchemaFacetApplicability.swift b/Sources/Schema/SchemaFacetApplicability.swift new file mode 100644 index 0000000..987e941 --- /dev/null +++ b/Sources/Schema/SchemaFacetApplicability.swift @@ -0,0 +1,106 @@ +extension PureXML.Schema.XSDParser { + /// Facet applicability to a simple type's variety (XSD 1.0 Part 2, 4.3): a + /// `list` may be constrained only by `length`/`minLength`/`maxLength`, + /// `pattern`, `enumeration`, and `whiteSpace`; a `union` only by `pattern` and + /// `enumeration`. A value-bound facet (`maxInclusive`, `totalDigits`, ...) + /// constrains an atomic value space, not a list or union, so restricting a list + /// by `maxInclusive` was wrongly accepted (the stF families). + /// + /// The base's variety is resolved from the raw tree, since this runs before the + /// types are compiled; an atomic, unresolvable, or external base is skipped, so + /// no valid restriction is rejected. + static func simpleTypeVarietyFacetErrors(_ schema: XSDTree) -> [String] { + let bindings = namespaceBindings(schema) + let target = PureXML.Schema.XSDNode.attribute(schema, "targetNamespace") + var errors: [String] = [] + forEachSimpleType(schema) { simpleType in + guard let restriction = PureXML.Schema.XSDNode.firstChild(simpleType, named: "restriction"), + let variety = restrictionBaseVariety(restriction, schema, bindings, target, []) + else { return } + let allowed = variety == "list" ? listFacets : unionFacets + let used = PureXML.Schema.XSDNode.elementChildren(restriction) + .compactMap(PureXML.Schema.XSDNode.localName) + .filter { facetNames.contains($0) } + for facet in used where !allowed.contains(facet) { + errors.append("the facet '\(facet)' does not apply to a \(variety) type") + } + } + return errors + } + + private static let facetNames: Set = [ + "minExclusive", "minInclusive", "maxExclusive", "maxInclusive", "totalDigits", + "fractionDigits", "length", "minLength", "maxLength", "enumeration", "whiteSpace", "pattern", + ] + private static let listFacets: Set = ["length", "minLength", "maxLength", "pattern", "enumeration", "whiteSpace"] + private static let unionFacets: Set = ["pattern", "enumeration"] + private static let builtinListTypes: Set = ["IDREFS", "ENTITIES", "NMTOKENS"] + + private static func forEachSimpleType(_ node: XSDTree, _ visit: (XSDTree) -> Void) { + let local = PureXML.Schema.XSDNode.localName(node) + if local == "appinfo" || local == "documentation" { return } + if local == "simpleType" { visit(node) } + for child in PureXML.Schema.XSDNode.elementChildren(node) { + forEachSimpleType(child, visit) + } + } + + /// The variety (`"list"`, `"union"`, or nil for atomic/unknown) a `restriction` + /// derives from: an inline `simpleType` base wins, otherwise the `base` QName. + private static func restrictionBaseVariety(_ restriction: XSDTree, _ schema: XSDTree, _ bindings: [String: String], _ target: String?, _ visited: Set) -> String? { + if let inlineBase = PureXML.Schema.XSDNode.firstChild(restriction, named: "simpleType") { + return simpleTypeVariety(inlineBase, schema, bindings, target, visited) + } + guard let base = PureXML.Schema.XSDNode.attribute(restriction, "base") else { return nil } + return namedTypeVariety(base, schema, bindings, target, visited) + } + + private static func simpleTypeVariety(_ simpleType: XSDTree, _ schema: XSDTree, _ bindings: [String: String], _ target: String?, _ visited: Set) -> String? { + if PureXML.Schema.XSDNode.firstChild(simpleType, named: "list") != nil { return "list" } + if PureXML.Schema.XSDNode.firstChild(simpleType, named: "union") != nil { return "union" } + if let restriction = PureXML.Schema.XSDNode.firstChild(simpleType, named: "restriction") { + return restrictionBaseVariety(restriction, schema, bindings, target, visited) + } + return nil + } + + /// The variety of a named base, resolved namespace-aware so a same-local-name + /// collision never flips the variety: a base in the XSD namespace is a built-in + /// (only the list datatypes are list-variety; the rest atomic, nil); a base in + /// the schema's own target namespace resolves to a declared global `simpleType` + /// (cycle-guarded); a base in any other namespace is external and skipped (nil). + private static func namedTypeVariety(_ base: String, _ schema: XSDTree, _ bindings: [String: String], _ target: String?, _ visited: Set) -> String? { + let (prefix, local) = splitQName(base) + let uri = prefix.map { bindings[$0] } ?? bindings[""] + if uri == xsdNamespace { + return builtinListTypes.contains(local) ? "list" : nil + } + guard uri == target, !visited.contains(local), let declared = globalSimpleType(named: local, schema) else { return nil } + return simpleTypeVariety(declared, schema, bindings, target, visited.union([local])) + } + + private static func splitQName(_ value: String) -> (prefix: String?, local: String) { + let parts = value.split(separator: ":", maxSplits: 1).map(String.init) + return parts.count == 2 ? (parts[0], parts[1]) : (nil, value) + } + + private static func globalSimpleType(named name: String, _ schema: XSDTree) -> XSDTree? { + PureXML.Schema.XSDNode.elementChildren(schema).first { + PureXML.Schema.XSDNode.localName($0) == "simpleType" && PureXML.Schema.XSDNode.attribute($0, "name") == name + } + } + + /// The prefix-to-namespace bindings on the schema element (its `xmlns:prefix` + /// attributes; the default namespace under the empty key). + private static func namespaceBindings(_ schema: XSDTree) -> [String: String] { + var bindings: [String: String] = [:] + for attribute in schema.attributes { + if attribute.name.prefix == "xmlns" { + bindings[attribute.name.localName] = attribute.value + } else if attribute.name.prefix == nil, attribute.name.localName == "xmlns" { + bindings[""] = attribute.value + } + } + return bindings + } +} diff --git a/Sources/Schema/SchemaStructure.swift b/Sources/Schema/SchemaStructure.swift index 3cf4d0f..61da636 100644 --- a/Sources/Schema/SchemaStructure.swift +++ b/Sources/Schema/SchemaStructure.swift @@ -56,6 +56,7 @@ extension PureXML.Schema.XSDParser { static func structureErrors(_ schema: XSDTree) -> [String] { var errors: [String] = [] collectStructure(schema, into: &errors) + errors += simpleTypeVarietyFacetErrors(schema) return errors } diff --git a/Tests/SchemaFacetApplicabilityTests.swift b/Tests/SchemaFacetApplicabilityTests.swift new file mode 100644 index 0000000..e798dd9 --- /dev/null +++ b/Tests/SchemaFacetApplicabilityTests.swift @@ -0,0 +1,73 @@ +@testable import PureXML +import Testing + +/// Facet applicability to a simple type's variety (XSD 1.0 Part 2, 4.3): a `list` +/// admits only the length-family, `pattern`, `enumeration`, `whiteSpace`; a `union` +/// only `pattern`/`enumeration`. A value-bound facet on a list or union is invalid +/// (the stF families). An atomic, unresolvable, or external base is left alone. +@Suite("Facet applicability to variety") +struct SchemaFacetApplicabilityTests { + 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("a value-bound facet on a list-derived type is rejected") + func test_listValueBoundFacet() { + for facet in ["maxInclusive", "maxExclusive", "minInclusive", "minExclusive", "totalDigits", "fractionDigits"] { + #expect(rejects(#""" + + + """#), "expected rejection of \(facet) on a list") + } + // A built-in list datatype (xs:NMTOKENS) is also a list. + #expect(rejects(#""#)) + } + + @Test("length-family, pattern, enumeration, whiteSpace apply to a list") + func test_listAllowedFacets() throws { + try compile(#""" + + + + + """#) + } + + @Test("only pattern and enumeration apply to a union") + func test_unionFacets() throws { + #expect(rejects(#""" + + + """#)) + try compile(#""" + + + """#) + } + + @Test("a value-bound facet on an atomic type is left alone") + func test_atomicUnaffected() throws { + try compile(#""#) + } + + @Test("variety resolution is namespace-aware: a same-named user type does not shadow a built-in") + func test_namespaceAwareResolution() throws { + // A user `simpleType` named `integer` (a list) must not be mistaken for the + // built-in `xs:integer` when it is the restriction base; the value-bound + // facet on the atomic built-in is valid. + try compile(#""" + + + """#) + } +} diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index ce21b15..877affe 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 = 72 - private let knownSchemaInvalidAccepted = 664 + private let knownSchemaInvalidAccepted = 638 private let knownInstanceValidRejected = 214 private let knownInstanceInvalidAccepted = 165 diff --git a/docs/schema-validity-burndown.md b/docs/schema-validity-burndown.md index e55874c..892df62 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 | we rarely reject a good schema | -| **invalid-schemas-accepted** | **2461 -> 664** | we rarely catch a bad one (facet, id, structural content-model, UPA determinism, complexContent + complexType content order, identity-constraint XPath-subset, and wildcard-namespace validity landed) | +| **invalid-schemas-accepted** | **2461 -> 638** | we rarely catch a bad one (facet definition + applicability, id, structural content-model, UPA determinism, complexContent + complexType content order, identity-constraint XPath-subset, and wildcard-namespace validity landed) | | valid-instances-rejected | 233 | (instance side, separately tracked) | | invalid-instances-accepted | 165 | |