From 880137a95594486b103fb944b8b9e5a27e16f82a Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Thu, 18 Jun 2026 00:52:40 +0200 Subject: [PATCH] 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. --- CHANGELOG.md | 2 ++ Sources/Schema/SchemaCompileValidation.swift | 4 +-- .../Schema/SchemaCompositionValidations.swift | 9 ++++++ Sources/Schema/XSDDerivation.swift | 32 +++++++++++++++++++ Tests/SchemaIncludeTests.swift | 28 ++++++++++++++++ Tests/ValidationConfigurationTests.swift | 1 + Tests/XSTSSuiteTests.swift | 7 ++-- 7 files changed, 78 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa1ae2d..a0ecc0c 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 (#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. diff --git a/Sources/Schema/SchemaCompileValidation.swift b/Sources/Schema/SchemaCompileValidation.swift index 91b1b51..5b79851 100644 --- a/Sources/Schema/SchemaCompileValidation.swift +++ b/Sources/Schema/SchemaCompileValidation.swift @@ -244,10 +244,10 @@ extension PureXML.Validation { Validator.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), diff --git a/Sources/Schema/SchemaCompositionValidations.swift b/Sources/Schema/SchemaCompositionValidations.swift index 9f7e90b..e92040f 100644 --- a/Sources/Schema/SchemaCompositionValidations.swift +++ b/Sources/Schema/SchemaCompositionValidations.swift @@ -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 { + 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 diff --git a/Sources/Schema/XSDDerivation.swift b/Sources/Schema/XSDDerivation.swift index a1b6a79..e439e52 100644 --- a/Sources/Schema/XSDDerivation.swift +++ b/Sources/Schema/XSDDerivation.swift @@ -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 `` 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. diff --git a/Tests/SchemaIncludeTests.swift b/Tests/SchemaIncludeTests.swift index 0351daf..73691c2 100644 --- a/Tests/SchemaIncludeTests.swift +++ b/Tests/SchemaIncludeTests.swift @@ -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 = """ + + + + """ + let loader: (String) -> String? = { $0 == "a.xsd" ? original : nil } + func redefine(_ selfRef: String) -> String { + """ + + + \(selfRef) + + + """ + } + // A non-unit self-reference is invalid. + #expect((try? PureXML.Schema.Document(redefine(#""#), schemaLoader: loader)) == nil) + #expect((try? PureXML.Schema.Document(redefine(#""#), schemaLoader: loader)) == nil) + // A unit self-reference is valid. + #expect((try? PureXML.Schema.Document(redefine(#""#), 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 diff --git a/Tests/ValidationConfigurationTests.swift b/Tests/ValidationConfigurationTests.swift index 2ffda2a..1421a89 100644 --- a/Tests/ValidationConfigurationTests.swift +++ b/Tests/ValidationConfigurationTests.swift @@ -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", diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 6c5f55a..42c11d7 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -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