Skip to content

Commit 9e2878f

Browse files
committed
new approach test
1 parent 94d92e0 commit 9e2878f

2 files changed

Lines changed: 23 additions & 54 deletions

File tree

dd-java-agent/appsec/src/main/java/com/datadog/appsec/event/data/ObjectIntrospection.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,40 @@
1616
import java.lang.reflect.Modifier;
1717
import java.util.ArrayList;
1818
import java.util.Collection;
19+
import java.util.Collections;
1920
import java.util.Date;
2021
import java.util.HashMap;
22+
import java.util.HashSet;
2123
import java.util.Iterator;
2224
import java.util.List;
2325
import java.util.Map;
26+
import java.util.Set;
2427
import org.slf4j.Logger;
2528
import org.slf4j.LoggerFactory;
2629

2730
public final class ObjectIntrospection {
2831

2932
private static final Logger log = LoggerFactory.getLogger(ObjectIntrospection.class);
3033

34+
/**
35+
* Field types excluded from object introspection. Covers Groovy meta-fields and logging framework
36+
* loggers — both introduce deep, cyclic, or sensitive object graphs that are irrelevant for WAF
37+
* inspection and can trigger false positives (e.g. crs-944-130).
38+
*/
39+
private static final Set<String> EXCLUDED_FIELD_TYPES;
40+
41+
static {
42+
final Set<String> types = new HashSet<>();
43+
types.add("groovy.lang.MetaClass");
44+
types.add("org.slf4j.Logger");
45+
types.add("org.apache.logging.log4j.Logger");
46+
types.add("org.apache.logging.log4j.core.Logger");
47+
types.add("java.util.logging.Logger");
48+
types.add("org.apache.commons.logging.Log");
49+
types.add("ch.qos.logback.classic.Logger");
50+
EXCLUDED_FIELD_TYPES = Collections.unmodifiableSet(types);
51+
}
52+
3153
private static final Method trySetAccessible;
3254

3355
static {
@@ -287,10 +309,7 @@ private static Object doConversion(Object obj, int depth, State state) {
287309
if (Modifier.isStatic(f.getModifiers())) {
288310
continue;
289311
}
290-
if (f.getType().getName().equals("groovy.lang.MetaClass")) {
291-
continue;
292-
}
293-
if (isLoggingType(f.getType())) {
312+
if (EXCLUDED_FIELD_TYPES.contains(f.getType().getName())) {
294313
continue;
295314
}
296315
String name = f.getName();
@@ -318,20 +337,6 @@ private static Object doConversion(Object obj, int depth, State state) {
318337
return newMap;
319338
}
320339

321-
private static boolean isLoggingType(final Class<?> type) {
322-
switch (type.getName()) {
323-
case "org.slf4j.Logger":
324-
case "org.apache.logging.log4j.Logger":
325-
case "org.apache.logging.log4j.core.Logger":
326-
case "java.util.logging.Logger":
327-
case "org.apache.commons.logging.Log":
328-
case "ch.qos.logback.classic.Logger":
329-
return true;
330-
default:
331-
return false;
332-
}
333-
}
334-
335340
private static boolean ignoredFieldName(final String name) {
336341
switch (name) {
337342
case "this$0":

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/event/data/ObjectIntrospectionSpecification.groovy

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -583,40 +583,4 @@ class ObjectIntrospectionSpecification extends DDSpecification {
583583
return map.entrySet().iterator()
584584
}
585585
}
586-
587-
static class DtoWithLogger {
588-
String userId = 'user123'
589-
java.util.logging.Logger logger = java.util.logging.Logger.getLogger('test')
590-
String payload = 'data'
591-
}
592-
593-
void 'logging framework fields are excluded from introspection'() {
594-
given:
595-
def input = new DtoWithLogger()
596-
597-
when:
598-
def result = convert(input, ctx) as Map
599-
600-
then:
601-
result['userId'] == 'user123'
602-
result['payload'] == 'data'
603-
!result.containsKey('logger')
604-
}
605-
606-
static class WrapperWithSoftRef {
607-
String name = 'test'
608-
java.lang.ref.SoftReference<String> ref = new java.lang.ref.SoftReference<>('test')
609-
}
610-
611-
void 'objects with inaccessible JDK fields skip those fields rather than expose toString()'() {
612-
given:
613-
def input = new WrapperWithSoftRef()
614-
615-
when:
616-
def result = convert(input, ctx) as Map
617-
618-
then:
619-
result['name'] == 'test'
620-
result['ref'] instanceof Map
621-
}
622586
}

0 commit comments

Comments
 (0)