PureXML/Sources/Schema/SchemaNestedDefinition.swift
Mihaela Mihaljevic 282ceb57fc feat(schema): a nested simpleType or attributeGroup may not be named (localSimpleType)
A simpleType nested inside a restriction/list/union/element/attribute is the
anonymous localSimpleType (no name), and a nested attributeGroup is always a
reference; named simpleType and attributeGroup definitions are top-level only (a
direct child of schema or redefine). A nested definition declaring a name was
accepted; it is now rejected. A nested anonymous simpleType, an attributeGroup
reference, and a top-level named definition are unaffected.

XSTS: invalid schemas accepted 403 to 394, no other bucket raised.
2026-06-15 15:41:48 +02:00

48 lines
2.4 KiB
Swift

private typealias NestedNode = PureXML.Schema.XSDNode
extension PureXML.Schema.XSDParser {
/// A named `simpleType` or `attributeGroup` definition is a top-level component:
/// the schema-for-schemas `localSimpleType` and the nested `attributeGroup`
/// (a reference) forms carry no `name`. A `simpleType` nested inside a
/// `restriction`/`list`/`union`/`element`/`attribute`, or an `attributeGroup`
/// nested inside a `complexType`/`attributeGroup`, that declares a `name` was
/// accepted; it is now rejected. A nested anonymous `simpleType` and an
/// `attributeGroup` reference (which carry no `name`) are unaffected.
///
/// "Top-level" means a direct child of `schema` or of a `redefine`; everything
/// deeper is nested.
static func nestedNamedDefinitionErrors(_ schema: XSDTree) -> [String] {
var errors: [String] = []
collectNestedNamed(schema, parentIsTopLevel: false, into: &errors)
return errors
}
private static func collectNestedNamed(_ node: XSDTree, parentIsTopLevel: Bool, into errors: inout [String]) {
let kind = NestedNode.localName(node)
// Foreign content inside an annotation is not the schema's own and is not
// structurally checked.
if kind == "appinfo" || kind == "documentation" { return }
if isNestedNamedDefinition(node, kind, parentIsTopLevel: parentIsTopLevel) {
errors.append("a nested '\(kind ?? "")' definition may not have a 'name'; only a top-level definition is named")
}
let childrenAreTopLevel = kind == "schema" || kind == "redefine"
for child in NestedNode.elementChildren(node) {
collectNestedNamed(child, parentIsTopLevel: childrenAreTopLevel, into: &errors)
}
}
/// Whether `node` is a nested (non-top-level) `simpleType`/`attributeGroup` in
/// the XSD namespace that declares a `name` (which only a top-level definition
/// may carry).
private static func isNestedNamedDefinition(_ node: XSDTree, _ kind: String?, parentIsTopLevel: Bool) -> Bool {
!parentIsTopLevel
&& node.name?.namespaceURI == xsdNamespace
&& (kind == "simpleType" || kind == "attributeGroup")
&& hasUnprefixedName(node)
}
private static func hasUnprefixedName(_ node: XSDTree) -> Bool {
node.attributes.contains { $0.name.prefix == nil && $0.name.localName == "name" }
}
}