Repository navigation
🐛 fix(xsd): reject children of inline simple types - #2127
Merged
Merged
Conversation
An element declared with an inline xs:simpleType was validated only on its direct text, so element children under it were accepted and the document was reported valid. The named-type branch already rejects them with a structure error on the first child element. The inline branch now reports the same error, so every route that resolves an inline simple type (root, particle, ref=) rejects child elements the same way.
Merging this PR will not alter performance
Comparing Footnotes
|
gaborbernat
added a commit
that referenced
this pull request
Nov 15, 2026
`XMLSchema.validate()` accepted attributes on an element whose type is a simple type. The two simple-type branches of `xsd_validate_element()` checked the element's child elements but never its attributes, and attributes were only checked on complex types, through `xsd_validate_attrs()`. A document with an extra attribute on such an element was reported valid. After this change that element fails with a `structure` error, `simple-typed element must not have attributes`, and the document is reported invalid. Fixes #2128. The check runs in both simple-type branches: the `type=` branch that names a built-in or named simple type, and the inline `xs:simpleType` branch. Elements reached through `ref=` and local element declarations go through the same function, so they are covered as well. ### Simple-typed elements were not checked for attributes XSD 1.0 §3.3.4 (Element Locally Valid (Type)) allows an element with a simple type no attributes other than `xsi:type`, `xsi:nil`, `xsi:schemaLocation` and `xsi:noNamespaceSchemaLocation`. Namespace declarations are not attributes in the infoset, so they remain allowed. The old simple-type branches, at the merge base: - https://github.com/tox-dev/turbohtml/blob/f7c67d85aedf108c1ce0e783a06d78ccdec86fdd/src/turbohtml/_c/validate/xsd.h#L1852-L1864 - https://github.com/tox-dev/turbohtml/blob/f7c67d85aedf108c1ce0e783a06d78ccdec86fdd/src/turbohtml/_c/validate/xsd.h#L1867-L1876 The new check, `xsd_simple_attr_allowed()` and `xsd_check_simple_attrs()`, at the pushed head: - https://github.com/tox-dev/turbohtml/blob/fe335c8e57aaf0ccde17489d4331ca4643ae6fa6/src/turbohtml/_c/validate/xsd.h#L1823-L1854 - Called from the `type=` branch: https://github.com/tox-dev/turbohtml/blob/fe335c8e57aaf0ccde17489d4331ca4643ae6fa6/src/turbohtml/_c/validate/xsd.h#L1887-L1889 - Called from the inline branch: https://github.com/tox-dev/turbohtml/blob/fe335c8e57aaf0ccde17489d4331ca4643ae6fa6/src/turbohtml/_c/validate/xsd.h#L1904-L1906 The xsi attributes are matched by namespace: a different prefix bound to the XSI namespace is accepted, and an attribute with an xsi local name in another namespace is rejected. ### Behavior changes - An element with a simple type that carries an attribute other than a namespace declaration or one of the four xsi attributes now produces one `structure` error for the element. - Prefixed attributes that complex types ignore, such as `xml:*` and attributes in other namespaces, are rejected on simple-typed elements, since the spec names no exception for them. - Child elements under an inline `xs:simpleType` are checked by #2127 (#1708), which is already on main; this change does not touch that check. ### Tests `tests/validate/test_xsd.py` runs the rejection and the allowed-attribute cases over five routes to a simple-typed element: a built-in type, a named simple type, an inline simple type, a local declaration and a `ref=` to a global declaration. All 20 rejection cases fail on a release LTO build of the merge base, and the allowed-attribute cases pass there. ### Performance Callgrind instruction counts (Ir) on release LTO builds (`buildtype=release`, `b_lto=true`, `CC=/usr/bin/cc`), with `PYTHONHASHSEED=0`, `setarch -R` and no `-I`. Each figure is the difference between 220 and 20 repetitions divided by 200, so it is Ir per call. | Row | Main | Main again (noise) | This PR | Change | | ---------------------------------- | ---------: | -----------------: | ---------: | --------------: | | `validate` | 16,710,175 | 16,710,175 (0) | 16,704,175 | -6,000 (-0.04%) | | `validate-facets-plain` | 3,552,066 | 3,552,066 (0) | 3,549,066 | -3,000 (-0.08%) | | `parse-xml` (control, no XSD code) | 6,572,699 | 6,572,699 (0) | 6,572,699 | 0 | The main column is built from the merge base, `f7c67d85a`, which includes #2127. The PR head is `fe335c8e5`. A first version called the helper for every simple-typed element. On the earlier base it measured +16,000 Ir per call on `validate` (+0.10%) and +8,000 on `validate-facets-plain` (+0.23%). Calling the helper only for elements that carry attributes, and keeping it out of line, removed that cost; the table above is for the final version.
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.
An element declared with an inline
xs:simpleTypeaccepted element children, and a document with them was reported valid. XML Schema Part 1 requires an element with a simple type to have no element children, and the named-type route already enforced it. ✅ After this change the first element child of such an element is reported with the samestructureerror, so the document is reported invalid.Architecture
The inline branch now runs the same child check as the named-type branch, before it validates the text. Comments, processing instructions and text under such an element are not element children, so they still pass the check and the text still goes through the value check. Every route to an inline simple type reaches that branch: the root element, a sequence or choice particle, an
xs:allmember and aref=to a global element.Behavior changes
An element declared with an inline
xs:simpleTypethat has element children is now invalid, with onestructureerror on its first element child. Elements without element children validate as before.Fixes #1708.