diff --git a/CHANGELOG.md b/CHANGELOG.md index f787fcd..91453b9 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): 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. - XSTS invalid-schemas-accepted (#145): an attribute's type must be a simple type (`au-props-correct` / `src-attribute`). An `attribute` whose `type` resolves to a complex type, or to the complex ur-type `xs:anyType`, is rejected; a built-in or user simple type, an inline `simpleType`, or no type (`anySimpleType`) stays valid. A locally-declared type takes precedence over the built-in reading. invalid-schemas-accepted 96 → 95, 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 new file mode 100644 index 0000000..e5b82ce --- /dev/null +++ b/Sources/Schema/SchemaImportNamespace.swift @@ -0,0 +1,32 @@ +private typealias ImportNode = PureXML.Schema.XSDNode + +extension PureXML.Schema.XSDParser { + /// XSD 1.0 `src-import.3.1`/`3.2`: an `import`'s `namespace` attribute must equal + /// the `targetNamespace` of the schema it resolves to (both present and equal, or + /// both absent). Runs only past the cross-document skip guard, so the imported + /// schema is loaded. Checked only when the `schemaLocation` resolved to a loaded + /// container (an unresolved import is the processor's choice and not an error), + /// which is the false-positive boundary. Imports keep their own target namespace + /// (no chameleon), so the imported document's literal `targetNamespace` is used. + static func importNamespaceErrors(_ context: PureXML.Schema.XSDContext, _ containers: [XSDTree]) -> [String] { + var locationTargetNS: [String: String?] = [:] + for container in containers where ImportNode.localName(container) == "schema" { + guard let location = context.containerLocations[ObjectIdentifier(container)] ?? nil else { continue } + locationTargetNS[location] = ImportNode.attribute(container, "targetNamespace") + } + var errors: [String] = [] + for container in containers { + for node in descendants(container, named: "import") where node.name?.namespaceURI == xsdNamespace { + guard let location = ImportNode.attribute(node, "schemaLocation"), + locationTargetNS.keys.contains(location) + else { continue } + let imported = locationTargetNS[location] ?? nil + let declared = ImportNode.attribute(node, "namespace") + if declared != imported { + errors.append("import of '\(location)' declares namespace '\(declared ?? "(none)")', not the imported target namespace '\(imported ?? "(none)")'") + } + } + } + return errors + } +} diff --git a/Sources/Schema/SchemaReferences.swift b/Sources/Schema/SchemaReferences.swift index 30390c0..74d7a19 100644 --- a/Sources/Schema/SchemaReferences.swift +++ b/Sources/Schema/SchemaReferences.swift @@ -99,6 +99,7 @@ extension PureXML.Schema.XSDParser { collectReferenceErrors(source, in: resolutionContext, inheritedBindings: sourceBindings, into: &errors) } return xsdErrors + simpleContentErrors + errors + redefineComponentExistenceErrors(containers) + + importNamespaceErrors(context, containers) } /// XSD 1.0 `src-redefine.6`/`7.2.1`/`7.2.2`: each component a `redefine` names diff --git a/Tests/SchemaIncludeTests.swift b/Tests/SchemaIncludeTests.swift index c333ee4..387f18c 100644 --- a/Tests/SchemaIncludeTests.swift +++ b/Tests/SchemaIncludeTests.swift @@ -47,6 +47,29 @@ struct SchemaIncludeTests { #expect((try? PureXML.Schema.Document(main, schemaLoader: { _ in nil })) != nil) } + /// src-import.3.1/3.2: an import's namespace attribute must equal the imported + /// schema's targetNamespace (both equal, or both absent). Checked only when the + /// schemaLocation resolves to a loaded schema. + @Test("an import's namespace must match the imported targetNamespace") + func test_importNamespaceMustMatchImportedTarget() { + let mainNS = """ + + + + """ + // Imported doc's targetNamespace does NOT match the declared import namespace. + let mismatched = "" + #expect((try? PureXML.Schema.Document(mainNS, schemaLoader: { $0 == "b.xsd" ? mismatched : nil })) == nil) + // Imported doc has NO targetNamespace but the import declares one. + let noTarget = "" + #expect((try? PureXML.Schema.Document(mainNS, schemaLoader: { $0 == "b.xsd" ? noTarget : nil })) == nil) + // Matching targetNamespace compiles. + let matched = "" + #expect((try? PureXML.Schema.Document(mainNS, schemaLoader: { $0 == "b.xsd" ? matched : nil })) != nil) + // An unresolved import (loader returns nil) is not an error. + #expect((try? PureXML.Schema.Document(mainNS, schemaLoader: { _ in nil })) != nil) + } + @Test("xs:include with a mismatched targetNamespace is rejected") func test_invalidIncludeDifferentTargetNamespace() { let included = """ diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 98c8f0f..5e7c6fe 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -146,10 +146,12 @@ struct XSTSSuiteTests { /// 130->110; identity-constraint content model 110->104; derivation base-type kind /// (src-ct.1/.2, local type beats the built-in reading) 104->99; element value /// constraint needs simple/mixed content (cos-valid-default) 99->96; an attribute's - /// type is a simple type (au-props-correct) 96->95; and a resolved include/import/ - /// redefine schemaLocation must be a well-formed schema (src-resolve) 95->87. + /// 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. private let knownSchemaValidRejected = 0 - private let knownSchemaInvalidAccepted = 87 + private let knownSchemaInvalidAccepted = 84 private let knownInstanceValidRejected = 0 private let knownInstanceInvalidAccepted = 133