diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a9e883..95b48ba 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 (#145, #161): a redefinition of an attribute group restricts the original, so it may neither eliminate a required attribute the original declares nor re-introduce a prohibited one as usable (src-redefine / cos-ct-restricts). attgZ002 (dropping the required `att1`) and attgZ003 (re-adding the prohibited `att1` as optional) are now rejected. Re-declaring a prohibited attribute AS prohibited keeps it prohibited and stays valid, and the drop-required check is skipped when the redefinition uses a nested attributeGroup reference (which could re-introduce the attribute), so no valid redefinition is rejected. invalid-schemas-accepted 67 → 65, no false positive (valid-schemas-rejected and valid-instances-rejected held at 0), no other bucket moved. + - A complex type whose `simpleContent` derives from a list or union simple type now keeps that variety, so its element content is validated as a list or union rather than collapsing to an atomic value space. Previously `simpleContentType` dropped the base's variety, so an invalid item in a list-typed simpleContent (e.g. a non-int in a list-of-int) was accepted. A valid list/union value is unaffected. (Under-rejection fix surfaced by the libxml2 differential; the W3C suite does not exercise it, so the conformance counts are unchanged.) - XSTS invalid-schemas-accepted (#145): a substitution-group member whose type is a list or union derives only from `anySimpleType`, so it may affiliate only to a head typed `anySimpleType` (or to the same type, or as a recorded restriction of the head's type). The substitution-type check skipped derivation entirely when either the member or head type was a list/union; a list/union-typed member affiliated to an unrelated head type (addB141: a union member of a list-typed head) is now rejected, while a list/union member of an `anySimpleType` head and a restriction of a list head remain valid. invalid-schemas-accepted 69 → 67, no false positive (valid-schemas-rejected and valid-instances-rejected held at 0), no other bucket moved. diff --git a/Sources/Schema/SchemaCompileValidation.swift b/Sources/Schema/SchemaCompileValidation.swift index e0d9b94..85e3357 100644 --- a/Sources/Schema/SchemaCompileValidation.swift +++ b/Sources/Schema/SchemaCompileValidation.swift @@ -247,7 +247,7 @@ extension PureXML.Validation { AnyValidation(componentNamesUnique), AnyValidation(simpleTypeFinalControlsValid), AnyValidation(contentModelsDeterministic), AnyValidation(referencedSchemasResolveToSchemas), AnyValidation(redefinitionsDeriveFromThemselves), - AnyValidation(redefineSelfReferencesAreWellFormed), + AnyValidation(redefineSelfReferencesAreWellFormed), AnyValidation(redefinedAttributeGroupsKeepRequired), ], reference: [ AnyValidation(typeDerivationAcyclic), diff --git a/Sources/Schema/SchemaCompositionValidations.swift b/Sources/Schema/SchemaCompositionValidations.swift index f031f2c..eadecfd 100644 --- a/Sources/Schema/SchemaCompositionValidations.swift +++ b/Sources/Schema/SchemaCompositionValidations.swift @@ -35,6 +35,16 @@ extension PureXML.Validation.SchemaCompile { } } + /// src-redefine: a redefinition of an attribute group restricts the original, so + /// it may not eliminate a required attribute use the original declares. + static var redefinedAttributeGroupsKeepRequired: PureXML.Validation.Validation { + compileRule("A redefined attribute group keeps the original's required attributes") { document in + PureXML.Schema.SchemaLocatedFinding.unlocated( + PureXML.Schema.XSDParser.redefineAttributeGroupRestrictionErrors(document.containers), + ) + } + } + /// Facet definition validity and pattern syntax validity (XSD Part 2 4.3): a /// constraining facet must be applicable to and valid for its base type, and a /// `pattern` must compile. These are gathered during simple-type compilation diff --git a/Sources/Schema/SchemaRedefineSelfReference.swift b/Sources/Schema/SchemaRedefineSelfReference.swift index a9534c0..0585dcf 100644 --- a/Sources/Schema/SchemaRedefineSelfReference.swift +++ b/Sources/Schema/SchemaRedefineSelfReference.swift @@ -47,6 +47,74 @@ extension PureXML.Schema.XSDParser { return errors } + /// A redefinition of an `attributeGroup` is a RESTRICTION of the original + /// (src-redefine / cos-ct-restricts), so it may neither eliminate a REQUIRED + /// attribute the original declares, nor re-introduce one the original prohibits. + /// To stay false-positive-free: the drop-required check fires only when the + /// redefinition declares its attributes directly (no nested `attributeGroup` + /// reference, which could re-introduce the attribute); the re-introduced- + /// prohibited check fires only when the original has no attribute wildcard (which + /// could otherwise admit the attribute). + static func redefineAttributeGroupRestrictionErrors(_ containers: [XSDTree]) -> [String] { + // Key the base by NAMESPACED identity: an attribute group is identified by + // {name, target namespace}, and a redefinition's base is in the redefining + // schema's own target namespace, so a same-local-name group imported from a + // different namespace must not be mistaken for the base (matching the + // namespace guard `redefineSelfReferenceErrors` already uses). + var base: [String: XSDTree] = [:] + for container in containers where RedefineNode.localName(container) != "redefine" { + for group in RedefineNode.children(container, named: "attributeGroup") { + if let name = RedefineNode.attribute(group, "name") { base[namespacedKey(name, owner: group)] = group } + } + } + var errors: [String] = [] + for container in containers where RedefineNode.localName(container) == "redefine" { + for redefinition in RedefineNode.children(container, named: "attributeGroup") { + guard let name = RedefineNode.attribute(redefinition, "name"), + let baseGroup = base[namespacedKey(name, owner: redefinition)] + else { continue } + errors += attributeGroupRestrictionErrors(name: name, base: baseGroup, redefinition: redefinition) + } + } + return errors + } + + /// The `{target-namespace}name` identity of a named component, keyed by its + /// owning schema's `targetNamespace` so same-local-name components in different + /// namespaces stay distinct. + private static func namespacedKey(_ name: String, owner node: XSDTree) -> String { + let target = RedefineNode.attribute(RedefineNode.schemaOwner(node), "targetNamespace") ?? "" + return "{\(target)}\(name)" + } + + private static func attributeGroupRestrictionErrors(name: String, base: XSDTree, redefinition: XSDTree) -> [String] { + var errors: [String] = [] + let children = RedefineNode.elementChildren(redefinition) + // The redefinition's attributes by name -> `use`; re-declaring a base + // prohibited attribute AS prohibited keeps it prohibited (valid), so the + // re-introduction check needs the use, not mere presence of the name. + var redefinedUse: [String: String] = [:] + for attribute in children where RedefineNode.localName(attribute) == "attribute" { + if let attrName = RedefineNode.attribute(attribute, "name") { + redefinedUse[attrName] = RedefineNode.attribute(attribute, "use") ?? "optional" + } + } + let hasReference = children.contains { RedefineNode.localName($0) == "attributeGroup" } + let baseHasWildcard = RedefineNode.elementChildren(base).contains { RedefineNode.localName($0) == "anyAttribute" } + for attribute in RedefineNode.children(base, named: "attribute") { + guard let attrName = RedefineNode.attribute(attribute, "name") else { continue } + switch RedefineNode.attribute(attribute, "use") { + case "required" where !hasReference && redefinedUse[attrName] == nil: + errors.append("a redefined attribute group '\(name)' may not eliminate the required attribute '\(attrName)'") + case "prohibited" where !baseHasWildcard && redefinedUse[attrName] != nil && redefinedUse[attrName] != "prohibited": + errors.append("a redefined attribute group '\(name)' may not re-introduce the prohibited attribute '\(attrName)'") + default: + break + } + } + return errors + } + /// The reference NODES of `kind` nested directly in `node`'s model, stopping at /// `element`/`complexType`/`simpleType`/`attribute` scopes so a reference inside a /// nested element's content (a data-structure recursion, not a redefinition diff --git a/Tests/SchemaRedefineAttributeGroupTests.swift b/Tests/SchemaRedefineAttributeGroupTests.swift new file mode 100644 index 0000000..498d08e --- /dev/null +++ b/Tests/SchemaRedefineAttributeGroupTests.swift @@ -0,0 +1,46 @@ +@testable import PureXML +import Testing + +/// A redefinition of an attribute group restricts the original (src-redefine), so +/// it may neither eliminate a required attribute nor re-introduce a prohibited one +/// as usable. Keeping a required attribute, and re-declaring a prohibited attribute +/// as prohibited, are both valid (W3C attgZ002/attgZ003, schT3). +@Suite("redefined attribute group restriction") +struct SchemaRedefineAttributeGroupTests { + private func redefine(group body: String, base: String) throws -> PureXML.Schema.Document { + let main = "" + + "\(body)" + + "" + let baseDoc = "" + + "\(base)" + return try PureXML.Schema.Document(main, schemaLoader: { _ in baseDoc }) + } + + @Test("eliminating a required attribute is rejected") + func test_dropRequiredRejected() throws { + #expect(throws: (any Error).self) { + try redefine( + group: "", + base: "", + ) + } + } + + @Test("re-introducing a prohibited attribute as usable is rejected") + func test_reintroduceProhibitedRejected() throws { + #expect(throws: (any Error).self) { + try redefine( + group: "", + base: "", + ) + } + } + + @Test("keeping a required attribute and re-prohibiting a prohibited one is valid") + func test_validRedefinitionAccepted() throws { + _ = try redefine( + group: "", + base: "", + ) + } +} diff --git a/Tests/ValidationConfigurationTests.swift b/Tests/ValidationConfigurationTests.swift index 48cb65c..8bfe014 100644 --- a/Tests/ValidationConfigurationTests.swift +++ b/Tests/ValidationConfigurationTests.swift @@ -93,6 +93,7 @@ struct ValidationConfigurationTests { "A resolved schemaLocation reference is a valid schema", "A redefined type restricts or extends itself", "A redefined group or attribute group has a well-formed self-reference", + "A redefined attribute group keeps the original's required attributes", "Type derivation chains contain no cycles", "Schema type references contain no cycles", "xs:all group references appear only where permitted", diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 79036c0..99ff859 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -148,7 +148,7 @@ struct XSTSSuiteTests { /// and the cross-document composition family (src-resolve, src-import.3, /// src-redefine.5/6.1/7.2.1, redefine cross-namespace, required schemaLocation). private let knownSchemaValidRejected = 0 - private let knownSchemaInvalidAccepted = 67 + private let knownSchemaInvalidAccepted = 65 private let knownInstanceValidRejected = 0 /// Instance bucket (133 -> 31), per-step deltas in CHANGELOG.md: xsi:type must derive from the /// declared type (complex, then atomic and list/union, resolved through the element-ref chain);