Skip to content

Commit

Permalink
Perf: limit attribute count per element to 512
Browse files Browse the repository at this point in the history
Prevents runaway situations

Should improve #1578
  • Loading branch information
jhy committed Jul 14, 2021
1 parent ab34da4 commit 1f0b68f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jsoup changelog
* Bugfix [Fuzz]: malformed deeply nested table elements could create a stack overflow.
<https://github.com/jhy/jsoup/issues/1577>

* Bugfix [Fuzz]: Speed optimized malformed HTML creating elements with thousands of elements - limit the attribute
count per element when parsing to 512 (in real-world HTML, P99 is ~ 8).
<https://github.com/jhy/jsoup/issues/1578>

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

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/jsoup/parser/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,16 @@ Tag reset() {
return this;
}

/* Limits runaway crafted HTML from spewing attributes and getting a little sluggish in ensureCapacity.
Real-world HTML will P99 around 8 attributes, so plenty of headroom. Implemented here and not in the Attributes
object so that API users can add more if ever required. */
private static final int MaxAttributes = 512;

final void newAttribute() {
if (attributes == null)
attributes = new Attributes();

if (pendingAttributeName != null) {
if (pendingAttributeName != null && attributes.size() < MaxAttributes) {
// the tokeniser has skipped whitespace control chars, but trimming could collapse to empty for other control codes, so verify here
pendingAttributeName = pendingAttributeName.trim();
if (pendingAttributeName.length() > 0) {
Expand Down

0 comments on commit 1f0b68f

Please sign in to comment.