diff --git a/CHANGELOG.md b/CHANGELOG.md index 3442721..3e34996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ Second pre-release. Continues the W3C XSTS schema-validity campaign: **invalid-s ### Fixed +- XSTS invalid-schemas-accepted (#145): unambiguously malformed regular expressions in `pattern` facets are now schema-invalid: a reversed character-class range (`[b-a]`), an empty class (`[]`), and a reversed bounded quantifier (`{37,17}`). The parser raises distinct errors for these, which the schema-validity check rejects, while constructs the engine merely does not support (an untabulated `\p{Is…}` block, the lenient `{,m}` form) stay non-rejecting so a valid pattern is never refused. Caught eighteen invalid schemas: invalid-schemas-accepted 262 → 244, no other bucket moved. + - XSTS invalid-instances-accepted (#147): identity constraints reject more invalid instances. Fields now enforce cardinality (a `field` must select at most one node; cvc-identity-constraint.3), so an instance whose `unique`/`key`/`keyref` field matches several nodes is rejected. Selector and field XPath prefixes now resolve against the schema's namespace context (where the constraint is declared) rather than the instance document, so a constraint like `.//v:vehicle` works whatever prefix the instance uses; a QName field value still compares using the instance's bindings. An attribute field's type also resolves through the selector's target global element, so values compare in their value space (3.0 and 3 are the same xsd:decimal key). Together these caught fifteen cases (idF005, idG005, idF010, the idc004/5/6 cluster, idZ010, and kin): invalid-instances-accepted 147 → 132, no other bucket moved. - XSTS valid-instances-rejected (#146): **173 → 3** on the 2006-11-06 archive (settled-status count; the four buckets now read 1/262/3/143). Regex patterns match Unicode scalars (not Swift grapheme clusters), fixing `\p{Is…}` block tests and combining-mark cases; empty patterns match only the empty string. The regex repertoire is pinned to Unicode 3.2, the version the corpus is consistent at: code points added in 3.2 (Tagalog, Cyrillic Komi) are word characters, while later additions (the Tamil digit zero) stay unassigned and match `\D` (`XSDCategory`, `Unicode32Assigned`). Per-child content assessment is driven by the content-model particle the automaton matched, not a by-name lookup (#180), so two same-named particles keep their own `fixed` values and an element matching a `skip` wildcard is not validated against its global declaration. Schema documents that form an import chain are compiled as a union rather than merged as separately compiled documents, so cross-document substitution groups resolve (#161, `Document.init(composing:)`). Plus attribute-group `anyAttribute` inheritance, extension wildcard union, namespace-qualified type tables for multi-document `xsi:schemaLocation`, per-container `elementFormDefault`, strict/lax attribute wildcard validation, built-in type resolution, and `xs:redefine` simpleType overrides. diff --git a/Sources/Regex/RegexParser.swift b/Sources/Regex/RegexParser.swift index b25da5b..9e5cb24 100644 --- a/Sources/Regex/RegexParser.swift +++ b/Sources/Regex/RegexParser.swift @@ -75,6 +75,7 @@ extension PureXML.Regex { maximum = peek() == "}" ? nil : parseInteger() } guard peek() == "}", let minimum else { throw RegexError.badQuantifier } + if let maximum, minimum > maximum { throw RegexError.reversedQuantifier } advance() return .repeated(atom, min: minimum, max: maximum) } @@ -178,6 +179,8 @@ extension PureXML.Regex { advance() cls.negated = true } + // A class must have at least one member; `[]` (or `[^]`) is invalid. + if peek() == "]" { throw RegexError.emptyClass } while let character = peek(), character != "]" { if character == "-", peek(1) == "[" { advance() @@ -204,7 +207,7 @@ extension PureXML.Regex { if peek() == "-", peek(1) != "]", peek(1) != "[" { advance() guard case let .scalar(high) = try nextClassMember() else { throw RegexError.badClass } - guard low <= high else { throw RegexError.badClass } + guard low <= high else { throw RegexError.reversedRange } cls.ranges.append(low ... high) } else { cls.ranges.append(low ... low) diff --git a/Sources/Regex/RegexSyntax.swift b/Sources/Regex/RegexSyntax.swift index 7f0de2e..ee6d4fe 100644 --- a/Sources/Regex/RegexSyntax.swift +++ b/Sources/Regex/RegexSyntax.swift @@ -7,6 +7,13 @@ extension PureXML.Regex { case badClass case badQuantifier case unsupported(String) + /// A character-class range whose first endpoint exceeds its second + /// (`[b-a]`): unambiguously invalid, never an engine limitation. + case reversedRange + /// An empty character class (`[]`): a class requires at least one member. + case emptyClass + /// A bounded quantifier whose minimum exceeds its maximum (`{37,17}`). + case reversedQuantifier var description: String { switch self { @@ -16,6 +23,9 @@ extension PureXML.Regex { case .badClass: "malformed character class" case .badQuantifier: "malformed quantifier" case let .unsupported(detail): "unsupported construct: \(detail)" + case .reversedRange: "character-class range is reversed" + case .emptyClass: "empty character class" + case .reversedQuantifier: "quantifier minimum exceeds maximum" } } } diff --git a/Sources/Schema/XSDSimpleParser.swift b/Sources/Schema/XSDSimpleParser.swift index 4d45579..b8f51c4 100644 --- a/Sources/Schema/XSDSimpleParser.swift +++ b/Sources/Schema/XSDSimpleParser.swift @@ -79,7 +79,7 @@ extension PureXML.Schema { /// pattern. Conservative: only the unambiguous structural errors reject. private static func rejectsPattern(_ error: PureXML.Regex.RegexError) -> Bool { switch error { - case .unbalanced, .danglingQuantifier: + case .unbalanced, .danglingQuantifier, .reversedRange, .emptyClass, .reversedQuantifier: true case .unsupported, .badQuantifier, .badEscape, .badClass: false diff --git a/Tests/XSTSSuiteTests.swift b/Tests/XSTSSuiteTests.swift index 14a00ef..f1e9591 100644 --- a/Tests/XSTSSuiteTests.swift +++ b/Tests/XSTSSuiteTests.swift @@ -68,8 +68,13 @@ struct XSTSSuiteTests { /// selector's target global element (not just the host's descendants) lets a /// `unique`/`key` compare values in their value space (3.0 and 3 are the same /// xsd:decimal key): invalid accepted 133 → 132, no other bucket moved. + /// Rejecting unambiguously malformed regex in `pattern` facets (a reversed + /// character-class range `[b-a]`, an empty class `[]`, a reversed quantifier + /// `{37,17}`) as schema-invalid, while staying lenient on constructs the + /// engine merely does not support, then caught eighteen invalid schemas: + /// invalid-schemas-accepted 262 → 244, no other bucket moved. private let knownSchemaValidRejected = 1 - private let knownSchemaInvalidAccepted = 262 + private let knownSchemaInvalidAccepted = 244 private let knownInstanceValidRejected = 3 private let knownInstanceInvalidAccepted = 132