Summary
NekoHTML 1.9.22 throws an uncaught ArrayIndexOutOfBoundsException when parsing a start tag containing five or more attributes, where a later bare xmlns: attribute duplicates an earlier one.
The issue occurs under the default parser configuration and is caused by an incorrect attribute index in HTMLTagBalancer.Info.
Environment
Artifact: net.sourceforge.nekohtml:nekohtml:1.9.22
Xerces: 2.11.0
JDK: 17
OS: Linux
Weakness: CWE-129 (Improper Validation of Array Index)
Steps to Reproduce
import java.io.StringReader;
import org.cyberneko.html.parsers.DOMParser;
import org.xml.sax.InputSource;
public class Repro {
public static void main(String[] args) throws Exception {
new DOMParser().parse(new InputSource(new StringReader(
"")));
}
}
Actual Result
java.lang.ArrayIndexOutOfBoundsException:
Index 4 out of bounds for length 4
at org.apache.xerces.util.XMLAttributesImpl.setNonNormalizedValue(...)
at org.cyberneko.html.HTMLTagBalancer$Info.<init>(HTMLTagBalancer.java:1350)
at org.cyberneko.html.HTMLTagBalancer.startElement(HTMLTagBalancer.java:749)</init>
Root Cause
HTMLTagBalancer.Info copies attributes using addAttribute() but incorrectly uses the original loop index when calling setNonNormalizedValue() and setSpecified().
When Xerces encounters a duplicate attribute, addAttribute() updates the existing entry rather than appending a new one. This causes a mismatch between the original attribute index and the actual index in the destination array.
A duplicate at index 4 can therefore access beyond the initial four-element backing array.
Affected Versions
The issue was reproduced with NekoHTML 1.9.22 using Xerces 2.11.0 and 2.10.0.
It was not reproduced with NekoHTML 1.9.12, 1.9.16, 1.9.20, or 1.9.21 in the tested configurations.
Potential Security Impact
Attacker-controlled HTML may trigger an uncaught runtime exception, interrupting a parsing request or document-processing job.
This may result in a context-dependent denial of service when the consuming application does not handle the exception. JVM termination or service-wide denial of service has not been demonstrated.
Suggested Fix
Use the actual index returned by addAttribute():
int idx = newattrs.addAttribute(aqname, type, value);
newattrs.setNonNormalizedValue(idx, nonNormalizedValue);
newattrs.setSpecified(idx, specified);
Duplicate attributes should also be handled consistently with HTML parsing rules.