diff --git a/CHANGELOG.md b/CHANGELOG.md index 91453b9..76df29b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Sources/Schema/SchemaImportNamespace.swift b/Sources/Schema/SchemaImportNamespace.swift index e5b82ce..9d7fd34 100644 --- a/Sources/Schema/SchemaImportNamespace.swift +++ b/Sources/Schema/SchemaImportNamespace.swift @@ -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 + } } diff --git a/Sources/Schema/XSDDerivation.swift b/Sources/Schema/XSDDerivation.swift index 10e6015..a7c2fe0 100644 --- a/Sources/Schema/XSDDerivation.swift +++ b/Sources/Schema/XSDDerivation.swift @@ -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 diff --git a/Tests/SchemaIncludeTests.swift b/Tests/SchemaIncludeTests.swift index 387f18c..0351daf 100644 --- a/Tests/SchemaIncludeTests.swift +++ b/Tests/SchemaIncludeTests.swift @@ -24,6 +24,48 @@ struct SchemaIncludeTests { #expect(try !doc.validate("abc").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 = """ + + + + """ + let imported = """ + + + + """ + 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 = """ + + + + + + + + + """ + #expect((try? PureXML.Schema.Document(foreign, schemaLoader: loader)) == nil) + // A proper self-redefine (base resolves to the redefining target namespace). + let selfRedefine = """ + + + + + + + + """ + #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 diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 5e7c6fe..65e9e79 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -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