Skip to content

Commit fad042b

Browse files
committed
wip - fix
1 parent dcc37b1 commit fad042b

File tree

3 files changed

+139
-85
lines changed

3 files changed

+139
-85
lines changed

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

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.datadog.appsec.gateway.AppSecRequestContext;
44
import datadog.trace.api.Platform;
5-
import datadog.trace.api.telemetry.WafMetricCollector;
65
import java.lang.reflect.Array;
76
import java.lang.reflect.Field;
87
import java.lang.reflect.InvocationTargetException;
@@ -38,25 +37,6 @@ public final class ObjectIntrospection {
3837

3938
private ObjectIntrospection() {}
4039

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-
6040
/**
6141
* Converts arbitrary objects compatible with ddwaf_object. Possible types in the result are:
6242
*
@@ -86,16 +66,18 @@ void onTruncation(
8666
* @param requestContext the request context
8767
* @return the converted object
8868
*/
89-
public static Object convert(Object obj, AppSecRequestContext requestContext) {
69+
public static ConversionResult<Object> convert(Object obj, AppSecRequestContext requestContext) {
9070
State state = new State(requestContext);
9171
Object converted = guardedConversion(obj, 0, state);
9272
if (state.stringTooLong || state.listMapTooLarge || state.objectTooDeep) {
9373
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);
9778
}
98-
return converted;
79+
return new ConversionResult<>(
80+
converted, state.stringTooLong, state.listMapTooLarge, state.objectTooDeep);
9981
}
10082

10183
private static class State {
@@ -222,8 +204,8 @@ private static Object doConversion(Object obj, int depth, State state) {
222204
Map<String, Object> newMap = new HashMap<>();
223205
List<Field[]> allFields = new ArrayList<>();
224206
for (Class<?> classToLook = clazz;
225-
classToLook != null && classToLook != Object.class;
226-
classToLook = classToLook.getSuperclass()) {
207+
classToLook != null && classToLook != Object.class;
208+
classToLook = classToLook.getSuperclass()) {
227209
allFields.add(classToLook.getDeclaredFields());
228210
}
229211

@@ -301,5 +283,42 @@ private static String checkStringLength(final String str, final State state) {
301283
}
302284
return str;
303285
}
304-
}
305286

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+
}

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/GatewayBridge.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public class GatewayBridge {
6969

7070
/** User tracking tags that will force the collection of request headers */
7171
private static final String[] USER_TRACKING_TAGS = {
72-
"appsec.events.users.login.success.track",
73-
"appsec.events.users.login.failure.track",
74-
"appsec.events.users.signup.track"
72+
"appsec.events.users.login.success.track",
73+
"appsec.events.users.login.failure.track",
74+
"appsec.events.users.signup.track"
7575
};
7676

7777
private static final String USER_COLLECTION_MODE_TAG = "_dd.appsec.user.collection_mode";
@@ -473,7 +473,7 @@ private Flow<Void> onGrpcServerRequestMessage(RequestContext ctx_, Object obj) {
473473
if (subInfo == null || subInfo.isEmpty()) {
474474
return NoopFlow.INSTANCE;
475475
}
476-
Object convObj = ObjectIntrospection.convert(obj, ctx);
476+
Object convObj = ObjectIntrospection.convert(obj, ctx).getValue();
477477
DataBundle bundle =
478478
new SingletonDataBundle<>(KnownAddresses.GRPC_SERVER_REQUEST_MESSAGE, convObj);
479479
try {
@@ -573,17 +573,16 @@ private Flow<Void> onRequestBodyProcessed(RequestContext ctx_, Object obj) {
573573
if (subInfo == null || subInfo.isEmpty()) {
574574
return NoopFlow.INSTANCE;
575575
}
576-
Object converted = ObjectIntrospection.convert(obj, ctx)
577-
// if (Config.get().isAppSecRaspCollectRequestBody()) {
578-
// ctx.setProcessedRequestBody(pair.getLeft());
579-
// Boolean limitsExceeded = pair.getRight();
580-
// if (Boolean.TRUE.equals(limitsExceeded)) {
581-
// ctx_.getTraceSegment().setTagTop("_dd.appsec.rasp.request_body_size.exceeded", true);
582-
// }
583-
// }
576+
ObjectIntrospection.ConversionResult<Object> converted =
577+
ObjectIntrospection.convert(obj, ctx);
578+
if (Config.get().isAppSecRaspCollectRequestBody()) {
579+
ctx.setProcessedRequestBody(converted.getValue());
580+
if (converted.isAnyTruncated()) {
581+
ctx_.getTraceSegment().setTagTop("_dd.appsec.rasp.request_body_size.exceeded", true);
582+
}
583+
}
584584
DataBundle bundle =
585-
new SingletonDataBundle<>(
586-
KnownAddresses.REQUEST_BODY_OBJECT,converted);
585+
new SingletonDataBundle<>(KnownAddresses.REQUEST_BODY_OBJECT, converted.getValue());
587586
try {
588587
GatewayContext gwCtx = new GatewayContext(false);
589588
return producerService.publishDataEvent(subInfo, ctx, bundle, gwCtx);
@@ -757,7 +756,7 @@ private NoopFlow onRequestEnded(RequestContext ctx_, IGSpanInfo spanInfo) {
757756
ctx.isWafRequestBlockFailure(), // blockFailure,
758757
ctx.isWafRateLimited(), // rateLimited,
759758
ctx.isWafTruncated() // inputTruncated
760-
);
759+
);
761760
}
762761

763762
ctx.close();
@@ -1142,4 +1141,3 @@ static Collection<datadog.trace.api.gateway.EventType<?>> additionalIGEventTypes
11421141
}
11431142
}
11441143
}
1145-

0 commit comments

Comments
 (0)