Skip to content

🐛 fix(xsd): check xs:list and xs:union values - #2147

Merged
gaborbernat merged 6 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/sxnmdl09
Nov 15, 2026
Merged

gaborbernat merged 6 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/sxnmdl09

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

XMLSchema accepted any text for an element or attribute typed by an xs:list or xs:union that 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-in NMTOKENS, IDREFS and ENTITIES list 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

  • A value of an xs:list or xs:union type that breaks its item or member types is reported. Before, it was accepted.
  • length, minLength and maxLength on a restricted list count its items. A list value is always collapsed, so a whiteSpace facet on a list restriction has no effect.
  • A restriction chain of any length keeps the list or union it ends in. A chain past the depth cutoff used to read as a plain string.
  • Compiling raises ValueError for an item or member type that names no type, for an xs:list without exactly one item type, for an xs:union without a member type, and for definitions that are circular, nest too deeply or are too wide.

Fixes #1765.

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.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 14.49%

2 warnings

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 1 benchmark with a significant change ran in a different software environment

Its base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the change might come from this difference rather than from the code. Open the benchmark to see what changed.

See affected benchmarks

⚡ 1 improved benchmark
✅ 581 untouched benchmarks
⏩ 89 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_mutating_feature[path-xpath-one-cold] 143.1 µs 125 µs +14.49%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/sxnmdl09 (63d8bbc) with main (aceeaf9)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

# 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.
@gaborbernat
gaborbernat merged commit 3034501 into tox-dev:main Nov 15, 2026
30 checks passed
gaborbernat added a commit that referenced this pull request Nov 15, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XMLSchema accepts any value for unrestricted xs:list and xs:union types

1 participant