Skip to content

fix: enforce acceptNames in GenericFastJsonRedisSerializer - #7901

Open
Support-itecz wants to merge 2 commits into
alibaba:mainfrom
Support-itecz:redis-serializer-accept-list
Open

Support-itecz wants to merge 2 commits into
alibaba:mainfrom
Support-itecz:redis-serializer-accept-list

Conversation

@Support-itecz

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

GenericFastJsonRedisSerializer(String[] acceptNames) is the way to restrict which classes a Redis value may name in @type, but the accept list does not restrict anything. The constructor calls this(), which turns on JSONReader.Feature.SupportAutoType, and then adds a ContextAutoTypeBeforeHandler. The before-handler only short-circuits the types it accepts. For any other type it returns null, and the reader falls through to ObjectReaderProvider.checkAutoType with SupportAutoType on, which loads and instantiates any class outside the ClassLoader/DataSource/RowSet deny set.

With new GenericFastJsonRedisSerializer(new String[]{"com.example."}), deserializing {"@type":"evil.Gadget","cmd":"x"} returns an evil.Gadget and runs its setCmd. The same happens in JSONB mode. The serializers in extension-spring5, extension-spring6 and fastjson1-compatible all behave this way. Anyone who can write a cache entry (a shared Redis, or another service on the same keys) can instantiate arbitrary classes and invoke their setters in the reading application, even though it configured an accept list. JSONBCodec(String... autoTypes) in extension already uses the filter without SupportAutoType, which is the behavior the accept-list constructors were meant to have.

Summary of your change

  • When the accept list is non-empty, the spring5/spring6 serializers no longer set SupportAutoType, and they build the handler with includeBasic so the JDK collections, maps, dates and boxed types that WriteClassName emits still resolve. The fastjson1-compatible serializer passes Feature.SupportAutoType only when no accept list is configured. A (boolean, String[]) constructor is added to its ContextAutoTypeBeforeHandler wrapper. The no-arg and (boolean jsonb) constructors are unchanged.
  • In ObjectReaderImplMapTyped (String-key branch) and ObjectReaderImplMapString, the leading @type key is now consumed when a context before-handler is set, not only under SupportAutoType. The non-String-key branch of ObjectReaderImplMapTyped already did this. Without the change, a bean's Map<String, Object> field kept "@type":"java.util.HashMap" as an entry once autoType was driven by the filter alone.

I compared 36 value shapes (beans with Object/List/Map/Set/enum fields, HashMap, LinkedHashMap, TreeMap, HashSet, Arrays.asList, empty list, arrays, Date, BigDecimal, LocalDateTime, Optional) in JSON and JSONB. Each one deserializes to the same result under the old SupportAutoType configuration and under the new accept-list configuration. The new GenericFastJsonRedisSerializerAcceptListTest in each of the three modules fails before this change because the out-of-list class gets instantiated, and passes after it. AutoTypeFilterMapTypeKeyTest covers the map change. core (8289 tests), fastjson1-compatible (1369), extension-spring5 and extension-spring6 pass, the touched tests also pass with -Dfastjson2.creator=reflect, and mvn validate is clean. The two map readObject methods were already above the C2 FreqInlineSize of 325 bytes (about 1257 and 483) and grow by about 30 and 21 bytes. The added check runs only on the first key.

This fix was found and prepared with AI assistance and reviewed by me before submitting.

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.
中文

这个 PR 做了什么 / 为什么需要它?

GenericFastJsonRedisSerializer(String[] acceptNames) 用来限制 Redis 中的值可以通过 @type 指定哪些类,但这个白名单实际上不起作用。该构造函数先调用 this() 开启了 JSONReader.Feature.SupportAutoType,再加上 ContextAutoTypeBeforeHandler。before-handler 只对白名单中的类型直接返回;其他类型返回 null,随后 reader 会走到开启了 SupportAutoType 的 ObjectReaderProvider.checkAutoType,加载并实例化除 ClassLoader/DataSource/RowSet 之外的任意类。

使用 new GenericFastJsonRedisSerializer(new String[]{"com.example."}) 反序列化 {"@type":"evil.Gadget","cmd":"x"},得到的是 evil.Gadget 实例,并执行了它的 setCmd;JSONB 模式同样如此。extension-spring5、extension-spring6 和 fastjson1-compatible 中的序列化器都有这个问题。只要能写入缓存(共享 Redis,或使用同一批 key 的其他服务),即使读取方配置了白名单,也能在读取方实例化任意类并调用其 setter。extension 中的 JSONBCodec(String... autoTypes) 已经只使用过滤器而不开启 SupportAutoType,这正是白名单构造函数应有的行为。

改动概要

  • 白名单非空时,spring5/spring6 序列化器不再开启 SupportAutoType,并以 includeBasic 构建 handler,保证 WriteClassName 输出的 JDK 集合、Map、日期和包装类型仍能解析。fastjson1-compatible 序列化器只在未配置白名单时传入 Feature.SupportAutoType,并为其 ContextAutoTypeBeforeHandler 包装类增加 (boolean, String[]) 构造函数。无参构造函数和 (boolean jsonb) 构造函数不变。
  • ObjectReaderImplMapTyped(String key 分支)和 ObjectReaderImplMapString 在配置了 context before-handler 时也会消费开头的 @type 键,而不仅限于 SupportAutoType;ObjectReaderImplMapTyped 的非 String key 分支本来就是这样处理的。否则仅靠过滤器做 autoType 时,bean 的 Map<String, Object> 字段会多出一个 "@type":"java.util.HashMap" 条目。

我在 JSON 和 JSONB 下对比了 36 种值形态(包含 Object/List/Map/Set/枚举字段的 bean、HashMap、LinkedHashMap、TreeMap、HashSet、Arrays.asList、空列表、数组、Date、BigDecimal、LocalDateTime、Optional),在原来的 SupportAutoType 配置和新的白名单配置下,每一种的反序列化结果都相同。三个模块中新增的 GenericFastJsonRedisSerializerAcceptListTest 在修改前失败(白名单外的类被实例化),修改后通过;AutoTypeFilterMapTypeKeyTest 覆盖 Map 的改动。core(8289 个测试)、fastjson1-compatible(1369 个)、extension-spring5 和 extension-spring6 全部通过,相关测试在 -Dfastjson2.creator=reflect 下也通过,mvn validate 无告警。两个 Map 的 readObject 方法改动前就已超过 C2 FreqInlineSize 的 325 字节(约 1257 和 483),分别增加约 30 和 21 字节;新增判断只在第一个 key 上执行。

Issue283 registers a global ObjectWriter for Long that renders longs as
strings, so acceptedTypeRoundTrip only round-tripped a long back to a Long
when it ran before Issue283. Surefire's filesystem run order put it after on
ubuntu, which failed the lane. Use int values instead.
@Support-itecz

Copy link
Copy Markdown
Contributor Author

Pushed a fix for the red ubuntu lanes. The new test's round-trip used a long, and Issue283 in extension-spring5 registers a global ObjectWriter for Long that writes longs as strings, so the assertion only held when my test ran first. Surefire's filesystem order puts it after Issue283 on ubuntu but before on macOS and Windows, which is why only those five lanes plus coverage failed. The test now uses int values; the fix itself is unchanged. Verified locally with -Dsurefire.runOrder=reversealphabetical, which reproduces the ubuntu ordering: extension-spring5 101/101 in both creator modes, extension-spring6 63/63, fastjson1-compatible 1369/1369, and mvn validate clean.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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