fix(schema): a redefined type must derive from itself, not a same-named foreign type (src-redefine.5); invalid-schemas-accepted 84 to 81 (#161)

A type inside xs:redefine must restrict or extend the type it redefines, which
lives in the redefining schema's own target namespace. checkRedefine compared
local names only (derivationInfo strips the prefix), so a base like foo:b-ct
-- an imported type sharing the redefined type's local name -- was treated as
self and accepted. Now the base's namespace is resolved: a base bound to an
explicit namespace other than the redefining schema's target namespace is not
self and is rejected (src-redefine.5).

redefineBaseIsForeign (SchemaImportNamespace.swift) leaves a base with no
resolvable namespace alone, so an unprefixed or own-namespace self-reference
never over-rejects -- the false-positive boundary, the same namespace-aware
fix applied across the campaign.

XSTS: invalid-schemas-accepted 84 to 81 (schQ2/schQ4/schP3),
valid-schemas-rejected held at 0, no other bucket moved. Differential:
agree-valid unchanged at 3161, PureXML-stricter unchanged at 87 over 9618
schemas. Third step of the cross-document composition campaign (#161).
This commit is contained in:
Mihaela Mihaljevic 2026-06-17 23:00:07 +02:00
parent c418147d30
commit a444604a80
5 changed files with 75 additions and 6 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 (#161): a type inside `xs:redefine` must restrict or extend the type it redefines, which lives in the redefining schema's own target namespace (`src-redefine.5`). A derivation base bound to a different namespace, such as an imported type that happens to share the redefined type's local name, is not a self-redefinition and is now rejected; the previous check compared local names only and accepted it. A base with no resolvable namespace is left alone, so an unprefixed or own-namespace self-reference is unaffected. invalid-schemas-accepted 84 → 81, no false positive (valid-schemas-rejected held at 0; differential PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved.
- XSTS invalid-schemas-accepted (#161): an `import`'s `namespace` attribute must equal the `targetNamespace` of the schema document it resolves to (`src-import.3.1`/`3.2`): both present and equal, or both absent. Importing a no-namespace document under a declared namespace, or a document whose target namespace differs from the declared one, is now rejected. Checked only when the `schemaLocation` resolves to a loaded schema (an unresolved import is the processor's choice and not an error); imports keep their own target namespace, so no chameleon adjustment applies. invalid-schemas-accepted 87 → 84, no false positive (valid-schemas-rejected held at 0; differential PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved.
- XSTS invalid-schemas-accepted (#145, #161): a resolved `include`/`import`/`redefine` `schemaLocation` must be a well-formed schema document (`src-resolve`). When the schema loader returns content for a referenced location, that content is now required to parse as an `xs:schema`; not-well-formed XML, or well-formed XML whose root is not `xs:schema`, makes the composing schema invalid. A location the loader does not resolve (returns nil) is not an error, since resolution is the processor's choice, so a self-contained schema is unaffected. invalid-schemas-accepted 95 → 87, no false positive (valid-schemas-rejected held at 0; differential PureXML-stricter unchanged at 87 over 9618 schemas), no other bucket moved.

View File

@ -29,4 +29,19 @@ extension PureXML.Schema.XSDParser {
}
return errors
}
/// src-redefine.5: a type inside `xs:redefine` must restrict or extend the type it
/// redefines, which lives in the redefining schema's OWN target namespace. So a
/// base bound to an EXPLICIT different namespace (for example an imported type
/// with the same local name) is not self and is invalid. A base with no resolvable
/// namespace is left alone, which keeps this from ever over-rejecting a
/// same-local-name self-reference.
static func redefineBaseIsForeign(_ type: XSDTree, _ rawBase: String?) -> Bool {
guard let rawBase else { return false }
let schema = ImportNode.schemaOwner(type)
let targetNamespace = ImportNode.attribute(schema, "targetNamespace")
let bindings = ImportNode.namespaceBindings(of: schema)
guard let baseNamespace = ImportNode.referenceNamespace(rawBase, bindings) else { return false }
return baseNamespace != targetNamespace
}
}

View File

@ -304,7 +304,14 @@ extension PureXML.Schema.XSDParser {
for container in containers where XSDDerivNode.localName(container) == "redefine" {
for type in XSDDerivNode.children(container, named: "complexType") {
guard let name = XSDDerivNode.attribute(type, "name") else { continue }
if derivationInfo(type)?.base != name { throw SchemaFault.redefineIncompatible(type: name) }
let derivationContainer = XSDDerivNode.firstChild(type, named: "complexContent")
?? XSDDerivNode.firstChild(type, named: "simpleContent")
let rawBase = derivationContainer
.flatMap { XSDDerivNode.firstChild($0, named: "extension") ?? XSDDerivNode.firstChild($0, named: "restriction") }
.flatMap { XSDDerivNode.attribute($0, "base") }
if derivationInfo(type)?.base != name || redefineBaseIsForeign(type, rawBase) {
throw SchemaFault.redefineIncompatible(type: name)
}
}
for type in XSDDerivNode.children(container, named: "simpleType") {
try checkRedefinedSimpleType(type)
@ -314,10 +321,11 @@ extension PureXML.Schema.XSDParser {
private static func checkRedefinedSimpleType(_ type: XSDTree) throws {
guard let name = XSDDerivNode.attribute(type, "name") else { return }
let base = XSDDerivNode.firstChild(type, named: "restriction")
let rawBase = XSDDerivNode.firstChild(type, named: "restriction")
.flatMap { XSDDerivNode.attribute($0, "base") }
.map(XSDDerivNode.stripPrefix)
if base != name { throw SchemaFault.redefineIncompatible(type: name) }
if rawBase.map(XSDDerivNode.stripPrefix) != name || redefineBaseIsForeign(type, rawBase) {
throw SchemaFault.redefineIncompatible(type: name)
}
}
/// Drops from each substitution group the members a head's `block` forbids: a

View File

@ -24,6 +24,48 @@ struct SchemaIncludeTests {
#expect(try !doc.validate("<code xmlns=\"urn:main\">abc</code>").isEmpty)
}
/// src-redefine.5: a type inside xs:redefine must restrict/extend the type it
/// redefines, which lives in the redefining schema's own target namespace. A base
/// bound to a foreign namespace with the same local name (an imported type) is not
/// self and is rejected; an unprefixed/own-namespace self-reference is valid.
@Test("a redefined type must derive from itself, not a same-named foreign type")
func test_redefineBaseMustBeSelfNamespace() {
let redefined = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:a">
<xs:complexType name="T"><xs:sequence><xs:element name="x" type="xs:string"/></xs:sequence></xs:complexType>
</xs:schema>
"""
let imported = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:foo">
<xs:complexType name="T"><xs:sequence/></xs:complexType>
</xs:schema>
"""
let loader: (String) -> String? = { $0 == "a.xsd" ? redefined : ($0 == "foo.xsd" ? imported : nil) }
// Redefine of T whose extension base is foo:T (a foreign same-local-name type).
let foreign = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:a" xmlns:foo="urn:foo">
<xs:import namespace="urn:foo" schemaLocation="foo.xsd"/>
<xs:redefine schemaLocation="a.xsd">
<xs:complexType name="T"><xs:complexContent><xs:extension base="foo:T">
<xs:sequence><xs:element name="y" type="xs:string"/></xs:sequence>
</xs:extension></xs:complexContent></xs:complexType>
</xs:redefine>
</xs:schema>
"""
#expect((try? PureXML.Schema.Document(foreign, schemaLoader: loader)) == nil)
// A proper self-redefine (base resolves to the redefining target namespace).
let selfRedefine = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:a" xmlns:a="urn:a">
<xs:redefine schemaLocation="a.xsd">
<xs:complexType name="T"><xs:complexContent><xs:extension base="a:T">
<xs:sequence><xs:element name="y" type="xs:string"/></xs:sequence>
</xs:extension></xs:complexContent></xs:complexType>
</xs:redefine>
</xs:schema>
"""
#expect((try? PureXML.Schema.Document(selfRedefine, schemaLoader: loader)) != nil)
}
/// src-resolve: when an include/import/redefine schemaLocation IS resolved (the
/// loader returns content), that content must be a well-formed schema. Not-well-
/// formed XML, or well-formed XML that is not an xs:schema, is rejected. A

View File

@ -149,9 +149,11 @@ struct XSTSSuiteTests {
/// type is a simple type (au-props-correct) 96->95; a resolved include/import/
/// redefine schemaLocation must be a well-formed schema (src-resolve) 95->87; and
/// an import's namespace must equal the imported targetNamespace (src-import.3)
/// 87->84.
/// 87->84; and a redefined type must derive from the type it redefines in the
/// redefining schema's own namespace, not a same-named foreign one (src-redefine.5)
/// 84->81.
private let knownSchemaValidRejected = 0
private let knownSchemaInvalidAccepted = 84
private let knownSchemaInvalidAccepted = 81
private let knownInstanceValidRejected = 0
private let knownInstanceInvalidAccepted = 133