|
2 | 2 |
|
3 | 3 | import com.datadog.appsec.gateway.AppSecRequestContext; |
4 | 4 | import datadog.trace.api.Platform; |
5 | | -import datadog.trace.api.telemetry.WafMetricCollector; |
6 | 5 | import java.lang.reflect.Array; |
7 | 6 | import java.lang.reflect.Field; |
8 | 7 | import java.lang.reflect.InvocationTargetException; |
@@ -38,25 +37,6 @@ public final class ObjectIntrospection { |
38 | 37 |
|
39 | 38 | private ObjectIntrospection() {} |
40 | 39 |
|
41 | | - /** Functional interface to receive truncation flags. */ |
42 | | - @FunctionalInterface |
43 | | - public interface TruncationCallback { |
44 | | - |
45 | | - /** |
46 | | - * Called when the object is converted. |
47 | | - * |
48 | | - * @param requestContext the request context |
49 | | - * @param stringTooLong true if a string was truncated |
50 | | - * @param listMapTooLarge true if a list or map was truncated |
51 | | - * @param objectTooDeep true if an object was too deep |
52 | | - */ |
53 | | - void onTruncation( |
54 | | - AppSecRequestContext requestContext, |
55 | | - boolean stringTooLong, |
56 | | - boolean listMapTooLarge, |
57 | | - boolean objectTooDeep); |
58 | | - } |
59 | | - |
60 | 40 | /** |
61 | 41 | * Converts arbitrary objects compatible with ddwaf_object. Possible types in the result are: |
62 | 42 | * |
@@ -86,16 +66,18 @@ void onTruncation( |
86 | 66 | * @param requestContext the request context |
87 | 67 | * @return the converted object |
88 | 68 | */ |
89 | | - public static Object convert(Object obj, AppSecRequestContext requestContext) { |
| 69 | + public static ConversionResult<Object> convert(Object obj, AppSecRequestContext requestContext) { |
90 | 70 | State state = new State(requestContext); |
91 | 71 | Object converted = guardedConversion(obj, 0, state); |
92 | 72 | if (state.stringTooLong || state.listMapTooLarge || state.objectTooDeep) { |
93 | 73 | requestContext.setWafTruncated(); |
94 | | - //TODO Enable when rebase with master |
95 | | -// WafMetricCollector.get() |
96 | | -// .wafInputTruncated(state.stringTooLong, state.listMapTooLarge, state.objectTooDeep); |
| 74 | + // TODO Enable when rebase with master |
| 75 | + // WafMetricCollector.get() |
| 76 | + // .wafInputTruncated(state.stringTooLong, state.listMapTooLarge, |
| 77 | + // state.objectTooDeep); |
97 | 78 | } |
98 | | - return converted; |
| 79 | + return new ConversionResult<>( |
| 80 | + converted, state.stringTooLong, state.listMapTooLarge, state.objectTooDeep); |
99 | 81 | } |
100 | 82 |
|
101 | 83 | private static class State { |
@@ -222,8 +204,8 @@ private static Object doConversion(Object obj, int depth, State state) { |
222 | 204 | Map<String, Object> newMap = new HashMap<>(); |
223 | 205 | List<Field[]> allFields = new ArrayList<>(); |
224 | 206 | for (Class<?> classToLook = clazz; |
225 | | - classToLook != null && classToLook != Object.class; |
226 | | - classToLook = classToLook.getSuperclass()) { |
| 207 | + classToLook != null && classToLook != Object.class; |
| 208 | + classToLook = classToLook.getSuperclass()) { |
227 | 209 | allFields.add(classToLook.getDeclaredFields()); |
228 | 210 | } |
229 | 211 |
|
@@ -301,5 +283,42 @@ private static String checkStringLength(final String str, final State state) { |
301 | 283 | } |
302 | 284 | return str; |
303 | 285 | } |
304 | | -} |
305 | 286 |
|
| 287 | + public static class ConversionResult<T> { |
| 288 | + private final T value; |
| 289 | + private final boolean truncatedByString; |
| 290 | + private final boolean truncatedByCollection; |
| 291 | + private final boolean truncatedByDepth; |
| 292 | + |
| 293 | + public ConversionResult( |
| 294 | + T value, |
| 295 | + boolean truncatedByString, |
| 296 | + boolean truncatedByCollection, |
| 297 | + boolean truncatedByDepth) { |
| 298 | + this.value = value; |
| 299 | + this.truncatedByString = truncatedByString; |
| 300 | + this.truncatedByCollection = truncatedByCollection; |
| 301 | + this.truncatedByDepth = truncatedByDepth; |
| 302 | + } |
| 303 | + |
| 304 | + public T getValue() { |
| 305 | + return value; |
| 306 | + } |
| 307 | + |
| 308 | + public boolean isStringTruncated() { |
| 309 | + return truncatedByString; |
| 310 | + } |
| 311 | + |
| 312 | + public boolean isCollectionTruncated() { |
| 313 | + return truncatedByCollection; |
| 314 | + } |
| 315 | + |
| 316 | + public boolean isDepthTruncated() { |
| 317 | + return truncatedByDepth; |
| 318 | + } |
| 319 | + |
| 320 | + public boolean isAnyTruncated() { |
| 321 | + return truncatedByString || truncatedByCollection || truncatedByDepth; |
| 322 | + } |
| 323 | + } |
| 324 | +} |
0 commit comments