Skip to content

Commit

Permalink
Check for circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinZakharov committed Jun 14, 2024
1 parent 8459f29 commit 1100efd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ public void init() {
StackTraceCollection stackTraceCollection = ctx.transferStackTracesCollection();
if (stackTraceCollection != null) {
Object flatStruct = ObjectFlattener.flatten(stackTraceCollection);
traceSeg.setMetaStructTop("_dd.stack", flatStruct);
if (flatStruct != null) {
traceSeg.setMetaStructTop("_dd.stack", flatStruct);
}
}

} else if (hasUserTrackingEvent(traceSeg)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.datadog.appsec.util;

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.Moshi;

/**
Expand All @@ -18,9 +19,13 @@ public class ObjectFlattener {
*
* @param obj the object to flatten
* @return the flattened object as a Map, or the original object if it's a primitive type or a
* Collection or a Map. Returns null if the input object is null.
* Collection or a Map. Returns null if the input object is null or if it cannot be flattened.
*/
public static Object flatten(Object obj) {
return JSON_ADAPTER.toJsonValue(obj);
try {
return JSON_ADAPTER.toJsonValue(obj);
} catch (JsonDataException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ class ObjectFlattenerSpecification extends DDSpecification {
result.map.key1 == [nestedKey: "nestedValue"]
}

def "flatten should handle circular references"() {
given:
def circular = new Circular()
circular.name = "circular"
circular.circular = circular

when:
def result = ObjectFlattener.flatten(circular)

then:
result == null
}

private static class TestObject {
String name
int value
Expand All @@ -108,4 +121,9 @@ class ObjectFlattenerSpecification extends DDSpecification {
this.nestedValue = nestedValue
}
}

private static class Circular {
String name
Circular circular
}
}

0 comments on commit 1100efd

Please sign in to comment.