fury icon indicating copy to clipboard operation
fury copied to clipboard

[Java] Fory JSON crashes the JVM when writing a String-keyed map with a null key

Open JasonBuildAI opened this issue 1 month ago • 1 comments

Search before asking

  • [X] I had searched in the issues and found no similar issues.

Version

  • Fory JSON: main (1.8.0-SNAPSHOT), commit 261dc92eabdc5c0fe08748a3ba476ae13f4e8bc9
  • JDK: Temurin 21.0.7
  • OS: Windows 11 (build 26100)

Component(s)

Java

Minimal reproduce step

Writing a declared Map<String, ...> whose key type is String and whose value type is a concrete scalar (String, Boolean, or any built-in number type) reaches MapCodec$StringKeyMapCodec fast paths that write the entry key without a null check. A null key is handed straight to StringJsonWriter.writeString, which reads it as a compact String through PlatformStringUtils.getStringValue and dereferences null with Unsafe, so the JVM dies with EXCEPTION_ACCESS_VIOLATION instead of raising the documented rejection.

import java.util.LinkedHashMap;
import org.apache.fory.json.ForyJson;
import org.apache.fory.reflect.TypeRef;

public class Repro {
  public static void main(String[] args) {
    ForyJson json = ForyJson.builder().build();
    LinkedHashMap<String, String> map = new LinkedHashMap<>();
    map.put(null, "value");
    System.out.println(json.toJson(map, new TypeRef<LinkedHashMap<String, String>>() {}));
  }
}

Run with fory-json on the classpath: java -cp <classpath> Repro.

Affected value types (all reachable from a null string key): String, Boolean, Integer, Long, Short, Byte, Float, Double, BigInteger, BigDecimal. Both toJson and toJsonBytes are affected. Maps declared with Object values, non-string keys (Integer, enum, ...), and dynamic toJson(map) calls already throw correctly, so only the string-keyed fast paths are broken.

What did you expect to see?

docs/json/object-mapping.md states that null map keys are rejected, and JsonAnyPropertyTest.rejectInvalidOutputKeys asserts that behaviour for an any-property map. A declared string-keyed map with a null key should raise the same ForyJsonException ("JSON map key cannot be null") as the other map key paths.

What did you see instead?

The process aborts:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000001c4104e7c1e, pid=26424, tid=31572
#
# Problematic frame:
# J 5376 c2 org.apache.fory.json.writer.StringJsonWriter.writeString(Ljava/lang/String;)V
#
siginfo: EXCEPTION_ACCESS_VIOLATION (0xc0000005), reading address 0x0000000000000014

Java frames:
j  jdk.internal.misc.Unsafe.getReference(Ljava/lang/Object;J)Ljava/lang/Object;
j  org.apache.fory.serializer.PlatformStringUtils.getStringValue(Ljava/lang/String;)Ljava/lang/Object;
j  org.apache.fory.serializer.StringSerializer.getStringBytes(Ljava/lang/String;)[B
j  org.apache.fory.json.writer.StringJsonWriter.writeString(Ljava/lang/String;)V
j  org.apache.fory.json.writer.StringJsonWriter.writeFieldName(Ljava/lang/String;)V
j  org.apache.fory.json.codec.MapCodec$StringStringMapCodec.writeMap(JsonWriter, Object)V

A Map<String, ...> with an attacker-controlled or accidental null key therefore terminates the process instead of returning an error. I found this while probing documented Fory JSON container behaviour and reproduced it on the commit above; I could not find an existing issue or PR for it.

Anything Else?

I am preparing a PR that routes the string-keyed map writes through the existing writeKey helper (which already rejects null keys with ForyJsonException), plus a regression test in JsonContainerTest.

Are you willing to submit a PR?

  • [X] I'm willing to submit a PR!

JasonBuildAI avatar Oct 09 '26 08:10 JasonBuildAI

I reproduced this on 261dc92e and traced it to the three string-keyed writeMap bodies in java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java, which write entry.getKey() directly instead of going through the existing writeKey helper. Routing them through writeKey (which already throws ForyJsonException for null keys) makes every string-keyed value type behave like the other map key paths, and I have a regression test ready for JsonContainerTest. I am opening a PR for it.

JasonBuildAI avatar Oct 09 '26 08:10 JasonBuildAI