Java中的JSON注入强化错误

shr*_*ria 7 java security json gson fortify

我从客户端获取SUBSCRIPTION_JSON,将其转换为String,然后使用gson库将其设置为Model Object。在Fortify安全性上运行代码时,它在以下代码上给我Json注入错误,并显示以下消息:

这是错误:

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.
Run Code Online (Sandbox Code Playgroud)

SubscriptionAction.java

final String subscriptionJson = subscriptionForm.getString(SUBSCRIPTION_JSON);
Run Code Online (Sandbox Code Playgroud)

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;
    }
Run Code Online (Sandbox Code Playgroud)

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"
        }]
    }]
}
Run Code Online (Sandbox Code Playgroud)

Ani*_*mar 6

在将 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..!!
Run Code Online (Sandbox Code Playgroud)


小智 -4

在将其设置为模型对象之前,您必须验证收到的 json 以确保它完全包含预期的内容。例如,您可以实现一个验证器,使用预期的字段/格式模式检查 json。