fix(schema): a redefined attribute group keeps required and re-prohibits prohibited attributes; invalid-schemas-accepted 67 to 65 (#145, #161)
A redefinition of an attribute group restricts the original (src-redefine /
cos-ct-restricts), so it may neither eliminate a required attribute the original
declares (attgZ002) nor re-introduce a prohibited one as usable (attgZ003).
redefineAttributeGroupRestrictionErrors compares the redefinition to the original
attribute group and reports either violation.
False positives the verification caught and the rule guards against: re-declaring
a base prohibited attribute AS prohibited keeps it prohibited and is valid (the
check requires a non-prohibited use); the drop-required check is skipped when the
redefinition pulls attributes back via a nested attributeGroup reference; and the
base is matched by NAMESPACED identity ({target}name), so a same-local-name
attribute group imported from a different namespace is never mistaken for the base
(the same namespace guard the redefine self-reference rule uses).
invalid-schemas-accepted 67 to 65, no false positive (valid-schemas-rejected and
valid-instances-rejected held at 0). The gate caught a re-prohibition false
positive and an adversarial review caught a cross-namespace collision false
positive; both are fixed and covered by tests.
This commit is contained in:
parent
872e6ea7d0
commit
be9ba9fc1e
@ -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.
|
||||
|
||||
@ -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),
|
||||
|
||||
@ -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<PureXML.Schema.SchemaCompileRoot, PureXML.Schema.SchemaCompileContext> {
|
||||
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
|
||||
|
||||
@ -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
|
||||
|
||||
46
Tests/SchemaRedefineAttributeGroupTests.swift
Normal file
46
Tests/SchemaRedefineAttributeGroupTests.swift
Normal file
@ -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 = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
|
||||
+ "<xs:redefine schemaLocation='b.xsd'><xs:attributeGroup name='g'>\(body)</xs:attributeGroup></xs:redefine>"
|
||||
+ "<xs:element name='e'/></xs:schema>"
|
||||
let baseDoc = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
|
||||
+ "<xs:attributeGroup name='g'>\(base)</xs:attributeGroup></xs:schema>"
|
||||
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: "<xs:attribute name='b' type='xs:string'/>",
|
||||
base: "<xs:attribute name='a' use='required' type='xs:string'/><xs:attribute name='b' type='xs:string'/>",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("re-introducing a prohibited attribute as usable is rejected")
|
||||
func test_reintroduceProhibitedRejected() throws {
|
||||
#expect(throws: (any Error).self) {
|
||||
try redefine(
|
||||
group: "<xs:attribute name='a' type='xs:string'/>",
|
||||
base: "<xs:attribute name='a' use='prohibited' type='xs:string'/>",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("keeping a required attribute and re-prohibiting a prohibited one is valid")
|
||||
func test_validRedefinitionAccepted() throws {
|
||||
_ = try redefine(
|
||||
group: "<xs:attribute name='a' use='required' type='xs:string'/><xs:attribute name='p' use='prohibited' type='xs:string'/>",
|
||||
base: "<xs:attribute name='a' use='required' type='xs:string'/><xs:attribute name='p' use='prohibited' type='xs:string'/><xs:attribute name='o' type='xs:string'/>",
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -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",
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user