Skip to content

validation/relaxng: fix div, annotation, QName, combine, and interleave-ref bugs - #79

Merged
jonwiggins merged 5 commits into
mainfrom
fix/relaxng-52-56
Nov 20, 2026
Merged

jonwiggins merged 5 commits into
mainfrom
fix/relaxng-52-56

Conversation

@jonwiggins

@jonwiggins jonwiggins commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes five RELAX NG schema parser/validator bugs found while loading the modular HTML5 RELAX NG schema. I reproduced each one on main before fixing it. There is one commit per issue, and all changes are in src/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 their ns down (§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 and xmlns:* declarations (e.g. xmlns:ns, xmlns:name) can't be mistaken for ns/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, and xml: is always bound (§4.10). An undeclared prefix or a malformed QName is a schema error. Also applies §4.8: the name shorthand on <attribute> without its own ns means an unqualified attribute; it no longer inherits the grammar's ns.

Fixes #55
combine ignored. Same-named <define>s (and multiple <start>s) are merged with choice/interleave per §4.17. These are now schema errors: more than one component without combine, mixing choice and interleave, and unknown combine values.

Hardening that comes with it, since merged defines can create recursion that no single define shows:

  • The parser enforces §4.19: a define reachable from start may only refer back to itself through an <element>. Before, <define name="a"><ref name="a"/></define> overflowed the stack during validation.
  • Ref chains that pass through no <element> are capped at 256.
  • Choice and interleave lists are built as balanced trees, so 5000 combined fragments don't create a 5000-deep pattern.
  • The checks walk the schema iteratively.

Fixes #56
Attributes reached through <ref> not matched. split_attributes now looks through Refs, 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 exponential RelaxNgSchemas terminate.

Not addressed (pre-existing, out of scope)

  • <include>/<externalRef>, nested <grammar>, and <parentRef> are still unsupported. <include> is silently ignored.
  • Attribute patterns under <choice>/<zeroOrMore>/<oneOrMore> are still not matched as attributes.
  • When an <element>/<attribute> gives its name with a child name class instead of a name attribute, every <choice> child (including content choices) is treated as part of the name class and dropped from the content.

Testing

  • 33 new unit tests: reproducers plus edge cases (nested divs, ns on 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, and cargo doc with -D warnings all pass
  • MSRV: cargo +1.81 test --features cli,ffi (the CI MSRV configuration) passes. cargo +1.81 check --all-features can't run on main either, because the optional libxml bench dependency needs edition 2024.

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.
@jonwiggins
jonwiggins merged commit f537a45 into main Nov 20, 2026
6 checks passed
@jonwiggins
jonwiggins deleted the fix/relaxng-52-56 branch November 20, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment