You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fory.apache.org by ch...@apache.org on 2026/10/09 11:24:10 UTC
(fory) branch main updated: fix(java): reject null keys in Fory JSON string-keyed maps (#4149)
This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new 670e7a1d5 fix(java): reject null keys in Fory JSON string-keyed maps (#4149)
670e7a1d5 is described below
commit 670e7a1d577f3a2b1a2dbd69f264adc1d39309a6
Author: Jason Cat <ja...@gmail.com>
AuthorDate: Fri Oct 9 19:24:04 2026 +0800
fix(java): reject null keys in Fory JSON string-keyed maps (#4149)
## Why?
Fory JSON rejects null map keys, and
`JsonAnyPropertyTest.rejectInvalidOutputKeys` asserts that behavior. But
when a value is declared as `Map<String, V>` with `V` a `String`,
`Boolean`, or built-in number type, the string-keyed fast paths in
`MapCodec` (`StringStringMapCodec`, `StringBooleanMapCodec`,
`StringNumberMapCodec`) write the entry key with no null check. A null
key is handed straight to `StringJsonWriter.writeString` /
`Utf8JsonWriter.writeString` -> `PlatformStringUtils.getStringValue`,
which reads the value through `Unsafe.getReference(null, ...)`, so the
JVM aborts with `EXCEPTION_ACCESS_VIOLATION` (reading address `0x14`)
instead of raising the documented `ForyJsonException`. An accidental or
attacker-controlled null key therefore kills the process instead of
returning an error.
Fixes #4148.
## What does this PR do?
Routes the three string-keyed fast-path `writeMap` bodies through the
existing `writeKey(writer, key, STRING_KEY_CODEC)` helper, which is
already used by every other map key path (object values, non-string
keys, dynamic writes). `writeKey` throws `ForyJsonException("JSON map
key cannot be null")` for a null key and otherwise writes the member
name exactly as before, so non-null behavior is unchanged.
-
`java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java`:
3 lines changed (one per codec).
-
`java/fory-json/src/test/java/org/apache/fory/json/JsonContainerTest.java`:
adds `rejectNullStringMapKeys`, covering `String`, `Boolean`, and `Long`
values and both `toJson` and `toJsonBytes`.
No public API, wire-format, or binary-compatibility change. The fix
brings the string-keyed paths in line with the already-rejecting paths
and the documented behavior in `docs/json/object-mapping.md`.
## Related issues
Fixes #4148
## Evidence
Standalone repro (`ZzNullKeyProbe`, fory-json on the classpath), before
vs after the change.
Before -- the string-keyed variants (`string-string`, `string-int`,
`string-bool`, `string-double`, `string-bean-value`, `utf8-string-key`)
abort the JVM (exit code 1):
```text
# A fatal error has been detected by the Java Runtime Environment:
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=..., pid=38184, tid=...
# Problematic frame:
# j org.apache.fory.json.writer.StringJsonWriter.writeString(Ljava/lang/String;)V+78
# j org.apache.fory.json.codec.MapCodec$StringStringMapCodec.writeMap(...)
siginfo: EXCEPTION_ACCESS_VIOLATION (0xc0000005), reading address 0x0000000000000014
```
(`object-value`, `int-key`, and `dynamic` already threw
`ForyJsonException` before the change.)
After -- all nine variants exit 0:
```text
THREW org.apache.fory.json.ForyJsonException: JSON map key cannot be null
```
Regression test, with the fix reverted vs applied:
```text
$ mvn -pl fory-json test -Dtest=JsonContainerTest#rejectNullStringMapKeys
# before (fix reverted):
[ERROR] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD FAILURE
[ERROR] The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
[ERROR] Crashed tests:
[ERROR] org.apache.fory.json.JsonContainerTest
# after:
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.475 s -- in org.apache.fory.json.JsonContainerTest
[INFO] BUILD SUCCESS
```
Project checks (from `java/`, after the fix):
```text
$ mvn -pl fory-json test
[INFO] Tests run: 1286, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
$ mvn -T16 spotless:check
[INFO] BUILD SUCCESS
$ mvn -T16 checkstyle:check
[INFO] BUILD SUCCESS
```
## AI Contribution Checklist
- [x] Substantial AI assistance was used in this PR: `yes`
- [x] If `yes`, I included the standardized `AI Usage Disclosure` block
below.
- [x] If `yes`, I can explain and defend all important changes without
AI help.
- [x] If `yes`, I reviewed AI-assisted code changes line by line before
submission.
- [x] If `yes`, I completed line-by-line self-review first and fixed
issues before requesting AI review.
- [x] If `yes`, I ran two fresh AI review agents on the current PR diff
or current HEAD after the latest code changes: one Fory-guided reviewer
using `AGENTS.md` and `.agents/ci-and-pr.md`, and one independent
general reviewer in a separate clean-context review session that was not
pointed to `.agents/ci-and-pr.md` or any copied Fory-specific review
checklist.
- [x] If `yes`, I addressed all AI review comments and repeated the
review loop until both AI reviewers reported no further actionable
comments.
- [x] If `yes`, I attached persisted links of the final clean AI review
results from both fresh reviewers on the current PR diff or current HEAD
after the latest code changes in this PR body.
- [x] If `yes`, I ran adequate human verification and recorded evidence
(checks run locally, pass/fail summary, and confirmation I reviewed
results).
- [x] If `yes`, I added/updated tests and specs where required.
- [x] If `yes`, I validated protocol/performance impacts with evidence
when applicable (N/A: no protocol or performance-sensitive path
changed).
- [x] If `yes`, I verified licensing and provenance compliance.
## AI Usage Disclosure
```text
AI Usage Disclosure
- substantial_ai_assistance: yes
- scope: design drafting, code drafting, tests
- affected_files_or_subsystems: java/fory-json (org.apache.fory.json.codec.MapCodec, org.apache.fory.json.JsonContainerTest)
- ai_review: line-by-line self-review completed; ran two fresh reviewers on the current diff (HEAD b76d96a87bf8c80748be6196244af11a5f9df01c) -- a Fory-guided reviewer following AGENTS.md and .agents/ci-and-pr.md, and an independent general reviewer in a clean-context session. No actionable comments remained; both reviewers reported no further comments. No code changed after this review round.
- ai_review_artifacts: https://gist.github.com/JasonBuildAI/9ebe2ccef8c31d5c53db2c863396abe3 (final clean results from both fresh reviewers on the current diff: review_fory_guided.md and review_independent.md)
- human_verification: ran locally from java/ -- `mvn -pl fory-json test` (Tests run: 1286, Failures: 0, Errors: 0, Skipped: 0, BUILD SUCCESS), targeted `mvn -pl fory-json test -Dtest=JsonContainerTest#rejectNullStringMapKeys` (2 pass, BUILD SUCCESS), `mvn -T16 spotless:check` (BUILD SUCCESS), `mvn -T16 checkstyle:check` (BUILD SUCCESS); also reverted the fix to confirm the standalone repro and the regression test crash the JVM before the change. Contributor reviewed all results.
- performance_verification: N/A (no serialization/deserialization hot path, allocation, codegen, or buffer logic changed; only a null guard on an error path)
- provenance_license_confirmation: Apache-2.0-compatible provenance confirmed; no incompatible third-party code introduced; change only reuses existing in-tree helpers
```
## Does this PR introduce any user-facing change?
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
N/A -- no performance-sensitive path changed.
---
.../java/org/apache/fory/json/codec/MapCodec.java | 6 +++---
.../org/apache/fory/json/JsonContainerTest.java | 23 ++++++++++++++++++++++
2 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java b/java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java
index 5ae9d6ac4..14d6aec02 100644
--- a/java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java
+++ b/java/fory-json/src/main/java/org/apache/fory/json/codec/MapCodec.java
@@ -983,7 +983,7 @@ public abstract class MapCodec<T extends Map<?, ?>> implements ContainerJsonCode
int index = 0;
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
writer.writeComma(index++);
- writer.writeFieldName((String) entry.getKey());
+ writeKey(writer, entry.getKey(), STRING_KEY_CODEC);
Object element = entry.getValue();
if (element == null) {
requireNullValue();
@@ -1040,7 +1040,7 @@ public abstract class MapCodec<T extends Map<?, ?>> implements ContainerJsonCode
int index = 0;
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
writer.writeComma(index++);
- writer.writeFieldName((String) entry.getKey());
+ writeKey(writer, entry.getKey(), STRING_KEY_CODEC);
Object element = entry.getValue();
if (element == null) {
requireNullValue();
@@ -1097,7 +1097,7 @@ public abstract class MapCodec<T extends Map<?, ?>> implements ContainerJsonCode
int index = 0;
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
writer.writeComma(index++);
- writer.writeFieldName((String) entry.getKey());
+ writeKey(writer, entry.getKey(), STRING_KEY_CODEC);
Object element = entry.getValue();
if (element == null) {
requireNullValue();
diff --git a/java/fory-json/src/test/java/org/apache/fory/json/JsonContainerTest.java b/java/fory-json/src/test/java/org/apache/fory/json/JsonContainerTest.java
index b52236ce5..a439bd3fd 100644
--- a/java/fory-json/src/test/java/org/apache/fory/json/JsonContainerTest.java
+++ b/java/fory-json/src/test/java/org/apache/fory/json/JsonContainerTest.java
@@ -682,6 +682,29 @@ public class JsonContainerTest extends ForyJsonTestModels {
}
}
+ @Test
+ public void rejectNullStringMapKeys() {
+ ForyJson json = newJson();
+ LinkedHashMap<String, String> stringValues = new LinkedHashMap<>();
+ stringValues.put(null, "value");
+ assertThrows(
+ ForyJsonException.class,
+ () -> json.toJson(stringValues, new TypeRef<LinkedHashMap<String, String>>() {}));
+ assertThrows(
+ ForyJsonException.class,
+ () -> json.toJsonBytes(stringValues, new TypeRef<LinkedHashMap<String, String>>() {}));
+ LinkedHashMap<String, Boolean> booleanValues = new LinkedHashMap<>();
+ booleanValues.put(null, true);
+ assertThrows(
+ ForyJsonException.class,
+ () -> json.toJson(booleanValues, new TypeRef<LinkedHashMap<String, Boolean>>() {}));
+ LinkedHashMap<String, Long> numberValues = new LinkedHashMap<>();
+ numberValues.put(null, 7L);
+ assertThrows(
+ ForyJsonException.class,
+ () -> json.toJson(numberValues, new TypeRef<LinkedHashMap<String, Long>>() {}));
+ }
+
@Test
public void readTypeRefOptional() {
ForyJson json = newJson();
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fory.apache.org
For additional commands, e-mail: commits-help@fory.apache.org