Repository navigation
🐛 fix(xsd): check xs:list and xs:union values - #2147
Conversation
XMLSchema read neither the itemType of an xs:list nor the memberTypes of an xs:union, so a value of either type was accepted as any text. The built-in list types NMTOKENS, IDREFS and ENTITIES had no list check either. Validation now checks each item of a list against its item type and tries each member type of a union in turn. The length facets of a list count its items. The built-in list types check each item as an NMTOKEN or an NCName. Compiling now rejects an item or member type that names no type, a list or union with the wrong number of types, and definitions that are circular or nest too deep or too wide, because validation recurses through them. A chain of restrictions is followed to the list or union it ends in at any length, so a list restricted past the base-depth cutoff still checks its items.
Merging this PR will improve performance by 14.49%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
# Conflicts: # src/turbohtml/_c/validate/xsd.h
The merge with tox-dev#2113 moved xsd_check_names to a switch on the child kind, so the xs:list and xs:union branches read locals that no longer exist. Names outside the kind table reach the XSD_CHILD_KINDS branch, so the two checks run there after the identity constraint check, in the same order.
The built-in datatype table in `datatypes.h` had no entry for `ID`, `IDREF`, `ENTITY`, `NOTATION`, `gYear`, `gYearMonth`, `gMonth`, `gMonthDay` or `gDay`. A schema that named one of them fell back to `xs:string` in [`xsd_builtin_id`](https://github.com/tox-dev/turbohtml/blob/9fdbc4e6254b6be80bc2c25120479cebf4d9ef6e/src/turbohtml/_c/validate/xsd.h#L879-L881), and a RELAX NG `<data>` element with the XSD datatype library did the same in [`rng_datatype_id`](https://github.com/tox-dev/turbohtml/blob/9fdbc4e6254b6be80bc2c25120479cebf4d9ef6e/src/turbohtml/_c/validate/relaxng.h#L1019). Both engines therefore accepted any text for those types. This change gives the nine types their lexical checks, so a value outside the XSD 1.0 forms now reports a `datatype` error. Fixes #2159. ### The table had no entry for nine built-in names XSD 1.0 Part 2 defines a lexical space for each of these types: NCName for `ID`, `IDREF` and `ENTITY`, QName for `NOTATION`, and the reduced date forms for `gYear`, `gYearMonth`, `gMonth`, `gMonthDay` and `gDay`. The name lookup found no entry, so the name became `DT_UNKNOWN` and then `DT_STRING`. The fallback name list in [`xsd_is_builtin_type`](https://github.com/tox-dev/turbohtml/blob/9fdbc4e6254b6be80bc2c25120479cebf4d9ef6e/src/turbohtml/_c/validate/xsd.h#L1345-L1346) kept these names as known built-ins without a handler, so the schema compiled and validation ran no lexical check. ### The nine types get their lexical checks The enum and the name table gain nine entries in the same order, since the table is indexed by enum value in the error path. The forms are checked by [the new lexers](https://github.com/tox-dev/turbohtml/blob/cf90a0cdae43337c8049406bad6c9f0b84e9ddf0/src/turbohtml/_c/validate/datatypes.h#L658-L709), and [the dispatch](https://github.com/tox-dev/turbohtml/blob/cf90a0cdae43337c8049406bad6c9f0b84e9ddf0/src/turbohtml/_c/validate/datatypes.h#L768-L783) routes `ID`, `IDREF` and `ENTITY` to the NCName check, `NOTATION` to the QName check, and each `g*` type to its own form. The fallback list keeps only `anyType`. `gMonthDay` accepts February 29 because the value names no year. ### Behavior changes - A value outside the lexical form of one of these types now fails with a `datatype` error, for XMLSchema element and attribute types, types derived from them, and RELAX NG `<data>` with the XSD datatype library. - These types now use the whitespace mode XSD gives them, collapse, instead of preserve. A pattern or enumeration facet on one of them sees the collapsed value. - Length facets and enumerations still compare the string form. Range facets on the `g*` types remain unchecked, as they were before, because the engine has no ordering for those types. ### Tests - `tests/validate/test_xsd.py` and `tests/validate/test_relaxng.py` gain valid and invalid cases for each type and each lexical rule. - This branch includes #2147, which merged into `main` and adds the list types. The `IDREFS` and `ENTITIES` invalid-item cases check the item rule from that change. Both fail on `upstream/main` before #2147 and pass here. ### Performance Callgrind on the release LTO build (`buildtype=release`, `b_lto=true`), 20 and 220 repetitions, difference divided by 200. Base is `9fdbc4e6`, head is this commit. Columns are instructions per call. | Row | main | head | delta | delta % | | ------------------------------- | ---------: | ---------: | -------: | ------: | | validate | 13449181.7 | 13450780.7 | +1599.0 | +0.012 | | validate-rng | 7207056.0 | 7224890.0 | +17834.0 | +0.247 | | validate-facets-plain | 3337592.0 | 3338591.0 | +999.0 | +0.030 | | validate-attributes-small | 12809.0 | 12812.0 | +3.0 | +0.023 | | validate-numeric-facets-bounded | 253305.0 | 253304.0 | -1.0 | -0.000 | | validate-pattern-plain | 3097717.0 | 3098716.0 | +999.0 | +0.032 | | validate-rng-reuse-small | 24666.0 | 24629.0 | -37.0 | -0.150 | | compile-rng-reuse-small | 69391.0 | 69392.3 | +1.3 | +0.002 | | build (control) | 408329.0 | 408259.0 | -70.0 | -0.017 | A second main build agrees with the first on every row, so the noise floor is 0. `validate-rng` is the only row above the +0.05% noise threshold. No benchmark input uses the nine types, so the change reaches that row only through the shared datatype dispatch. The cost has not been localized and no optimization round has been run on it.
XMLSchemaaccepted any text for an element or attribute typed by anxs:listorxs:unionthat had no restriction. 📐 The schema compiler never read the item type or the member types, so a document that broke the list or union the schema describes validated without errors. The built-inNMTOKENS,IDREFSandENTITIESlist types had the same gap. After this change each list item is checked against its item type, and a union value is checked against its member types.XML Schema Part 2 defines a list as a whitespace-separated sequence of items of one item type, and a union as valid when one member type accepts the value. Compiling a schema now records the variety and the item or member types of each simple type, including the list or union at the end of a restriction chain at any depth. Validation dispatches on that variety.
RelaxNG<data>elements that name the XSD datatype library check the built-in list types too.Compiling also bounds the recursion. It rejects circular definitions, nesting past 100 levels, and a union that would try one value against more than 1,000 member types.
Behavior changes
xs:listorxs:uniontype that breaks its item or member types is reported. Before, it was accepted.length,minLengthandmaxLengthon a restricted list count its items. A list value is always collapsed, so awhiteSpacefacet on a list restriction has no effect.ValueErrorfor an item or member type that names no type, for anxs:listwithout exactly one item type, for anxs:unionwithout a member type, and for definitions that are circular, nest too deeply or are too wide.Fixes #1765.