40
40
import datadog .trace .api .gateway .Flow ;
41
41
import datadog .trace .api .telemetry .LogCollector ;
42
42
import datadog .trace .api .telemetry .WafMetricCollector ;
43
- import datadog .trace .api .telemetry .WafTruncatedType ;
44
43
import datadog .trace .api .time .SystemTimeSource ;
45
44
import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
46
45
import datadog .trace .bootstrap .instrumentation .api .AgentTracer ;
@@ -440,20 +439,17 @@ public void onDataAvailable(
440
439
WafMetricCollector .get ().raspTimeout (gwCtx .raspRuleType );
441
440
} else {
442
441
reqCtx .increaseWafTimeouts ();
443
- WafMetricCollector .get ().wafRequestTimeout ();
444
442
log .debug (LogCollector .EXCLUDE_TELEMETRY , "Timeout calling the WAF" , tpe );
445
443
}
446
444
return ;
447
445
} catch (UnclassifiedWafException e ) {
448
446
if (!reqCtx .isWafContextClosed ()) {
449
447
log .error ("Error calling WAF" , e );
450
448
}
451
- // TODO this is wrong and will be fixed in another PR
452
- WafMetricCollector .get ().wafRequestError ();
453
449
incrementErrorCodeMetric (gwCtx , e .code );
454
450
return ;
455
451
} catch (AbstractWafException e ) {
456
- incrementErrorCodeMetric (gwCtx , e .code );
452
+ incrementErrorCodeMetric (reqCtx , gwCtx , e .code );
457
453
return ;
458
454
} finally {
459
455
if (log .isDebugEnabled ()) {
@@ -467,17 +463,8 @@ public void onDataAvailable(
467
463
final long listMapTooLarge = wafMetrics .getTruncatedListMapTooLargeCount ();
468
464
final long objectTooDeep = wafMetrics .getTruncatedObjectTooDeepCount ();
469
465
470
- if (stringTooLong > 0 ) {
471
- WafMetricCollector .get ()
472
- .wafInputTruncated (WafTruncatedType .STRING_TOO_LONG , stringTooLong );
473
- }
474
- if (listMapTooLarge > 0 ) {
475
- WafMetricCollector .get ()
476
- .wafInputTruncated (WafTruncatedType .LIST_MAP_TOO_LARGE , listMapTooLarge );
477
- }
478
- if (objectTooDeep > 0 ) {
479
- WafMetricCollector .get ()
480
- .wafInputTruncated (WafTruncatedType .OBJECT_TOO_DEEP , objectTooDeep );
466
+ if (stringTooLong > 0 || listMapTooLarge > 0 || objectTooDeep > 0 ) {
467
+ reqCtx .setWafTruncated ();
481
468
}
482
469
}
483
470
}
@@ -501,10 +488,12 @@ public void onDataAvailable(
501
488
ActionInfo actionInfo = new ActionInfo (actionType , actionParams );
502
489
503
490
if ("block_request" .equals (actionInfo .type )) {
504
- Flow .Action .RequestBlockingAction rba = createBlockRequestAction (actionInfo );
491
+ Flow .Action .RequestBlockingAction rba =
492
+ createBlockRequestAction (actionInfo , reqCtx , gwCtx .isRasp );
505
493
flow .setAction (rba );
506
494
} else if ("redirect_request" .equals (actionInfo .type )) {
507
- Flow .Action .RequestBlockingAction rba = createRedirectRequestAction (actionInfo );
495
+ Flow .Action .RequestBlockingAction rba =
496
+ createRedirectRequestAction (actionInfo , reqCtx , gwCtx .isRasp );
508
497
flow .setAction (rba );
509
498
} else if ("generate_stack" .equals (actionInfo .type )) {
510
499
if (Config .get ().isAppSecStackTraceEnabled ()) {
@@ -516,7 +505,9 @@ public void onDataAvailable(
516
505
}
517
506
} else {
518
507
log .info ("Ignoring action with type {}" , actionInfo .type );
519
- WafMetricCollector .get ().wafRequestBlockFailure ();
508
+ if (!gwCtx .isRasp ) {
509
+ reqCtx .setWafRequestBlockFailure ();
510
+ }
520
511
}
521
512
}
522
513
Collection <AppSecEvent > events = buildEvents (resultWithData );
@@ -543,12 +534,16 @@ public void onDataAvailable(
543
534
reqCtx .reportEvents (events );
544
535
} else {
545
536
log .debug ("Rate limited WAF events" );
546
- WafMetricCollector .get ().wafRequestRateLimited ();
537
+ if (!gwCtx .isRasp ) {
538
+ reqCtx .setWafRateLimited ();
539
+ }
547
540
}
548
541
}
549
542
550
543
if (flow .isBlocking ()) {
551
- reqCtx .setBlocked ();
544
+ if (!gwCtx .isRasp ) {
545
+ reqCtx .setWafBlocked ();
546
+ }
552
547
}
553
548
}
554
549
@@ -557,7 +552,8 @@ public void onDataAvailable(
557
552
}
558
553
}
559
554
560
- private Flow .Action .RequestBlockingAction createBlockRequestAction (ActionInfo actionInfo ) {
555
+ private Flow .Action .RequestBlockingAction createBlockRequestAction (
556
+ final ActionInfo actionInfo , final AppSecRequestContext reqCtx , final boolean isRasp ) {
561
557
try {
562
558
int statusCode ;
563
559
Object statusCodeObj = actionInfo .parameters .get ("status_code" );
@@ -578,12 +574,15 @@ private Flow.Action.RequestBlockingAction createBlockRequestAction(ActionInfo ac
578
574
return new Flow .Action .RequestBlockingAction (statusCode , blockingContentType );
579
575
} catch (RuntimeException cce ) {
580
576
log .warn ("Invalid blocking action data" , cce );
581
- WafMetricCollector .get ().wafRequestBlockFailure ();
577
+ if (!isRasp ) {
578
+ reqCtx .setWafRequestBlockFailure ();
579
+ }
582
580
return null ;
583
581
}
584
582
}
585
583
586
- private Flow .Action .RequestBlockingAction createRedirectRequestAction (ActionInfo actionInfo ) {
584
+ private Flow .Action .RequestBlockingAction createRedirectRequestAction (
585
+ final ActionInfo actionInfo , final AppSecRequestContext reqCtx , final boolean isRasp ) {
587
586
try {
588
587
int statusCode ;
589
588
Object statusCodeObj = actionInfo .parameters .get ("status_code" );
@@ -604,7 +603,9 @@ private Flow.Action.RequestBlockingAction createRedirectRequestAction(ActionInfo
604
603
return Flow .Action .RequestBlockingAction .forRedirect (statusCode , location );
605
604
} catch (RuntimeException cce ) {
606
605
log .warn ("Invalid blocking action data" , cce );
607
- WafMetricCollector .get ().wafRequestBlockFailure ();
606
+ if (!isRasp ) {
607
+ reqCtx .setWafRequestBlockFailure ();
608
+ }
608
609
return null ;
609
610
}
610
611
}
@@ -649,11 +650,13 @@ private Waf.ResultWithData runWafContext(
649
650
}
650
651
}
651
652
652
- private static void incrementErrorCodeMetric (GatewayContext gwCtx , int code ) {
653
+ private static void incrementErrorCodeMetric (
654
+ AppSecRequestContext reqCtx , GatewayContext gwCtx , int code ) {
653
655
if (gwCtx .isRasp ) {
654
656
WafMetricCollector .get ().raspErrorCode (gwCtx .raspRuleType , code );
655
657
} else {
656
658
WafMetricCollector .get ().wafErrorCode (code );
659
+ reqCtx .setWafErrors ();
657
660
}
658
661
}
659
662
0 commit comments