From ef474db99b35f2fe325195fb853627ce7c2e6336 Mon Sep 17 00:00:00 2001 From: "Scott Beddall (from Dev Box)" Date: Wed, 24 Apr 2024 11:02:30 -0700 Subject: [PATCH] BodyKeySanitizer doesn't handle it when no jtokens are parsed. fix that. --- .../Sanitizers/BodyKeySanitizer.cs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs index d3b1949a040..9ee1c28428c 100644 --- a/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs +++ b/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Sanitizers/BodyKeySanitizer.cs @@ -1,4 +1,4 @@ -using Azure.Sdk.Tools.TestProxy.Common; +using Azure.Sdk.Tools.TestProxy.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -65,28 +65,31 @@ public override string SanitizeTextBody(string contentType, string body) } - foreach (JToken token in jsonO.SelectTokens(_jsonPath)) + if (jsonO != null) { - // HasValues is false for tokens with children. We will not apply sanitization if that is the case. - if (!token.HasValues) + foreach (JToken token in jsonO.SelectTokens(_jsonPath)) { - var originalValue = token.Value(); - - // regex replacement does not support null - if (originalValue == null) + // HasValues is false for tokens with children. We will not apply sanitization if that is the case. + if (!token.HasValues) { - continue; - } + var originalValue = token.Value(); - var replacement = StringSanitizer.SanitizeValue(originalValue, _newValue, _regexValue, _groupForReplace); + // regex replacement does not support null + if (originalValue == null) + { + continue; + } - // this sanitizer should only apply to actual values - // if we attempt to apply a regex update to a jtoken that has a more complex type, throw - token.Replace(JToken.FromObject(replacement)); + var replacement = StringSanitizer.SanitizeValue(originalValue, _newValue, _regexValue, _groupForReplace); - if(originalValue != replacement) - { - sanitized = true; + // this sanitizer should only apply to actual values + // if we attempt to apply a regex update to a jtoken that has a more complex type, throw + token.Replace(JToken.FromObject(replacement)); + + if (originalValue != replacement) + { + sanitized = true; + } } } }