web-dev-qa-db-ja.com

JavaのJSONインジェクションのFortifyエラー

クライアントからSUBSCRIPTION_JSONを取得しています。これを文字列に変換し、gsonライブラリを使用してモデルオブジェクトに設定しています。 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.

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"
        }]
    }]
}
7
shrey mathuria

モデルオブジェクトに設定する前に、受け取ったjsonを検証して、予想されるコンテンツが正確に含まれていることを確認する必要があります。たとえば、フィールド/フォーマットのパターンを使用してjsonをチェックするバリデーターを実装できます。

0
JuliaVI

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..!!
3
Anil Kumar

1)「JsonSanitizer.sanitize(string)」を使用します。 (ここでメソッドをサニタイズするパラメーターはJSON入力です)

2)JsonSanitizer依存関係を使用するには、pom.xmlに以下のように追加できます。

<dependency>
    <groupId>com.mikesamuel</groupId>
    <artifactId>json-sanitizer</artifactId>
    <version>1.2.0</version>
</dependency>
0
Suchi