Skip to content

Commit

Permalink
BodyKeySanitizer doesn't handle it when no jtokens are parsed. fix that.
Browse files Browse the repository at this point in the history
  • Loading branch information
scbedd committed Apr 24, 2024
1 parent 077bb4f commit ef474db
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string>();

// 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<string>();

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;
}
}
}
}
Expand Down

0 comments on commit ef474db

Please sign in to comment.