1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 import com.code_intelligence.jazzer.api.FuzzedDataProvider; 18 import com.code_intelligence.jazzer.api.FuzzerSecurityIssueLow; 19 20 import com.google.gson.Gson; 21 import com.google.gson.JsonElement; 22 import com.google.json.JsonSanitizer; 23 24 public class ValidJsonFuzzer { 25 private static Gson gson = new Gson(); 26 fuzzerTestOneInput(FuzzedDataProvider data)27 public static void fuzzerTestOneInput(FuzzedDataProvider data) { 28 String input = data.consumeRemainingAsString(); 29 String output; 30 try { 31 output = JsonSanitizer.sanitize(input, 10); 32 } catch (ArrayIndexOutOfBoundsException e) { 33 // ArrayIndexOutOfBoundsException is expected if nesting depth is 34 // exceeded. 35 return; 36 } 37 38 // Check that the output is valid JSON. Invalid JSON may crash other parts 39 // of the application that trust the output of the sanitizer. 40 try { 41 Gson gson = new Gson(); 42 gson.fromJson(output, JsonElement.class); 43 } catch (Exception e) { 44 throw new FuzzerSecurityIssueLow("Output is invalid JSON", e); 45 } 46 } 47 } 48