java - Java 中 JSON 注入(inject)的强化错误

标签 java json security gson fortify

我正在从客户端获取 SUBSCRIPTION_JSON,我将其转换为字符串,然后使用 gson 库将其设置为模型对象。在 Fortify security 上运行代码时,它在下面的代码中给我 Json 注入(inject)错误,并显示以下消息:

这是错误:

On line 159 of ActionHelper.java, the method jsonToObject() writes unvalidated input into JSON. This call could allow an attacker to inject arbitrary elements or attributes into the JSON entity.The method writes unvalidated input into JSON. This call could allow an attacker to inject arbitrary elements or attributes into the JSON entity.

Explanation
JSON injection occurs when:

1. Data enters a program from an untrusted source.

In this case the data enters at getString() in **SubscriptionAction.java** at line 355.


2. The data is written to a JSON stream.

In this case the JSON is written by fromJson() in **ActionHelper.java** at line 159.

SubscriptionAction.java

final String subscriptionJson = subscriptionForm.getString(SUBSCRIPTION_JSON);

ActionHelper.java

public static <T> T jsonToObject(final String jsonString, final Class<T> className) {
        T object = null;
        if (StringUtils.isNotBlank(jsonString)) {
            final Gson gson = (Gson) BeanLocator.getInstance().getBean(GSON);
            object = gson.fromJson(jsonString, className);
        }
        return object;
    }

SUBSCRIPTION_JSON ->

{
    "subscriptions": [{
        "attributeId": "1",
        "items": [{
            "strId": "ALL",
            "nodeType": "G"
        }, {
            "strId": "VO_ENTRY_TIMING_DELAY",
            "nodeType": "L"
        }, {
            "strId": "O_INVALID",
            "nodeType": "L"
        }, {
            "strId": "O_LINE_INVALID",
            "nodeType": "L"
        }, {
            "strId": "V_INVALID",
            "nodeType": "L"
        }, {
            "strId": "V_ADDRESS_INVALID",
            "nodeType": "L"
        }]
    }, {
        "attributeId": "2001",
        "items": [{
            "strId": "OSTBU",
            "nodeType": "L"
        }]
    }]
}

最佳答案

在将 JSON 转换为 java 对象之前,您必须对其进行清理。这是经过测试的解决方案,它删除了这个强化警告。

<dependency>
        <groupId>com.mikesamuel</groupId>
        <artifactId>json-sanitizer</artifactId>
        <version>1.0</version>
</dependency>

InputStream responseBodyAsStream = null;
responseString = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
String wellFormedJson = com.google.json.JsonSanitizer.sanitize(responseString);

Map map = mapper.readValue(wellFormedJson, Map.class);

Hope this helps..!!

关于java - Java 中 JSON 注入(inject)的强化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49800113/

相关文章:

java - keytool - 查看公钥和私钥

.net - 我遗漏了一些关于哈希值的有用性

java - 如何在 Java 中关闭隐式流?

java - 尝试将json数据放入viewlist php+json+java时出现android错误

java - 在struts的索引属性中添加新项目

php - 在php中将json响应写入mysql数据库

json - Angular : Pass JSON Parameter to Controller for filtering

node.js - NodeJS 如何保护不同国家/地区的 socket.io session

java - 从同一地理点绘制相等的路径

java - Android 图像 GridView 消耗所有内存并崩溃