Skip to content

HTML: Cap nesting depth #3732

Open
Open

Description

@DemiMarie

Almost all HTML parsers are vulnerable to stack overflows and denial-of-service attacks, because HTML parsing can easily be made to exhibit quadratic behavior by deep nesting of documents, and because naive tree-traversal algorithms are recursive.

I propose that the nesting depth of an HTML document be capped at 500. Any attempt to insert a node at a depth past that is erroneous — DOM APIs throw exceptions, while an implicit end tag is inserted if this is encountered during parsing.

Activity

  1. domenic commented on Jun 4, 2018

    @domenic
    Member

    I don't believe that preventing pages from crashing themselves is in our threat model, but maybe @whatwg/security can confirm.

  2. DemiMarie commented on Jun 4, 2018

    @DemiMarie
    Author
  3. domenic commented on Jun 4, 2018

    @domenic
    Member

    Those parsers aren't covered by this specification; if that is your concern, you should file issues on those parsers instead of on the specification for web browsers.

  4. DemiMarie commented on Jun 4, 2018

    @DemiMarie
    Author
  5. domenic commented on Jun 4, 2018

    @domenic
    Member

    Thanks for the clarification. I'll close this, and you can file bugs on those other parsers if you want them to not follow the spec (which makes sense for non web browsers with different threat models), and on Safari if you want it to follow the spec.

  6. added
    topic: parser
    on Jun 4, 2018
  7. domenic commented on Jun 4, 2018

    @domenic
    Member

    Reopening as I forgot I wanted to wait a while to confirm the threat model with @whatwg/security. Please forgive the sloppy 4am bug-triaging :)

  8. reopened this on Jun 4, 2018
  9. bzbarsky commented on Jun 4, 2018

    @bzbarsky
    Contributor

    @domenic Firefox and Blink (as of Sept 2017 at least) also violate the current HTML parsing spec at large nesting depths, fwiw. They just violate it in a different way than Safari (and different versions of Firefox do it slightly differently).

    Please see discussion and testcases in https://bugzilla.mozilla.org/show_bug.cgi?id=256180 and https://bugzilla.mozilla.org/show_bug.cgi?id=1188731 and https://groups.google.com/d/msg/mozilla.dev.platform/SUknMzK1ZAc/sMNBAjKCAgAJ (this last complete with @hsivonen saying "I'm rather unhappy about the prospect of having to examine another browser's HTML parser beyond reading the spec in order to achieve interop".... of course then he had to do just that with Blink, to achieve interop).

  10. bzbarsky commented on Jun 4, 2018

    @bzbarsky
    Contributor

    And to be clear, a hard limit of 500 with failures when it's reached is probably not web-compatible enough, but it's hard to say without more data.

  11. DemiMarie commented on Jun 4, 2018

    @DemiMarie
    Author
  12. bzbarsky commented on Jun 4, 2018

    @bzbarsky
    Contributor

    Does any browser have the "fail if added by JS" behavior?

    For the other, please check with @hsivonen what his investigations mentioned above showed.

  13. zcorpan commented on Jun 4, 2018

    @zcorpan
    Member

    The HTML parser already has some limits, I don't see a problem in principle to specify this one as well.

  14. hsivonen commented on Jun 4, 2018

    @hsivonen
    Member

    The Gecko-side status is:

    1. I want Gecko's parser limit to match Blink's parser limit exactly. Blink's magic number is 512, but if you put it in the spec, be sure to check that you count the same way as Blink and use the same comparison operator. (My Gecko patch has 511 as the magic number in order to get the same black-box behavior with different counting.) Please don't invent a new arbitrary number (i.e. please don't make it 500 exactly for no good reason).
    2. It's very important that the parser not throw away nodes but instead reparent them. See the nodeFromStackWithBlinkCompat() method in my patch
    3. The Gecko parser patch needs a corresponding layout patch to allow deeper frame construction.
    4. The patches haven't landed, because I failed to figure out how to increase the runtime stack on Android so as to not crash on Robohornet with the higher layout limits. Robohornet says "RoboHornet is a benchmark designed around performance pain points real web developers care about." Yet, it creates an unrealistically deep DOM tree. Still, the abandoned benchmark is out there and it would look bad if Firefox for Android crashed on it. So in the mean time, some webmail users don't see parts of emails from MUAs that generate bad HTML. But hey, at least we don't crash on an unrealistic benchmark.

    I'm not convinced that we need to make DOM APIs throw at a certain depth limit. Has interop on that point been a problem in practice. (It is on the parser side.)

  15. 10 remaining items

  16. hsivonen commented on Aug 12, 2020

    @hsivonen
    Member

    would you be willing to submit a pull request?

    Unfortunately, I expect not to have time for this in the near future. However, if someone want to volunteer, I still recommend looking at the Java patch.

  17. nicolo-ribaudo commented on Oct 14, 2025

    @nicolo-ribaudo
    Member

    Copy-pasting here what I first submitted as a new issue:


    What is the issue with the HTML Standard?

    Browsers do not support excessively nested DOM trees.

    Consider this demo, which consists of a page with a <body> containing 600 <div> elements nested inside each other:

    Details
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <title>Deep nesting</title>
    
      <style>
        div {
          border: 1px solid black;
          margin: 1px;
          min-height: 30px;
          min-width: 30px;
        }
    
        html {
          counter-reset: count;
        }
    
        * {
          counter-increment: count;
        }
    
        div:has(+ div):not(div + div)::before {
          content: counter(count);
        }
      </style>
    </head>
    
    <body>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
      <div>
    </body>
    </html>

    Each div has a border of 1px and a margin of 1px, so the expected rendering would be something like the following:

    Image

    However, Chrome/Firefox/Safari render something like this:

    Image

    You can see that at some point it stops nesting elements, and instead adds them at sibling. In all browser, there is some MAX_DEPTH, and if the stack of open elements has already a length of MAX_DEPTH then before pushing a new element to the stack they'll pop the previous one. Adding some CSS counters we can see that in Chrome&Firefox the max depth is 513, while in Safari it is 512.

    This behavior does not seem to be defined (or at least, not defined in https://html.spec.whatwg.org/multipage/parsing.html#insert-a-foreign-element which is where I'd expect to see it).

    There are three things that can be defined here:

    1. that there is a maximum depth
    2. what the maximum depth should be
    3. what to do when reaching it

    I care especially about (3), since I hit a bug in a "spec-compliant HTML parsing library", whose code is basically a copy of the HTML parsing algorithms, where it was giving different results from browsers (jhy/jsoup#2416).

  18. added
    needs tests
    interop
    needs concrete proposal
    on Oct 14, 2025
  19. nicolo-ribaudo commented on Oct 20, 2025

    @nicolo-ribaudo
    Member

    I'm slowly investigating this, and I'm collecting some test cases in https://github.com/nicolo-ribaudo/deeply-nested-html, where the folders naming scheme is (last element before the limit)-(first element after the limit).

    Other than the limit inconsistency (513 vs 512) I noticed another one when it comes to autogenerated elements.

    When the last element before the limit is a <table>, and it contains a <tr>/<td>, Firefox will generate <table></table><tbody></tbody><tr></tr>, while Chrome/Safari generate <table></table><tbody><tr></tr></tbody> thus getting past the limit.

    As a consequence of that, two sibling <td> elements might not end up as siblings in Chrome/Safari (see https://github.com/nicolo-ribaudo/deeply-nested-html/tree/main/tbody-2td). If we specify something, it should probably be Firefox's behavior.

  20. nicolo-ribaudo commented on Oct 21, 2025

    @nicolo-ribaudo
    Member

    Another difference, where Firefox/Chrome behave differently from Safari, is what to do when figuring out what is the opening element corresponding to a closing tag: https://github.com/nicolo-ribaudo/deeply-nested-html/tree/main/span-span-a-closed-span

    Given an input of <span><span><a></a></span>, where <a> is put as a sibling of the innermost <span> due to the depth limit:

    • Chrome/Firefox will consider the </span> as closing the innermost span (effectively ignoring it?)
    • Safari will consider it as closing the outermost span, because the innermost one is already closed by pushing <a> as a sibling of it

    I think the Chrome/Firefox behavior is more intuitive, but it means that the stack size is actually unlimited.

  21. DemiMarie commented on Oct 21, 2025

    @DemiMarie
    Author

    Any limit needs to ensure that it does not create mutation XSS vulnerabilities. I’m not sure how to ensure that, though.

  22. nicolo-ribaudo commented on Nov 26, 2025

    @nicolo-ribaudo
    Member

    I'm working towards a concrete proposal for this.

    @annevk Do you think Safari would be open to aligning with Chrome/Firefox's parsing approach (limited tree depth, but unlimited stack size for matching opening&closing tags)?

  23. annevk commented on Nov 27, 2025

    @annevk
    Member

    Maybe. Can you trigger a stack overflow in Chrome and Firefox?

  24. nicolo-ribaudo commented on Nov 27, 2025

    @nicolo-ribaudo
    Member

    Both Chrome and Firefox can handle a stack size of 2+218 (I'm testing with a document that just contains 2n nested divs). WebKit at lower numbers (I'm getting a "page unresponsive" popup, but I'm testing in Gnome Web and not in Safari right now).

    Looking at Chrome's code the maximum stack size is represented by a u32, but it's impossible to actually reach it (https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/html/parser/html_element_stack.h;l=173;drc=97e7fa75720cf98a23b74404a0b14b9e7ac249d1). Even just assuming 3 bytes per tag, you'd need a 12GB document. It takes about 2 minutes to parse/render one with 218, so for one with 232 it would take days.

    EDIT: 220 crashes the page in Chrome ("Aw, Snap"), and gets stuck forever in firefox.

  25. annevk commented on Nov 27, 2025

    @annevk
    Member

    Thanks! I suspect that's fine.

  26. DemiMarie commented on Nov 29, 2025

    @DemiMarie
    Author

    Would it be possible to impose a hard limit, above which the parser gives up with a fatal error? I think anything above 216 is almost certainly an exploit attempt rather than a legitimate website. A smaller limit like 212 might be appropriate.

  27. nicolo-ribaudo commented on Nov 16, 2026

    @nicolo-ribaudo
    Member

    @cathiechen and I did some research again on this recently. The situation has changed a little since the last comments. Here are the differences between the engines:

    1. The maximum DOM tree depth differs. WebKit allows 512 levels, Gecko and Chromium allow 513 (counting html). Test case: https://github.com/nicolo-ribaudo/deeply-nested-html/tree/main/div-div. The WebKit difference comes from Fix conditional check for reaching maximum DOM tree depth WebKit/WebKit#47829, which changed the comparison from > to >=.

    2. End tags can match the wrong element in WebKit. When the limit is hit, WebKit pops the current node off the stack of open elements before inserting the new element as a sibling. The popped element can no longer be matched by its end tag, so </span> closes the outer span instead of the inner one. Gecko and Chromium keep the element on the stack and only bound the tree, so their end tags keep matching. Test case: https://github.com/nicolo-ribaudo/deeply-nested-html/tree/main/span-span-a-closed-span. This was introduced by Fix stack overflow triggered by HTML file with deep <div> nesting WebKit/WebKit#47830 to fix a stack overflow with hundreds of thousands of nested divs, and shipped in Safari 26. Chrome and Firefox don't have that stack overflow problem, so probably WebKit cam fix it matching their behavior.

    3. Implied table elements at the limit. With <table><td> at the limit, Gecko makes every element a sibling: <table></table><tbody></tbody><tr></tr><td></td>. WebKit nests the implied elements inside each other past the limit: <table></table><tbody><tr><td></td></tr></tbody>. The reason is that WebKit queues insertions until the end of the token, and the redirect checks the current node's parentNode(), which is still null for a just-queued <tbody>, so the <tr> is not redirected. Chromium used to behave like WebKit for the same reason. Since Blink removed its task queue in September 2026 (https://chromium-review.googlesource.com/c/chromium/src/+/7849912) insertions are synchronous and Chromium produces Gecko's tree; we verified this in Chromium 156. Test cases: https://github.com/nicolo-ribaudo/deeply-nested-html/tree/main/table-td and https://github.com/nicolo-ribaudo/deeply-nested-html/tree/main/tbody-2td.

    4. WebKit does not render the overflow siblings. Because of a performance problem with pathological content (REGRESSION (297241@main): Overflow siblings at parser's max DOM tree depth get fully rendered, causing slow layout WebKit/WebKit#64319), WebKit only gives a renderer to the first child at the maximum depth. The overflow siblings are in the DOM like in the other engines, but they are not rendered. This is a rendering limit rather than a parsing difference, so it may not need spec text, but it is part of what users see. Test case: https://github.com/nicolo-ribaudo/deeply-nested-html/tree/main/600-div.

    So Gecko and Chromium are aligned now. Our understanding of their shared behavior, which we would propose as the expected behavior:

    • Elements nest up to 513 levels deep, counting html. An element that would be deeper is still created and inserted at the deepest allowed level, next to the element that would have been its parent, rather than being dropped.
    • The limit changes where an element is placed, not what an end tag matches. An end tag still closes the element it was written for, even when that element was placed at the limit.
    • Implied elements such as tbody and tr follow the same rule as explicit ones, so they do not nest beyond the limit either.
    • Text nodes stay within the element they are written in.

    Given that two browsers are aligned, we are starting to work on a PR encoding their behavior, hoping that WebKit can align.

    (on a side note, that 513 magic number looks really weird, so if there are two browsers in favor we are also happy to make it 512 like WebKit instead!)

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

    interopneeds concrete proposalneeds teststopic: parser

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions