fix(schema): enforce derivation base-type kind (src-ct.1/.2); invalid-schemas-accepted 104 to 99 (#145)

A content-model derivation's base must match its content-model keyword
(XSD 1.0 Structures 3.4.6):

- src-ct.1: a complexContent restriction/extension base must be a complex
  type. A simpleType or a built-in simple type (xs:string) is rejected; the
  ur-type xs:anyType is complex and remains a valid base.

- src-ct.2 (extension): a simpleContent extension base must be a simple type
  or a complex type with simple content. A complex type with element-only,
  mixed, or empty content, including xs:anyType, is rejected.

A locally-declared type takes precedence over the built-in reading, so a
schema that targets the XSD namespace and extends its own complex types
(the schema-for-schemas, e.g. xs:openAttrs) is not rejected. This caught a
self-inflicted false positive during development; the fix restores it.

XSTS: invalid-schemas-accepted 104 to 99 (ctE003/ctE004/ctK002/ctJ002/
ctJ003), valid-schemas-rejected held at 0, no other bucket moved. The
differential corroborates: PureXML-stricter unchanged at 87 over 9618
schemas (zero new over-rejection).
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 21:49:12 +02:00
parent 606bc35a45
commit 9c57dedb64
5 changed files with 133 additions and 9 deletions

View File

@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- XSTS invalid-schemas-accepted (#145): the base-type kind of a content-model derivation is enforced. A `complexContent` restriction or extension base must be a complex type (`src-ct.1`), so a `simpleType` or a built-in simple type such as `xs:string` is rejected (the ur-type `xs:anyType` is complex and stays valid). A `simpleContent` extension base must be a simple type or a complex type with simple content (`src-ct.2`), so a complex type with element-only, mixed, or empty content, including `xs:anyType`, is rejected. A locally-declared type takes precedence over the built-in reading, so a schema that targets the XSD namespace and extends its own complex types (the schema-for-schemas) is not rejected. invalid-schemas-accepted 104 → 99, no false positive (valid-schemas-rejected held at 0, corroborated by the differential: PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved.
- XSTS invalid-schemas-accepted (#145): an identity constraint's content model is enforced. `unique`, `key`, and `keyref` must contain exactly one `selector` followed by one or more `field`s (after an optional `annotation`); a `field` before the `selector`, a second `selector`, or no `field` is now rejected (previously only child-element membership was checked, not their cardinality or order). invalid-schemas-accepted 110 → 104, no false positive (valid-schemas-rejected held at 0), no other bucket moved.
- XSTS invalid-schemas-accepted (#145): the occurrence-range constraints on model groups are enforced. A particle's `minOccurs` may not exceed its `maxOccurs`, both defaulting to 1 when absent (`p-props-correct.1`), so `<xs:sequence minOccurs="2">` (2 > the default 1) and `<xs:all maxOccurs="0">` (the default 1 > 0) are rejected; and an `all` group's `maxOccurs` must be 1 and `minOccurs` 0 or 1, with every particle it contains limited to `maxOccurs` 0 or 1 (`cos-all-limited.2`). The previous check fired only when both attributes were present, missing the common default-`maxOccurs` cases. Triple-confirmed against the W3C-labelled suite and libxml2: invalid-schemas-accepted 130 → 110, with no false positive (valid-schemas-rejected held at 0) and no other bucket moved.

View File

@ -365,7 +365,9 @@ extension PureXML.Validation.SchemaCompile {
return PureXML.Schema.SchemaLocatedFinding.unlocated(
PureXML.Schema.XSDParser.simpleContentExtensionBaseErrors(document.schema, document.context, namedTypes)
+ PureXML.Schema.XSDParser.extensionMixedAgreementErrors(document.schema, document.context, namedTypes)
+ PureXML.Schema.XSDParser.simpleContentRestrictionBaseErrors(document.schema, document.context, namedTypes),
+ PureXML.Schema.XSDParser.simpleContentRestrictionBaseErrors(document.schema, document.context, namedTypes)
+ PureXML.Schema.XSDParser.complexContentBaseKindErrors(document.schema, document.context, namedTypes)
+ PureXML.Schema.XSDParser.simpleContentExtensionBaseKindErrors(document.schema, document.context, namedTypes),
)
}
}

View File

@ -138,6 +138,69 @@ extension PureXML.Schema.XSDParser {
return errors
}
/// XSD 1.0 `src-ct.1`: the base of a `complexContent` derivation (restriction or
/// extension) must be a COMPLEX type. The ur-type `xs:anyType` is complex and is
/// the legitimate base of most complex types; every other XSD built-in, and a
/// user `simpleType`, is a simple type and may not be a complexContent base. A
/// LOCALLY-declared type takes precedence over the built-in reading: a schema may
/// target the XSD namespace and define its own components there (the
/// schema-for-schemas extends its own `xs:openAttrs`), so resolve `types` first
/// and only fall back to the built-in table when there is no local declaration.
/// Self-contained schema only; a foreign or unresolved base is left alone.
static func complexContentBaseKindErrors(_ 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 derivation = ExtAllNode.firstChild(content, named: "extension")
?? ExtAllNode.firstChild(content, named: "restriction"),
let base = ExtAllNode.attribute(derivation, "base")
else { continue }
let namespace = ExtAllNode.referenceNamespace(base, bindings)
let baseName = ExtAllNode.stripPrefix(base)
if namespace == context.targetNamespace, let local = types[baseName] {
if case .simple = local {
errors.append("a complexContent base must be a complex type, not the simple type '\(baseName)'")
}
} else if namespace == xsdNamespace, types[baseName] == nil, baseName != "anyType" {
errors.append("a complexContent base must be a complex type, not the built-in simple type '\(baseName)'")
}
}
return errors
}
/// XSD 1.0 `src-ct.2` (extension): the base of a `simpleContent` EXTENSION must be
/// a simple type or a complex type whose content type is simple. A complex type
/// with element-only, mixed, or empty content (including the ur-type
/// `xs:anyType`) may not be a simpleContent extension base, since the result must
/// have simple content. A LOCALLY-declared type takes precedence over the
/// built-in reading (a schema may target the XSD namespace), so resolve `types`
/// first. A simple-type base (built-in or user) is valid; only `xs:anyType` as a
/// built-in, or a local complex type without simple content, is an error.
/// Self-contained schema only; a foreign or unresolved base is left alone.
static func simpleContentExtensionBaseKindErrors(_ 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: "simpleContent") {
guard let ext = ExtAllNode.firstChild(content, named: "extension"),
let base = ExtAllNode.attribute(ext, "base")
else { continue }
let namespace = ExtAllNode.referenceNamespace(base, bindings)
let baseName = ExtAllNode.stripPrefix(base)
if namespace == context.targetNamespace, case let .complex(complex)? = types[baseName] {
switch complex.content {
case .simpleContent: break
case .mixed, .elementOnly, .empty:
errors.append("a simpleContent extension's base complex type '\(baseName)' must have simple content")
}
} else if namespace == xsdNamespace, types[baseName] == nil, baseName == "anyType" {
errors.append("a simpleContent extension's base must be a simple type or a complex type with simple content, not '\(baseName)'")
}
}
return errors
}
/// XSD 1.0 `cos-ct-extends.1.4.3.2.2.1`: a `complexContent` extension's mixedness
/// must match its base type's. The effective content joins the base content and
/// the extension's, so a mixed extension may extend only a mixed base and an

View File

@ -56,4 +56,59 @@ struct SchemaExtensionAllGroupTests {
let base = "<xs:complexType name=\"mixedEmpty\" mixed=\"true\"><xs:sequence/></xs:complexType>"
#expect(!compiles(base + ext(base: "mixedEmpty", content: allContent)))
}
private let simpleBase = "<xs:complexType name=\"simpleC\"><xs:simpleContent>"
+ "<xs:extension base=\"xs:string\"><xs:attribute name=\"a\" type=\"xs:string\"/></xs:extension>"
+ "</xs:simpleContent></xs:complexType>"
private func simpleExt(base: String) -> String {
"<xs:complexType name=\"fooType\"><xs:simpleContent>"
+ "<xs:extension base=\"\(base)\"/></xs:simpleContent></xs:complexType>"
}
/// src-ct.1: a complexContent base must be a complex type. A user simpleType or
/// an XSD built-in simple type (`xs:string`) is rejected; the complex ur-type
/// `xs:anyType` and a user complex type are valid bases.
@Test("a complexContent base must be a complex type (src-ct.1)")
func test_complexContentBaseKind() {
let simpleType = "<xs:simpleType name=\"st\"><xs:restriction base=\"xs:string\"/></xs:simpleType>"
#expect(!compiles(simpleType + ext(base: "st", content: allContent)))
#expect(!compiles(ext(base: "xs:string", content: allContent)))
// anyType is complex (the ur-type) and a user complex base are valid.
#expect(compiles(ext(base: "xs:anyType", content: allContent)))
#expect(compiles(seqBase + ext(base: "myType", content: "<xs:sequence><xs:element name=\"b\" type=\"xs:string\"/></xs:sequence>")))
}
/// src-ct.2 (extension): a simpleContent extension base must be a simple type or
/// a complex type with simple content. A complex type with element-only or mixed
/// content, or `xs:anyType`, is rejected; a simple type or a simple-content
/// complex type is valid.
@Test("a simpleContent extension base must have simple content (src-ct.2)")
func test_simpleContentExtensionBaseKind() {
// anyType (complex content) and an element-only complex type are invalid.
#expect(!compiles(simpleExt(base: "xs:anyType")))
#expect(!compiles(seqBase + simpleExt(base: "myType")))
let mixed = "<xs:complexType name=\"m\" mixed=\"true\"><xs:sequence/></xs:complexType>"
#expect(!compiles(mixed + simpleExt(base: "m")))
// A simple-type base, and a complex type with simple content, are valid.
#expect(compiles(simpleExt(base: "xs:string")))
#expect(compiles(simpleBase + simpleExt(base: "simpleC")))
}
/// A schema may target the XSD namespace and define its own components there (as
/// the schema-for-schemas does, extending its own complex `openAttrs`). A
/// locally-declared type must take precedence over the built-in reading, so such
/// a complexContent extension of a local complex type is not rejected.
@Test("a schema targeting the XSD namespace may extend its own complex types")
func test_localTypePrecedenceOverBuiltin() {
let document = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="openAttrs"><xs:complexContent><xs:restriction base="xs:anyType">
<xs:anyAttribute namespace="##other"/></xs:restriction></xs:complexContent></xs:complexType>
<xs:complexType name="annotated"><xs:complexContent><xs:extension base="xs:openAttrs">
<xs:sequence><xs:element name="annotation" minOccurs="0"/></xs:sequence></xs:extension></xs:complexContent></xs:complexType>
</xs:schema>
"""
#expect((try? PureXML.Schema.Document(document)) != nil)
}
}

View File

@ -141,15 +141,17 @@ struct XSTSSuiteTests {
/// gap) is accepted, 132 → 133. Per the production standard over-rejection is the
/// non-starter and under-rejection is recoverable; this debt is recovered by the
/// full effective-total-range Particle-Valid-Restriction (tracked).
/// Enforcing the occurrence-range constraints on model groups (p-props-correct.1:
/// minOccurs may not exceed maxOccurs, both defaulting to 1; cos-all-limited.2: an
/// all group's maxOccurs is 1 and minOccurs 0 or 1, its particles' maxOccurs 0 or
/// 1) then caught twenty more invalid schemas with no false positive: 130 → 110.
/// Enforcing the identity-constraint content model (`unique`/`key`/`keyref` is
/// `(annotation?, selector, field+)`: exactly one selector, then one or more
/// fields, in order) caught six more with no false positive: 110 → 104.
/// Three further rules, each with valid-schemas-rejected held at 0: model-group
/// occurrence ranges (p-props-correct.1 minOccurs<=maxOccurs both defaulting to 1;
/// cos-all-limited.2 an all group's maxOccurs is 1, minOccurs 0/1, its particles'
/// maxOccurs 0/1) 130 -> 110; the identity-constraint content model (unique/key/
/// keyref is annotation?, one selector, then field+) 110 -> 104; the derivation
/// base-type kind (src-ct.1 a complexContent base is complex; src-ct.2 a
/// simpleContent extension base is a simple type or a simple-content complex type;
/// a local type takes precedence over the built-in reading so the schema-for-
/// schemas, which targets the XSD namespace, is not rejected) 104 -> 99.
private let knownSchemaValidRejected = 0
private let knownSchemaInvalidAccepted = 104
private let knownSchemaInvalidAccepted = 99
private let knownInstanceValidRejected = 0
private let knownInstanceInvalidAccepted = 133