Skip to content

Commit 703af33

Browse files
Limit the collections that the iast visitor can handle (#7764)
1 parent ee74fe9 commit 703af33

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

dd-java-agent/agent-iast/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656
testImplementation libs.bytebuddy
5757
testImplementation('org.skyscreamer:jsonassert:1.5.1')
5858
testImplementation('org.codehaus.groovy:groovy-yaml:3.0.17')
59+
testImplementation libs.guava
5960

6061
testImplementation group: 'io.grpc', name: 'grpc-core', version: grpcVersion
6162
testImplementation group: 'io.grpc', name: 'grpc-protobuf', version: grpcVersion

dd-java-agent/agent-iast/src/main/java/com/datadog/iast/util/ObjectVisitor.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import java.lang.reflect.InvocationTargetException;
1010
import java.lang.reflect.Method;
1111
import java.lang.reflect.Modifier;
12+
import java.util.Arrays;
1213
import java.util.Collections;
1314
import java.util.IdentityHashMap;
15+
import java.util.List;
1416
import java.util.Map;
1517
import java.util.Set;
1618
import java.util.function.Predicate;
@@ -23,6 +25,12 @@
2325
public class ObjectVisitor {
2426

2527
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectVisitor.class);
28+
private static final List<String> ALLOWED_COLLECTION_PKGS =
29+
Arrays.asList(
30+
"java.util",
31+
"com.google.protobuf",
32+
"org.apache.commons.collections",
33+
"com.google.common.collect");
2634

2735
private static final int MAX_VISITED_OBJECTS = 1000;
2836
private static final int MAX_DEPTH = 10;
@@ -122,6 +130,9 @@ private State visitArray(final int depth, final String path, final Object[] arra
122130
}
123131

124132
private State visitMap(final int depth, final String path, final Map<?, ?> map) {
133+
if (!isAllowedCollection(map)) {
134+
return CONTINUE;
135+
}
125136
final int mapDepth = depth + 1;
126137
for (final Map.Entry<?, ?> entry : map.entrySet()) {
127138
final Object key = entry.getKey();
@@ -145,6 +156,9 @@ private State visitMap(final int depth, final String path, final Map<?, ?> map)
145156
}
146157

147158
private State visitIterable(final int depth, final String path, final Iterable<?> iterable) {
159+
if (!isAllowedCollection(iterable)) {
160+
return CONTINUE;
161+
}
148162
final int iterableDepth = depth + 1;
149163
int index = 0;
150164
for (final Object item : iterable) {
@@ -188,6 +202,19 @@ private State visitObject(final int depth, final String path, final Object value
188202
return ObjectVisitor.State.CONTINUE;
189203
}
190204

205+
private static boolean isAllowedCollection(final Object value) {
206+
if (value == null) {
207+
return false;
208+
}
209+
final String packageName = value.getClass().getPackage().getName();
210+
for (final String allowed : ALLOWED_COLLECTION_PKGS) {
211+
if (packageName.startsWith(allowed)) {
212+
return true;
213+
}
214+
}
215+
return false;
216+
}
217+
191218
public static boolean inspectClass(final Class<?> cls) {
192219
if (cls.isPrimitive()) {
193220
return false; // skip primitives

dd-java-agent/agent-iast/src/test/groovy/com/datadog/iast/GrpcRequestMessageHandlerTest.groovy

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class GrpcRequestMessageHandlerTest extends IastModuleImplTestBase {
6666
given:
6767
final visitor = Mock(ObjectVisitor.Visitor) {
6868
visit(_ as String, _ as Object) >> {
69-
println 'feo'
7069
return CONTINUE
7170
}
7271
}

dd-java-agent/agent-iast/src/test/groovy/com/datadog/iast/util/ObjectVisitorTest.groovy

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.datadog.iast.util
22

3+
import com.google.common.collect.Iterables
34
import foo.bar.VisitableClass
45
import spock.lang.Specification
56

@@ -39,12 +40,7 @@ class ObjectVisitorTest extends Specification {
3940
given:
4041
final visitor = Mock(ObjectVisitor.Visitor)
4142
final wrapped = ['1', '2', '3']
42-
final target = new Iterable() {
43-
@Override
44-
Iterator iterator() {
45-
return wrapped.iterator()
46-
}
47-
}
43+
final target = Iterables.unmodifiableIterable(wrapped)
4844

4945
when:
5046
ObjectVisitor.visit(target, visitor)
@@ -74,6 +70,29 @@ class ObjectVisitorTest extends Specification {
7470
0 * _
7571
}
7672

73+
void 'test visiting ignored collection'() {
74+
given:
75+
final visitor = Mock(ObjectVisitor.Visitor)
76+
final target = new AbstractList<String>() {
77+
@Override
78+
String get(int index) {
79+
assert index == 0
80+
return 'value'
81+
}
82+
83+
@Override
84+
int size() {
85+
return 1
86+
}
87+
}
88+
89+
when:
90+
ObjectVisitor.visit(target, visitor)
91+
92+
then:
93+
0 * _
94+
}
95+
7796
void 'test max depth'() {
7897
given:
7998
final visitor = Mock(ObjectVisitor.Visitor)

0 commit comments

Comments
 (0)