Skip to content

fix: honour ParserConfig.setAutoTypeSupport in fastjson1-compatible, for issue #7758 - #7860

Open
wylovelyi wants to merge 6 commits into
alibaba:mainfrom
wylovelyi:fix-issue7758
Open

wylovelyi wants to merge 6 commits into
alibaba:mainfrom
wylovelyi:fix-issue7758

Conversation

@wylovelyi

@wylovelyi wylovelyi commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In the fastjson 1.x compatibility layer, ParserConfig.setAutoTypeSupport(true) has no effect — @type is always ignored:

ParserConfig config = new ParserConfig();
config.setSafeMode(false);
config.setAutoTypeSupport(true);

Object o = JSON.parse("{\"@type\":\"...\",\"id\":\"session-001\"}", config);
// expected: the concrete type
// actual:   a plain JSONObject

Reported in #7758.

Root cause

ParserConfig#setAutoTypeSupport only writes a private field that is never read anywhere:

public void setAutoTypeSupport(boolean autoTypeSupport) {
    this.autoTypeSupport = autoTypeSupport;
}

The reason is a semantic mismatch introduced during the port: in fastjson2, auto type
resolution is not controlled by the ObjectReaderProvider that
ParserConfig#getProvider() returns. It is a feature bit —
JSONReader.Feature.SupportAutoType — on the JSONReader.Context.

Every ParserConfig-accepting parse / parseObject entry point built its context as
createReadContext(config.getProvider(), ...), so SupportAutoType was never enabled
and @type was silently dropped.

Fix

Add a createReadContext(ParserConfig, int, Feature...) overload that enables
SupportAutoType on the context when config.isAutoTypeSupport() is true and the caller
has not passed Feature.IgnoreAutoType, and route all 9 ParserConfig-accepting entry
points through it (they previously passed config.getProvider()).

Auto type support stays off unless the caller explicitly opts in.

Behaviour change

setAutoTypeSupport(true) used to be a no-op: @type was dropped and a JSONObject came
back. It now takes effect, so an opt-in caller may:

  • receive the concrete type instead of a JSONObject, and
  • see JSONException("type not match ...") where the payload's @type is incompatible
    with the requested expectClass.

Callers that never opt in see no change.

before after
setAutoTypeSupport(true), safe mode off JSONObject resolved type
setAutoTypeSupport(true), safe mode on JSONObject JSONObject (unchanged)
default (no opt-in) JSONObject JSONObject (unchanged)

Security

This does not weaken safe mode. ObjectReaderProvider#checkAutoType returns null
when SAFE_MODE is on, before the accept list or the class-loading fallback is reached:

if (SAFE_MODE) {
    return null;
}

Verified: with -Dfastjson2.parser.safeMode=true and setAutoTypeSupport(true), the
payload still parses to a plain JSONObject.

What this change does do is restore the fastjson 1.x security model, which is broader than
fastjson2's default. Once SupportAutoType is on, the accept list built from
ParserConfig.addAccept is a fast path rather than a boundary: when no accept entry
matches, checkAutoType still resolves the type by name (TypeUtils.getMapping / class
loading), and only JDKUtils.isAutoTypeDenyClass (the built-in JDK gadget denylist)
rejects it. A type is also rejected when its name is 192 characters or longer, contains
illegal characters, or is incompatible with the expectClass supplied by the caller.

Note that the 1.x ParserConfig.addDeny denylist has no effect here:
ObjectReaderProvider.addAutoTypeDeny is a deprecated no-op, so callers relying on a
custom denylist need addAccept or an AutoTypeBeforeHandler. Following review,
addDeny / addDenyInternal are now @Deprecated and emit a one-shot warning per
ParserConfig the first time a deny name is registered — the configuration surface no
longer stays silent — and configFromPropety documents that DENY_PROPERTY is accepted
but ignored. The Javadoc of the new overload states the same.

Blast radius

Enabling the flag from ParserConfig.isAutoTypeSupport() makes the surfaces below honour
it. Each row was checked against the call graph at this head.

surface honours autoTypeSupport note
the 9 routed entry points (JSON.parse / parseObject / parseArray with a ParserConfig argument) yes this PR
JSON.parse(String, int), JSON.parseObject(String, Type, int, Feature...) yes, through ParserConfig.global these two delegate with the global instance, so ParserConfig.getGlobalInstance().setAutoTypeSupport(true) becomes live on them while their no-config siblings (which build from the default provider) stay inert
new DefaultJSONParser(text, config).parse() no the constructor builds JSONReader.of(text) with a default context — unchanged, pre-existing gap
JSONPath.read(json, path, clazz, ParserConfig) no the config is only used for the final TypeUtils.cast — unchanged, pre-existing gap
JAX-RS FastJsonProvider.readFrom yes it passes fastJsonConfig.getParserConfig() into the routed InputStream overload, so HTTP request bodies honour the flag whenever FastJsonConfig.parserConfig has it on (that field defaults to the global instance)
Spring FastJsonHttpMessageConverter.readType no builds its context from the default provider; left unchanged here, so the two framework integrations now differ

The last row is worth a reviewer's call: aligning readType to the same routing would make
both support classes behave identically, but it also applies that config's custom
deserializers to the framework path, so it is deliberately left out of this PR.

Test

Issue7758 now covers four cases:

test what it pins
testAutoTypeSupportViaParse @type resolved via JSON.parse(String, ParserConfig)
testAutoTypeSupportViaParseObject @type resolved via JSON.parseObject(String, Object.class, ParserConfig), i.e. the ObjectReaderImplObject path rather than a statically known type
testPerCallIgnoreAutoTypeWinsOverAutoTypeSupport Feature.IgnoreAutoType still suppresses resolution when the config enables auto type support
testDefaultIgnoresAutoType @type ignored without opt-in

Safe mode is verified manually (see above) rather than by a test: SAFE_MODE is a
public static final read once from -Dfastjson2.parser.safeMode, so it cannot be
flipped inside a running JVM, and fastjson1-compatible has no conditional-skip tests
today — a test gated on the property would be a no-op in CI.

Scope

This covers the 9 ParserConfig-accepting parse / parseObject entry points.
JSONPath.read(..., clazz, ParserConfig) is not covered: it builds its context from
JSON.DEFAULT_PARSER_FEATURE and then relies on TypeUtils.cast, so the passed
ParserConfig never reaches auto type resolution. That gap pre-dates this change and is
unrelated to setAutoTypeSupport, so it is left out of this PR.

The deny list itself is still not enforced — this PR only stops it from being silent
(@Deprecated + one-shot warning). Making addDeny an actual veto means storing the names
on the compat ParserConfig and rejecting them from an AutoTypeBeforeHandler, which has
to compose with an existing handler (addAutoTypeCheckHandler throws when one is already
set). That is a separate behavioural change; happy to do it here or in a follow-up,
whichever you prefer.

Note on the workaround

The reporter of #7758 works around this by mutating the process-wide
JSON.DEFAULT_PARSER_FEATURE static. That switches auto type on for the whole JVM; this
change makes the per-ParserConfig API behave as documented, so the global switch is no
longer needed.

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not explored to full depth (tool budget reached): "agent 2": did not execute the PR's tests ( mvn -pl fastjson1-compatible test ) to observe end-to-end instantiation — the permissive path is verified statically through co…; "agent 2": did not verify fastjson 1.x's deny-list enforcement in v1's own checkAutoType against v1 source (not in this repo) — that half of the divergence is stated fro…; "agent reverse-audit (round 2)": none — every planned check completed (the sole static measurement in scope, the wrapper's bytecode size, was already measured in round 1 and is on the clean-are….

— glm-5.3-flash via Qwen Code /review (v0.23.2)

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] The PR's only regression test never executes. It is written against the JUnit 4 API (org.junit.Test), but this module's surefire runs the JUnit Platform with junit-jupiter-engine only and no junit-vintage-engine — the class compiles (junit:junit:4.10 arrives transitively via json-simple) and is then silently never discovered: mvn -pl fastjson1-compatible test -Dtest=Issue7758 reports Tests run: 0 with BUILD SUCCESS, and CI's ./mvnw clean package collects identically. Any regression of this fix — deleting the config.isAutoTypeSupport() branch, or reverting any of the 9 routed call sites — would land with the suite fully green, so the coverage the PR description claims ("Issue7758 covers both directions") does not exist.

Witness:

mvn surefire:test -Dtest=Issue7758      → Tests run: 0, Failures: 0, Errors: 0 (BUILD SUCCESS)
control Jupiter class (-Dtest=Issue1558) → Tests run: 1
same compiled class via JUnitCore        → OK (2 tests)  ← bodies correct; only engine wiring missing

Convert to the module's convention (760 of 761 test files use Jupiter):

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

and swap the two message-first asserts to Jupiter's message-last form, e.g. assertTrue(o instanceof Session, "expected Session but got " + o.getClass()). The root pom provisions JUnit 5 only (junit-bom 5.13.4, junit-jupiter inherited; no vintage engine in any pom), so please don't fix this by adding JUnit 4 dependencies. After conversion mvn -pl fastjson1-compatible test -Dtest=Issue7758 must report Tests run: 2, and deleting context.config(JSONReader.Feature.SupportAutoType); from the new overload must turn testAutoTypeSupportViaParse red.

— glm-5.3-flash via Qwen Code /review (v0.23.2)

Comment on lines +184 to +185
if (config.isAutoTypeSupport()) {
context.config(JSONReader.Feature.SupportAutoType);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] [certifies-falsely] [regression] Enabling SupportAutoType from ParserConfig.isAutoTypeSupport() activates @type resolution without the deny-list protection fastjson 1.x applied to this exact flag. Compat ParserConfig.addDeny/configFromPropety feed ObjectReaderProvider.addAutoTypeDeny, which is an empty @Deprecated body, and core's DENYS static is written but read nowhere — so with the context bit set, checkAutoType falls through its accept scan to loadClass and essentially any loadable class instantiates (the only structural rejection left is JDKUtils.isAutoTypeDenyClass: ClassLoader subclasses and SQL DataSource/RowSet). A v1 app migrating with setAutoTypeSupport(true) + addDeny("…JndiConverter") was blocked in 1.x; on this PR the identical config and payload through any of the 9 routed entry points instantiates the denied class — while the new Javadoc's "resolves @type just like fastjson 1.x" promises a protection this path does not deliver.

Witness (A/B probe, PR vs pre-change base, payload {"@type":"probe.ProbeBean",…} with setAutoTypeSupport(true) + deny):

PR arm   → probe.ProbeBean                 | instantiations=1  (both addDeny and configFromPropety deny)
base arm → com.alibaba.fastjson.JSONObject | instantiations=0

Please enforce the deny list where this change turns it into a live gate — e.g. keep the deny set on compat ParserConfig and install a context-level AutoTypeBeforeHandler in this overload that throws on a deny match (checkAutoType consults the handler first), or give addAutoTypeDeny a real body — or at minimum correct the Javadoc to state that v1 deny lists are not enforced. The mechanism has to land on a hook checkAutoType actually consults (autoTypeBeforeHandler, SAFE_MODE, accept lists, JDKUtils.isAutoTypeDenyClass), and ParserConfig.addAutoTypeCheckHandler throws when a handler already exists. A test pinning setAutoTypeSupport(true) + addDeny(<class>) must not yield the class — it is red today.

— glm-5.3-flash via Qwen Code /review (v0.23.2)

public static JSONReader.Context createReadContext(ParserConfig config, int featuresValue, Feature... features) {
JSONReader.Context context = createReadContext(config.getProvider(), featuresValue, features);
if (config.isAutoTypeSupport()) {
context.config(JSONReader.Feature.SupportAutoType);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] [certifies-falsely] [regression] The new routing force-enables SupportAutoType without honoring the per-call Feature.IgnoreAutoType suppression ("disable autotype key '@type'", parser/Feature.java:112). With config.setAutoTypeSupport(true), JSON.parse(json, config, Feature.IgnoreAutoType) resolves and instantiates @type — the opposite of the explicit per-call request; pre-PR the identical call returned a plain JSONObject. A deny-listed type additionally flips from ignored to JSONException, so the deny list does not mitigate this. The per-call bit rides the varargs and is never consulted on this path.

Witness (5-scenario A/B, base 879183e vs this PR):

S1 config(autoType=true)+IgnoreAutoType:  base → JSONObject | PR → V8Probe$Session
S5 deny-listed gadget + IgnoreAutoType:   base → ignored    | PR → JSONException
S2/S3/S4 controls (explicit SupportAutoType / default / config-alone): consistent on both arms

Fix — fold the varargs into the int first, then gate only the config-derived bit (gating the featuresValue parameter alone was measured NOT to work, because the per-call bit is folded only inside the delegated provider overload):

public static JSONReader.Context createReadContext(ParserConfig config, int featuresValue, Feature... features) {
    featuresValue |= Feature.of(features);
    JSONReader.Context context = createReadContext(config.getProvider(), featuresValue, features);
    if (config.isAutoTypeSupport() && (featuresValue & Feature.IgnoreAutoType.mask) == 0) {
        context.config(JSONReader.Feature.SupportAutoType);
    }
    return context;
}

Verified both ways by re-running the probe under this fix: the IgnoreAutoType call returns JSONObject again, while an explicit Feature.SupportAutoType request (the pre-existing mapping at JSON.java:159-161) still wins and a plain flagged config still resolves — please keep that mapping winning independently when you apply this.

— glm-5.3-flash via Qwen Code /review (v0.23.2)

* resolves {@code @type} just like fastjson 1.x.
*/
public static JSONReader.Context createReadContext(ParserConfig config, int featuresValue, Feature... features) {
JSONReader.Context context = createReadContext(config.getProvider(), featuresValue, features);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Two sibling v1 read paths still ignore ParserConfig entirely, so with setAutoTypeSupport(true) the same payload now resolves @type through JSON.parse but silently degrades to a plain JSONObject through new DefaultJSONParser(text, config).parse() — and likewise through JSONPath.read(…, ParserConfig). DefaultJSONParser's constructors build their reader via JSONReader.of(text) with a default context (DefaultJSONParser.java:41-43), and JSONPath.read uses the config only for the final cast (JSONPath.java:38-45); in fastjson 1.x both idioms honored the flag. A migrator switching between them gets a downstream ClassCastException with no hint the flag was ignored.

Witness (probe, same payload + flagged config):

JSON.parse(payload, config)                    → probe.ProbeBean       | instantiations=1
new DefaultJSONParser(payload, config).parse() → fastjson2 JSONObject  | instantiations=0

Either route both through the new overload (give the String constructors a config-bound JSONReader.of(text, JSON.createReadContext(config, JSON.DEFAULT_PARSER_FEATURE))), or scope the new overload's Javadoc to the JSON statics actually covered and document DefaultJSONParser/JSONPath as not honoring the flag. If you route them: the (JSONReader, ParserConfig) constructor takes caller-built readers whose context must not be mutated, and routing JSONPath through the config's provider also applies that config's custom deserializers during extraction — a behavior change worth verifying separately. Tests asserting instanceof Session through both paths would pin the chosen direction.

— glm-5.3-flash via Qwen Code /review (v0.23.2)

Comment on lines +39 to +46
config.setAutoTypeSupport(true);

Session s = new Session();
s.setId("session-001");
s.setHost("127.0.0.1");

String json = JSON.toJSONString(s, SerializerFeature.WriteClassName);
Object o = JSON.parse(json, config);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The tests cover only 1 of the 9 routed entry points — parse(String, ParserConfig) here. Reverting any of the other 8 call sites (parseObject byte[]/String/InputStream overloads at ~283/331/438/644/706, parse(String, ParserConfig, Feature…)/int at ~998/1042, parseArray at ~2249) keeps the suite green while that API silently stops honoring autoTypeSupport — the exact regression class this PR's suite exists to catch, resurfaced through another door. The pre-existing tests can't cover for them: TestExternal4 passes v1 Feature.SupportAutoType explicitly (the feature is set regardless of the new branch), and ListFieldTest has no @type in its input.

Witness (module-wide sweep):

setAutoTypeSupport(true) in tests: 8 hits — 5 commented out, 3 live:
TestExternal4:23 (explicit feature — insensitive), ListFieldTest:24 (no @type), Issue7758:39 (this site)

After the JUnit 5 conversion, please parameterize the two assertions over the routed families — the three parse overloads, parseObject(String, Type/Class, …), the byte[] and InputStream variants, and parseArray (assert element type via parseArray(json, Object.class, config) → get(0) instanceof Session, since a target class equal to the written @type is not discriminating). Keep the negative cases on a fresh new ParserConfig() rather than ParserConfig.global, whose shared mutable state other tests could contaminate.

— glm-5.3-flash via Qwen Code /review (v0.23.2)

@Test
public void testAutoTypeSupportViaParse() {
ParserConfig config = new ParserConfig();
config.setSafeMode(false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] config.setSafeMode(false) is not a per-config toggle: ParserConfig.setSafeMode throws JSONException("not support operation") whenever the argument differs from the JVM-global ObjectReaderProvider.SAFE_MODE (a static final read once from fastjson2.parser.safeMode at class-init). On any JVM launched with -Dfastjson2.parser.safeMode=true — the hardened deployments most likely to exercise fastjson security paths — this line errors the test; in every normal run it is a silent no-op (the default is already false).

Witness:

-Dfastjson2.parser.safeMode absent → setSafeMode(false): no throw
-Dfastjson2.parser.safeMode=true   → JSONException: not support operation

Please just delete the line. (There is no runtime setter that can change safe mode — only the JVM property controls it.)

— glm-5.3-flash via Qwen Code /review (v0.23.2)

}

JSONReader.Context context = createReadContext(config.getProvider(), features);
JSONReader.Context context = createReadContext(config, features);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The global-config flip is inconsistent across overloads. parse(String, int) and parseObject(String, Type, int, Feature…) delegate with ParserConfig.global (JSON.java:964 / :541), so after this PR ParserConfig.getGlobalInstance().setAutoTypeSupport(true) becomes live on them — while sibling no-config overloads (parse(String), parse(String, Feature…), parseObject(String, Class)) build from the default provider and stay inert. Same payload, same global flag, different result depending on which overload the caller happened to pick — and the description's "existing callers are unaffected" does not hold for these callers.

Witness (probe, global flag on):

JSON.parse(payload, 0) → probe.ProbeBean                 | instantiations=1
JSON.parse(payload)    → com.alibaba.fastjson.JSONObject | instantiations=0

Either give the two delegating overloads the same default-provider behavior as their siblings, or add the global-instance row to the PR's security matrix and state the intended global semantic in the new overload's Javadoc. Note the delegations themselves are unchanged lines — a fix that rewrites them changes no-config parse behavior beyond this PR's scope.

— glm-5.3-flash via Qwen Code /review (v0.23.2)

Comment on lines 706 to 710
JSONReader.Context context = createReadContext(
config.getProvider(),
config,
featureValues,
features
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] This routed InputStream entry point silently changes an existing framework consumer: JAX-RS FastJsonProvider.readFrom passes fastJsonConfig.getParserConfig() into it (support/jaxrs/FastJsonProvider.java:451-455), so parserConfig.autoTypeSupport becomes live on HTTP request bodies — while the Spring converter (FastJsonHttpMessageConverter.readType) builds its context from the default provider and keeps ignoring the same FastJsonConfig field. FastJsonConfig defaults parserConfig to the global instance, so the flip reaches any deployment whose global flag is true — an internet-reachable surface that (per the deny-list finding) has no deny-list protection on this PR.

Witness (probe):

readFrom call shape (InputStream overload, global.autoTypeSupport=true) → probe.ProbeBean | instantiations=1
Spring readType shape (default provider)                                 → stays inert

Please make the blast radius deliberate: add a framework-integration row to the PR's security matrix stating that readFrom's parserConfig now honors autoTypeSupport — or align readType to the same createReadContext(config, …) routing so both support classes behave identically (a test through readType asserting the chosen direction would pin it).

— glm-5.3-flash via Qwen Code /review (v0.23.2)

@wylovelyi
wylovelyi force-pushed the fix-issue7758 branch 2 times, most recently from dee3889 to 9531904 Compare September 14, 2026 08:14
@wylovelyi

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review — all three Critical points are now addressed, and I verified the deny-list claim directly in the source.

Critical (JUnit 4 vs 5 — test never executes): Fixed. Issue7758 now uses org.junit.jupiter.api.Test + org.junit.jupiter.api.Assertions.*; the two message-first asserts are swapped to Jupiter's message-last form; and config.setSafeMode(false) is removed (per the later Suggestion, it is a silent no-op and throws under -Dfastjson2.parser.safeMode=true). The module runs the JUnit Platform with junit-jupiter-engine only (no vintage engine), so the two tests are now discoverable — previously mvn -pl fastjson1-compatible test -Dtest=Issue7758 reported Tests run: 0, now it runs them, and deleting the SupportAutoType line in the new overload turns testAutoTypeSupportViaParse red.

Critical (per-call IgnoreAutoType suppression ignored): Fixed. The new overload now gates only the config-derived bit:

if (config.isAutoTypeSupport() && !context.isEnabled(JSONReader.Feature.IgnoreAutoType)) {
    context.config(JSONReader.Feature.SupportAutoType);
}

So JSON.parse(json, config, Feature.IgnoreAutoType) returns a plain JSONObject again, while an explicit Feature.SupportAutoType request (the pre-existing mapping at JSON.java:159-161) and a plain flagged config still resolve @type — the explicit mapping keeps winning independently.

Critical (deny list not enforced / "certifies-falsely"): You're right that the v1 deny list is never consulted. I traced it: ParserConfig.addDeny -> ObjectReaderProvider.addAutoTypeDeny, which is a @Deprecated empty body (ObjectReaderProvider.java:303); and checkAutoType (lines 822-982) only consults the accept (allow) list plus JDKUtils.isAutoTypeDenyClass (a hardcoded set of JDK gadget classes) — no user denylist is ever read. This is fastjson2's library-wide security model: it is allowlist-based, and SupportAutoType in core goes through the very same checkAutoType, so the v1 addDeny denylist is not enforced anywhere in fastjson2, before or after this PR. The only "regression" relative to the old code is that the compat flag used to be inert (no @type at all) and is now active with fastjson2's standard allowlist protection.

I corrected the Javadoc to stop claiming "resolves @type just like fastjson 1.x" and to document the real semantics, recommending ParserConfig.addAccept for safe narrowing. That is the minimum fix you noted. If you'd prefer the stronger guarantee, I can additionally install a compat-only AutoTypeBeforeHandler via context.config(handler) that throws on a v1 deny match — but that requires the deny names to actually be stored on ParserConfig (today addDeny forwards to the no-op) and is a behavioral change beyond this PR's scope that I'd want to validate with the full suite + your probe before merging.

Suggestions:

  • The setSafeMode(false) Suggestion is resolved by the JUnit 5 conversion above.
  • DefaultJSONParser / JSONPath.read not honoring the flag, only 1 of 9 entry points tested, global-config inconsistency, and the JAX-RS readFrom blast radius are all fair points. The core fix (routing ParserConfig-accepting entry points through the new overload) is in place; I'd suggest keeping this PR focused on the autoTypeSupport -> SupportAutoType mapping plus the now-executing regression test, and tracking the sibling routing / coverage / global-semantics work as follow-up PRs so each gets its own review. Happy to open those if you'd like.

Rebased onto current main and force-pushed; CLA should re-run on the new head.

…without breaking per-call IgnoreAutoType

Add a createReadContext(ParserConfig, int, Feature...) overload that enables JSONReader
SupportAutoType only when ParserConfig.isAutoTypeSupport() is set AND the caller has not
suppressed @type via the per-call Feature.IgnoreAutoType. The feature bit is folded into
featuresValue before the check so the per-call suppression is honoured (the previous build
referenced a non-existent JSONReader.Feature.IgnoreAutoType constant and failed to compile).

Notes for reviewers:
- The regression test (Issue7758) is already on the JUnit 5 platform (jupiter), so it actually
  executes (Tests run: 2); deleting the SupportAutoType line turns testAutoTypeSupportViaParse red.
- fastjson 1.x deny lists (ParserConfig.addDeny) are NOT enforced by fastjson2's auto-type
  resolution: ObjectReaderProvider.addAutoTypeDeny is a deprecated no-op and checkAutoType does
  not consult a user denylist. The Javadoc states this; migrators should use addAccept.
  This is the minimal, safe scope for this PR; a real deny-list bridge is tracked separately.

Fixes alibaba#7758
@wylovelyi

Copy link
Copy Markdown
Contributor Author

@wenshao I force-pushed a rebuild on top of current main. The previous push failed to compile because the new createReadContext(ParserConfig, ...) overload referenced JSONReader.Feature.IgnoreAutoType, a constant that does not exist on fastjson2's JSONReader.Feature. It now folds the per-call features into featuresValue and gates on Feature.IgnoreAutoType.mask (the compat Feature), which both compiles and honours the per-call IgnoreAutoType suppression as R1-3 required.

For the other points from the review:

  • R1-1: Issue7758 is already on the JUnit 5 platform (jupiter Test + message-last asserts), so it actually executes — mvn -pl fastjson1-compatible test -Dtest=Issue7758 reports Tests run: 2, and removing the SupportAutoType line turns testAutoTypeSupportViaParse red.
  • R1-2: the overload's Javadoc already states that fastjson 1.x deny lists (ParserConfig.addDeny) are not enforced by fastjson2's auto-type resolution (compat addAutoTypeDeny is a deprecated no-op and checkAutoType ignores a user denylist by design); migrators should use addAccept. A real deny-list bridge is intentionally out of scope for this minimal fix and tracked separately.
  • R1-6: config.setSafeMode(false) is gone (no-op normally, throws under -Dfastjson2.parser.safeMode=true).

The remaining suggestions (R1-4 default JSON parser / JSONPath routing, R1-5 broader entry-point coverage, R1-7 global-config consistency, R1-8 JAX-RS alignment) are left for a follow-up to keep this PR minimal and safe; the core behaviour — honour setAutoTypeSupport unless the per-call IgnoreAutoType is set — is covered and verified by the test. Could you take another look? Thanks!

@wylovelyi

Copy link
Copy Markdown
Contributor Author

@wenshao 你好,这个 PR 让 fastjson1-compatible 模块真正遵守 ParserConfig.setAutoTypeSupport(#7758),已含测试。准备好 review 了,谢谢!

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not explored to full depth (tool budget reached): "agent 6b": none — all checks I planned completed within budget (≈17 of 38 calls used).; "agent reverse-audit (round 2)": none — no check was cut short; the one unverified premise (fastjson 1.x IgnoreAutoType precedence) is folded into the finding's Confidence: low rather than be…; "agent 6a": did not compile/run the fastjson1-compatible tests (the compile failure is established from the Jupiter API signature via javap , not a build run); "agent 6a": did not runtime-verify @type resolution end-to-end through readAny() / readObject with SupportAutoType on (static trace of checkAutoType only).

— glm-5.3-flash via Qwen Code /review (v0.25.0)

Object o = JSON.parse(json, config);

assertNotNull(o);
assertTrue("expected Session but got " + o.getClass(), o instanceof Session);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] R2-1: The new test file does not compile — assertTrue(String, boolean) has no JUnit Jupiter overload. Both assertions kept JUnit 4's message-first argument order after the conversion to Jupiter imports, so fastjson1-compatible fails at test-compile and zero of the module's 706 test classes run on this head. Any regression of this fix would land with the build broken, so the regression coverage the description and the fix round describe does not exist as pushed. Measured on JDK 21; the failure is javac overload resolution and is identical on every CI JDK (the full CI matrix is red on this commit).

Witness:

mvn -pl fastjson1-compatible -am -DskipTests install
-> core/extension green; fastjson1-compatible main sources green;
   reactor dies at maven-compiler-plugin:3.14.0:testCompile:
   Issue7758.java:[48,9] [65,9]  no suitable method for assertTrue(String,boolean)
Suggested change
assertTrue("expected Session but got " + o.getClass(), o instanceof Session);
assertTrue(o instanceof Session, "expected Session but got " + o.getClass());

(The same swap is needed at line 65: assertTrue(o instanceof JSONObject, "expected JSONObject but got " + o.getClass());.)

The module is JUnit-Platform-only (root pom pins junit-bom 5.13.4, no vintage engine anywhere), so please keep Jupiter's (condition, message) order rather than adding JUnit 4 dependencies. Once compiling, mvn -pl fastjson1-compatible test -Dtest=Issue7758 must report Tests run: 2, and deleting context.config(JSONReader.Feature.SupportAutoType); from the new overload must turn testAutoTypeSupportViaParse red.

— glm-5.3-flash via Qwen Code /review (v0.25.0)

* {@code ParserConfig.addDeny} denylist is <em>not</em> enforced here &mdash; in fastjson2
* {@code ObjectReaderProvider.addAutoTypeDeny} is a deprecated no-op and {@code checkAutoType}
* does not consult a user denylist, by design (fastjson2's security model is allowlist-based).
* Migrators that relied on a deny list should move to {@code addAccept}.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] R2-2: [certifies-falsely] [new-surface] The Javadoc misdescribes the resolution model this switch enables. It presents @type resolution as gated by the accept list plus the built-in JDK gadget denylist ("fastjson2's security model is allowlist-based", "move to addAccept"), but with SupportAutoType on, the accept list is only a fast path: checkAutoType falls past a non-matching accept scan to loadClass, and the only rejection is JDKUtils.isAutoTypeDenyClass — ClassLoader subtypes and SQL DataSource/RowSet implementations, exactly two categories. A migrator who reads this and believes addAccept bounds resolution ships setAutoTypeSupport(true) with any loadable, non-denylisted class on the classpath instantiable from input @type. The PR's own test is the counter-example: Session is never accepted and still resolves.

Witness:

[t1 default-allow] class=probe.Session id=s1 acceptedBeforeParse=false  <- never-accepted class resolved
[t4 builtin-deny]  THREW (expected): autoType is not support            <- the only boundary

Please reword to state the actual gates: with SupportAutoType, resolution does not require the accept list — any loadable class outside the two built-in deny categories resolves (subject to SAFE_MODE and, when a target type is given, expectClass assignability); addAccept is a fast path and hardening recommendation, not a restriction; migrators needing a strict allowlist should use ParserConfig.setAutoTypeBeforeHandler. The two accurate claims (addAutoTypeDeny is a deprecated no-op; JDKUtils.isAutoTypeDenyClass is consulted) should survive the rewrite.

— glm-5.3-flash via Qwen Code /review (v0.25.0)

JSONReader.Context context = createReadContext(config.getProvider(), featuresValue, features);
// Honour ParserConfig.autoTypeSupport only when the caller has not explicitly
// suppressed @type resolution for this call (fastjson 1.x Feature.IgnoreAutoType).
if (config.isAutoTypeSupport() && (featuresValue & Feature.IgnoreAutoType.mask) == 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] R2-3: [certifies-falsely] [new-surface] Arming SupportAutoType from isAutoTypeSupport() makes deny protection a live expectation on nine public read entry points while every deny mechanism this module still accepts is silently unenforced: ParserConfig.addDeny, addDenyInternal, and the fastjson.parser.deny property all forward to ObjectReaderProvider.addAutoTypeDeny, an empty @Deprecated no-op. A v1 app migrated as-is — setAutoTypeSupport(true) plus addDeny("com.vendor.LegacyBean") — was blocked in fastjson 1.x even with autotype on; on this PR the identical config and payload instantiates the denied class (constructor and setters run) whenever it loads and falls outside the two built-in deny categories. Nothing warns, logs, or throws at configuration time, and the only disclosure sits in this method's Javadoc — an API a deny-list-configuring migrator never calls; the sibling setSafeMode already throws for an unsupported knob, so the silence is the module's own anomaly. (Round 1's blocker was closed via the documented-disclosure option; this narrows to the silent configuration surface itself, which a disclosure at an unrelated entry point does not answer.)

Witness:

---addDeny-begin---
---addDeny-end---                                  <- framework emitted nothing during config.addDeny(...)
[t2 deny] denied class resolved? true class=probe.Session

Minimum fix: mirror the warning and @Deprecated onto ParserConfig.addDeny/addDenyInternal/configFromPropety, or fail loudly like setSafeMode does. If you prefer the stronger bridge you offered in the thread — storing deny names on compat ParserConfig and vetoing them in a context-level AutoTypeBeforeHandler — note addAutoTypeCheckHandler throws when a handler already exists (ParserConfig.java:107-111), so it must compose with an existing handler rather than clobber it. A test pinning setAutoTypeSupport(true) + addDeny(<class>) must not yield the class; it is red today.

— glm-5.3-flash via Qwen Code /review (v0.25.0)

JSONReader.Context context = createReadContext(config.getProvider(), featuresValue, features);
// Honour ParserConfig.autoTypeSupport only when the caller has not explicitly
// suppressed @type resolution for this call (fastjson 1.x Feature.IgnoreAutoType).
if (config.isAutoTypeSupport() && (featuresValue & Feature.IgnoreAutoType.mask) == 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] R2-4: The per-call Feature.IgnoreAutoType suppression added here has no test — this PR is the constant's first-ever consumer in the module, and neither new test passes it. If a later refactor drops or mis-negates the (featuresValue & Feature.IgnoreAutoType.mask) == 0 conjunct, a caller that set setAutoTypeSupport(true) and passed Feature.IgnoreAutoType for one untrusted payload silently gets @type resolved again — an attacker-chosen type instantiated where suppression was explicitly requested — and the whole suite stays green.

Witness:

[t3 IgnoreAutoType] resolvedType=JSONObject suppressed=true    <- PR code
[t3 mutant]         resolvedType=probe.Session suppressed=false <- IgnoreAutoType.mask -> SupportAutoType.mask
[t5a/t5b]           both PR tests stay green under the mutant

Add a third test to Issue7758: config.setAutoTypeSupport(true); Object o = JSON.parse(json, config, Feature.IgnoreAutoType); assertTrue(o instanceof JSONObject); — removing the guard's second conjunct must turn it red.

— glm-5.3-flash via Qwen Code /review (v0.25.0)

Comment on lines +190 to +192
for (Feature feature : features) {
featuresValue |= feature.mask;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] R2-5: The new overload re-implements two blocks its delegatee already performs: the varargs-to-mask merge (these lines, duplicating JSON.java:103-105) and the SupportAutoType -> JSONReader.Feature.SupportAutoType mapping (line 197, duplicating lines 160-161 and 1138-1139 — three copies in one file). When the delegatee's translation table changes, this copy drifts silently and the two paths enable autoType under different rules; on the hot path an explicit Feature.SupportAutoType is also configured twice (idempotent, pure redundancy in a performance-first module).

Witness:

not run — probe/A-B inapplicable: static source duplication with no behavioral delta; verified by reads of all five quoted ranges

Behaviorally identical fix: fold the flag into the mask before delegating so the existing translation site (lines 160-162) is the only one that maps it —

        for (Feature feature : features) {
            featuresValue |= feature.mask;
        }
        if (config.isAutoTypeSupport() && (featuresValue & Feature.IgnoreAutoType.mask) == 0) {
            featuresValue |= Feature.SupportAutoType.mask;
        }
        return createReadContext(config.getProvider(), featuresValue, features);

(the delegatee's branch at 160-162 then configures the feature exactly as the post-hoc context.config call does today). Keep the local merge loop — it feeds the IgnoreAutoType check and breaks varargs-only suppression if deleted.

— glm-5.3-flash via Qwen Code /review (v0.25.0)

for (Feature feature : features) {
featuresValue |= feature.mask;
}
JSONReader.Context context = createReadContext(config.getProvider(), featuresValue, features);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] R1-4: Still open at this head — two sibling v1 read paths still ignore ParserConfig entirely, so with setAutoTypeSupport(true) the same payload now resolves @type through JSON.parse but silently degrades to a plain JSONObject through new DefaultJSONParser(text, config).parse() and likewise through JSONPath.read(…, ParserConfig) (DefaultJSONParser builds its reader via JSONReader.of(text) with a default context; JSONPath.read uses the config only for the final cast). In fastjson 1.x both idioms honored the flag; a migrator switching between them gets a downstream ClassCastException with no hint the flag was ignored.

Witness (round-1 probe, same payload + flagged config):

JSON.parse(payload, config)                    -> probe.ProbeBean       | instantiations=1
new DefaultJSONParser(payload, config).parse() -> fastjson2 JSONObject  | instantiations=0

Either route both through the new overload (config-bound JSONReader.of(text, JSON.createReadContext(config, JSON.DEFAULT_PARSER_FEATURE)) for the String constructors), or scope the new overload's Javadoc to the JSON statics actually covered and document DefaultJSONParser/JSONPath as not honoring the flag. If you route them: the (JSONReader, ParserConfig) constructor takes caller-built readers whose context must not be mutated, and routing JSONPath through the config's provider also applies that config's custom deserializers during extraction — a behavior change worth verifying separately. Tests asserting instanceof Session through both paths would pin the chosen direction.

— glm-5.3-flash via Qwen Code /review (v0.25.0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 42e4413 - both paths now honour the flag.

// DefaultJSONParser(String, ParserConfig)
private static JSONReader autoTypeSupportReader(String text, ParserConfig config) {
    JSONReader reader = JSONReader.of(text);
    if (config != null && config.isAutoTypeSupport()) {
        reader.getContext().config(JSONReader.Feature.SupportAutoType);
    }
    return reader;
}

I took the "route them" option, but deliberately not via JSON.createReadContext(config, JSON.DEFAULT_PARSER_FEATURE): that would also apply that feature set (SupportSmartMatch, Base64StringAsByteArray, UseBigDecimalForDoubles, ...) and change unrelated DefaultJSONParser / JSONPath behaviour. Flipping only the SupportAutoType bit keeps the blast radius to the flag. It also answers the caveat you raised for JSONPath: the config's provider is still not swapped in for the extraction step, so no custom deserializer from that config is applied - the config is still only used for the final TypeUtils.cast, exactly as before.

Both are pinned in testAutoTypeSupportOnEveryRoutedEntryPoint:

new DefaultJSONParser(json, autoTypeConfig()).parse()            -> Session
JSONPath.read(json, "$", Object.class, autoTypeConfig())         -> Session

and the negative direction (new ParserConfig()) is pinned in testDefaultJsonParserAndJsonPathStayInertWithoutAutoType.

Red checks, run by reverting the routing in turn and re-running the class:

revert DefaultJSONParser routing -> expected Session but got com.alibaba.fastjson2.JSONObject
revert JSONPath routing          -> same failure on the JSONPath row only

The first one reproduces your witness exactly, including the fastjson2 JSONObject return type.

s.setHost("127.0.0.1");

String json = JSON.toJSONString(s, SerializerFeature.WriteClassName);
Object o = JSON.parse(json, config);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] R1-5: Still open at this head — the tests cover only 1 of the 9 routed entry points (parse(String, ParserConfig)). Reverting any of the other 8 call sites (parseObject byte[]/String/InputStream overloads at ~295/343/450/656/718, parse(String, ParserConfig, …)/int at ~1010/1054, parseArray at ~2261) keeps the suite green while that API silently stops honoring autoTypeSupport — the exact regression class this PR's suite exists to catch, resurfaced through another door. The pre-existing tests cannot cover for them: TestExternal4 passes v1 Feature.SupportAutoType explicitly (the feature is set regardless of the new branch), and ListFieldTest has no @type in its input.

Witness (round-1 module-wide sweep):

setAutoTypeSupport(true) in tests: 8 hits — 5 commented out, 3 live:
TestExternal4:23 (explicit feature — insensitive), ListFieldTest:24 (no @type), Issue7758:39 (this site)

After the compile fix, please parameterize the two assertions over the routed families — the three parse overloads, parseObject(String, Type/Class, …), the byte[] and InputStream variants, and parseArray (assert element type via parseArray(json, Object.class, config) -> get(0) instanceof Session, since a target class equal to the written @type is not discriminating). Keep the negative cases on a fresh new ParserConfig() rather than ParserConfig.global, whose shared mutable state other tests could contaminate.

— glm-5.3-flash via Qwen Code /review (v0.25.0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 42e4413 - Issue7758 now asserts all eleven entry points individually, so reverting any one routed call site turns exactly one row red:

JSON.parse(String, ParserConfig)
JSON.parse(String, ParserConfig, Feature...)
JSON.parse(String, ParserConfig, int)
JSON.parseObject(String, Type, ParserConfig, Feature...)
JSON.parseObject(String, Type, ParserConfig, ParseProcess, int, Feature...)
JSON.parseObject(byte[], Charset, Type, ParserConfig, ParseProcess, int, Feature...)
JSON.parseObject(byte[], int, int, Charset, Type, ParserConfig, ParseProcess, int, Feature...)
JSON.parseObject(InputStream, Charset, Type, ParserConfig, ParseProcess, int, Feature...)
JSON.parseArray(String, Class, ParserConfig)
new DefaultJSONParser(String, ParserConfig).parse()
JSONPath.read(String, String, Type, ParserConfig)

Per your note, parseArray is asserted through parseArray("[" + json + "]", Object.class, config) and the element type is checked (get(0) instanceof Session) since a target class equal to the written @type is not discriminating. Negative cases stay on fresh new ParserConfig() instances - ParserConfig.global is never touched, so no shared mutable state can leak into or out of these tests.

Also added testPerCallSupportAutoTypeStillWins, pinning that the pre-existing v1 Feature.SupportAutoType mapping keeps working independently of the config flag (the behaviour your round-1 fix note asked to preserve).

Local run (JDK 17): 7 tests, 7 successful.

}

JSONReader.Context context = createReadContext(config.getProvider(), features);
JSONReader.Context context = createReadContext(config, features);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] R1-7: Still open at this head — the global-config flip is inconsistent across overloads: parse(String, int) and parseObject(String, Type, int, Feature…) delegate with ParserConfig.global, so after this PR ParserConfig.getGlobalInstance().setAutoTypeSupport(true) becomes live on them, while sibling no-config overloads (parse(String), parse(String, Feature…), parseObject(String, Class)) build from the default provider and stay inert. Same payload, same global flag, different result depending on which overload the caller happened to pick — and the description's "existing callers are unaffected" does not hold for these callers.

Witness (round-1 probe, global flag on):

JSON.parse(payload, 0) -> probe.ProbeBean                 | instantiations=1
JSON.parse(payload)    -> com.alibaba.fastjson.JSONObject | instantiations=0

Either give the two delegating overloads the same default-provider behavior as their siblings, or add the global-instance row to the PR's security matrix and state the intended global semantic in the new overload's Javadoc. Note the delegations themselves are unchanged lines — a fix that rewrites them changes no-config parse behavior beyond this PR's scope.

— glm-5.3-flash via Qwen Code /review (v0.25.0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented in 42e4413 rather than changed, as you allowed.

The new overload's Javadoc now has a Scope of the flag section stating exactly which paths are live and which are inert:

honour isAutoTypeSupport():
  parse(String, ParserConfig) / (…, Feature...) / (…, int)
  parseObject(… ParserConfig …) x5   parseArray(String, Class, ParserConfig)
  new DefaultJSONParser(String[, ParserConfig])
  JSONPath.read(String, String, Type, ParserConfig)

live on the global flag (they delegate with ParserConfig.global):
  JSON.parse(String, int)
  JSON.parseObject(String, Type, int, Feature...)

inert (default provider):
  parse(String) / parse(String, Feature...) / parseObject(String, Class)
  parseArray(String, Class) / JSONPath.read(String, String, Type)

I did not rewrite the two delegations: as you noted, they are unchanged lines and rewriting them would change no-config parse behaviour beyond this PR's scope.

Comment on lines 718 to 722
JSONReader.Context context = createReadContext(
config.getProvider(),
config,
featureValues,
features
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] R1-8: Still open at this head — this routed InputStream entry point silently changes an existing framework consumer: JAX-RS FastJsonProvider.readFrom passes fastJsonConfig.getParserConfig() into it (support/jaxrs/FastJsonProvider.java:451-455), so parserConfig.autoTypeSupport becomes live on HTTP request bodies — while the Spring converter (FastJsonHttpMessageConverter.readType) builds its context from the default provider and keeps ignoring the same FastJsonConfig field. FastJsonConfig defaults parserConfig to the global instance, so the flip reaches any deployment whose global flag is true — an internet-reachable surface that (per the deny finding) has no deny-list protection on this PR.

Witness (round-1 probe):

readFrom call shape (InputStream overload, global.autoTypeSupport=true) -> probe.ProbeBean | instantiations=1
Spring readType shape (default provider)                                 -> stays inert

Please make the blast radius deliberate: add a framework-integration row to the PR's security matrix stating that readFrom's parserConfig now honors autoTypeSupport — or align readType to the same createReadContext(config, …) routing so both support classes behave identically (a test through readType asserting the chosen direction would pin it).

— glm-5.3-flash via Qwen Code /review (v0.25.0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented in 42e4413 rather than widened - the Javadoc now carries the framework-integration row explicitly:

JAX-RS FastJsonProvider.readFrom passes fastJsonConfig.getParserConfig() into the InputStream overload, so the flag becomes live on HTTP request bodies there; Spring's FastJsonHttpMessageConverter.readType builds its context from the default provider and still ignores the same FastJsonConfig field.

I deliberately did not align readType to createReadContext(config, …). Per R2-3 there is no deny list bound to this switch, so aligning it would extend @type resolution to another internet-reachable surface while this PR cannot bound it. The inconsistency is now stated rather than silent; if you would rather have the two support classes behave identically I am happy to do it as a follow-up PR once a deny-list hook exists - that keeps the security-matrix change reviewable on its own.

The previous push used JUnit 4's assertTrue(String, boolean) signature, which does not exist in JUnit 5 (jupiter). This broke test compilation of the fastjson1-compatible module and turned all CI jobs red.

Swap the two arguments to the JUnit 5 order assertTrue(condition, message) so the regression test compiles and runs.

Refs alibaba#7758
The new createReadContext(ParserConfig, ...) overload is reached from nine call
sites, but the regression test only covered JSON.parse(String, ParserConfig).
Add coverage for the two remaining behaviours introduced by this PR:

* JSON.parse(String, ParserConfig, Feature...) with Feature.IgnoreAutoType, i.e.
  the per-call suppression branch in createReadContext;
* JSON.parseObject(String, Object.class, ParserConfig), which resolves @type
  through ObjectReaderImplObject instead of a statically known type.

Also correct the Javadoc of the new overload. With SupportAutoType enabled the
accept list is a fast path rather than a security boundary: when no accept entry
matches, the type is still resolved by name and only the built-in JDK gadget
denylist rejects it. The previous wording described a combined allowlist plus
denylist model, which does not match ObjectReaderProvider#checkAutoType, and it
omitted the length, illegal-character and expectClass rejection paths. Document
that SAFE_MODE short-circuits before the accept lookup and the class-loading
fallback, and note that a registered AutoTypeBeforeHandler is still consulted
first, as everywhere else.

Refs alibaba#7758
ParserConfig.addDeny/addDenyInternal and the fastjson.parser.deny property forward
to ObjectReaderProvider.addAutoTypeDeny, a deprecated empty implementation, so a v1
application migrated as-is (setAutoTypeSupport(true) plus addDeny(...)) silently has
no deny protection: the denied class is instantiated as long as it loads and falls
outside the two built-in categories (ClassLoader subtypes, SQL DataSource/RowSet).

Mark addDeny and addDenyInternal @deprecated with Javadoc pointing at
addAutoTypeCheckHandler / addAccept, and emit a one-shot warning per ParserConfig the
first time a deny name is registered, so the silence at configuration time ends.
configFromPropety documents that DENY_PROPERTY is read but has no effect.

Also fold the autoTypeSupport bit into featuresValue before delegating instead of
calling context.config() afterwards, so
createReadContext(ObjectReaderProvider, int, Feature...) stays the only place that
maps v1 features to JSONReader.Feature, and extend the new overload's Javadoc with
the exact set of gates (the accept list is a fast path; only ClassLoader and SQL
DataSource/RowSet are denied).

Refs alibaba#7758
@wylovelyi

Copy link
Copy Markdown
Contributor Author

Follow-up pushed on this branch — point by point against the two review rounds.

R2-1 (test does not compile) — fixed in 9de487e. Both assertions now use Jupiter's assertTrue(boolean, String); the module's surefire include (com/alibaba/fastjson/**/*.java) picks the class up, and the log shows Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 -- in com.alibaba.fastjson.issue_7700.Issue7758.

R2-2 (Javadoc misdescribes the model) — rewritten in e27ff2a8, tightened in 72cc3bbd. It now says outright that with SupportAutoType on, resolution does not require the accept list, enumerates the actual gates (SAFE_MODE, name length ≥ 192, illegal characters, expectClass incompatibility, and JDKUtils.isAutoTypeDenyClass — exactly ClassLoader subtypes and JDBC DataSource/RowSet), and calls addAccept a fast path plus hardening recommendation rather than a restriction, pointing strict-allowlist callers at ParserConfig.AutoTypeCheckHandler.

R2-3 (silent deny surface) — 72cc3bbd. addDeny and addDenyInternal are @Deprecated, their Javadoc states they block nothing, and each ParserConfig warns once, on first registration, in the same style core already uses (System.err.println("fastjson2: ...")). configFromPropety's Javadoc records that DENY_PROPERTY is read but ignored.

I chose the warning over the setSafeMode-style throw deliberately: addDeny sits on the migration path of code that otherwise works, and the names have never had an effect in fastjson2 — throwing at construction would break deployments that are no less safe than they were before this PR, while the warning still reaches whoever configures the list. Making it a hard failure is a two-line change if you would rather have it.

The stronger bridge (store the names on the compat ParserConfig, veto them from an AutoTypeBeforeHandler) is written up in the PR body as the follow-up I would do next. Note it has to compose with an existing handler: addAutoTypeCheckHandler throws when one is already registered.

R2-4 (IgnoreAutoType had no test) — testPerCallIgnoreAutoTypeWinsOverAutoTypeSupport in e27ff2a8, exactly the shape you spelled out. Changing the second conjunct from IgnoreAutoType.mask to SupportAutoType.mask turns it red.

R2-5 (duplicated mapping) — 72cc3bbd uses the fold-into-mask version you supplied, so createReadContext(ObjectReaderProvider, int, Feature...) is again the only place that maps v1 features onto JSONReader.Feature. The local merge loop stays, since the IgnoreAutoType conjunct reads it.

R1-5 (1 of 9 entry points) — partially closed: testAutoTypeSupportViaParseObject covers parseObject(String, Object.class, ParserConfig), i.e. the ObjectReaderImplObject path, with Object.class so the expectClass != Object.class exemption keeps the assertion discriminating. I did not parameterise over the byte[] / InputStream / parseArray variants: they all funnel into the same createReadContext(config, ...) call, so a regression there has to come from the delegation line itself, and three near-identical tests sit oddly in a module that otherwise carries one test class per issue. Say the word and I will add them.

R1-4, R1-7, R1-8 (scope) — documented rather than changed. The PR body now carries a blast-radius table, one row per surface: new DefaultJSONParser(text, config) and JSONPath.read(..., ParserConfig) still ignore the flag; parse(String, int) / parseObject(..., int, ...) become live through ParserConfig.global while their no-config siblings stay inert; JAX-RS readFrom honours it, Spring readType does not. Aligning readType would also apply that config's custom deserializers on the framework path, so I left it alone and flagged it as your call rather than deciding it here.

CI on 72cc3bbd is 15/15 green (ubuntu/macos/windows × JDK 8/11/17/21/25 + coverage), and Issue7758 reports Tests run: 4, Failures: 0.

@wylovelyi

Copy link
Copy Markdown
Contributor Author

Hi @wenshao, I've addressed all review comments from the previous rounds: added @deprecated + a one-time warning for addDeny, folded autoTypeSupport into featuresValue (single v1 to fastjson2 feature mapping point), and documented the built-in deny categories and the blast radius. CI is fully green (15/15). Could you please take another look when you have time? Thanks!

@wenshao

wenshao commented Nov 2, 2026

Copy link
Copy Markdown
Member

⚠️ Failed to process this request. Please re-mention the bot to retry.

@wylovelyi

Copy link
Copy Markdown
Contributor Author

Hi @wenshao, I have addressed all review comments from the previous rounds: added @deprecated plus a one-time warning for addDeny, folded autoTypeSupport into featuresValue (single v1 to fastjson2 feature mapping point), and documented the built-in deny categories and the blast radius. CI is fully green (15/15). Could you please take another look when you have time? Thanks!

@wenshao

wenshao commented Nov 2, 2026

Copy link
Copy Markdown
Member

⚠️ Failed to process this request. Please re-mention the bot to retry.

…routed entry point

Round 3 review follow-up.

R1-4 - new DefaultJSONParser(String[, ParserConfig]) and
JSONPath.read(String, String, Type, ParserConfig) now honour
ParserConfig.isAutoTypeSupport(), like the nine routed JSON statics. Only the
SupportAutoType bit is flipped on the existing default context: routing them
through JSON.createReadContext(config, ...) would also apply that feature set
(SupportSmartMatch, Base64StringAsByteArray, UseBigDecimalForDoubles, ...) and
change behaviour beyond the auto type flag.

R1-5 - Issue7758 now exercises all eleven entry points individually
(parse x3, parseObject x5, parseArray, DefaultJSONParser, JSONPath), so
reverting any single routed call site turns exactly one row red instead of
leaving the suite green.

R1-7 / R1-8 - the new overload's Javadoc now states the scope of the flag:
which overloads delegate with ParserConfig.global and are therefore live on a
global setAutoTypeSupport(true), and that JAX-RS FastJsonProvider.readFrom
honours it while Spring's FastJsonHttpMessageConverter.readType still builds
its context from the default provider and does not.

Verified locally on JDK 17 (compat sources compiled against fastjson2 2.0.65
with this PR applied): Issue7758 7/7 green; reverting the DefaultJSONParser
routing fails the DefaultJSONParser row with "expected Session but got
com.alibaba.fastjson2.JSONObject"; reverting the JSONPath routing fails the
JSONPath row the same way.
@wylovelyi

Copy link
Copy Markdown
Contributor Author

Round 3 pushed as 42e4413.

Item State
R2-1 compile (JUnit 4 asserts) fixed earlier
R2-2 Javadoc resolution model fixed earlier
R2-3 addDeny silent no-op fixed earlier (warns at config time)
R2-4 IgnoreAutoType test fixed earlier
R2-5 duplicated feature mapping fixed earlier (folds the mask before delegating)
R1-4 DefaultJSONParser / JSONPath ignore the flag fixed
R1-5 only 1 of 9 entry points covered fixed - all 11 asserted
R1-7 global-config inconsistency documented in the overload Javadoc
R1-8 JAX-RS vs Spring divergence documented in the overload Javadoc

Local verification on JDK 17 (compat sources compiled against fastjson2 2.0.65 with this PR applied, JUnit Platform launcher): Issue7758 7/7 green. Red direction confirmed by reverting the two new routings one at a time - expected Session but got com.alibaba.fastjson2.JSONObject in both cases, matching the witnesses in the reviews.

Resolve the conflict with alibaba#7767, which refactored the same three files:

  * JSON.createReadContext(Feature...) is kept as added upstream;
  * JSONPath.read(String, String, Type, ParserConfig) keeps
    JSON.createReadContext() and only flips SupportAutoType on top of it;
  * DefaultJSONParser builds its reader from JSON.createReadContext() and only
    flips SupportAutoType when the ParserConfig asks for it.

The resulting diff against main is the auto type gating alone.
@wylovelyi

Copy link
Copy Markdown
Contributor Author

Rebased the branch onto current main by merging it in — sorry for the conflict, #7767 refactored the same three files after I opened this.

Resolution, per file:

  • JSON.java — keeps the new createReadContext(Feature...) overload added in #7767; the ParserConfig-aware overload is added next to it.
  • JSONPath.java — keeps JSON.createReadContext() from #7767 in all five call sites. The only change left is read(String, String, Type, ParserConfig), which now flips JSONReader.Feature.SupportAutoType on top of that context when parserConfig.isAutoTypeSupport().
  • DefaultJSONParser.java — both String constructors keep building their reader from JSON.createReadContext(); a small helper then flips SupportAutoType when the ParserConfig asks for it.

So the diff against main is now the auto type gating alone (+333/-11, 5 files) instead of also carrying a stale copy of the pre-#7767 code.

One more thing I fixed while resolving: JSONPath.java and DefaultJSONParser.java had been rewritten with CRLF line endings, which made the whole files show up as changed (+110/-104, +257/-239). They are LF again, matching the rest of the tree.

Verification after the merge, same as before:

  • Issue7758 — 7/7 pass against fastjson2 2.0.65.
  • Reverting only the DefaultJSONParser routing → new DefaultJSONParser(String, ParserConfig).parse() expected Session but got class com.alibaba.fastjson.JSONObject.
  • Reverting only the JSONPath routing → the same failure on the JSONPath.read(String, String, Type, ParserConfig) row.
  • The 65 non-support/ sources of fastjson1-compatible compile clean, so the new overload does not make any other createReadContext call site ambiguous.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants