Repository navigation
fix: enforce acceptNames in GenericFastJsonRedisSerializer - #7901
Support-itecz wants to merge 2 commits into
Conversation
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.
|
Pushed a fix for the red ubuntu lanes. The new test's round-trip used a |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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 callsthis(), which turns onJSONReader.Feature.SupportAutoType, and then adds aContextAutoTypeBeforeHandler. The before-handler only short-circuits the types it accepts. For any other type it returnsnull, and the reader falls through toObjectReaderProvider.checkAutoTypewithSupportAutoTypeon, 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 anevil.Gadgetand runs itssetCmd. The same happens in JSONB mode. The serializers inextension-spring5,extension-spring6andfastjson1-compatibleall 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)inextensionalready uses the filter withoutSupportAutoType, which is the behavior the accept-list constructors were meant to have.Summary of your change
SupportAutoType, and they build the handler withincludeBasicso the JDK collections, maps, dates and boxed types thatWriteClassNameemits still resolve. The fastjson1-compatible serializer passesFeature.SupportAutoTypeonly when no accept list is configured. A(boolean, String[])constructor is added to itsContextAutoTypeBeforeHandlerwrapper. The no-arg and(boolean jsonb)constructors are unchanged.ObjectReaderImplMapTyped(String-key branch) andObjectReaderImplMapString, the leading@typekey is now consumed when a context before-handler is set, not only underSupportAutoType. The non-String-key branch ofObjectReaderImplMapTypedalready did this. Without the change, a bean'sMap<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 oldSupportAutoTypeconfiguration and under the new accept-list configuration. The newGenericFastJsonRedisSerializerAcceptListTestin each of the three modules fails before this change because the out-of-list class gets instantiated, and passes after it.AutoTypeFilterMapTypeKeyTestcovers the map change.core(8289 tests),fastjson1-compatible(1369),extension-spring5andextension-spring6pass, the touched tests also pass with-Dfastjson2.creator=reflect, andmvn validateis clean. The two mapreadObjectmethods were already above the C2FreqInlineSizeof 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:
中文
这个 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,这正是白名单构造函数应有的行为。改动概要
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方法改动前就已超过 C2FreqInlineSize的 325 字节(约 1257 和 483),分别增加约 30 和 21 字节;新增判断只在第一个 key 上执行。