From eda681e73d2db016d43f5faeea9df111374395ed Mon Sep 17 00:00:00 2001 From: Scott Beddall Date: Fri, 23 Aug 2024 15:01:00 -0700 Subject: [PATCH] protect json comparer from invalid json --- .../Common/JsonComparer.cs | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/JsonComparer.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/JsonComparer.cs index 469007e5e36..2e237ec83df 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/JsonComparer.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/JsonComparer.cs @@ -1,4 +1,7 @@ +using System; using System.Collections.Generic; +using System.Linq.Expressions; +using System.Text; using System.Text.Json; namespace Azure.Sdk.Tools.TestProxy.Common @@ -7,12 +10,32 @@ public class JsonComparer { public static List CompareJson(byte[] json1, byte[] json2) { + var differences = new List(); + JsonDocument doc1; + JsonDocument doc2; + // Deserialize the byte arrays to JsonDocument - JsonDocument doc1 = JsonDocument.Parse(json1); - JsonDocument doc2 = JsonDocument.Parse(json2); + try + { + doc1 = JsonDocument.Parse(json1); + } + catch(Exception ex) + { + differences.Add($"Unable to parse the request json body. Content \"{Encoding.UTF8.GetString(json1)}.\" Exception: {ex.Message}"); + return differences; + } + + try + { + doc2 = JsonDocument.Parse(json2); + } + + catch (Exception ex) + { + differences.Add($"Unable to parse the record json body. Content \"{Encoding.UTF8.GetString(json2)}.\" Exception: {ex.Message}"); + return differences; + } - // Compare the JSON objects - var differences = new List(); CompareElements(doc1.RootElement, doc2.RootElement, differences, ""); return differences;