Menu ▾ ▴

#169 NekoHTML 1.9.22: Infinite Loop and OutOfMemoryError Triggered by Unterminated Processing Instructions

1.9.15
open
nobody
None
5
2026-10-29
2026-10-29
Wang xinxin
No

Summary

NekoHTML 1.9.22 contains an end-of-file handling defect in HTMLScanner.ContentScanner.scanPI() that can cause an infinite parsing loop, unbounded memory growth, or an uncaught ArrayIndexOutOfBoundsException when processing malformed HTML processing instructions.

The issue is reproducible under the default parser configuration with extremely small inputs:

  • 4-byte input: <?x? triggers an uncaught ArrayIndexOutOfBoundsException.
  • 10-byte input: <!--!><?x? causes the parser to loop indefinitely, repeatedly appending characters to an internal buffer.

The second case was independently reproduced with a 32 MiB heap limit, resulting in OutOfMemoryError. A related input discovered during Jazzer fuzzing kept the parser running for at least 1,588 seconds before the fuzzing watchdog terminated execution.

Applications that parse attacker-controlled HTML using NekoHTML may therefore be vulnerable to denial of service through CPU exhaustion, memory consumption, or stalled parsing threads.

Environment

  • Artifact: net.sourceforge.nekohtml:nekohtml
  • Latest tested version: 1.9.22
  • JDK: 17
  • OS: Linux
  • Affected component: org.cyberneko.html.HTMLScanner
  • Affected method: HTMLScanner.ContentScanner.scanPI()
  • Potential weaknesses: CWE-835 (Loop with Unreachable Exit Condition), CWE-400 (Uncontrolled Resource Consumption), CWE-248 (Uncaught Exception)

Steps to Reproduce

Case A: Uncaught ArrayIndexOutOfBoundsException

import java.io.StringReader;
import org.cyberneko.html.parsers.DOMParser;
import org.xml.sax.InputSource;

public class ReproA {
    public static void main(String[] args) throws Exception {
        new DOMParser().parse(
            new InputSource(new StringReader("<?x?"))
        );
    }
}

Actual result:

java.lang.ArrayIndexOutOfBoundsException:
Index -1 out of bounds for length 2048
    at org.cyberneko.html.HTMLScanner$CurrentEntity.read(HTMLScanner.java:1895)
    at org.cyberneko.html.HTMLScanner$ContentScanner.scanPI(HTMLScanner.java:2587)
    at org.cyberneko.html.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2090)
    at org.cyberneko.html.HTMLScanner.scanDocument(HTMLScanner.java:920)
    at org.cyberneko.html.HTMLConfiguration.parse(HTMLConfiguration.java:499)

Case B: Infinite Loop and Memory Exhaustion

import java.io.StringReader;
import org.cyberneko.html.parsers.DOMParser;
import org.xml.sax.InputSource;

public class ReproB {
    public static void main(String[] args) throws Exception {
        new DOMParser().parse(
            new InputSource(new StringReader("<!--!><?x?"))
        );
    }
}

Actual result:

The parser does not return. The parsing thread remains inside scanPI(), repeatedly executing the append-and-rewind logic.

Observed behavior:

  • A 10-byte input causes a reproducible nonterminating parsing loop.
  • With a 32 MiB heap limit, the parser eventually raises OutOfMemoryError.
  • During fuzzing, a related 72-byte input remained unprocessed after 1,588 seconds.
  • The thread stack repeatedly points to HTMLScanner$ContentScanner.scanPI().

The hanging case should be tested in a separate JVM process with an external timeout rather than inside an unrestricted test runner.

Affected Versions

NekoHTML Version 4-byte Exception 10-byte Hang/OOM
1.9.12 Reproduced Throws exception instead
1.9.16 Reproduced Reproduced
1.9.20 Reproduced Reproduced
1.9.21 Reproduced Reproduced
1.9.22 Reproduced Reproduced

Versions 1.9.17–1.9.19 were not tested.

Root Cause

The processing-instruction scanning loop in HTMLScanner.ContentScanner.scanPI() does not correctly handle EOF after a trailing ? or /.

When the scanner reads one of these characters, it looks ahead for >. If the following character is not >, the scanner appends the original character, rewinds the input, and continues.

When the lookahead reaches EOF, this logic can cause either:

  1. An invalid rewind that results in an out-of-bounds read.
  2. Repeated processing of the same character, causing an infinite append-and-rewind loop.

The exact outcome depends on the parser's preceding input and entity-buffer state.

Expected Behavior

Malformed or unterminated processing instructions should be handled gracefully at EOF.

The parser should terminate normally or report a controlled parsing error. It must not enter an infinite loop, grow memory without bound, or throw an internal array-index exception.

Security Impact

A remote or local attacker who can supply HTML content to an application using NekoHTML may be able to trigger a denial-of-service condition.

The infinite-loop variant can occupy a parsing thread indefinitely and continuously consume heap memory. Repeated malicious requests may degrade service availability, depending on application-level timeouts, memory limits, and concurrency controls.

The proof of concept demonstrates parser-level nontermination and memory exhaustion. Service-wide availability impact depends on the consuming application.

Suggested Fix

Update HTMLScanner.ContentScanner.scanPI() to handle EOF explicitly during lookahead.

When a trailing ? or / is followed by EOF, the scanner should terminate the processing-instruction parsing loop without rewinding beyond the valid input boundary.

The fix should also include regression tests for:

  • <?x?
  • <?x/
  • <!--><?x?
  • <?x?>

Both exception-triggering and nonterminating input variants should be covered.

Discovery

The issue was identified during differential fuzzing of four Java HTML parsers using Jazzer and subsequently minimized into deterministic 4-byte and 10-byte reproducer inputs.

The tested alternative parsers (jsoup, validator.nu HTML Parser, and TagSoup) completed processing of the corresponding inputs without hanging.

Discussion


Log in to post a comment.