fix(schema): a redefined group's self-reference must occur exactly once (src-redefine.6.1.2); invalid-schemas-accepted 79 to 77 (#161)

A group inside xs:redefine may reference itself (the group being redefined) at
most once, and that self-reference must have minOccurs = maxOccurs = 1. A
self-reference with minOccurs="0", maxOccurs="unbounded", or a count above 1 is
invalid. redefineGroupSelfReferenceErrors flags it, surfaced by the pre-compile
Validation redefineGroupSelfReferencesAreUnit.

The self-reference is matched by NAMESPACE (referenceNamespace == the redefining
schema's targetNamespace), not local name alone, so a redefined group that
references an imported group merely sharing its local name is correctly left
unconstrained -- the false-positive boundary an adversarial review flagged
before merge, mirroring the namespace guard the sibling redefine/cycle rules use.

XSTS: invalid-schemas-accepted 79 to 77 (schR3, schR4), valid-schemas-rejected
held at 0, no other bucket moved.
This commit is contained in:
Mihaela Mihaljevic 2026-06-18 00:52:40 +02:00
parent c45e1eb856
commit 880137a955
7 changed files with 78 additions and 5 deletions

View File

@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- XSTS invalid-schemas-accepted (#161): a `group` inside `xs:redefine` may reference itself at most once, and that self-reference must occur exactly once, `minOccurs` = `maxOccurs` = 1 (`src-redefine.6.1.2`). A self-reference with `minOccurs="0"`, `maxOccurs="unbounded"`, or any count above 1 is rejected. The self-reference is matched by namespace, not local name alone, so a reference to an imported group that merely shares the local name is unconstrained (no false positive). invalid-schemas-accepted 79 → 77, no false positive (valid-schemas-rejected held at 0), no other bucket moved.
- XSTS invalid-schemas-accepted (#145): a local `element` declaration must identify itself with a `name` or a `ref`; an element with neither is rejected. invalid-schemas-accepted 80 → 79, no false positive (valid-schemas-rejected held at 0), no other bucket moved.
- XSTS invalid-schemas-accepted (#145): a `complexType` nested in an `element` is a local, anonymous type and may not carry a `name` attribute; only a top-level `complexType` (or a `redefine` redefinition) is named. invalid-schemas-accepted 81 → 80, no false positive (valid-schemas-rejected held at 0), no other bucket moved.

View File

@ -244,10 +244,10 @@ extension PureXML.Validation {
Validator<PureXML.Schema.SchemaCompileContext>.defaults(
nonReference: [
AnyValidation(idAttributesValid), AnyValidation(schemaStructureValid),
AnyValidation(componentNamesUnique),
AnyValidation(simpleTypeFinalControlsValid),
AnyValidation(componentNamesUnique), AnyValidation(simpleTypeFinalControlsValid),
AnyValidation(contentModelsDeterministic),
AnyValidation(referencedSchemasResolveToSchemas), AnyValidation(redefinitionsDeriveFromThemselves),
AnyValidation(redefineGroupSelfReferencesAreUnit),
],
reference: [
AnyValidation(typeDerivationAcyclic),

View File

@ -25,6 +25,15 @@ extension PureXML.Validation.SchemaCompile {
}
}
/// src-redefine.6.1.2: a redefined group's self-reference has minOccurs = maxOccurs = 1.
static var redefineGroupSelfReferencesAreUnit: PureXML.Validation.Validation<PureXML.Schema.SchemaCompileRoot, PureXML.Schema.SchemaCompileContext> {
compileRule("A redefined group's self-reference occurs exactly once") { document in
PureXML.Schema.SchemaLocatedFinding.unlocated(
PureXML.Schema.XSDParser.redefineGroupSelfReferenceErrors(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

View File

@ -330,6 +330,38 @@ extension PureXML.Schema.XSDParser {
return errors
}
/// src-redefine.6.1.2: a `group` inside `xs:redefine` may reference ITSELF (the
/// group it redefines) at most once, and that self-reference must have
/// `minOccurs` = `maxOccurs` = 1. A self-reference with any other occurrence
/// (`minOccurs="0"`, `maxOccurs="unbounded"`, a count above 1) is invalid. A
/// reference is the self-reference only when it resolves (by NAMESPACE, not local
/// name alone) to the redefined group: a `<group ref="b:g">` to an imported group
/// that merely shares the local name is a different component and is unconstrained,
/// so the namespace guard prevents a false positive.
static func redefineGroupSelfReferenceErrors(_ containers: [XSDTree]) -> [String] {
var errors: [String] = []
for container in containers where XSDDerivNode.localName(container) == "redefine" {
let schema = XSDDerivNode.schemaOwner(container)
let target = XSDDerivNode.attribute(schema, "targetNamespace")
let bindings = XSDDerivNode.namespaceBindings(of: schema)
for group in XSDDerivNode.children(container, named: "group") {
guard let name = XSDDerivNode.attribute(group, "name") else { continue }
for ref in descendants(group, named: "group") {
guard let refName = XSDDerivNode.attribute(ref, "ref"),
XSDDerivNode.stripPrefix(refName) == name,
XSDDerivNode.referenceNamespace(refName, bindings) == target
else { continue }
let minOccurs = XSDDerivNode.attribute(ref, "minOccurs") ?? "1"
let maxOccurs = XSDDerivNode.attribute(ref, "maxOccurs") ?? "1"
if maxOccurs == "unbounded" || canonicalMagnitude(minOccurs) != "1" || canonicalMagnitude(maxOccurs) != "1" {
errors.append("a redefined group's self-reference must have minOccurs and maxOccurs of 1")
}
}
}
}
return errors
}
/// Drops from each substitution group the members a head's `block` forbids: a
/// member whose type derives from the head's type by a blocked method may not
/// substitute.

View File

@ -66,6 +66,34 @@ struct SchemaIncludeTests {
#expect((try? PureXML.Schema.Document(selfRedefine, schemaLoader: loader)) != nil)
}
/// src-redefine.6.1.2: a group inside xs:redefine may reference itself, but that
/// self-reference must occur exactly once (minOccurs = maxOccurs = 1). A
/// self-reference with minOccurs="0" or maxOccurs="unbounded" is invalid; a unit
/// self-reference (or absent occurrence, defaulting to 1) is valid.
@Test("a redefined group's self-reference must occur exactly once")
func test_redefineGroupSelfReferenceOccurrence() {
let original = """
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:a">
<xs:group name="g"><xs:choice><xs:element name="x" type="xs:string"/></xs:choice></xs:group>
</xs:schema>
"""
let loader: (String) -> String? = { $0 == "a.xsd" ? original : nil }
func redefine(_ selfRef: String) -> String {
"""
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:a" xmlns="urn:a">
<xs:redefine schemaLocation="a.xsd">
<xs:group name="g"><xs:choice><xs:element name="y" type="xs:string"/>\(selfRef)</xs:choice></xs:group>
</xs:redefine>
</xs:schema>
"""
}
// A non-unit self-reference is invalid.
#expect((try? PureXML.Schema.Document(redefine(#"<xs:group ref="g" minOccurs="0"/>"#), schemaLoader: loader)) == nil)
#expect((try? PureXML.Schema.Document(redefine(#"<xs:group ref="g" maxOccurs="unbounded"/>"#), schemaLoader: loader)) == nil)
// A unit self-reference is valid.
#expect((try? PureXML.Schema.Document(redefine(#"<xs:group ref="g"/>"#), schemaLoader: loader)) != nil)
}
/// src-resolve: when an include/import/redefine schemaLocation IS resolved (the
/// loader returns content), that content must be a well-formed schema. Not-well-
/// formed XML, or well-formed XML that is not an xs:schema, is rejected. A

View File

@ -92,6 +92,7 @@ struct ValidationConfigurationTests {
"Schema content models are deterministic (UPA)",
"A resolved schemaLocation reference is a valid schema",
"A redefined type restricts or extends itself",
"A redefined group's self-reference occurs exactly once",
"Type derivation chains contain no cycles",
"Schema type references contain no cycles",
"xs:all group references appear only where permitted",

View File

@ -148,10 +148,11 @@ struct XSTSSuiteTests {
/// (cos-valid-default), attribute type must be simple (au-props-correct), resolved
/// schemaLocation must be a schema (src-resolve), import namespace matches the
/// imported target (src-import.3), redefined type derives from itself
/// (src-redefine.5), local complexType is anonymous, and a local element has a
/// name or ref.
/// (src-redefine.5), local complexType is anonymous, a local element has a name
/// or ref, and a redefined group's self-reference occurs exactly once
/// (src-redefine.6.1.2).
private let knownSchemaValidRejected = 0
private let knownSchemaInvalidAccepted = 79
private let knownSchemaInvalidAccepted = 77
private let knownInstanceValidRejected = 0
private let knownInstanceInvalidAccepted = 133