fix(schema): implement namespace-aware reference resolution checks
This commit is contained in:
parent
ffff13da57
commit
0496c6948a
@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
### Fixed
|
||||
|
||||
- Implement namespace-aware schema reference resolution checks, validating type references, element/attribute/group references, and substitutionGroup headers using XML namespace prefix bindings (XSD 1.0 Structures §3.14). If a prefix is bound to a namespace that is neither the targetNamespace nor the XMLSchema namespace (when no external schemas are loaded), the reference is flagged as undeclared. Bypasses built-in validation checks on references in the XMLSchema namespace when the targetNamespace is the XMLSchema namespace itself. Validate simpleContent base restrictions namespace-aware. XSTS: invalid schemas accepted 333 to 327, no other bucket raised.
|
||||
- Enforce simpleType restriction structure and prefix binding constraints (XSD 1.0 Datatypes §3.2.1 / Structures §3.3.1 / §3.14.3). Verify that restriction under simpleType only admits annotation, simpleType and facets, disallowing attributes, wildcards, model groups, and multiple or misplaced inline simpleTypes. Reject a restriction base, list itemType, or union memberType resolving to a complex type (e.g. anyType). Enforce that all QName attributes have bound prefixes. XSTS: invalid schemas accepted 350 to 333, no other bucket raised.
|
||||
- A top-level group or attributeGroup definition may no longer carry local-use attributes (XSD 1.0 Structures §3.6.1 / §3.6.2). A global group definition (including one defined inside `xs:redefine`) must only have name and id attributes, and local-use attributes like `ref`, `minOccurs`, or `maxOccurs` are rejected; a global attributeGroup definition may not carry a `ref` attribute. XSTS: invalid schemas accepted 354 to 350, no other bucket raised.
|
||||
- Support attribute inheritance and prohibition in complexContent restriction derivations (XSD 1.0 Structures §3.4.6 / Schema Component Constraint: Derivation Valid (Restriction, Complex)). When a complexType restricts a base type, the restricted type inherits all base attributes that are not explicitly overridden or prohibited by the restriction. A prohibited attribute is excluded from the final type's attributes, preventing its use in instance validation. Resolves referenced attributes namespace-aware using prefix bindings. XSTS: valid instances rejected 180 to 171, invalid instances accepted 158 to 155, no other bucket raised.
|
||||
|
||||
@ -7,6 +7,13 @@ extension PureXML.Schema.XSDParser {
|
||||
return names
|
||||
}()
|
||||
|
||||
private struct ResolutionContext {
|
||||
let types: Set<String>
|
||||
let pools: [String: Set<String>]
|
||||
let bindings: [String: String]
|
||||
let targetNamespace: String?
|
||||
}
|
||||
|
||||
/// Schema-validity findings for unresolvable references: every QName a schema
|
||||
/// names (a `type`/`base`/`itemType`/`memberTypes` type, an `element`/
|
||||
/// `attribute`/`group`/`attributeGroup` `ref`, an element `substitutionGroup`)
|
||||
@ -17,7 +24,9 @@ extension PureXML.Schema.XSDParser {
|
||||
/// `import`/`include`/`redefine`: the default compile does not load them, so
|
||||
/// the pools would be incomplete and a reference into them must not be flagged.
|
||||
static func referenceErrors(_ schema: XSDTree, in context: PureXML.Schema.XSDContext, elements: [String: PureXML.Schema.ElementType]) -> [String] {
|
||||
if hasExternalReference(schema) { return [] }
|
||||
let xsdErrors = xsdNamespaceReferenceErrors(schema)
|
||||
let simpleContentErrors = simpleContentBaseErrors(schema, in: context)
|
||||
if hasExternalReference(schema) { return xsdErrors + simpleContentErrors }
|
||||
let types = referenceBuiltins.union(context.simpleTypes.keys).union(context.complexTypeNodes.keys)
|
||||
let pools: [String: Set<String>] = [
|
||||
"element": Set(elements.keys),
|
||||
@ -25,9 +34,12 @@ extension PureXML.Schema.XSDParser {
|
||||
"group": Set(context.groups.keys),
|
||||
"attributeGroup": Set(context.attributeGroups.keys),
|
||||
]
|
||||
let bindings = PureXML.Schema.XSDNode.namespaceBindings(of: schema)
|
||||
let targetNamespace = context.targetNamespace
|
||||
var errors: [String] = []
|
||||
collectReferenceErrors(schema, types: types, pools: pools, into: &errors)
|
||||
return errors
|
||||
let resolutionContext = ResolutionContext(types: types, pools: pools, bindings: bindings, targetNamespace: targetNamespace)
|
||||
collectReferenceErrors(schema, in: resolutionContext, into: &errors)
|
||||
return xsdErrors + simpleContentErrors + errors
|
||||
}
|
||||
|
||||
/// Whether the document declares an `import`, `include`, or `redefine`, so its
|
||||
@ -59,37 +71,73 @@ extension PureXML.Schema.XSDParser {
|
||||
|
||||
private static func collectReferenceErrors(
|
||||
_ node: XSDTree,
|
||||
types: Set<String>,
|
||||
pools: [String: Set<String>],
|
||||
in resolutionContext: ResolutionContext,
|
||||
into errors: inout [String],
|
||||
) {
|
||||
let local = PureXML.Schema.XSDNode.localName(node)
|
||||
if local == "appinfo" || local == "documentation" { return }
|
||||
if node.name?.namespaceURI == xsdNamespace, let local {
|
||||
errors += referenceErrors(at: node, local: local, types: types, pools: pools)
|
||||
errors += referenceErrors(at: node, local: local, in: resolutionContext)
|
||||
}
|
||||
for child in PureXML.Schema.XSDNode.elementChildren(node) {
|
||||
collectReferenceErrors(child, types: types, pools: pools, into: &errors)
|
||||
collectReferenceErrors(child, in: resolutionContext, into: &errors)
|
||||
}
|
||||
}
|
||||
|
||||
private static func referenceErrors(at node: XSDTree, local: String, types: Set<String>, pools: [String: Set<String>]) -> [String] {
|
||||
private static func referenceErrors(
|
||||
at node: XSDTree,
|
||||
local: String,
|
||||
in resolutionContext: ResolutionContext,
|
||||
) -> [String] {
|
||||
var errors: [String] = []
|
||||
|
||||
func isUndeclaredType(_ qname: String) -> Bool {
|
||||
let prefix = PureXML.Schema.XSDNode.prefix(qname)
|
||||
let uri = prefix.flatMap { resolutionContext.bindings[$0] } ?? resolutionContext.bindings[""]
|
||||
let localPart = localName(qname)
|
||||
if uri == xsdNamespace {
|
||||
if resolutionContext.targetNamespace == xsdNamespace {
|
||||
return !referenceBuiltins.contains(localPart) && !resolutionContext.types.contains(localPart)
|
||||
} else {
|
||||
return !referenceBuiltins.contains(localPart)
|
||||
}
|
||||
}
|
||||
if uri == resolutionContext.targetNamespace || uri == nil || uri == "" {
|
||||
return !resolutionContext.types.contains(localPart)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isUndeclaredRef(_ qname: String, _ poolName: String) -> Bool {
|
||||
let prefix = PureXML.Schema.XSDNode.prefix(qname)
|
||||
let uri = prefix.flatMap { resolutionContext.bindings[$0] } ?? resolutionContext.bindings[""]
|
||||
let localPart = localName(qname)
|
||||
if uri == resolutionContext.targetNamespace || uri == nil || uri == "" {
|
||||
return resolutionContext.pools[poolName]?.contains(localPart) != true
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
for attribute in ["type", "base", "itemType"] {
|
||||
if let qname = PureXML.Schema.XSDNode.attribute(node, attribute), !types.contains(localName(qname)) {
|
||||
if let qname = PureXML.Schema.XSDNode.attribute(node, attribute), isUndeclaredType(qname) {
|
||||
errors.append("\(attribute) references undeclared type '\(qname)'")
|
||||
}
|
||||
}
|
||||
if let members = PureXML.Schema.XSDNode.attribute(node, "memberTypes") {
|
||||
for token in members.split(whereSeparator: \.isWhitespace) where !types.contains(localName(String(token))) {
|
||||
errors.append("memberTypes references undeclared type '\(token)'")
|
||||
for token in members.split(whereSeparator: \.isWhitespace) {
|
||||
let qname = String(token)
|
||||
if isUndeclaredType(qname) {
|
||||
errors.append("memberTypes references undeclared type '\(qname)'")
|
||||
}
|
||||
}
|
||||
}
|
||||
if let head = PureXML.Schema.XSDNode.attribute(node, "substitutionGroup"), pools["element"]?.contains(localName(head)) != true {
|
||||
if let head = PureXML.Schema.XSDNode.attribute(node, "substitutionGroup"), isUndeclaredRef(head, "element") {
|
||||
errors.append("substitutionGroup references undeclared element '\(head)'")
|
||||
}
|
||||
if let reference = PureXML.Schema.XSDNode.attribute(node, "ref"), let pool = pools[local], !pool.contains(localName(reference)) {
|
||||
errors.append("\(local) ref references undeclared '\(reference)'")
|
||||
if let reference = PureXML.Schema.XSDNode.attribute(node, "ref"), resolutionContext.pools[local] != nil {
|
||||
if isUndeclaredRef(reference, local) {
|
||||
errors.append("\(local) ref references undeclared '\(reference)'")
|
||||
}
|
||||
}
|
||||
return errors
|
||||
}
|
||||
@ -112,7 +160,11 @@ extension PureXML.Schema.XSDParser {
|
||||
let uri = prefix.flatMap { bindings[$0] } ?? bindings[""]
|
||||
let local = PureXML.Schema.XSDNode.stripPrefix(trimmed)
|
||||
if uri == xsdNamespace {
|
||||
return local == "anyType"
|
||||
if targetNamespace == xsdNamespace {
|
||||
return local == "anyType" || context.complexTypeNodes[local] != nil
|
||||
} else {
|
||||
return local == "anyType"
|
||||
}
|
||||
}
|
||||
if uri == targetNamespace || uri == nil || uri == "" {
|
||||
return context.complexTypeNodes[local] != nil
|
||||
@ -165,4 +217,89 @@ extension PureXML.Schema.XSDParser {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static func xsdNamespaceReferenceErrors(_ schema: XSDTree) -> [String] {
|
||||
let targetNamespace = PureXML.Schema.XSDNode.attribute(schema, "targetNamespace")
|
||||
if targetNamespace == xsdNamespace {
|
||||
return []
|
||||
}
|
||||
var errors: [String] = []
|
||||
let bindings = PureXML.Schema.XSDNode.namespaceBindings(of: schema)
|
||||
|
||||
func isUndeclaredBuiltin(_ qname: String) -> Bool {
|
||||
let prefix = PureXML.Schema.XSDNode.prefix(qname)
|
||||
let uri = prefix.flatMap { bindings[$0] } ?? bindings[""]
|
||||
if uri == xsdNamespace {
|
||||
let localName = PureXML.Schema.XSDNode.stripPrefix(qname)
|
||||
return !referenceBuiltins.contains(localName)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func check(_ node: XSDTree) {
|
||||
let local = PureXML.Schema.XSDNode.localName(node)
|
||||
if local == "appinfo" || local == "documentation" { return }
|
||||
|
||||
if node.name?.namespaceURI == xsdNamespace {
|
||||
for attribute in ["type", "base", "itemType"] {
|
||||
if let qname = PureXML.Schema.XSDNode.attribute(node, attribute), isUndeclaredBuiltin(qname) {
|
||||
errors.append("\(attribute) references undeclared type '\(qname)'")
|
||||
}
|
||||
}
|
||||
if let members = PureXML.Schema.XSDNode.attribute(node, "memberTypes") {
|
||||
for token in members.split(whereSeparator: \.isWhitespace) {
|
||||
let qname = String(token)
|
||||
if isUndeclaredBuiltin(qname) {
|
||||
errors.append("memberTypes references undeclared type '\(qname)'")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for child in PureXML.Schema.XSDNode.elementChildren(node) {
|
||||
check(child)
|
||||
}
|
||||
}
|
||||
check(schema)
|
||||
return errors
|
||||
}
|
||||
|
||||
private static func simpleContentBaseErrors(_ schema: XSDTree, in context: PureXML.Schema.XSDContext) -> [String] {
|
||||
var errors: [String] = []
|
||||
let bindings = PureXML.Schema.XSDNode.namespaceBindings(of: schema)
|
||||
let targetNamespace = context.targetNamespace
|
||||
|
||||
func check(_ node: XSDTree) {
|
||||
let local = PureXML.Schema.XSDNode.localName(node)
|
||||
if local == "appinfo" || local == "documentation" { return }
|
||||
|
||||
let isSimpleContentRestriction = node.name?.namespaceURI == xsdNamespace && local == "restriction"
|
||||
&& node.parent?.name?.namespaceURI == xsdNamespace
|
||||
&& node.parent.flatMap { PureXML.Schema.XSDNode.localName($0) } == "simpleContent"
|
||||
if isSimpleContentRestriction {
|
||||
if let base = PureXML.Schema.XSDNode.attribute(node, "base") {
|
||||
let prefix = PureXML.Schema.XSDNode.prefix(base)
|
||||
let uri = prefix.flatMap { bindings[$0] } ?? bindings[""]
|
||||
let localPart = PureXML.Schema.XSDNode.stripPrefix(base)
|
||||
if uri == targetNamespace || uri == nil || uri == "" {
|
||||
if let baseNode = context.complexTypeNodes[localPart] {
|
||||
let children = PureXML.Schema.XSDNode.elementChildren(baseNode)
|
||||
let hasSimpleContent = children.contains { child in
|
||||
child.name?.namespaceURI == xsdNamespace && PureXML.Schema.XSDNode.localName(child) == "simpleContent"
|
||||
}
|
||||
if !hasSimpleContent {
|
||||
errors.append("base type '\(base)' of simpleContent restriction must be a simple type or complex type with simpleContent")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for child in PureXML.Schema.XSDNode.elementChildren(node) {
|
||||
check(child)
|
||||
}
|
||||
}
|
||||
check(schema)
|
||||
return errors
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ struct XSTSSuiteTests {
|
||||
/// XSTS` (debug is far slower and the corpus is large). Per-case deviations
|
||||
/// are written to /tmp/xsts-failures.txt for the burn-down.
|
||||
private let knownSchemaValidRejected = 1
|
||||
private let knownSchemaInvalidAccepted = 333
|
||||
private let knownSchemaInvalidAccepted = 327
|
||||
private let knownInstanceValidRejected = 171
|
||||
private let knownInstanceInvalidAccepted = 155
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ irreducible tail. Do not deviate to new features while this is open.
|
||||
| Bucket | Count | Reading |
|
||||
|---|---|---|
|
||||
| valid-schemas-rejected | **72 -> 4** | production target is 0: rejecting a correct schema is the most user-hostile defect (#148). Particle-restriction over-rejection fixed: pointless `maxOccurs=0` particles removed, content-free restriction of an EMPTY base, empty-derived emptiable-base, and (#163) the pairwise check over the normalized content model with occurrence subsumption applied only to same-kind pairings (cross-kind element/group, group/element, group/wildcard match occurrence inside, by effective total range / member). #164 resolved element-ref namespaces by prefix binding (the `particlesJj/Jk/Jm/Jn/Js`, `particlesQ/R` cluster, -28). Content-free (`maxOccurs=0`) derived groups and substitution-group declaration order closed `particlesW006`/`elemZ027_a`; the W3C MapAndSum rule (Sequence:Choice, #165) closed `particlesV003`. Remaining 1 (`particlesZ001`): a documented spec ambiguity (RecurseAsIfGroup; the reference rejects it), not a defect. |
|
||||
| **invalid-schemas-accepted** | **2461 -> 333** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion, elementTypeRestrictionOK NameAndTypeOK union checks, prohibited attribute type-check bypass, keyref arity verification, top-level group/attributeGroup local-attribute checks, simpleType restriction structure and prefix binding checks landed). |
|
||||
| **invalid-schemas-accepted** | **2461 -> 327** | we rarely catch a bad one (facet, default/fixed value, particle-restriction name+type, id, structural content-model, UPA determinism, content order, identity-constraint XPath-subset, wildcard-namespace, final/block, simpleType content, group/wildcard and group/element cardinality, element-ref namespace, sequence/choice MapAndSum, circular type derivation, circular group/attributeGroup/substitution references, attribute-use uniqueness and single-ID, type-excludes-inline-type, ID-typed value-constraint exclusion, all-group reference placement, substitution-member type derivation, default/fixed mutual exclusion, elementTypeRestrictionOK NameAndTypeOK union checks, prohibited attribute type-check bypass, keyref arity verification, top-level group/attributeGroup local-attribute checks, simpleType restriction structure and prefix binding checks, namespace-aware reference resolution checks landed). |
|
||||
| valid-instances-rejected | **233 -> 171** | (instance side; #164's element-ref namespace fix is shared with instance validation, complexContent restriction attributes inheritance and namespace fixes) |
|
||||
| invalid-instances-accepted | **160 -> 155** | (instance side; resolving inline `<simpleType>` restriction base, complexContent restriction attributes prohibition) |
|
||||
|
||||
|
||||
@ -34,7 +34,7 @@ entries removed about 70 apparent deviations that were never bugs.
|
||||
| Kind | First measurement | Now |
|
||||
|---|---|---|
|
||||
| valid schema rejected | 75 | 1 |
|
||||
| invalid schema accepted | 2467 | 333 |
|
||||
| invalid schema accepted | 2467 | 327 |
|
||||
| valid instance rejected | 604 | 171 |
|
||||
| invalid instance accepted | 582 | 155 |
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user