@@ -17,6 +17,12 @@ import com.datadog.appsec.event.data.MapDataBundle
17
17
import com.datadog.appsec.gateway.AppSecRequestContext
18
18
import com.datadog.appsec.gateway.GatewayContext
19
19
import com.datadog.appsec.report.AppSecEvent
20
+ import com.datadog.ddwaf.WafErrorCode
21
+ import com.datadog.ddwaf.exception.AbstractWafException
22
+ import com.datadog.ddwaf.exception.InternalWafException
23
+ import com.datadog.ddwaf.exception.InvalidArgumentWafException
24
+ import com.datadog.ddwaf.exception.InvalidObjectWafException
25
+ import com.datadog.ddwaf.exception.UnclassifiedWafException
20
26
import datadog.trace.api.telemetry.RuleType
21
27
import datadog.trace.util.stacktrace.StackTraceEvent
22
28
import com.datadog.appsec.test.StubAppSecConfigService
@@ -1755,6 +1761,85 @@ class WAFModuleSpecification extends DDSpecification {
1755
1761
0 * _
1756
1762
}
1757
1763
1764
+ void ' test raspErrorCode metric is increased when waf call throws #wafErrorCode ' () {
1765
+ setup :
1766
+ ChangeableFlow flow = Mock ()
1767
+ GatewayContext gwCtxMock = new GatewayContext (false , RuleType . SQL_INJECTION )
1768
+ WafContext wafContext = Mock ()
1769
+
1770
+ when :
1771
+ setupWithStubConfigService(' rules_with_data_config.json' )
1772
+ dataListener = wafModule. dataSubscriptions. first()
1773
+
1774
+ def bundle = MapDataBundle . of(
1775
+ KnownAddresses . USER_ID ,
1776
+ ' legit-user'
1777
+ )
1778
+ dataListener. onDataAvailable(flow, ctx, bundle, gwCtxMock)
1779
+
1780
+ then :
1781
+ (1 .. 2 ) * ctx. isWafContextClosed() >> false // if UnclassifiedWafException it's called twice
1782
+ 1 * ctx. getOrCreateWafContext(_, true , true ) >> wafContext
1783
+ 1 * wafMetricCollector. raspRuleEval(RuleType . SQL_INJECTION )
1784
+ 1 * wafContext. run(_, _, _) >> { throw createWafException(wafErrorCode) }
1785
+ 1 * wafMetricCollector. wafInit(Waf . LIB_VERSION , _, true )
1786
+ 1 * ctx. getRaspMetrics()
1787
+ 1 * ctx. getRaspMetricsCounter()
1788
+ (0 .. 1 ) * WafMetricCollector . get(). wafRequestError() // TODO: remove this line when WAFModule is removed
1789
+ 1 * wafMetricCollector. raspErrorCode(RuleType . SQL_INJECTION , wafErrorCode. code)
1790
+ 0 * _
1791
+
1792
+ where :
1793
+ wafErrorCode << WafErrorCode . values()
1794
+ }
1795
+
1796
+ void ' test wafErrorCode metric is increased when waf call throws #wafErrorCode ' () {
1797
+ setup :
1798
+ ChangeableFlow flow = Mock ()
1799
+ WafContext wafContext = Mock ()
1800
+
1801
+ when :
1802
+ setupWithStubConfigService(' rules_with_data_config.json' )
1803
+ dataListener = wafModule. dataSubscriptions. first()
1804
+
1805
+ def bundle = MapDataBundle . of(
1806
+ KnownAddresses . USER_ID ,
1807
+ ' legit-user'
1808
+ )
1809
+ dataListener. onDataAvailable(flow, ctx, bundle, gwCtx)
1810
+
1811
+ then :
1812
+ (1 .. 2 ) * ctx. isWafContextClosed() >> false // if UnclassifiedWafException it's called twice
1813
+ 1 * ctx. getOrCreateWafContext(_, true , false ) >> wafContext
1814
+ 1 * wafContext. run(_, _, _) >> { throw createWafException(wafErrorCode) }
1815
+ 1 * wafMetricCollector. wafInit(Waf . LIB_VERSION , _, true )
1816
+ 2 * ctx. getWafMetrics()
1817
+ (0 .. 1 ) * WafMetricCollector . get(). wafRequestError() // TODO: remove this line when WAFModule is removed
1818
+ 1 * wafMetricCollector. wafErrorCode(wafErrorCode. code)
1819
+ 0 * _
1820
+
1821
+ where :
1822
+ wafErrorCode << WafErrorCode . values()
1823
+ }
1824
+
1825
+ /**
1826
+ * Helper to return a concrete Waf exception for each WafErrorCode
1827
+ */
1828
+ static AbstractWafException createWafException (WafErrorCode code ) {
1829
+ switch (code) {
1830
+ case WafErrorCode . INVALID_ARGUMENT :
1831
+ return new InvalidArgumentWafException (code. code)
1832
+ case WafErrorCode . INVALID_OBJECT :
1833
+ return new InvalidObjectWafException (code. code)
1834
+ case WafErrorCode . INTERNAL_ERROR :
1835
+ return new InternalWafException (code. code)
1836
+ case WafErrorCode . BINDING_ERROR :
1837
+ return new UnclassifiedWafException (code. code)
1838
+ default :
1839
+ throw new IllegalStateException (" Unhandled WafErrorCode: $code " )
1840
+ }
1841
+ }
1842
+
1758
1843
private Map<String , Object > getDefaultConfig () {
1759
1844
def service = new StubAppSecConfigService ()
1760
1845
service. init()
0 commit comments