Skip to content

Avoid taking more than O(n) time even for malicious input #289

Open

Description

@DemiMarie

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.

Activity

  1. Ygg01 commented on Jul 31, 2017

    @Ygg01
    Contributor

    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) )?

  2. DemiMarie commented on Jul 31, 2017

    @DemiMarie
    Author
  3. DemiMarie commented on Jul 31, 2017

    @DemiMarie
    Author
  4. changed the title [-]Guarantee O(n) time even for malicious input[/-] [+]Avoid taking more than O(n) time even for malicious input[/+] on Jul 31, 2017
  5. SimonSapin commented on Jul 31, 2017

    @SimonSapin
    Member

    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?

  6. DemiMarie commented on Jul 31, 2017

    @DemiMarie
    Author

    @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

    perl -e 'my $x = 20000; print("<i>"x$x . "<a>"x$x . "</a>"x$x)' |
    target/release/examples/html2html
  7. DemiMarie commented on Jul 31, 2017

    @DemiMarie
    Author

    So does

    perl -We 'use strict; my $q = 40000; print "<i>"x$q, "<div>"x$q; ' |
     target/release/examples/arena

    Time is spent in

    <html5ever::tree_builder::TreeBuilder<Handle, Sink> as html5ever::tree_builder::actions::TreeBuilderActions<Handle>>::close_p_element_in_button_scope
    

    according to perf.

  8. tothambrus11 commented on Sep 10, 2026

    @tothambrus11
  9. Develata commented on Oct 20, 2026

    @Develata

    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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions