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.
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 13:02:37 +02:00
parent 39649a1940
commit ac242a980b
5 changed files with 57 additions and 1 deletions

View File

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

View File

@ -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<PureXML.Schema.SchemaCompileRoot, PureXML.Schema.SchemaCompileContext> {
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),
)
}
}
}

View File

@ -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 (`<sequence/>`)
/// 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"

View File

@ -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",

View File

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