简体   繁体   English

杰克逊序列化不包括字段

[英]Jackson Serialization excludes fields

First of all I want to give you a bit of context.首先,我想给你一些背景信息。

We have an interface A which is implemented by class B. Class B also extends abstract class C .我们有一个由类 B实现的接口A。类 B扩展了抽象类 C

What we usually return when the requests are called (rest GET endpoint) are the values in B and the values in all the classes that form the hierarchy (D extending B and so on).我们通常在调用请求时返回的(rest GET 端点)是 B 中的值以及构成层次结构的所有类中的值(D 扩展 B 等)。

B contains getters to access these values. B 包含用于访问这些值的getter These getters are NOT DEFINED in the A interface.这些 getter 未在 A 接口中定义。 But the response works, the values are converted into a JSON so there's no problem so far.但是响应有效,值被转换为 JSON,所以到目前为止没有问题。 Jackson is able to call the getters and create the JSON based on them Jackson 能够调用 getter 并基于它们创建 JSON

Now we're upgrading Jackson to a newer version from 2.6.7.现在我们将 Jackson 从 2.6.7 升级到更新的版本。 We tried 2.10.3 and upward.我们尝试了 2.10.3 及更高版本。

The problem is that those getters are not being serialized anymore .问题是这些 getter 不再被序列化 If we define them in the interface is ok but the risk of doing this in our context is high.如果我们在接口中定义它们是可以的,但是在我们的上下文中这样做的风险很高。

Does anyone know why with the newer versions of Jackson the getters which are not defined in the interface are ignored?有谁知道为什么在较新版本的 Jackson 中,未在接口中定义的 getter 会被忽略? Is there a configuration, tweak or something which can force its previous behavior?是否有配置,调整或可以强制其先前行为的东西?

Later Edit 1后来编辑 1

Important to note how the objects are created : Interface A obj = new Class C() or whichever is needed from the hierarchy;重要的是要注意对象是如何创建的接口 A obj = new Class C()或层次结构中需要的任何一个;

Later Edit 2后来编辑2

Maybe it helps also providing the information that Spring was updated from version 4.0.0 to 5.3.0.也许它还有助于提供 Spring 从 4.0.0 版更新到 5.3.0 版的信息。 The application uses Spring-mvc, Spring-core, it's a non-spring-boot application.该应用程序使用 Spring-mvc,Spring-core,它是一个非 spring-boot 应用程序。

The dependencies used:使用的依赖项:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${com.fasterxml.jackson.databind}</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${com.fasterxml.jackson.databind}</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jdk8</artifactId>
    <version>${com.fasterxml.jackson.version}</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${com.fasterxml.jackson.version}</version>
</dependency>

Back since 2018 or so the exploitation of serialization validation limitations in authenticity were being abused and led to vulnerability patches.早在 2018 年左右,序列化验证的真实性限制就被滥用并导致漏洞补丁。 These were called “serialization gadgets”;这些被称为“序列化小工具”; but by the time the vulnerability reports made it into public eye - many different serialization frameworks and libraries,including Jackson, had already distributed bundled packages with their installers.但是当漏洞报告进入公众视野时 - 许多不同的序列化框架和库,包括 Jackson,已经分发了与其安装程序捆绑在一起的软件包。

For Java - some example datatypes which may be exploitable may be:对于 Java - 一些可利用的示例数据类型可能是:

java.lang.Object
java.io.Serializable
java.util.Comparable

For Jackson - around 2.10.xa change to serialization was released, but I was unable to find a specific reference to patching Gadgets.对于 Jackson - 大约 2.10.xa 发布了对序列化的更改,但我无法找到修补小工具的具体参考。

Typically the resolution is to blacklist serialization of Classes which contain such datatypes.通常,解决方法是将包含此类数据类型的类的序列化列入黑名单。 Luckily, the developers often include a flag (like a ToS agreement) which you can specify to re-enable serialization of these Classes.幸运的是,开发人员通常包含一个标志(如 ToS 协议),您可以指定该标志以重新启用这些类的序列化。

Unfortunately, I last programmed in Java 5 years ago - but based on quick reading I imagine the "flag" would include some form of annotation decorator or xml config tag.不幸的是,我上一次用 Java 编程是在 5 年前——但基于快速阅读,我想“标志”将包含某种形式的annotation decorator或 xml 配置标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM