Skip to content

Commit

Permalink
Add fix and tests (#5664)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoEchevarria authored and andrewlock committed Jun 14, 2024
1 parent 92cc9eb commit a489d7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
16 changes: 13 additions & 3 deletions tracer/src/Datadog.Trace/AppSec/ObjectExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using Datadog.Trace.AppSec.Waf;
Expand Down Expand Up @@ -56,13 +57,22 @@ internal static class ObjectExtractor

private static IReadOnlyDictionary<string, object?> ExtractProperties(object body, int depth, HashSet<object> visited)
{
if (visited.Contains(body))
try
{
if (visited.Contains(body))
{
return EmptyDictionary;
}

visited.Add(body);
}
catch
{
// Contains and Add call GetHashCode which can throw an exception if has a custom implementation
// If visited is empty, we could potentially get the exception only when calling Add
return EmptyDictionary;
}

visited.Add(body);

if (Log.IsEnabled(LogEventLevel.Debug))
{
Log.Debug("ExtractProperties - body: {Body}", body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ public void TestCyclicDictionary()
Assert.Empty(result);
}

[Fact]
public void TestNullValues()
{
TestObject testObject = new TestObject();
testObject.Test = new();
var result = ObjectExtractor.Extract(testObject);
result.Should().NotBeNull();
}

private static void PopulateNestedTarget(TestNestedPropertiesPoco target, int count)
{
var current = target;
Expand Down Expand Up @@ -534,4 +543,16 @@ public class TestNestedDictionaryPoco
{
public Dictionary<string, TestNestedDictionaryPoco> TestDictionary { get; set; } = new Dictionary<string, TestNestedDictionaryPoco>();
}

public class TestObject
{
public TestObject Test { get; set; }

public object Prop { get; set; }

public override int GetHashCode()
{
return Test.GetHashCode() + Prop.GetHashCode();
}
}
}

0 comments on commit a489d7b

Please sign in to comment.