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.
This commit is contained in:
Mihaela Mihaljevic 2026-06-14 11:36:21 +02:00
parent 9d4ce304c2
commit 0143381f67
6 changed files with 183 additions and 2 deletions

View File

@ -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).

View File

@ -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<String> = [
"minExclusive", "minInclusive", "maxExclusive", "maxInclusive", "totalDigits",
"fractionDigits", "length", "minLength", "maxLength", "enumeration", "whiteSpace", "pattern",
]
private static let listFacets: Set<String> = ["length", "minLength", "maxLength", "pattern", "enumeration", "whiteSpace"]
private static let unionFacets: Set<String> = ["pattern", "enumeration"]
private static let builtinListTypes: Set<String> = ["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>) -> 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>) -> 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>) -> 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
}
}

View File

@ -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
}

View File

@ -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("""
<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("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(#"""
<xs:simpleType name="l"><xs:list itemType="xs:integer"/></xs:simpleType>
<xs:simpleType name="t"><xs:restriction base="l"><xs:\#(facet) value="5"/></xs:restriction></xs:simpleType>
"""#), "expected rejection of \(facet) on a list")
}
// A built-in list datatype (xs:NMTOKENS) is also a list.
#expect(rejects(#"<xs:simpleType name="t"><xs:restriction base="xs:NMTOKENS"><xs:maxLength value="3"/><xs:maxInclusive value="5"/></xs:restriction></xs:simpleType>"#))
}
@Test("length-family, pattern, enumeration, whiteSpace apply to a list")
func test_listAllowedFacets() throws {
try compile(#"""
<xs:simpleType name="l"><xs:list itemType="xs:integer"/></xs:simpleType>
<xs:simpleType name="t"><xs:restriction base="l">
<xs:minLength value="1"/><xs:maxLength value="9"/><xs:enumeration value="1 2 3"/>
</xs:restriction></xs:simpleType>
"""#)
}
@Test("only pattern and enumeration apply to a union")
func test_unionFacets() throws {
#expect(rejects(#"""
<xs:simpleType name="u"><xs:union memberTypes="xs:integer xs:string"/></xs:simpleType>
<xs:simpleType name="t"><xs:restriction base="u"><xs:maxLength value="3"/></xs:restriction></xs:simpleType>
"""#))
try compile(#"""
<xs:simpleType name="u"><xs:union memberTypes="xs:integer xs:string"/></xs:simpleType>
<xs:simpleType name="t"><xs:restriction base="u"><xs:pattern value="\d+"/><xs:enumeration value="1"/></xs:restriction></xs:simpleType>
"""#)
}
@Test("a value-bound facet on an atomic type is left alone")
func test_atomicUnaffected() throws {
try compile(#"<xs:simpleType name="t"><xs:restriction base="xs:integer"><xs:maxInclusive value="5"/></xs:restriction></xs:simpleType>"#)
}
@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(#"""
<xs:simpleType name="integer"><xs:list itemType="xs:string"/></xs:simpleType>
<xs:simpleType name="t"><xs:restriction base="xs:integer"><xs:maxInclusive value="5"/></xs:restriction></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 = 72
private let knownSchemaInvalidAccepted = 664
private let knownSchemaInvalidAccepted = 638
private let knownInstanceValidRejected = 214
private let knownInstanceInvalidAccepted = 165

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 | 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 | |