Repository navigation
validation/relaxng: fix div, annotation, QName, combine, and interleave-ref bugs - #79
Merged
Merged
Conversation
Per RELAX NG §4.11 each <div> is replaced by its children, so <define> and <start> elements nested in any number of <div> levels belong to the enclosing grammar. Previously only direct <grammar> children were collected, so refs into div-wrapped defines (e.g. rnginline output) failed with 'undefined reference'.
RELAX NG §3 allows foreign (non-RELAX NG namespace) elements anywhere and foreign attributes on any element, and §4.1 removes them during simplification. The schema parser now skips foreign child elements such as <a:documentation> compatibility annotations, and only reads unqualified attributes, so foreign attributes and xmlns:* declarations (e.g. xmlns:ns) can no longer be mistaken for name/ns attributes.
#54) Per RELAX NG §4.10 a prefixed name (name="xml:lang" or <name>x:a</name>) is resolved against the namespace declarations in scope on the schema element, with the xml prefix always bound. Previously the raw QName was used as the local name, so it never matched a namespaced instance attribute or element. Undeclared prefixes and malformed QNames are now schema errors. Also apply §4.8: the name shorthand on <attribute> without its own ns attribute names an unqualified attribute instead of inheriting ns.
Per RELAX NG §4.17, all <define>s with the same name (and all <start>s) in a grammar are merged with the choice or interleave given by their combine attributes. Previously each define silently overwrote the last, so schemas built from modules that contribute alternatives to a shared pattern validated far too strictly. More than one component without combine, conflicting combine methods, and unknown combine values are now schema errors. Because combining can assemble recursion that no single define shows, the parser now enforces §4.19: a define reachable from the start may only refer back to itself through an <element>. Ref chains without an element are also capped at 256 levels, and choice/interleave lists are folded into balanced trees, so untrusted schemas cannot drive the recursive validator into unbounded recursion or stack overflow.
…56) split_attributes, which separates an element's attribute patterns from its child-content pattern, only recognised attribute patterns written inline. Attributes in a define reached through <ref> (the usual way to share attribute groups such as common.attrs) stayed in the content pattern and every instance attribute was reported as unexpected. A ref is equivalent to its define's content (RELAX NG §4.19), so the splitter now looks through refs, inlining defines that contribute attributes and keeping other refs unchanged. Attributes under <optional> (including via a ref) are extracted as optional, and <mixed> content is searched too. Ref depth and total work per element are capped so hand-built cyclic or exponentially branching schemas cannot hang or overflow the stack.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes five RELAX NG schema parser/validator bugs found while loading the modular HTML5 RELAX NG schema. I reproduced each one on
mainbefore fixing it. There is one commit per issue, and all changes are insrc/validation/relaxng.rs.Fixes #52
<div>not traversed. Only direct<grammar>children were collected. Now<start>and<define>are collected recursively through any number of nested<div>s, which also pass theirnsdown (§4.11, §4.9).Fixes #53
Annotation elements rejected. The parser now skips foreign child elements (anything outside the RELAX NG namespace; no-namespace is still accepted for compatibility), such as
<a:documentation>(§3, §4.1). It now reads only unqualified attributes, so foreign attributes andxmlns:*declarations (e.g.xmlns:ns,xmlns:name) can't be mistaken forns/name. A root element in a foreign namespace is now a schema error.Fixes #54
Prefixed names not resolved.
name="p:local"and<name>p:local</name>are now resolved against the namespace declarations in scope in the schema, andxml:is always bound (§4.10). An undeclared prefix or a malformed QName is a schema error. Also applies §4.8: thenameshorthand on<attribute>without its ownnsmeans an unqualified attribute; it no longer inherits the grammar'sns.Fixes #55
combineignored. Same-named<define>s (and multiple<start>s) are merged withchoice/interleaveper §4.17. These are now schema errors: more than one component withoutcombine, mixingchoiceandinterleave, and unknowncombinevalues.Hardening that comes with it, since merged defines can create recursion that no single define shows:
<element>. Before,<define name="a"><ref name="a"/></define>overflowed the stack during validation.<element>are capped at 256.Fixes #56
Attributes reached through
<ref>not matched.split_attributesnow looks throughRefs, which are equivalent to their define's content (§4.19). Defines that contribute attributes are inlined; other refs stay as they are. Attributes under<optional>, including through a ref, are extracted as optional, and<mixed>is searched too. Ref depth and total steps per element are capped (both fail closed), so hand-built cyclic or exponentialRelaxNgSchemas terminate.Not addressed (pre-existing, out of scope)
<include>/<externalRef>, nested<grammar>, and<parentRef>are still unsupported.<include>is silently ignored.<choice>/<zeroOrMore>/<oneOrMore>are still not matched as attributes.<element>/<attribute>gives its name with a child name class instead of anameattribute, every<choice>child (including content choices) is treated as part of the name class and dropped from the content.Testing
nson div, annotations in name classes, foreign attributes,xml:/declared/undeclared/malformed QNames, combine across divs, combined<start>, combine errors, ref cycles through combined defines, unreachable cycles allowed, chain-depth limit, 5000 combined defines, nested-interleave refs, optional refs, cyclic/exponential hand-built schemas)cargo fmt --all,cargo clippy --all-targets --all-features -- -D warnings(stable and 1.100.0),cargo test --all-features, andcargo docwith-D warningsall passcargo +1.81 test --features cli,ffi(the CI MSRV configuration) passes.cargo +1.81 check --all-featurescan't run onmaineither, because the optionallibxmlbench dependency needs edition 2024.