Skip to content

Commit

Permalink
Limit stack depth when missing tags hit resetInsertionMode()
Browse files Browse the repository at this point in the history
Fixes #1606
  • Loading branch information
jhy committed Aug 4, 2021
1 parent b4f4f2c commit 436d119
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -47,6 +47,10 @@ jsoup changelog
vs being able to read in one hit.
<https://github.com/jhy/jsoup/issues/1605>

* Bugfix [Fuzz]: Speed improvement when closing missing empty tags (in XML comment processed as HTML) when thousands
deep in stack.
<https://github.com/jhy/jsoup/issues/1606>

*** Release 1.14.1 [2021-Jul-10]
* Change: updated the minimum supported Java version from Java 7 to Java 8.

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jsoup/parser/HtmlTreeBuilder.java
Expand Up @@ -443,7 +443,10 @@ private void replaceInQueue(ArrayList<Element> queue, Element out, Element in) {
void resetInsertionMode() {
// https://html.spec.whatwg.org/multipage/parsing.html#the-insertion-mode
boolean last = false;
for (int pos = stack.size() -1; pos >= 0; pos--) {
final int bottom = stack.size() - 1;
final int upper = bottom >= maxQueueDepth ? bottom - maxQueueDepth : 0;

for (int pos = bottom; pos >= upper; pos--) {
Element node = stack.get(pos);
if (pos == 0) {
last = true;
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/jsoup/integration/FuzzFixesTest.java
Expand Up @@ -171,4 +171,14 @@ public void parseTimeout1605() throws IOException {
Document docXml = Jsoup.parse(new FileInputStream(in), "UTF-8", "https://example.com", Parser.xmlParser());
assertNotNull(docXml);
}

@Test
public void parseTimeout1606() throws IOException {
// https://github.com/jhy/jsoup/issues/1606
// Timesink when closing missing empty tag (in XML comment processed as HTML) when thousands deep
File in = ParseTest.getFile("/fuzztests/1606.html.gz");

Document docXml = Jsoup.parse(new FileInputStream(in), "UTF-8", "https://example.com", Parser.xmlParser());
assertNotNull(docXml);
}
}
Binary file added src/test/resources/fuzztests/1606.html.gz
Binary file not shown.

0 comments on commit 436d119

Please sign in to comment.