Skip to content

Fix selector matching being skipped for consecutive integration-point siblings - #329

Merged
orium merged 1 commit into
cloudflare:mainfrom
robobun:fix-stale-hint-flag-integration-points
Jul 29, 2026
Merged

orium merged 1 commit into
cloudflare:mainfrom
robobun:fix-stale-hint-flag-integration-points

Conversation

@robobun

@robobun robobun commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

The bug

When two SVG/MathML integration-point elements are adjacent siblings, the second one never reaches selector matching. It is emitted to the output verbatim, but no element handler runs for it, including the universal selector:

// handler for "foreignobject" (or "*")
rewrite_str(r#"<svg><title>x</title><foreignObject></foreignObject></svg>"#, settings)
// handler fires for svg and title, never for foreignObject

Remove the <title> sibling and the same foreignObject matches fine. Intervening text does not re-arm matching; only an intervening non-integration-point element does. This makes selector-based sanitizers built on lol-html bypassable by prefixing the payload element with another integration-point sibling.

The cause

The tag scanner emits a tag hint for most start tags; Dispatcher::apply_capture_flags_from_hint_and_get_next_parser_directive sets got_flags_from_hint so the lexer's handle_tag does not re-run selector matching for a tag the hint already matched. Integration-point start tags take a different path: the tree-builder simulator answers RequestLexeme (it needs self_closing to decide namespace entry) and the scanner switches to the lexer without emitting a hint.

If the previous tag's hint left the parser in scan mode (nothing to capture), got_flags_from_hint stayed true. The next RequestLexeme tag then reached handle_tag with the stale flag, so adjust_capture_flags_for_tag_lexeme was skipped and handle_start_tag never ran for it.

The fix

The flag is only meaningful when the hint results in an immediate switch to the lexer for the same tag, so set it to matches!(directive, ParserDirective::Lex) instead of unconditionally true.

Two regression tests added next to the existing foreign-content tests in rewritable_units/element.rs (SVG desc/title/foreignObject and MathML mi/mo/mn/annotation-xml sibling chains); both fail on main and pass with the fix. cargo test and cargo test --features=_integration_test are green.

Found while debugging Bun's HTMLRewriter (oven-sh/bun#35673, ships this change as a vendored patch).

… siblings

The tag scanner emits a hint for most start tags, and the dispatcher sets
got_flags_from_hint so that the lexer's handle_tag does not re-run selector
matching for the same tag. Integration-point start tags (title/desc/
foreignObject in SVG, mi/mo/mn/ms/mtext/annotation-xml in MathML) skip the
hint: the tree-builder simulator answers RequestLexeme and the scanner
switches to the lexer directly.

When a hint kept the parser in scan mode (nothing to capture), the flag was
left set. The next RequestLexeme tag then reached handle_tag with the stale
flag, so selector matching was skipped for it entirely: the element was
emitted verbatim but none of its handlers ran, including the universal
selector. In practice the second of two adjacent integration-point siblings
was invisible to handlers:

    <svg><title>x</title><foreignObject>...</foreignObject></svg>

a handler for foreignobject (or *) never fired, while the same element
without the preceding title sibling matched fine.

Clear the flag whenever the hint does not result in a switch to the lexer;
it is only meaningful when the lexer is about to emit the same tag.
@robobun
robobun requested review from a team, Noah-Kennedy, jasnell and orium as code owners July 25, 2026 13:35
robobun added a commit to oven-sh/bun that referenced this pull request Jul 25, 2026
Bumps the vendored pin from 77127cd2 (v2.7.2) to 02f139c4 (the v3.0.0 tag)
and rebases the integration-point patch, which is unchanged upstream and
submitted as cloudflare/lol-html#329.

v3.0.0 made Settings, MemorySettings and RewriteStrSettings consuming
builders with private fields, so the struct-literal construction in
html_rewriter.rs and HTMLScanner.rs moves to Settings::new() with with_*
setters and append_*_content_handler loops. No behavior change; the
OutputSink trait gained set_encoding with a default impl, so our sinks are
unaffected.

Lockfile: lol_html now wants hashbrown 0.17 and selectors 0.37, both
already compatible with the rest of the workspace.

@Jarred-Sumner Jarred-Sumner left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robobun add a test and make sure you match the style of other commits and any lints in this repo

robobun added a commit to oven-sh/bun that referenced this pull request Jul 27, 2026
The patch is waiting on cloudflare/lol-html#329; the registry entry trips
on the next lol_html bump past 3.0.0 so the patch cannot outlive the
upstream fix silently. Also trims the test comment to the upstream link.
@orium
orium merged commit 633435e into cloudflare:main Jul 29, 2026
3 checks passed
robobun added a commit to oven-sh/bun that referenced this pull request Aug 12, 2026
Upstream merged the integration-point fix (cloudflare/lol-html#329) and
released v3.0.1, so the patch, its workarounds-registry entry, and the
patches list on the dep go away. v3.0.1 also carries two upstream fixes:
linear-depth descendant matching for stray end tags and ASCII
case-insensitive attribute-name matching.
robobun added a commit to oven-sh/bun that referenced this pull request Aug 16, 2026
Main moved the lolhtml pin to oven-sh/lol-html (bun branch, based on
upstream v2.7.2) for content-handler suspension, which reintroduced the
integration-point selector-skip bug: upstream fixed it in v3.0.1
(cloudflare/lol-html#329), but the fork predates that. Carry the fix as
a vendored patch on the fork pin, registered in the workarounds
registry so the next fork rebase past v3.0.0 drops it.

The v3.0.1 pin and settings-builder migration from earlier revisions of
this branch are gone: the fork keeps the v2.7.2 API, so html_rewriter.rs
and HTMLScanner.rs stay as they are on main.
robobun added a commit to oven-sh/bun that referenced this pull request Aug 16, 2026
Main moved the lolhtml pin to oven-sh/lol-html (bun branch, based on
upstream v2.7.2) for content-handler suspension, which reintroduced the
integration-point selector-skip bug: upstream fixed it in v3.0.1
(cloudflare/lol-html#329), but the fork predates that. Carry the fix as
a vendored patch on the fork pin, registered in the workarounds
registry so the next fork rebase past v3.0.0 drops it.

The v3.0.1 pin and settings-builder migration from earlier revisions of
this branch are gone: the fork keeps the v2.7.2 API, so html_rewriter.rs
and HTMLScanner.rs stay as they are on main.
robobun added a commit to oven-sh/bun that referenced this pull request Nov 15, 2026
Main moved the lolhtml pin to oven-sh/lol-html (bun branch, based on
upstream v2.7.2) for content-handler suspension, which reintroduced the
integration-point selector-skip bug: upstream fixed it in v3.0.1
(cloudflare/lol-html#329), but the fork predates that. Carry the fix as
a vendored patch on the fork pin, registered in the workarounds
registry so the next fork rebase past v3.0.0 drops it.

The v3.0.1 pin and settings-builder migration from earlier revisions of
this branch are gone: the fork keeps the v2.7.2 API, so html_rewriter.rs
and HTMLScanner.rs stay as they are on main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants