Skip to content

Commit

Permalink
Make sure resetInsertionMode breaks out to Body if nothing left on stack
Browse files Browse the repository at this point in the history
Fixes #1607
  • Loading branch information
jhy committed Aug 5, 2021
1 parent d6a4d20 commit b4f20f0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ jsoup changelog
deep in stack.
<https://github.com/jhy/jsoup/issues/1606>

* Bugfix [Fuzz]: Fix a potential stack-overflow in the parser given crafted HTML, when the parser looped in the
InSelectInTable state.

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

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jsoup/parser/HtmlTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ void resetInsertionMode() {
final int bottom = stack.size() - 1;
final int upper = bottom >= maxQueueDepth ? bottom - maxQueueDepth : 0;

if (stack.size() == 0) { // nothing left of stack, just get to body
transition(HtmlTreeBuilderState.InBody);
}

for (int pos = bottom; pos >= upper; pos--) {
Element node = stack.get(pos);
if (pos == 0) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/jsoup/parser/HtmlTreeBuilderState.java
Original file line number Diff line number Diff line change
Expand Up @@ -1419,12 +1419,14 @@ private boolean anythingElse(Token t, HtmlTreeBuilder tb) {
boolean process(Token t, HtmlTreeBuilder tb) {
if (t.isStartTag() && inSorted(t.asStartTag().normalName(), InSelecTableEnd)) {
tb.error(this);
tb.processEndTag("select");
tb.popStackToClose("select");
tb.resetInsertionMode();
return tb.process(t);
} else if (t.isEndTag() && inSorted(t.asEndTag().normalName(),InSelecTableEnd )) {
tb.error(this);
if (tb.inTableScope(t.asEndTag().normalName())) {
tb.processEndTag("select");
tb.popStackToClose("select");
tb.resetInsertionMode();
return (tb.process(t));
} else
return false;
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/jsoup/integration/FuzzFixesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,16 @@ public void parseTimeout1606() throws IOException {
Document docXml = Jsoup.parse(new FileInputStream(in), "UTF-8", "https://example.com", Parser.xmlParser());
assertNotNull(docXml);
}

@Test
public void overflow1607() throws IOException {
// https://github.com/jhy/jsoup/issues/1607
File in = ParseTest.getFile("/fuzztests/1607.html.gz");

Document doc = Jsoup.parse(in, "UTF-8");
assertNotNull(doc);

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

0 comments on commit b4f20f0

Please sign in to comment.