Skip to content

Commit

Permalink
Removed the dependency on Json.Net and stopped flatting and unflattin…
Browse files Browse the repository at this point in the history
…g the Json

Now it just finds the value using JsonPath and generates a JsonPatch to update the value with a redacted value
  • Loading branch information
Thomas Anderson committed Dec 4, 2024
1 parent 245435a commit d9c114f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
14 changes: 8 additions & 6 deletions Btms.SensitiveData/Btms.SensitiveData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonFlatten" Version="1.0.4" />
<Compile Remove="Flattener\**" />
<EmbeddedResource Remove="Flattener\**" />
<None Remove="Flattener\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="JsonPatch.Net" Version="3.2.2" />
<PackageReference Include="JsonPath.Net" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Btms.Common\Btms.Common.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Flattener\" />
</ItemGroup>

</Project>
58 changes: 33 additions & 25 deletions Btms.SensitiveData/SensitiveDataSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using JsonFlatten;
using Btms.Common.Extensions;
using Json.Patch;
using Json.Path;
using Json.Pointer;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace Btms.SensitiveData;

Expand Down Expand Up @@ -45,43 +46,50 @@ public T Deserialize<T>(string json, Action<JsonSerializerOptions> optionsOverri
}

}

public string RedactRawJson(string json, Type type)
{
if (options.Value.Include)
{
return json;
}
var sensitiveFields = SensitiveFieldsProvider.Get(type);

var jObject = JObject.Parse(json);

var fields = jObject.Flatten();

foreach (var field in sensitiveFields)
var rootNode = JsonNode.Parse(json);
foreach (var sensitiveField in sensitiveFields)
{
if (fields.TryGetValue(field, out var value))
var jsonPath = JsonPath.Parse($"$.{sensitiveField}");
var result = jsonPath.Evaluate(rootNode);

foreach (var match in result.Matches)
{
if (!options.Value.Include)
JsonPatch patch;
if (match.Value is JsonArray jsonArray)
{
fields[field] = options.Value.Getter(value.ToString()!);
var redactedList = jsonArray.Select(x =>
{
var redactedValue = options.Value.Getter(x?.GetValue<string>()!);
return redactedValue;
}).ToJson();

patch = new JsonPatch(PatchOperation.Replace(JsonPointer.Parse($"{match.Location!.AsJsonPointer()}"), JsonNode.Parse(redactedList)));
}
}
else
{
for (int i = 0; i < fields.Keys.Count; i++)
else
{
var key = fields.Keys.ElementAt(i);
var replaced = Regex.Replace(key, "\\[.*?\\]", "", RegexOptions.NonBacktracking);
if (replaced == field && fields.TryGetValue(key, out var v) && !options.Value.Include)
{
fields[key] = options.Value.Getter(v.ToString()!);
}
var redactedValue = options.Value.Getter(match.Value?.GetValue<string>()!);
patch = new JsonPatch(PatchOperation.Replace(JsonPointer.Parse(match.Location!.AsJsonPointer()), redactedValue));
}


var patchResult = patch.Apply(rootNode);
if (patchResult.IsSuccess)
{
rootNode = patchResult.Result;
}
}
}

var redactedString = fields.Unflatten().ToString();
return redactedString;
return rootNode!.ToJsonString();
}
}

0 comments on commit d9c114f

Please sign in to comment.