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
interopImplementations are not interoperable with each otherneeds concrete proposalMoving the issue forward requires someone to figure out a detailed planneeds testsMoving the issue forward requires someone to write teststopic: parserThe HTML parser and serializer, including fragment parsing and character references
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.
The problem is that while every modern browser uses a multiprocess
architecture, there are entities that parse HTML other than browsers, and
these are subject to the same attacks. They should not need to parse HTML
to different parse trees for security.
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.
Correct, they are not. But they are generally trying to follow it.
I know of no way that a server side application can safely parse HTML
according to the specs and not be vulnerable to denial of service attacks.
Furthermore, Safari already has a nesting limit in its parser. Firefox and
Chrome don’t. I think that there should be a single nesting limit that
applies everywhere.
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.
@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).
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).
It's very important that the parser not throw away nodes but instead reparent them. See the nodeFromStackWithBlinkCompat() method in my patch
The Gecko parser patch needs a corresponding layout patch to allow deeper frame construction.
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.)
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.
Each div has a border of 1px and a margin of 1px, so the expected rendering would be something like the following:
However, Chrome/Firefox/Safari render something like this:
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.
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).
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.
@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)?
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).
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.
@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:
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!)
interopImplementations are not interoperable with each otherneeds concrete proposalMoving the issue forward requires someone to figure out a detailed planneeds testsMoving the issue forward requires someone to write teststopic: parserThe HTML parser and serializer, including fragment parsing and character references
Activity
domenic commentedon Jun 4, 2018
I don't believe that preventing pages from crashing themselves is in our threat model, but maybe @whatwg/security can confirm.
DemiMarie commentedon Jun 4, 2018
domenic commentedon Jun 4, 2018
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.
DemiMarie commentedon Jun 4, 2018
domenic commentedon Jun 4, 2018
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.
domenic commentedon Jun 4, 2018
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 :)
bzbarsky commentedon Jun 4, 2018
@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).
bzbarsky commentedon Jun 4, 2018
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.
DemiMarie commentedon Jun 4, 2018
bzbarsky commentedon Jun 4, 2018
Does any browser have the "fail if added by JS" behavior?
For the other, please check with @hsivonen what his investigations mentioned above showed.
zcorpan commentedon Jun 4, 2018
The HTML parser already has some limits, I don't see a problem in principle to specify this one as well.
hsivonen commentedon Jun 4, 2018
The Gecko-side status is:
nodeFromStackWithBlinkCompat()method in my patchI'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.)
10 remaining items
hsivonen commentedon Aug 12, 2020
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.
nicolo-ribaudo commentedon Oct 14, 2025
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
Each div has a border of 1px and a margin of 1px, so the expected rendering would be something like the following:
However, Chrome/Firefox/Safari render something like this:
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:
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).
nicolo-ribaudo commentedon Oct 20, 2025
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.nicolo-ribaudo commentedon Oct 21, 2025
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:</span>as closing the innermost span (effectively ignoring it?)<a>as a sibling of itI think the Chrome/Firefox behavior is more intuitive, but it means that the stack size is actually unlimited.
DemiMarie commentedon Oct 21, 2025
Any limit needs to ensure that it does not create mutation XSS vulnerabilities. I’m not sure how to ensure that, though.
nicolo-ribaudo commentedon Nov 26, 2025
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)?
annevk commentedon Nov 27, 2025
Maybe. Can you trigger a stack overflow in Chrome and Firefox?
nicolo-ribaudo commentedon Nov 27, 2025
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.
annevk commentedon Nov 27, 2025
Thanks! I suspect that's fine.
DemiMarie commentedon Nov 29, 2025
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.
nicolo-ribaudo commentedon Nov 16, 2026
@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:
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>=.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.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'sparentNode(), 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.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:
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.tbodyandtrfollow the same rule as explicit ones, so they do not nest beyond the limit either.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!)