Skip to content

🐛 fix(xsd): reject children of inline simple types - #2127

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/8179kwfa
Nov 15, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/8179kwfa

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

An element declared with an inline xs:simpleType accepted 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 same structure error, 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:all member and a ref= to a global element.

Behavior changes

An element declared with an inline xs:simpleType that has element children is now invalid, with one structure error on its first element child. Elements without element children validate as before.

Fixes #1708.

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.
@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 not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:sec-fix/8179kwfa (c83a7d6) with main (d132721)

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. ↩

@gaborbernat
gaborbernat merged commit f7c67d8 into tox-dev:main Nov 15, 2026
30 checks passed
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.
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 child elements under an element with an inline simple type

1 participant