From ac242a980b5e4462e15a1a0078c3f23c71872d8a Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Wed, 17 Jun 2026 13:02:37 +0200 Subject: [PATCH] fix(schema): reject complexContent extension adding element content over a simpleContent base (#145) A complexContent extension that adds element content (a non-empty model group) produces an element-only/mixed content type, so its base must have complex or empty content, not simpleContent (cos-ct-extends.1.4.2.2). Such schemas were accepted. The check requires the extension to add actual element content (an element or wildcard, not an empty group), so an attribute-only complexContent extension of a simpleContent base stays valid (the complexContent-around-simpleContent idiom, e.g. particlesZ031). XSTS invalid-schemas-accepted 235 to 233, no other bucket moved. --- CHANGELOG.md | 2 ++ Sources/Schema/SchemaCompileValidation.swift | 14 ++++++++ Sources/Schema/SchemaExtensionAllGroup.swift | 34 ++++++++++++++++++++ Tests/ValidationConfigurationTests.swift | 1 + Tests/XSTSSuiteTests.swift | 7 +++- 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d35959..c5f3d04 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): a `complexContent` extension that adds element content (a non-empty model group) may not extend a base whose content is simple (`cos-ct-extends.1.4.2.2`); a simple-content type is extended through `simpleContent`. An attribute-only `complexContent` extension of a simpleContent base stays valid (the `complexContent`-around-`simpleContent` idiom). Caught two invalid schemas: invalid-schemas-accepted 235 → 233, no other bucket moved. + - XSTS invalid-schemas-accepted (#145): two more schema-for-schemas structural rules. A named model group's compositor (`all`/`choice`/`sequence`) may not carry `minOccurs`/`maxOccurs` (those belong on a group reference), and an attribute in the XML Schema namespace (such as `xsd:type`) is rejected since the schema vocabulary defines no namespaced attributes (a foreign attribute in any other namespace stays the author's own). Caught nine invalid schemas: invalid-schemas-accepted 244 → 235, no other bucket moved. - XSTS invalid-schemas-accepted (#145): unambiguously malformed regular expressions in `pattern` facets are now schema-invalid: a reversed character-class range (`[b-a]`), an empty class (`[]`), and a reversed bounded quantifier (`{37,17}`). The parser raises distinct errors for these, which the schema-validity check rejects, while constructs the engine merely does not support (an untabulated `\p{Is…}` block, the lenient `{,m}` form) stay non-rejecting so a valid pattern is never refused. Caught eighteen invalid schemas: invalid-schemas-accepted 262 → 244, no other bucket moved. diff --git a/Sources/Schema/SchemaCompileValidation.swift b/Sources/Schema/SchemaCompileValidation.swift index fe55ecc..0202098 100644 --- a/Sources/Schema/SchemaCompileValidation.swift +++ b/Sources/Schema/SchemaCompileValidation.swift @@ -265,6 +265,7 @@ extension PureXML.Validation { AnyValidation(idValueConstraintsValid), AnyValidation(userTypeValueConstraintsValid), AnyValidation(extensionAllGroupsValid), + AnyValidation(complexExtensionBaseValid), AnyValidation(attributeRestrictionsFaithful), AnyValidation(simpleTypeBasesAreSimple), AnyValidation(simpleTypeVarietiesValid), @@ -336,3 +337,16 @@ extension PureXML.Validation { } } } + +extension PureXML.Validation.SchemaCompile { + /// cos-ct-extends.1.4.2.2: a complexContent extension adding element content + /// must have a base with complex (or empty) content, not simpleContent. + static var complexExtensionBaseValid: PureXML.Validation.Validation { + compileRule("A complexContent extension's base has complex content") { document in + guard let namedTypes = document.namedTypes else { return [] } + return PureXML.Schema.SchemaLocatedFinding.unlocated( + PureXML.Schema.XSDParser.simpleContentExtensionBaseErrors(document.schema, document.context, namedTypes), + ) + } + } +} diff --git a/Sources/Schema/SchemaExtensionAllGroup.swift b/Sources/Schema/SchemaExtensionAllGroup.swift index 04e8182..48a5b64 100644 --- a/Sources/Schema/SchemaExtensionAllGroup.swift +++ b/Sources/Schema/SchemaExtensionAllGroup.swift @@ -29,6 +29,40 @@ extension PureXML.Schema.XSDParser { return errors } + /// XSD 1.0 `cos-ct-extends.1.4.2.2`: a `complexContent` extension that adds + /// element content (a model group) produces an element-only/mixed content type, + /// so its base must have complex or empty content. A `simpleContent` base has a + /// simple content type and cannot gain element content this way. An extension + /// that adds only attributes keeps the base's simple content and is valid (it + /// is the `complexContent`-around-`simpleContent` idiom), so the model group is + /// required for the error. Resolved in this schema's own target namespace. + static func simpleContentExtensionBaseErrors(_ schema: XSDTree, _ context: PureXML.Schema.XSDContext, _ types: [String: PureXML.Schema.ElementType]) -> [String] { + guard !skipsCrossDocumentRules(schema, compositionLoaded: context.compositionLoaded) else { return [] } + let bindings = context.namespaceBindings + var errors: [String] = [] + for content in descendants(schema, named: "complexContent") { + guard let ext = ExtAllNode.firstChild(content, named: "extension"), + extensionAddsElementContent(ext), + let base = ExtAllNode.attribute(ext, "base"), + ExtAllNode.referenceNamespace(base, bindings) == context.targetNamespace, + case let .complex(complex)? = types[ExtAllNode.stripPrefix(base)], + case .simpleContent = complex.content + else { continue } + errors.append("a complexContent extension that adds element content may not extend '\(ExtAllNode.stripPrefix(base))', which has simple content") + } + return errors + } + + /// Whether a `complexContent` extension adds element content: an `element` or + /// a wildcard `any` somewhere in its model group. An empty group (``) + /// or an attribute-only extension adds no element content, so it keeps the + /// base's simple content and is not flagged. + private static func extensionAddsElementContent(_ extension: XSDTree) -> Bool { + let hasElement = descendants(`extension`, named: "element").contains { $0.name?.namespaceURI == xsdNamespace } + let hasWildcard = descendants(`extension`, named: "any").contains { $0.name?.namespaceURI == xsdNamespace } + return hasElement || hasWildcard + } + private static func extensionHasAllGroup(_ extension: XSDTree) -> Bool { ExtAllNode.elementChildren(`extension`).contains { child in child.name?.namespaceURI == xsdNamespace && ExtAllNode.localName(child) == "all" diff --git a/Tests/ValidationConfigurationTests.swift b/Tests/ValidationConfigurationTests.swift index e420ae7..5c2fc51 100644 --- a/Tests/ValidationConfigurationTests.swift +++ b/Tests/ValidationConfigurationTests.swift @@ -100,6 +100,7 @@ struct ValidationConfigurationTests { "ID-typed value constraints are valid", "Element value constraints are valid against their declared types", "Complex types extending xs:all groups satisfy XSD placement rules", + "A complexContent extension's base has complex content", "Attribute restrictions are faithful to their bases", "Simple types do not derive from complex types", "Simple-type varieties are declared consistently", diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 4024fb1..9e8fc90 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -78,8 +78,13 @@ struct XSTSSuiteTests { /// belong on a reference), and an attribute in the XML Schema namespace (e.g. /// `xsd:type`) is never valid. Invalid-schemas accepted 244 → 235, no other /// bucket moved. + /// Rejecting a complexContent extension that adds element content (a model + /// group) on top of a simpleContent base (cos-ct-extends.1.4.2.2) then caught + /// two more invalid schemas; an attribute-only complexContent extension of a + /// simpleContent base stays valid. Invalid-schemas accepted 235 → 233, no other + /// bucket moved. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 235 + private let knownSchemaInvalidAccepted = 233 private let knownInstanceValidRejected = 3 private let knownInstanceInvalidAccepted = 132