Skip to content

Commit 795d68a

Browse files
Unify truncation limits between ObjectIntrospection and WAFModule (#9168)
1 parent 562e533 commit 795d68a

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

dd-java-agent/appsec/src/main/java/com/datadog/appsec/ddwaf/WAFModule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@
6969
public class WAFModule implements AppSecModule {
7070
private static final Logger log = LoggerFactory.getLogger(WAFModule.class);
7171

72-
private static final int MAX_DEPTH = 10;
73-
private static final int MAX_ELEMENTS = 150;
74-
private static final int MAX_STRING_SIZE = 4096;
72+
public static final int MAX_DEPTH = 20;
73+
public static final int MAX_ELEMENTS = 256;
74+
public static final int MAX_STRING_SIZE = 4096;
7575
private static volatile Waf.Limits LIMITS;
7676
private static final Class<?> PROXY_CLASS =
7777
Proxy.getProxyClass(WAFModule.class.getClassLoader(), Set.class);

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.datadog.appsec.event.data;
22

3+
import static com.datadog.appsec.ddwaf.WAFModule.MAX_DEPTH;
4+
import static com.datadog.appsec.ddwaf.WAFModule.MAX_ELEMENTS;
5+
import static com.datadog.appsec.ddwaf.WAFModule.MAX_STRING_SIZE;
6+
37
import com.datadog.appsec.gateway.AppSecRequestContext;
48
import datadog.environment.JavaVirtualMachine;
59
import datadog.trace.api.telemetry.WafMetricCollector;
@@ -20,9 +24,7 @@
2024
import org.slf4j.LoggerFactory;
2125

2226
public final class ObjectIntrospection {
23-
private static final int MAX_DEPTH = 20;
24-
private static final int MAX_ELEMENTS = 256;
25-
private static final int MAX_STRING_LENGTH = 4096;
27+
2628
private static final Logger log = LoggerFactory.getLogger(ObjectIntrospection.class);
2729

2830
private static final Method trySetAccessible;
@@ -337,9 +339,9 @@ private static boolean setAccessible(Field field) {
337339
}
338340

339341
private static String checkStringLength(final String str, final State state) {
340-
if (str.length() > MAX_STRING_LENGTH) {
342+
if (str.length() > MAX_STRING_SIZE) {
341343
state.stringTooLong = true;
342-
return str.substring(0, MAX_STRING_LENGTH);
344+
return str.substring(0, MAX_STRING_SIZE);
343345
}
344346
return str;
345347
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import spock.lang.Shared
1010

1111
import java.nio.CharBuffer
1212

13+
import static com.datadog.appsec.ddwaf.WAFModule.MAX_DEPTH
14+
import static com.datadog.appsec.ddwaf.WAFModule.MAX_ELEMENTS
15+
import static com.datadog.appsec.ddwaf.WAFModule.MAX_STRING_SIZE
1316
import static com.datadog.appsec.event.data.ObjectIntrospection.convert
1417

1518
class ObjectIntrospectionSpecification extends DDSpecification {
@@ -381,7 +384,7 @@ class ObjectIntrospectionSpecification extends DDSpecification {
381384

382385
void 'jackson string truncation'() {
383386
setup:
384-
final longString = 'A' * (ObjectIntrospection.MAX_STRING_LENGTH + 1)
387+
final longString = 'A' * (MAX_STRING_SIZE + 1)
385388
final jsonInput = '{"long": "' + longString + '"}'
386389

387390
when:
@@ -390,14 +393,14 @@ class ObjectIntrospectionSpecification extends DDSpecification {
390393
then:
391394
1 * ctx.setWafTruncated()
392395
1 * wafMetricCollector.wafInputTruncated(true, false, false)
393-
result["long"].length() <= ObjectIntrospection.MAX_STRING_LENGTH
396+
result["long"].length() <= MAX_STRING_SIZE
394397
}
395398

396399
void 'jackson with deep nesting triggers depth limit'() {
397400
setup:
398401
// Create deeply nested JSON
399402
final json = JsonOutput.toJson(
400-
(1..(ObjectIntrospection.MAX_DEPTH + 1)).inject([:], { result, i -> [("child_$i".toString()) : result] })
403+
(1..(MAX_DEPTH + 1)).inject([:], { result, i -> [("child_$i".toString()) : result] })
401404
)
402405

403406
when:
@@ -407,13 +410,13 @@ class ObjectIntrospectionSpecification extends DDSpecification {
407410
// Should truncate at max depth and set truncation flag
408411
1 * ctx.setWafTruncated()
409412
1 * wafMetricCollector.wafInputTruncated(false, false, true)
410-
countNesting(result as Map, 0) <= ObjectIntrospection.MAX_DEPTH
413+
countNesting(result as Map, 0) <= MAX_DEPTH
411414
}
412415

413416
void 'jackson with large arrays triggers element limit'() {
414417
setup:
415418
// Create large array
416-
final largeArray = (1..(ObjectIntrospection.MAX_ELEMENTS + 1)).toList()
419+
final largeArray = (1..(MAX_ELEMENTS + 1)).toList()
417420
final json = new JsonBuilder(largeArray).toString()
418421

419422
when:
@@ -423,7 +426,7 @@ class ObjectIntrospectionSpecification extends DDSpecification {
423426
// Should truncate and set truncation flag
424427
1 * ctx.setWafTruncated()
425428
1 * wafMetricCollector.wafInputTruncated(false, true, false)
426-
result.size() <= ObjectIntrospection.MAX_ELEMENTS
429+
result.size() <= MAX_ELEMENTS
427430
}
428431

429432
void 'jackson number type variations'() {

0 commit comments

Comments
 (0)