From ee5d180ba3e6b9de2b0c8021fa96885500cb6904 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 17 Jun 2026 16:45:35 +0200 Subject: [PATCH] fix(schema): enforce the NameAndTypeOK block-superset rule (#145) Particle Valid (Restriction) now requires a restricting element's {disallowed substitutions} to be a superset of the base element's: a restriction may block more substitutions, never fewer. The element particle (Term.element) carries a block set, populated from the element's block attribute or the schema's blockDefault. XSTS invalid-schemas-accepted 179 to 163, no other bucket moved. --- CHANGELOG.md | 2 ++ Sources/Schema/ComplexType.swift | 5 ++++- Sources/Schema/ComplexValidatorContentModel.swift | 6 +++--- Sources/Schema/ComplexValidatorHelpers.swift | 4 ++-- Sources/Schema/ContentMatcher.swift | 2 +- Sources/Schema/ParticleRestriction.swift | 14 +++++++++----- Sources/Schema/XSDCompletions.swift | 2 +- Sources/Schema/XSDContext.swift | 3 +++ Sources/Schema/XSDParser.swift | 1 + Sources/Schema/XSDParserParticles.swift | 1 + Tests/XSTSSuiteTests.swift | 6 +++++- 11 files changed, 32 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b05607..ceca15f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-s ### Fixed +- XSTS invalid-schemas-accepted (#145): Particle Valid (Restriction) now enforces the NameAndTypeOK block-superset clause. A restricting element's `{disallowed substitutions}` (its `block`, or the schema's `blockDefault`) must be a superset of the base element's, so a restriction may block more substitutions but never fewer. The element particle now carries this `block` set. Caught sixteen invalid schemas: invalid-schemas-accepted 179 → 163, no other bucket moved. + - XSTS invalid-schemas-accepted (#145): the RecurseAsIfGroup case of Particle Valid (Restriction) now checks occurrence. An element restricting a base model group is treated as a synthetic group with `minOccurs=maxOccurs=1` holding the element (XSD 1.0 §3.9.6), so a base group that must occur two or more times cannot be restricted to a single element. The element's own occurrence is still matched against the base branch it maps to, so an optional element restricting a once-or-more base group stays valid (no false rejection). Caught two invalid schemas: invalid-schemas-accepted 181 → 179, no other bucket moved. - XSTS invalid-schemas-accepted (#145): a `redefine` is now checked against the schema it redefines once that schema is loaded: every component the `redefine` names (a `complexType`, `simpleType`, `group`, or `attributeGroup`) must have a same-kind component of the same name in the redefined schema (`src-redefine.6`/`7.2.1`), and the same component may not be redefined twice in one `redefine` (`src-redefine.7.2.2`). A redefinition naming a component that does not exist there, or a duplicate redefinition, is rejected. The check runs only when the redefined document was loaded through a `schemaLoader`, so a stand-alone schema is unaffected. Caught two invalid schemas: invalid-schemas-accepted 183 → 181, no other bucket moved. diff --git a/Sources/Schema/ComplexType.swift b/Sources/Schema/ComplexType.swift index 106d684..322c274 100644 --- a/Sources/Schema/ComplexType.swift +++ b/Sources/Schema/ComplexType.swift @@ -104,7 +104,10 @@ public extension PureXML.Schema { /// (a built-in, a user type, or `anyType` for an absent type), or nil when /// the type is an inline anonymous definition. It preserves the derivation /// identity the flattened `type` (`ElementType`) loses, for NameAndTypeOK. - case element(name: PureXML.Model.QualifiedName, type: ElementType?, typeName: String?, valueConstraint: ValueConstraint? = nil) + /// `block` is the element's `{disallowed substitutions}` (its own `block`, or + /// the schema's `blockDefault`), needed for the NameAndTypeOK block-superset + /// rule in Particle Valid (Restriction); empty when nothing is blocked. + case element(name: PureXML.Model.QualifiedName, type: ElementType?, typeName: String?, valueConstraint: ValueConstraint? = nil, block: Set = []) case group(Group) case wildcard(Wildcard) } diff --git a/Sources/Schema/ComplexValidatorContentModel.swift b/Sources/Schema/ComplexValidatorContentModel.swift index 878acc7..31188b6 100644 --- a/Sources/Schema/ComplexValidatorContentModel.swift +++ b/Sources/Schema/ComplexValidatorContentModel.swift @@ -40,7 +40,7 @@ extension PureXML.Schema.ComplexValidator { counts[position] += 1 } for (index, member) in group.particles.enumerated() where counts[index] < member.minOccurs { - if case let .element(name, _, _, _) = member.term { + if case let .element(name, _, _, _, _) = member.term { errors.append(XSDFailure(reason: "element '\(name.localName)' is required but missing", at: path)) } } @@ -48,7 +48,7 @@ extension PureXML.Schema.ComplexValidator { static func memberMatches(_ term: PureXML.Schema.Term, _ name: PureXML.Model.QualifiedName) -> Bool { switch term { - case let .element(declared, _, _, _): declared.localName == name.localName && declared.namespaceURI == name.namespaceURI + case let .element(declared, _, _, _, _): declared.localName == name.localName && declared.namespaceURI == name.namespaceURI case let .wildcard(wildcard): wildcard.admits(name) case .group: false } @@ -77,7 +77,7 @@ extension PureXML.Schema.ComplexValidator { static func matchedParticle(of term: PureXML.Schema.Term) -> PureXML.Schema.MatchedParticle? { switch term { - case let .element(_, type, _, valueConstraint): .element(type: type, valueConstraint: valueConstraint) + case let .element(_, type, _, valueConstraint, _): .element(type: type, valueConstraint: valueConstraint) case let .wildcard(wildcard): .wildcard(wildcard) case .group: nil } diff --git a/Sources/Schema/ComplexValidatorHelpers.swift b/Sources/Schema/ComplexValidatorHelpers.swift index c278714..d157262 100644 --- a/Sources/Schema/ComplexValidatorHelpers.swift +++ b/Sources/Schema/ComplexValidatorHelpers.swift @@ -34,7 +34,7 @@ extension PureXML.Schema.ComplexValidator { fileprivate static func label(of term: XSDTerm) -> XSDTermLabel { switch term { - case let .element(name, _, _, _): .name(name) + case let .element(name, _, _, _, _): .name(name) case let .wildcard(wildcard): .wildcard(wildcard) case .group: .wildcard(XSDWildcard()) } @@ -64,7 +64,7 @@ extension PureXML.Schema.ComplexValidator { fileprivate static func collectTypes(_ term: XSDTerm, into result: inout [String: XSDElementType]) { switch term { - case let .element(name, type, _, _): + case let .element(name, type, _, _, _): if let type { result[key(name)] = type } case let .group(group): for member in group.particles { diff --git a/Sources/Schema/ContentMatcher.swift b/Sources/Schema/ContentMatcher.swift index 15d9c95..0a1f180 100644 --- a/Sources/Schema/ContentMatcher.swift +++ b/Sources/Schema/ContentMatcher.swift @@ -247,7 +247,7 @@ extension PureXML.Schema { private mutating func term(_ term: PureXML.Schema.Term) -> (start: Int, accept: Int) { switch term { - case let .element(name, type, _, constraint): + case let .element(name, type, _, constraint, _): let fragment = labeled(.name(name)) states[fragment.start].elementType = type states[fragment.start].valueConstraint = constraint diff --git a/Sources/Schema/ParticleRestriction.swift b/Sources/Schema/ParticleRestriction.swift index c77990f..625ba86 100644 --- a/Sources/Schema/ParticleRestriction.swift +++ b/Sources/Schema/ParticleRestriction.swift @@ -66,11 +66,15 @@ extension PureXML.Schema { return emptiable(base) } switch (restricted.term, base.term) { - case let (.element(restrictedName, _, restrictedTypeName, _), .element(baseName, _, baseTypeName, _)): + case let (.element(restrictedName, _, restrictedTypeName, _, restrictedBlock), .element(baseName, _, baseTypeName, _, baseBlock)): + // NameAndTypeOK: beyond name, occurrence, and type derivation, the + // restricting element's {disallowed substitutions} must be a superset + // of the base's (a restriction may block more, never fewer). return rangeSubsumed(restricted, base) && sameName(restrictedName, baseName) + && restrictedBlock.isSuperset(of: baseBlock) && elementTypeRestrictionOK(restrictedTypeName, baseTypeName, types, derivation) - case let (.element(name, _, _, _), .wildcard(wildcard)): + case let (.element(name, _, _, _, _), .wildcard(wildcard)): return rangeSubsumed(restricted, base) && wildcard.admits(name) case let (.wildcard(restrictedWildcard), .wildcard(baseWildcard)): return rangeSubsumed(restricted, base) && narrows(restrictedWildcard, baseWildcard) @@ -85,7 +89,7 @@ extension PureXML.Schema { // element restricting a once-or-more base group. return rangeWithinWildcard(derivedMin: 1, derivedMax: 1, wildcardMin: base.minOccurs, wildcardMax: base.maxOccurs) && groupValid(Group(compositor: .sequence, particles: [restricted]), baseGroup, types, derivation) - case let (.group, .element(baseName, _, baseTypeName, _)): + case let (.group, .element(baseName, _, baseTypeName, _, _)): // The mirror of RecurseAsIfGroup, by the same cardinality argument as // group/wildcard: a base element accepts only its own name, its own // occurrence count of times. The derived group is a valid restriction @@ -282,7 +286,7 @@ extension PureXML.Schema { /// element by name, a nested wildcard by narrowing. private static func leavesAdmitted(_ particle: Particle, by wildcard: Wildcard) -> Bool { switch particle.term { - case let .element(name, _, _, _): wildcard.admits(name) + case let .element(name, _, _, _, _): wildcard.admits(name) case let .wildcard(inner): narrows(inner, wildcard) case let .group(group): group.particles.allSatisfy { leavesAdmitted($0, by: wildcard) } } @@ -300,7 +304,7 @@ extension PureXML.Schema { _ derivation: [String: TypeDerivation], ) -> Bool { switch particle.term { - case let .element(name, _, typeName, _): + case let .element(name, _, typeName, _, _): sameName(name, baseName) && elementTypeRestrictionOK(typeName, baseTypeName, types, derivation) case .wildcard: false diff --git a/Sources/Schema/XSDCompletions.swift b/Sources/Schema/XSDCompletions.swift index 2895be4..25d8a17 100644 --- a/Sources/Schema/XSDCompletions.swift +++ b/Sources/Schema/XSDCompletions.swift @@ -111,7 +111,7 @@ public extension PureXML.Schema { private static func collect(_ term: Term, into result: inout [String: ElementType]) { switch term { - case let .element(name, type, _, _): + case let .element(name, type, _, _, _): if let type { result[name.description] = type } case let .group(group): for member in group.particles { diff --git a/Sources/Schema/XSDContext.swift b/Sources/Schema/XSDContext.swift index 2fc4724..7e255c5 100644 --- a/Sources/Schema/XSDContext.swift +++ b/Sources/Schema/XSDContext.swift @@ -296,6 +296,9 @@ extension PureXML.Schema { /// `schemaLoader`, so cross-document rules may run over the merged component /// set instead of standing down. var compositionLoaded: Bool = false + /// The schema's `blockDefault`, the `{disallowed substitutions}` a local or + /// global element inherits when it states no `block` of its own. + var blockDefault: String? /// The location of each container schema document. var containerLocations: [ObjectIdentifier: String?] = [:] /// Target namespace imposed on an included schema with no `targetNamespace` diff --git a/Sources/Schema/XSDParser.swift b/Sources/Schema/XSDParser.swift index 3232ace..508e831 100644 --- a/Sources/Schema/XSDParser.swift +++ b/Sources/Schema/XSDParser.swift @@ -85,6 +85,7 @@ extension PureXML.Schema.XSDParser { compositionLoaded: compositionLoaded, containerLocations: containerLocations, ) + context.blockDefault = XSDNode.attribute(schema, "blockDefault") context.elementFormQualified = XSDNode.attribute(schema, "elementFormDefault") == "qualified" context.attributeFormQualified = XSDNode.attribute(schema, "attributeFormDefault") == "qualified" context.namespaceBindings = XSDNode.namespaceBindings(of: schema) diff --git a/Sources/Schema/XSDParserParticles.swift b/Sources/Schema/XSDParserParticles.swift index 0aabdac..cd0c33e 100644 --- a/Sources/Schema/XSDParserParticles.swift +++ b/Sources/Schema/XSDParserParticles.swift @@ -73,6 +73,7 @@ extension PureXML.Schema.XSDParser { type: elementType(node, context), typeName: elementTypeName(node), valueConstraint: valueConstraint(of: node), + block: methodSet(XSDNode.attribute(node, "block") ?? context.blockDefault), ), ) } diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 9ccea04..e0ec23f 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -87,8 +87,12 @@ struct XSTSSuiteTests { /// that must occur 2+ times cannot be restricted to one element; §3.9.6) then /// caught two more invalid schemas: invalid-schemas accepted 181 → 179, no other /// bucket moved. + /// Enforcing the NameAndTypeOK block-superset clause (a restricting element's + /// `{disallowed substitutions}` must be a superset of the base element's, with + /// `block`/`blockDefault` carried on the particle) then caught sixteen more + /// invalid schemas: invalid-schemas accepted 179 → 163, no other bucket moved. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 179 + private let knownSchemaInvalidAccepted = 163 private let knownInstanceValidRejected = 3 private let knownInstanceInvalidAccepted = 132