@@ -30,6 +30,11 @@ describe('Appsec Waf Telemetry metrics', () => {
3030 afterEach ( sinon . restore )
3131
3232 describe ( 'if enabled' , ( ) => {
33+ const metrics = {
34+ wafVersion,
35+ rulesVersion
36+ }
37+
3338 beforeEach ( ( ) => {
3439 appsecTelemetry . enable ( {
3540 enabled : true ,
@@ -38,11 +43,6 @@ describe('Appsec Waf Telemetry metrics', () => {
3843 } )
3944
4045 describe ( 'updateWafRequestsMetricTags' , ( ) => {
41- const metrics = {
42- wafVersion,
43- rulesVersion
44- }
45-
4646 it ( 'should skip update if no request is provided' , ( ) => {
4747 const result = appsecTelemetry . updateWafRequestsMetricTags ( metrics )
4848
@@ -57,7 +57,8 @@ describe('Appsec Waf Telemetry metrics', () => {
5757 event_rules_version : rulesVersion ,
5858 request_blocked : false ,
5959 rule_triggered : false ,
60- waf_timeout : false
60+ waf_timeout : false ,
61+ input_truncated : false
6162 } )
6263 } )
6364
@@ -66,6 +67,7 @@ describe('Appsec Waf Telemetry metrics', () => {
6667 blockTriggered : true ,
6768 ruleTriggered : true ,
6869 wafTimeout : true ,
70+ maxTruncatedString : 5000 ,
6971 ...metrics
7072 } , req )
7173
@@ -74,7 +76,8 @@ describe('Appsec Waf Telemetry metrics', () => {
7476 event_rules_version : rulesVersion ,
7577 request_blocked : true ,
7678 rule_triggered : true ,
77- waf_timeout : true
79+ waf_timeout : true ,
80+ input_truncated : true
7881 } )
7982 } )
8083
@@ -93,7 +96,8 @@ describe('Appsec Waf Telemetry metrics', () => {
9396 event_rules_version : rulesVersion ,
9497 request_blocked : false ,
9598 rule_triggered : true ,
96- waf_timeout : false
99+ waf_timeout : false ,
100+ input_truncated : false
97101 } )
98102 } )
99103
@@ -102,6 +106,7 @@ describe('Appsec Waf Telemetry metrics', () => {
102106 blockTriggered : true ,
103107 ruleTriggered : true ,
104108 wafTimeout : true ,
109+ maxTruncatedContainerSize : 300 ,
105110 ...metrics
106111 } , req )
107112
@@ -120,7 +125,8 @@ describe('Appsec Waf Telemetry metrics', () => {
120125 event_rules_version : rulesVersion ,
121126 request_blocked : true ,
122127 rule_triggered : true ,
123- waf_timeout : true
128+ waf_timeout : true ,
129+ input_truncated : true
124130 } )
125131 } )
126132
@@ -250,7 +256,8 @@ describe('Appsec Waf Telemetry metrics', () => {
250256 rule_triggered : false ,
251257 waf_timeout : true ,
252258 waf_version : wafVersion ,
253- event_rules_version : rulesVersion
259+ event_rules_version : rulesVersion ,
260+ input_truncated : false
254261 } )
255262 } )
256263
@@ -260,6 +267,63 @@ describe('Appsec Waf Telemetry metrics', () => {
260267 expect ( count ) . to . not . have . been . called
261268 } )
262269 } )
270+
271+ describe ( 'WAF Truncation metrics' , ( ) => {
272+ it ( 'should report truncated string metrics' , ( ) => {
273+ const result = appsecTelemetry . updateWafRequestsMetricTags ( { maxTruncatedString : 5000 } , req )
274+ expect ( result ) . to . have . property ( 'input_truncated' , true )
275+
276+ expect ( count ) . to . have . been . calledWith ( 'waf.input_truncated' , { truncation_reason : 1 } )
277+ expect ( inc ) . to . have . been . calledWith ( 1 )
278+
279+ expect ( distribution ) . to . have . been . calledWith ( 'waf.truncated_value_size' , { truncation_reason : 1 } )
280+ expect ( track ) . to . have . been . calledWith ( 5000 )
281+ } )
282+
283+ it ( 'should report truncated container size metrics' , ( ) => {
284+ const result = appsecTelemetry . updateWafRequestsMetricTags ( { maxTruncatedContainerSize : 300 } , req )
285+ expect ( result ) . to . have . property ( 'input_truncated' , true )
286+
287+ expect ( count ) . to . have . been . calledWith ( 'waf.input_truncated' , { truncation_reason : 2 } )
288+ expect ( inc ) . to . have . been . calledWith ( 1 )
289+
290+ expect ( distribution ) . to . have . been . calledWith ( 'waf.truncated_value_size' , { truncation_reason : 2 } )
291+ expect ( track ) . to . have . been . calledWith ( 300 )
292+ } )
293+
294+ it ( 'should report truncated container depth metrics' , ( ) => {
295+ const result = appsecTelemetry . updateWafRequestsMetricTags ( { maxTruncatedContainerDepth : 20 } , req )
296+ expect ( result ) . to . have . property ( 'input_truncated' , true )
297+
298+ expect ( count ) . to . have . been . calledWith ( 'waf.input_truncated' , { truncation_reason : 4 } )
299+ expect ( inc ) . to . have . been . calledWith ( 1 )
300+
301+ expect ( distribution ) . to . have . been . calledWith ( 'waf.truncated_value_size' , { truncation_reason : 4 } )
302+ expect ( track ) . to . have . been . calledWith ( 20 )
303+ } )
304+
305+ it ( 'should combine truncation reasons when multiple truncations occur' , ( ) => {
306+ const result = appsecTelemetry . updateWafRequestsMetricTags ( {
307+ maxTruncatedString : 5000 ,
308+ maxTruncatedContainerSize : 300 ,
309+ maxTruncatedContainerDepth : 20
310+ } , req )
311+ expect ( result ) . to . have . property ( 'input_truncated' , true )
312+
313+ expect ( count ) . to . have . been . calledWith ( 'waf.input_truncated' , { truncation_reason : 7 } )
314+ expect ( distribution ) . to . have . been . calledWith ( 'waf.truncated_value_size' , { truncation_reason : 1 } )
315+ expect ( distribution ) . to . have . been . calledWith ( 'waf.truncated_value_size' , { truncation_reason : 2 } )
316+ expect ( distribution ) . to . have . been . calledWith ( 'waf.truncated_value_size' , { truncation_reason : 4 } )
317+ } )
318+
319+ it ( 'should not report truncation metrics when no truncation occurs' , ( ) => {
320+ const result = appsecTelemetry . updateWafRequestsMetricTags ( metrics , req )
321+ expect ( result ) . to . have . property ( 'input_truncated' , false )
322+
323+ expect ( count ) . to . not . have . been . calledWith ( 'waf.input_truncated' )
324+ expect ( distribution ) . to . not . have . been . calledWith ( 'waf.truncated_value_size' )
325+ } )
326+ } )
263327 } )
264328
265329 describe ( 'if disabled' , ( ) => {
0 commit comments