3

我打算使用 gson 的 fromJson() 方法来解析来自浏览器的字符串。这样做是否有任何潜在的漏洞?我要转换的数据类型比较简单,一个列表和一个布尔值。但是由于 gson 使用反射,我有什么需要注意的吗?

例如,对于较旧的 jvm(6.24 之前的版本),可以对整数使用 DOS 攻击,导致整数解析器挂起。

一些聪明的 json 可以导致 gson 开始加载它应该是的类吗?

4

3 回答 3

3

出于安全原因,Gson 项目开发人员建议不允许反序列化代码加载用户指定的类定义——应该仔细控制泛型类型事物的反序列化。

于 2011-12-08T20:19:18.253 回答
3

使用 Gson 需要注意的是您使用的是什么类型的构建器(请参阅自定义反序列化器/序列化器)

Gson 有另一个弱点,当您反序列化时(假设使用自定义的),您最好检查您传递的对象的类型(使用 instanceof)。

其他要点:Gson 会根据传递的类型自动转换变量。

IE。{ "var1":1 , "var2":"1"} 第一个将转换为整数,第二个将基于字符串,因此我会注意您的对象转换。

于 2011-12-06T16:42:13.620 回答
-2

It's not that hard to write a JSON parser, and any well-used open source version should be about as safe as one could hope for. Of course, the parser could contain a bug that makes it subject to buffer overrun and the like, but, again, the logic is simple enough that that shouldn't happen if the code is reasonably well written and well reviewed.

A bigger danger is that you yourself might not properly inspect the results of the parse and accept, say, a number that is out of range for your application, or a string that's too long.

于 2011-12-06T16:47:20.443 回答