You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid taking more than O(n) time even for malicious input #289
HTML parsers are not just used in client-side applications: they are also used on servers, such as in HTML sanitizers. html5ever (and xml5ever) should guarantee that they cannot be coerced into taking more than O(n), or at worst O(n log n), time. This may be difficult, especially if one does not want to use massively connected datastructures.
Right now, it seems that the worst offenders are likely to be calls to Vec::remove in various places, such as in the adoption agency algorithm.
Excellent point. I think attribute duplication is probably the worst offender, especially in xml5ever, since it does attribute removal twice (first time in tokenizer and second after namespace resolution).
Any ideas how other parsers limit the execution time to O(n) (or O(n log n) )?
I know Gumbo does worse than html5ever; really deep nesting is enough to
make it go quadratic.
The main problem seems to be the active formatting elements and unclosed
elements vectors. I am not sure how to pull off guaranteed linear time.
Maybe by caching, or by storing flags instead of searching the lists? Some
steps seem to require a linked list be used instead of a vector, so as to
allow O(1) removal from the middle of the list. Others seem to require a
hash set of every node, keyed by name, attributes, and values, that allows
up to 3 duplicates! I think that using vectors is a losing battle here,
which strongly favors using an arena for all allocations inside the parser.
Another option is to deliberately impose an artificial cap on the length of
those lists. Ugly, and in violation of the spec, but the only simple
option I see.
On Jul 31, 2017 9:19 AM, "Demi Obenour" ***@***.***> wrote:
I know Gumbo does worse than html5ever; really deep nesting is enough to
make it go quadratic.
The main problem seems to be the active formatting elements and unclosed
elements vectors. I am not sure how to pull off guaranteed linear time.
Maybe by caching, or by storing flags instead of searching the lists? Some
steps seem to require a linked list be used instead of a vector, so as to
allow O(1) removal from the middle of the list. Others seem to require a
hash set of every node, keyed by name, attributes, and values, that allows
up to 3 duplicates! I think that using vectors is a losing battle here,
which strongly favors using an arena for all allocations inside the parser.
On Jul 31, 2017 7:55 AM, "Ygg01" ***@***.***> wrote:
> Excellent point. I think attribute duplication is probably the worst
> offender, especially in xml5ever, since it does attribute removal twice
> (first time in tokenizer and second after namespace resolution).
>
> Any ideas how other parsers limit the execution time to O(n) (or O(n log
> n) )?
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <#289 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AGGWB0Z5v_U33BeSW7xEUzMO5V0g-15Cks5sTcCpgaJpZM4OnxxF>
> .
>
I’ve changed the title because "guarantee" is not gonna happen unless we have static analysis that can tell if we’re doing something wrong, and that sounds like a hard research problem.
Do you know specific inputs where html5ever currently behaves badly?
@SimonSapin A long string of opening <i> tags, followed by a bunch of nested links, causes quadratic time consumption in the adoption agency algorithm. You can reproduce this with
A data point from an embedder, plus a request that would help until every path is linear.
We sanitize untrusted feed HTML with ammonia, which uses html5ever 0.39. To keep a hostile article from freezing the UI, we pre-parse each body with a counting TreeSink, feed it in small chunks, and stop when a work budget runs out. That catches the open-element stack scans from #788, the Noah's Ark comparisons, and reconstruction blow-up. Adversarial review kept finding work the sink cannot see, though:
trace_handles also skips markers, so the size of the active formatting list cannot be observed from outside.
Requests:
A work budget on TreeBuilderOpts/TokenizerOpts, or a hook called per token with an abort result, so that embedders can bound parse cost without modeling the algorithm's internals.
Failing that, a way to read the current lengths of the open-element stack and the active formatting list, markers included.
Activity
Ygg01 commentedon Jul 31, 2017
Excellent point. I think attribute duplication is probably the worst offender, especially in xml5ever, since it does attribute removal twice (first time in tokenizer and second after namespace resolution).
Any ideas how other parsers limit the execution time to
O(n)(orO(n log n))?DemiMarie commentedon Jul 31, 2017
DemiMarie commentedon Jul 31, 2017
[-]Guarantee O(n) time even for malicious input[/-][+]Avoid taking more than O(n) time even for malicious input[/+]SimonSapin commentedon Jul 31, 2017
I’ve changed the title because "guarantee" is not gonna happen unless we have static analysis that can tell if we’re doing something wrong, and that sounds like a hard research problem.
Do you know specific inputs where html5ever currently behaves badly?
DemiMarie commentedon Jul 31, 2017
@SimonSapin A long string of opening
<i>tags, followed by a bunch of nested links, causes quadratic time consumption in the adoption agency algorithm. You can reproduce this withDemiMarie commentedon Jul 31, 2017
So does
Time is spent in
according to
perf.tothambrus11 commentedon Sep 10, 2026
For a more efficient vector based implementation of a doubly linked list, see: https://infoscience.epfl.ch/server/api/core/bitstreams/01f2b47c-dbc4-4bae-b8cd-5db7a8663e11/content
Develata commentedon Oct 20, 2026
A data point from an embedder, plus a request that would help until every path is linear.
We sanitize untrusted feed HTML with ammonia, which uses html5ever 0.39. To keep a hostile article from freezing the UI, we pre-parse each body with a counting
TreeSink, feed it in small chunks, and stop when a work budget runs out. That catches the open-element stack scans from #788, the Noah's Ark comparisons, and reconstruction blow-up. Adversarial review kept finding work the sink cannot see, though:position_in_active_formatting, including over leftover markers (Tree builder: leftover active-formatting markers make each formatting end tag O(list length) #801);eq_ignore_ascii_caseon long names for foreign-content end tags;trace_handlesalso skips markers, so the size of the active formatting list cannot be observed from outside.Requests:
TreeBuilderOpts/TokenizerOpts, or a hook called per token with an abort result, so that embedders can bound parse cost without modeling the algorithm's internals.html5ever: Find duplicate attributes in linear time
html5ever: Find duplicate attributes in linear time
html5ever: Find duplicate attributes in linear time